gitlab-org--gitlab-foss/spec/support/matchers.rb

80 lines
1.7 KiB
Ruby
Raw Normal View History

2011-10-08 17:36:38 -04:00
RSpec::Matchers.define :be_valid_commit do
match do |actual|
actual != nil
actual.id == ValidCommit::ID
actual.message == ValidCommit::MESSAGE
2013-04-03 03:06:31 -04:00
actual.author_name == ValidCommit::AUTHOR_FULL_NAME
2011-10-08 17:36:38 -04:00
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
2011-10-08 17:36:38 -04:00
end
2011-10-17 06:39:03 -04:00
RSpec::Matchers.define :be_404_for do |user|
match do |url|
include UrlAccess
url_404?(user, url)
end
2011-10-17 06:39:03 -04:00
end
2012-08-29 01:49:26 -04:00
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
2011-10-08 17:36:38 -04:00
def url_allowed?(user, url)
emulate_user(user)
visit url
2014-09-24 02:28:41 -04:00
(status_code != 404 && current_path != new_user_session_path)
2011-10-08 17:36:38 -04:00
end
def url_denied?(user, url)
emulate_user(user)
visit url
2014-09-24 02:28:41 -04:00
(status_code == 404 || current_path == new_user_session_path)
2011-10-17 06:39:03 -04:00
end
def url_404?(user, url)
emulate_user(user)
visit url
2014-09-24 02:28:41 -04:00
status_code == 404
2011-10-08 17:36:38 -04:00
end
def emulate_user(user)
user = case user
when :user then create(:user)
2011-10-08 17:36:38 -04:00
when :visitor then nil
when :admin then create(:admin)
2011-10-08 17:36:38 -04:00
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)
2012-09-19 12:14:42 -04:00
is_at_least(range.min) && is_at_most(range.max)
end
end
end