whatsapp_message_spec.rb 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Validations::TicketArticleValidator::WhatsappMessage do
  4. it 'is called when a whatsapp ticket article is created' do
  5. expect_any_instance_of(described_class).to receive(:validate)
  6. create(:whatsapp_article)
  7. end
  8. it 'calls validations when an outgoing whatsapp ticket article is created' do
  9. expect_any_instance_of(described_class).to receive(:validate_body)
  10. create(:whatsapp_article, sender_name: 'Agent')
  11. end
  12. it 'does not call validations when an incoming whatsapp ticket article is created' do
  13. expect_any_instance_of(described_class).not_to receive(:validate_body)
  14. create(:whatsapp_article)
  15. end
  16. describe '#validate' do
  17. let(:ticket) { create(:whatsapp_ticket) }
  18. it 'allows blank body with attachment' do
  19. instance = build(:whatsapp_article,
  20. :with_prepended_attachment,
  21. sender_name: 'Agent',
  22. ticket:,
  23. body: '')
  24. described_class.new(instance).validate
  25. expect(instance.errors).to be_blank
  26. end
  27. it 'requires body without attachments' do
  28. instance = build(:whatsapp_article,
  29. sender_name: 'Agent',
  30. ticket:,
  31. body: '')
  32. described_class.new(instance).validate
  33. expect(instance.errors).to have_attributes(
  34. errors: include(have_attributes(message: match(%r{Text or attachment is required})))
  35. )
  36. end
  37. it 'allows body' do
  38. instance = build(:whatsapp_article, :with_prepended_attachment, ticket:, body: 'sample text')
  39. described_class.new(instance).validate
  40. expect(instance.errors).to be_blank
  41. end
  42. it 'does not allow body with some attachment types' do
  43. instance = build(:whatsapp_article,
  44. :with_prepended_attachment,
  45. sender_name: 'Agent',
  46. ticket:,
  47. body: 'sample text',
  48. override_content_type: 'audio/ogg')
  49. described_class.new(instance).validate
  50. expect(instance.errors).to have_attributes(
  51. errors: include(have_attributes(message: match(%r{Audio file is sent without text caption})))
  52. )
  53. end
  54. it 'allows no attachments' do
  55. instance = build(:whatsapp_article,
  56. sender_name: 'Agent',
  57. ticket:)
  58. described_class.new(instance).validate
  59. expect(instance.errors).to be_blank
  60. end
  61. it 'allows 1 attachment' do
  62. instance = build(:whatsapp_article,
  63. :with_prepended_attachment,
  64. sender_name: 'Agent',
  65. ticket:)
  66. described_class.new(instance).validate
  67. expect(instance.errors).to be_blank
  68. end
  69. it 'does not allow multiple attachments' do
  70. instance = build(:whatsapp_article,
  71. :with_prepended_attachment,
  72. sender_name: 'Agent',
  73. ticket:,
  74. attachments_count: 2)
  75. described_class.new(instance).validate
  76. expect(instance.errors).to have_attributes(
  77. errors: include(have_attributes(message: match(%r{Only 1 attachment allowed})))
  78. )
  79. end
  80. it 'checks attachment size' do
  81. stub_const("#{described_class}::CONTENT_TYPE_OPTIONS", [
  82. { size: 10, identifier: :document, label: __('Document file') },
  83. ])
  84. instance = build(:whatsapp_article,
  85. :with_prepended_attachment,
  86. sender_name: 'Agent',
  87. ticket:)
  88. described_class.new(instance).validate
  89. expect(instance.errors).to have_attributes(
  90. errors: include(have_attributes(message: match(%r{File is too big. Document file has to be 10 Bytes or smaller.})))
  91. )
  92. end
  93. it 'checks attachment type' do
  94. instance = build(:whatsapp_article, :with_prepended_attachment,
  95. sender_name: 'Agent',
  96. ticket:,
  97. attachment: File.open('spec/fixtures/files/upload/test.rtf'))
  98. described_class.new(instance).validate
  99. expect(instance.errors).to have_attributes(
  100. errors: include(have_attributes(message: match(%r{File format is not allowed: application/rtf})))
  101. )
  102. end
  103. end
  104. end