delete_spec.rb 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Service::User::Device::Delete do
  4. subject(:service) { described_class.new(user: agent, device: device) }
  5. context 'with given user having one device and one related session' do
  6. let(:agent) { create(:agent) }
  7. let(:device) { create(:user_device, user_id: agent.id) }
  8. it 'destroys the device and the related session' do
  9. create(:session,
  10. data: {
  11. 'user_id' => agent.id,
  12. 'user_device_fingerprint' => device.fingerprint,
  13. 'persistent' => true
  14. })
  15. expect { service.execute }.to change(UserDevice, :count).by(-1).and change(Session, :count).by(-1)
  16. end
  17. end
  18. context 'with given user having multiple devices and multiple related session' do
  19. let(:agent) { create(:agent) }
  20. let(:device) { create(:user_device, user_id: agent.id) }
  21. let(:agents) { create_list(:agent, Faker::Number.within(range: 2..42)) } # rubocop:disable Zammad/FakerUnique
  22. let(:devices) do
  23. agents.map do |agent|
  24. create(:user_device, user_id: agent.id)
  25. end
  26. end
  27. it 'destroys only the selected device and all the related session' do
  28. sessions = Faker::Number.within(range: 2..42) # rubocop:disable Zammad/FakerUnique
  29. create_list(:session, sessions,
  30. data: {
  31. 'user_id' => agent.id,
  32. 'user_device_fingerprint' => device.fingerprint,
  33. 'persistent' => true
  34. })
  35. devices.each do |device|
  36. create_list(:session, Faker::Number.within(range: 2..42), # rubocop:disable Zammad/FakerUnique
  37. data: {
  38. 'user_id' => device.user_id,
  39. 'user_device_fingerprint' => device.fingerprint,
  40. 'persistent' => true
  41. })
  42. end
  43. expect { service.execute }.to change(UserDevice, :count).by(-1).and change(Session, :count).by(-1 * sessions)
  44. end
  45. end
  46. end