user_info_spec.rb 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe UserInfo do
  4. describe '#current_user_id' do
  5. it 'is nil by default' do
  6. expect(described_class.current_user_id).to be_nil
  7. end
  8. it 'takes a User ID as paramter and returns it' do
  9. test_id = 99
  10. described_class.current_user_id = test_id
  11. expect(described_class.current_user_id).to eq(test_id)
  12. end
  13. end
  14. describe '#ensure_current_user_id' do
  15. let(:return_value) { 'Hello World' }
  16. it 'uses and keeps set User IDs' do
  17. test_id = 99
  18. described_class.current_user_id = test_id
  19. described_class.ensure_current_user_id do
  20. expect(described_class.current_user_id).to eq(test_id)
  21. end
  22. expect(described_class.current_user_id).to eq(test_id)
  23. end
  24. it 'sets and resets temporary User ID 1' do
  25. described_class.current_user_id = nil
  26. described_class.ensure_current_user_id do
  27. expect(described_class.current_user_id).to eq(1)
  28. end
  29. expect(described_class.current_user_id).to be_nil
  30. end
  31. it 'resets current_user_id in case of an exception' do
  32. begin
  33. described_class.ensure_current_user_id do
  34. raise 'error'
  35. end
  36. rescue # rubocop:disable Lint/SuppressedException
  37. end
  38. expect(described_class.current_user_id).to be_nil
  39. end
  40. it 'passes return value of given block' do
  41. received = described_class.ensure_current_user_id do
  42. return_value
  43. end
  44. expect(received).to eq(return_value)
  45. end
  46. end
  47. end