2015-07-22 02:09:02 +00:00
|
|
|
# AccessMatchers
|
|
|
|
#
|
|
|
|
# The custom matchers contained in this module are used to test a user's access
|
|
|
|
# to a URL by emulating a specific user or type of user account, visiting the
|
|
|
|
# URL, and then checking the response status code and resulting path.
|
|
|
|
module AccessMatchers
|
|
|
|
extend RSpec::Matchers::DSL
|
|
|
|
include Warden::Test::Helpers
|
|
|
|
|
2016-11-29 07:08:11 +00:00
|
|
|
def emulate_user(user, membership = nil)
|
2015-07-22 02:09:02 +00:00
|
|
|
case user
|
|
|
|
when :user
|
|
|
|
login_as(create(:user))
|
|
|
|
when :visitor
|
|
|
|
logout
|
|
|
|
when :admin
|
|
|
|
login_as(create(:admin))
|
2016-03-10 21:08:11 +00:00
|
|
|
when :external
|
|
|
|
login_as(create(:user, external: true))
|
2015-07-22 02:09:02 +00:00
|
|
|
when User
|
2016-11-28 05:08:14 +00:00
|
|
|
login_as(user)
|
2016-11-29 07:08:11 +00:00
|
|
|
when *Gitlab::Access.sym_options_with_owner.keys
|
|
|
|
raise ArgumentError, "cannot emulate #{user} without membership parent" unless membership
|
2016-11-28 05:08:14 +00:00
|
|
|
|
|
|
|
role = user
|
2016-11-29 07:08:11 +00:00
|
|
|
|
|
|
|
if role == :owner && membership.owner
|
|
|
|
user = membership.owner
|
|
|
|
else
|
|
|
|
user = create(:user)
|
|
|
|
membership.public_send(:"add_#{role}", user)
|
|
|
|
end
|
2016-11-28 05:08:14 +00:00
|
|
|
|
2015-07-22 02:09:02 +00:00
|
|
|
login_as(user)
|
|
|
|
else
|
|
|
|
raise ArgumentError, "cannot emulate user #{user}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def description_for(user, type)
|
|
|
|
if user.kind_of?(User)
|
2016-11-28 05:08:14 +00:00
|
|
|
# User#inspect displays too much information for RSpec's descriptions
|
2016-03-20 20:03:53 +00:00
|
|
|
"be #{type} for the specified user"
|
2015-07-22 02:09:02 +00:00
|
|
|
else
|
|
|
|
"be #{type} for #{user}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
matcher :be_allowed_for do |user|
|
|
|
|
match do |url|
|
2016-11-29 07:08:11 +00:00
|
|
|
emulate_user(user, @membership)
|
2016-11-28 05:08:14 +00:00
|
|
|
visit(url)
|
|
|
|
|
2015-07-22 02:09:02 +00:00
|
|
|
status_code != 404 && current_path != new_user_session_path
|
|
|
|
end
|
|
|
|
|
2016-11-29 07:08:11 +00:00
|
|
|
chain :of do |membership|
|
|
|
|
@membership = membership
|
2016-11-28 05:08:14 +00:00
|
|
|
end
|
|
|
|
|
2015-07-22 02:09:02 +00:00
|
|
|
description { description_for(user, 'allowed') }
|
|
|
|
end
|
|
|
|
|
|
|
|
matcher :be_denied_for do |user|
|
|
|
|
match do |url|
|
2016-11-29 07:08:11 +00:00
|
|
|
emulate_user(user, @membership)
|
2016-11-28 05:08:14 +00:00
|
|
|
visit(url)
|
|
|
|
|
2015-07-22 02:09:02 +00:00
|
|
|
status_code == 404 || current_path == new_user_session_path
|
|
|
|
end
|
|
|
|
|
2016-11-29 07:08:11 +00:00
|
|
|
chain :of do |membership|
|
|
|
|
@membership = membership
|
2016-11-28 05:08:14 +00:00
|
|
|
end
|
|
|
|
|
2015-07-22 02:09:02 +00:00
|
|
|
description { description_for(user, 'denied') }
|
|
|
|
end
|
|
|
|
end
|