Commit ac7ad422 authored by Shinya Maeda's avatar Shinya Maeda

IMprove access_matchers

parent 043f1b82
......@@ -127,7 +127,8 @@ describe Projects::PipelineSchedulesController do
describe 'PUT update' do
let(:action) do
proc do |user|
put :update, namespace_id: project.namespace.to_param, project_id: project, id: pipeline_schedule.id
put :update, namespace_id: project.namespace.to_param, project_id: project, id: pipeline_schedule.id,
schedule: { description: 'a' }
end
end
......
......@@ -5,8 +5,8 @@ module AccessMatchersForController
extend RSpec::Matchers::DSL
include Warden::Test::Helpers
EXPECTED_STATUS_CODE_ALLOWED = [200, 302].freeze
EXPECTED_STATUS_CODE_DENIED = [404].freeze
EXPECTED_STATUS_CODE_ALLOWED = [200, 201, 302].freeze
EXPECTED_STATUS_CODE_DENIED = [401, 404].freeze
def emulate_user(role, membership = nil)
case role
......@@ -19,9 +19,22 @@ module AccessMatchersForController
when :external
user = create(:user, external: true)
sign_in(user)
when :visitor # rubocop:disable Lint/EmptyWhen
# no-op
when :visitor
user = nil
when User
user = role
sign_in(user)
when *Gitlab::Access.sym_options_with_owner.keys # owner, master, developer, reporter, guest
user = cerate_user_by_membership(role, membership)
sign_in(user)
else
raise ArgumentError, "cannot emulate user #{role}"
end
user
end
def cerate_user_by_membership(role, membership = nil)
raise ArgumentError, "cannot emulate #{role} without membership parent" unless membership
if role == :owner && membership.owner
......@@ -30,12 +43,6 @@ module AccessMatchersForController
user = create(:user)
membership.public_send(:"add_#{role}", user)
end
sign_in(user)
else
raise ArgumentError, "cannot emulate user #{role}"
end
user
end
......@@ -47,12 +54,7 @@ module AccessMatchersForController
matcher :be_allowed_for do |role|
match do |action|
user = emulate_user(role, @membership)
begin
action.call(user)
rescue
# Ignore internal exceptions which will be caused in the controller
# In such cases, response.status will be 200.
end
EXPECTED_STATUS_CODE_ALLOWED.include?(response.status)
end
......@@ -68,12 +70,7 @@ module AccessMatchersForController
matcher :be_denied_for do |role|
match do |action|
user = emulate_user(role, @membership)
begin
action.call(user)
rescue
# Ignore internal exceptions which will be caused in the controller
# In such cases, response.status will be 200.
end
EXPECTED_STATUS_CODE_DENIED.include?(response.status)
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment