avatar_create_job_spec.rb 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe AvatarCreateJob, type: :job do
  4. subject(:perform) { described_class.perform_now user }
  5. let(:user) { create(:user) }
  6. let(:hash) { SecureRandom.hex(16) }
  7. context 'with avatar auto detection' do
  8. before do
  9. allow(Avatar).to receive(:auto_detection).and_return(avatar)
  10. user
  11. travel 1.minute
  12. end
  13. context 'when succesful' do
  14. let(:avatar) { create(:avatar, o_id: user.id, store_hash: hash) }
  15. it 'changes user image' do
  16. expect { perform }
  17. .to change { user.reload.image }
  18. .from(nil)
  19. end
  20. it 'touches user' do
  21. expect { perform }
  22. .to change { user.reload.updated_at }
  23. end
  24. end
  25. context 'when unsuccesful' do
  26. let(:avatar) { nil }
  27. it 'does not change user image' do
  28. expect { perform }
  29. .not_to change { user.reload.image }
  30. .from(nil)
  31. end
  32. it 'does not touch user' do
  33. expect { perform }
  34. .not_to change { user.reload.updated_at }
  35. end
  36. end
  37. end
  38. it 'retries on exception' do
  39. allow(Avatar).to receive(:auto_detection).and_raise(RuntimeError)
  40. perform
  41. expect(described_class).to have_been_enqueued
  42. end
  43. end