Commit b7634cfb authored by Sean McGivern's avatar Sean McGivern

Merge branch...

Merge branch '332442-flaky-test-ee-spec-requests-api-vulnerabilities_spec-rb-166-api-vulnerabilities-post' into 'master'

RSpec access matcher: Remove patched user after matcher is run

See merge request gitlab-org/gitlab!63099
parents 04e26c94 17f9c9e3
# frozen_string_literal: true # frozen_string_literal: true
module AccessMatchersHelpers module AccessMatchersHelpers
include Gitlab::Utils::StrongMemoize
USER_ACCESSOR_METHOD_NAME = 'user' USER_ACCESSOR_METHOD_NAME = 'user'
def provide_user(role, membership = nil) def provide_user(role, membership = nil)
...@@ -61,11 +63,6 @@ module AccessMatchersHelpers ...@@ -61,11 +63,6 @@ module AccessMatchersHelpers
# (or defined by `method_name`) method generated by `let` definition in example group before it's used by `subject`. # (or defined by `method_name`) method generated by `let` definition in example group before it's used by `subject`.
# This override is per concrete example only because the example group class gets re-created for each example. # This override is per concrete example only because the example group class gets re-created for each example.
instance_eval(<<~CODE, __FILE__, __LINE__ + 1) instance_eval(<<~CODE, __FILE__, __LINE__ + 1)
if instance_variable_get(:@__#{USER_ACCESSOR_METHOD_NAME}_patched)
raise ArgumentError, 'An access matcher be_allowed_for/be_denied_for can be used only once per example (`it` block)'
end
instance_variable_set(:@__#{USER_ACCESSOR_METHOD_NAME}_patched, true)
def #{USER_ACCESSOR_METHOD_NAME} def #{USER_ACCESSOR_METHOD_NAME}
@#{USER_ACCESSOR_METHOD_NAME} ||= User.find(#{user.id}) @#{USER_ACCESSOR_METHOD_NAME} ||= User.find(#{user.id})
end end
...@@ -81,6 +78,13 @@ module AccessMatchersHelpers ...@@ -81,6 +78,13 @@ module AccessMatchersHelpers
end end
end end
def reset_matcher_environment
instance_eval(<<~CODE, __FILE__, __LINE__ + 1)
clear_memoization(:#{USER_ACCESSOR_METHOD_NAME})
undef #{USER_ACCESSOR_METHOD_NAME} if defined? user
CODE
end
def run_matcher(action, role, membership, owned_objects) def run_matcher(action, role, membership, owned_objects)
raise_if_non_block_expectation!(action) raise_if_non_block_expectation!(action)
...@@ -91,5 +95,7 @@ module AccessMatchersHelpers ...@@ -91,5 +95,7 @@ module AccessMatchersHelpers
else else
action.call action.call
end end
reset_matcher_environment
end end
end 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