retry_media_download_spec.rb 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Mutations::Ticket::Article::RetryMediaDownload, :aggregate_failures, type: :graphql do
  4. let(:query) do
  5. <<~QUERY
  6. mutation ticketArticleRetryMediaDownload($articleId: ID!) {
  7. ticketArticleRetryMediaDownload(articleId: $articleId) {
  8. success
  9. errors {
  10. message
  11. field
  12. }
  13. }
  14. }
  15. QUERY
  16. end
  17. let(:group) { create(:group) }
  18. let(:agent) { create(:agent, groups: [group]) }
  19. let(:customer) { create(:customer) }
  20. let(:channel) { create(:whatsapp_channel) }
  21. let(:ticket) { create(:ticket, group:, preferences: { channel_id: channel.id }) }
  22. let(:article) do
  23. create(:whatsapp_article, :with_attachment_media_document, ticket: ticket, created_by: customer).tap do |article|
  24. article.preferences[:whatsapp][:media_error] = true
  25. article.save!
  26. article.attachments.delete_all
  27. end
  28. end
  29. let(:variables) { { articleId: gql.id(article) } }
  30. context "when retrying an article's media download" do
  31. context 'with an agent', authenticated_as: :agent do
  32. context 'with a whatsapp article with failed media download' do
  33. it 'creates the attachment' do
  34. expect_any_instance_of(Whatsapp::Retry::Media).to receive(:process).and_return(true) # rubocop:disable RSpec/StubbedMock
  35. gql.execute(query, variables: variables)
  36. expect(gql.result.data['success']).to be true
  37. end
  38. end
  39. context 'with a whatsapp article without failed media download' do
  40. it 'returns a user error' do
  41. article.preferences.delete(:whatsapp)
  42. article.save!
  43. gql.execute(query, variables: variables)
  44. expect(gql.result.data['success']).not_to be true
  45. expect(gql.result.data['errors'].first).to include(
  46. { 'message' => 'Retrying to download the sent media via WhatsApp failed. The given article is not a media article.' }
  47. )
  48. end
  49. end
  50. end
  51. context 'with a customer', authenticated_as: :customer do
  52. it 'raises an error' do
  53. gql.execute(query, variables: variables)
  54. expect(gql.result.error_type).to eq(Exceptions::Forbidden)
  55. end
  56. end
  57. end
  58. end