agent_spec.rb 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Chat::Agent, type: :model do
  4. describe '.state' do
  5. let(:user) { create(:agent) }
  6. context 'when no record exists for User' do
  7. it 'returns false' do
  8. expect(described_class.state(1337)).to be(false)
  9. end
  10. end
  11. context 'when active flag is set to true' do
  12. before do
  13. create(:'chat/agent', active: true, updated_by: user)
  14. end
  15. it 'returns true' do
  16. expect(described_class.state(user.id)).to be(true)
  17. end
  18. end
  19. context 'when active flag is set to false' do
  20. before do
  21. create(:'chat/agent', active: false, updated_by: user)
  22. end
  23. it 'returns false' do
  24. expect(described_class.state(user.id)).to be(false)
  25. end
  26. end
  27. context 'when setting state for not existing record' do
  28. it 'creates a record' do
  29. expect { described_class.state(user.id, true) }.to change { described_class.exists?(updated_by: user) }.from(false).to(true)
  30. end
  31. end
  32. context 'when setting same state for record' do
  33. let(:record) { create(:'chat/agent', active: true, updated_by: user) }
  34. before do
  35. # avoid race condition with same updated_at time
  36. record
  37. travel_to 5.minutes.from_now
  38. end
  39. it 'updates updated_at timestamp' do
  40. expect { described_class.state(record.updated_by_id, record.active) }.to change { record.reload.updated_at }
  41. end
  42. it 'returns false' do
  43. expect(described_class.state(record.updated_by_id, record.active)).to be(false)
  44. end
  45. end
  46. context 'when setting different state for record' do
  47. let(:record) { create(:'chat/agent', active: true, updated_by: user) }
  48. before do
  49. # avoid race condition with same updated_at time
  50. record
  51. travel_to 5.minutes.from_now
  52. end
  53. it 'updates updated_at timestamp' do
  54. expect { described_class.state(record.updated_by_id, !record.active) }.to change { record.reload.updated_at }
  55. end
  56. it 'returns true' do
  57. expect(described_class.state(record.updated_by_id, !record.active)).to be(true)
  58. end
  59. end
  60. end
  61. end