user_info.rb 992 B

1234567891011121314151617181920212223
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. # This file registers a before and after each hook callback that
  3. # resets the stored current_user_id in the UserInfo which will otherwise
  4. # persists across multiple examples.
  5. # This can lead to issues where actions were performed by a user created
  6. # via a FactoryBot factory which will get removed after the example is
  7. # completed. The UserInfo.current_user_id will persist which leads to e.g.
  8. # DB ForeignKey violation errors.
  9. # If a `:current_user_id` metadata argument is set the initial value for
  10. # UserInfo.current_user_id will be set to the arguments given value
  11. # if it's a Proc it will get evaluated
  12. RSpec.configure do |config|
  13. config.before do |example|
  14. current_user_id = example.metadata[:current_user_id]
  15. UserInfo.current_user_id = current_user_id.is_a?(Proc) ? instance_exec(&current_user_id) : current_user_id
  16. end
  17. config.after do |_example|
  18. UserInfo.current_user_id = nil
  19. end
  20. end