Welcome to mirror list, hosted at ThFree Co, Russian Federation.

matchers.rb « support « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 52b11bd6323c717231890d41b9e3add601f14147 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
RSpec::Matchers.define :be_valid_commit do
  match do |actual|
    actual != nil
    actual.id == ValidCommit::ID
    actual.message == ValidCommit::MESSAGE
    actual.author_name == ValidCommit::AUTHOR_FULL_NAME
  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)
  end
end

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

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

module UrlAccess
  def url_allowed?(user, url)
    emulate_user(user)
    visit url
    (status_code != 404 && current_path != new_user_session_path)
  end

  def url_denied?(user, url)
    emulate_user(user)
    visit url
    (status_code == 404 || current_path == new_user_session_path)
  end

  def url_404?(user, url)
    emulate_user(user)
    visit url
    status_code == 404
  end

  def emulate_user(user)
    user = case user
           when :user then create(:user)
           when :visitor then nil
           when :admin then create(:admin)
           else user
           end
    login_with(user) if user
  end
end

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