attachment_factory_spec.rb 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. require 'lib/import/import_factory_examples'
  4. RSpec.describe Import::OTRS::Article::AttachmentFactory do
  5. let(:start_import) do
  6. described_class.import(
  7. attachments: attachments,
  8. local_article: local_article
  9. )
  10. end
  11. let(:attachments) do
  12. [
  13. load_attachment_json('default'),
  14. load_attachment_json('default'),
  15. load_attachment_json('default')
  16. ]
  17. end
  18. let(:local_article) { instance_double(Ticket::Article, ticket_id: 1337, id: 42) }
  19. def load_attachment_json(file)
  20. json_fixture("import/otrs/article/attachment/#{file}")
  21. end
  22. def import_expectations
  23. expect(Store).to receive(:create!).exactly(3).times.with(hash_including(
  24. object: 'Ticket::Article',
  25. o_id: local_article.id,
  26. ))
  27. end
  28. def article_attachment_expectations(article_attachments)
  29. allow(local_article).to receive(:attachments).and_return(article_attachments)
  30. end
  31. it_behaves_like 'Import factory'
  32. it 'imports' do
  33. article_attachment_expectations([])
  34. import_expectations
  35. start_import
  36. end
  37. it 'deletes old and reimports' do
  38. dummy_attachment = double
  39. expect(dummy_attachment).to receive(:delete)
  40. article_attachment_expectations([dummy_attachment])
  41. import_expectations
  42. start_import
  43. end
  44. it 'skips import for same count' do
  45. article_attachment_expectations([1, 2, 3])
  46. expect(Store).not_to receive(:create!)
  47. start_import
  48. end
  49. end