media_spec.rb 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Whatsapp::Retry::Media, :aggregate_failures do
  4. subject(:instance) { described_class.new(article:) }
  5. let(:group) { create(:group) }
  6. let(:agent) { create(:agent, groups: [group]) }
  7. let(:customer) { create(:customer) }
  8. let(:channel) { create(:whatsapp_channel) }
  9. let(:ticket) { create(:ticket, group:, preferences: { channel_id: channel.id }) }
  10. let(:article) do
  11. create(:whatsapp_article, :with_attachment_media_document, ticket: ticket, created_by: customer).tap do |article|
  12. article.preferences[:whatsapp][:media_error] = true
  13. article.save!
  14. article.attachments.delete_all
  15. end
  16. end
  17. context "when retrying an article's media download" do
  18. context 'with a whatsapp article with failed media download' do
  19. context 'with a successful cloud response' do
  20. before do
  21. allow_any_instance_of(Whatsapp::Incoming::Media).to receive(:download).and_return(['example-content', 'text/plain'])
  22. UserInfo.current_user_id = agent.id
  23. end
  24. it 'creates the attachment' do
  25. expect { instance.process }.to change { article.attachments.count }.by(1)
  26. expect(article.reload.preferences[:whatsapp]).not_to have_key(:media_error)
  27. expect(article.attachments.first.preferences).to include('Mime-Type': 'text/plain')
  28. expect(article.attachments.first.store_file.content).to eq('example-content')
  29. end
  30. end
  31. context 'with a failed cloud response' do
  32. before do
  33. allow_any_instance_of(Whatsapp::Incoming::Media).to receive(:download).and_raise(Whatsapp::Client::CloudAPIError.new('example error', 'for rspec'))
  34. end
  35. it 'raises an error' do
  36. expect { instance.process }.to raise_error(Whatsapp::Client::CloudAPIError)
  37. expect(article.reload.preferences[:whatsapp]).to have_key(:media_error)
  38. expect(article.attachments.count).to eq(0)
  39. end
  40. end
  41. end
  42. context 'with a whatsapp article without failed media download' do
  43. it 'returns a user error' do
  44. article.preferences.delete(:whatsapp)
  45. article.save!
  46. expect { instance.process }.to raise_error(Whatsapp::Retry::Media::ArticleInvalidError, 'Retrying to download the sent media via WhatsApp failed. The given article is not a media article.')
  47. end
  48. end
  49. end
  50. end