2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2019-09-18 17:25:04 +00:00
|
|
|
# This file registers a before and after each hook callback that
|
2018-03-28 05:33:41 +00:00
|
|
|
# resets the stored current_user_id in the UserInfo which will otherwise
|
|
|
|
# persists across multiple examples.
|
|
|
|
# This can lead to issues where actions were performed by a user created
|
|
|
|
# via a FactoryBot factory which will get removed after the example is
|
|
|
|
# completed. The UserInfo.current_user_id will persist which leads to e.g.
|
|
|
|
# DB ForeignKey violation errors.
|
2019-09-18 17:25:04 +00:00
|
|
|
# If a `:current_user_id` metadata argument is set the initial value for
|
|
|
|
# UserInfo.current_user_id will be set to the arguments given value
|
2020-07-31 13:41:26 +00:00
|
|
|
# if it's a Proc it will get evaluated
|
2018-03-28 05:33:41 +00:00
|
|
|
RSpec.configure do |config|
|
2019-03-13 23:51:22 +00:00
|
|
|
|
2021-07-19 12:24:09 +00:00
|
|
|
config.before do |example|
|
2020-07-31 13:41:26 +00:00
|
|
|
current_user_id = example.metadata[:current_user_id]
|
|
|
|
UserInfo.current_user_id = current_user_id.is_a?(Proc) ? instance_exec(¤t_user_id) : current_user_id
|
2019-09-18 17:25:04 +00:00
|
|
|
end
|
|
|
|
|
2021-07-19 12:24:09 +00:00
|
|
|
config.after do |_example|
|
2019-09-18 17:25:04 +00:00
|
|
|
UserInfo.current_user_id = nil
|
2019-03-13 23:51:22 +00:00
|
|
|
end
|
2018-03-28 05:33:41 +00:00
|
|
|
end
|