BigW Consortium Gitlab

matchers.rb 1.67 KB
Newer Older
gitlabhq committed
1 2 3 4 5
RSpec::Matchers.define :be_valid_commit do
  match do |actual|
    actual != nil
    actual.id == ValidCommit::ID
    actual.message == ValidCommit::MESSAGE
6
    actual.author_name == ValidCommit::AUTHOR_FULL_NAME
gitlabhq committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20
  end
end

RSpec::Matchers.define :be_allowed_for do |user|
  match do |url|
    include UrlAccess
    url_allowed?(user, url)
  end
end

RSpec::Matchers.define :be_denied_for do |user|
  match do |url|
    include UrlAccess
    url_denied?(user, url)
Nihad Abbasov committed
21
  end
gitlabhq committed
22 23
end

gitlabhq committed
24 25 26 27
RSpec::Matchers.define :be_404_for do |user|
  match do |url|
    include UrlAccess
    url_404?(user, url)
Nihad Abbasov committed
28
  end
gitlabhq committed
29 30
end

31 32 33 34 35 36 37 38 39 40
RSpec::Matchers.define :include_module do |expected|
  match do
    described_class.included_modules.include?(expected)
  end

  failure_message_for_should do
    "expected #{described_class} to include the #{expected} module"
  end
end

Nihad Abbasov committed
41
module UrlAccess
gitlabhq committed
42 43 44
  def url_allowed?(user, url)
    emulate_user(user)
    visit url
gitlabhq committed
45
    (page.status_code != 404 && current_path != new_user_session_path)
gitlabhq committed
46 47 48 49 50
  end

  def url_denied?(user, url)
    emulate_user(user)
    visit url
gitlabhq committed
51 52 53 54 55 56 57
    (page.status_code == 404 || current_path == new_user_session_path)
  end

  def url_404?(user, url)
    emulate_user(user)
    visit url
    page.status_code == 404
gitlabhq committed
58 59 60 61
  end

  def emulate_user(user)
    user = case user
62
           when :user then create(:user)
gitlabhq committed
63
           when :visitor then nil
64
           when :admin then create(:admin)
gitlabhq committed
65 66 67 68 69
           else user
           end
    login_with(user) if user
  end
end
70 71 72 73 74 75

# Extend shoulda-matchers
module Shoulda::Matchers::ActiveModel
  class EnsureLengthOfMatcher
    # Shortcut for is_at_least and is_at_most
    def is_within(range)
76
      is_at_least(range.min) && is_at_most(range.max)
77 78 79
    end
  end
end