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
|
|
|
|
2017-04-27 08:17:10 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe UserInfo do
|
|
|
|
|
|
|
|
describe '#current_user_id' do
|
|
|
|
|
|
|
|
it 'is nil by default' do
|
2022-03-01 08:00:40 +00:00
|
|
|
expect(described_class.current_user_id).to be_nil
|
2017-04-27 08:17:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'takes a User ID as paramter and returns it' do
|
|
|
|
test_id = 99
|
|
|
|
described_class.current_user_id = test_id
|
|
|
|
expect(described_class.current_user_id).to eq(test_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#ensure_current_user_id' do
|
|
|
|
|
2019-07-12 13:59:18 +00:00
|
|
|
let(:return_value) { 'Hello World' }
|
|
|
|
|
2017-04-27 08:17:10 +00:00
|
|
|
it 'uses and keeps set User IDs' do
|
|
|
|
test_id = 99
|
|
|
|
described_class.current_user_id = test_id
|
|
|
|
|
|
|
|
described_class.ensure_current_user_id do
|
|
|
|
expect(described_class.current_user_id).to eq(test_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(described_class.current_user_id).to eq(test_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets and resets temporary User ID 1' do
|
|
|
|
described_class.current_user_id = nil
|
|
|
|
|
|
|
|
described_class.ensure_current_user_id do
|
|
|
|
expect(described_class.current_user_id).to eq(1)
|
|
|
|
end
|
|
|
|
|
2022-03-01 08:00:40 +00:00
|
|
|
expect(described_class.current_user_id).to be_nil
|
2017-04-27 08:17:10 +00:00
|
|
|
end
|
2019-07-12 13:59:18 +00:00
|
|
|
|
|
|
|
it 'resets current_user_id in case of an exception' do
|
|
|
|
begin
|
|
|
|
described_class.ensure_current_user_id do
|
|
|
|
raise 'error'
|
|
|
|
end
|
2020-02-18 19:51:31 +00:00
|
|
|
rescue # rubocop:disable Lint/SuppressedException
|
2019-07-12 13:59:18 +00:00
|
|
|
end
|
|
|
|
|
2022-03-01 08:00:40 +00:00
|
|
|
expect(described_class.current_user_id).to be_nil
|
2019-07-12 13:59:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'passes return value of given block' do
|
|
|
|
|
|
|
|
received = described_class.ensure_current_user_id do
|
|
|
|
return_value
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(received).to eq(return_value)
|
|
|
|
end
|
|
|
|
|
2017-04-27 08:17:10 +00:00
|
|
|
end
|
|
|
|
end
|