article_spec.rb 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Import::OTRS::Article do
  4. def creates_with(zammad_structure)
  5. allow(import_object).to receive(:new).with(zammad_structure).and_call_original
  6. expect_any_instance_of(import_object).to receive(:save)
  7. expect_any_instance_of(described_class).to receive(:reset_primary_key_sequence)
  8. start_import_test
  9. end
  10. def updates_with(zammad_structure)
  11. allow(import_object).to receive(:find_by).and_return(existing_object)
  12. expect(existing_object).to receive(:update!).with(zammad_structure)
  13. expect(import_object).not_to receive(:new)
  14. start_import_test
  15. end
  16. def load_article_json(file)
  17. json_fixture("import/otrs/article/#{file}")
  18. end
  19. let(:import_object) { Ticket::Article }
  20. let(:existing_object) { instance_double(import_object) }
  21. let(:start_import_test) { described_class.new(object_structure) }
  22. before do
  23. Import::OTRS::ArticleCustomerFactory.import([object_structure])
  24. end
  25. context 'customer phone' do
  26. let(:object_structure) { load_article_json('customer_phone_attachment') }
  27. let(:zammad_structure) do
  28. {
  29. created_by_id: '3',
  30. updated_by_id: '3',
  31. ticket_id: '730',
  32. id: '3970',
  33. body: 'test #3',
  34. from: '"Betreuter Kunde" <kunde2@kunde.de>,',
  35. to: 'Postmaster',
  36. cc: '',
  37. content_type: 'text/plain',
  38. subject: 'test #3',
  39. in_reply_to: '',
  40. message_id: '',
  41. updated_at: '2014-11-21 00:21:08',
  42. created_at: '2014-11-21 00:17:41',
  43. type_id: 5,
  44. internal: false,
  45. sender_id: 2
  46. }
  47. end
  48. it 'creates' do
  49. expect(Import::OTRS::Article::AttachmentFactory).to receive(:import)
  50. creates_with(zammad_structure)
  51. end
  52. it 'updates' do
  53. expect(Import::OTRS::Article::AttachmentFactory).to receive(:import)
  54. updates_with(zammad_structure)
  55. end
  56. end
  57. context 'content type with comma' do
  58. let(:object_structure) { load_article_json('content_type_comma') }
  59. let(:zammad_structure) do
  60. {
  61. created_by_id: '3',
  62. updated_by_id: '3',
  63. ticket_id: '730',
  64. id: '3970',
  65. body: 'test #3',
  66. from: '"Betreuter Kunde" <kunde2@kunde.de>,',
  67. to: 'Postmaster',
  68. cc: '',
  69. content_type: 'text/plain',
  70. subject: 'test #3',
  71. in_reply_to: '',
  72. message_id: '',
  73. updated_at: '2014-11-21 00:21:08',
  74. created_at: '2014-11-21 00:17:41',
  75. type_id: 5,
  76. internal: false,
  77. sender_id: 2
  78. }
  79. end
  80. it 'creates' do
  81. expect(Import::OTRS::Article::AttachmentFactory).to receive(:import)
  82. creates_with(zammad_structure)
  83. end
  84. it 'updates' do
  85. expect(Import::OTRS::Article::AttachmentFactory).to receive(:import)
  86. updates_with(zammad_structure)
  87. end
  88. end
  89. context 'no content type' do
  90. let(:object_structure) { load_article_json('no_content_type') }
  91. let(:zammad_structure) do
  92. customer = User.find_by(email: 'feedback@otrs.org')
  93. {
  94. created_by_id: customer.id,
  95. updated_by_id: customer.id,
  96. ticket_id: '999',
  97. id: '999',
  98. body: "Welcome!\n\nThank you for installing OTRS.\n\nYou will find updates and patches at http://www.otrs.com/open-source/.\nOnline documentation is available at http://doc.otrs.org/.\nYou can also use our mailing lists http://lists.otrs.org/\nor our forums at http://forums.otrs.org/\n\nRegards,\n\nThe OTRS Project\n",
  99. from: 'OTRS Feedback <feedback@otrs.org>',
  100. to: 'Your OTRS System <otrs@localhost>',
  101. cc: nil,
  102. subject: 'Welcome to OTRS!',
  103. in_reply_to: nil,
  104. message_id: '<007@localhost>',
  105. updated_at: '2014-06-24 09:32:14',
  106. created_at: '2010-08-02 14:00:00',
  107. type_id: 1,
  108. internal: false,
  109. sender_id: 2
  110. }
  111. end
  112. it 'creates' do
  113. creates_with(zammad_structure)
  114. end
  115. it 'updates' do
  116. updates_with(zammad_structure)
  117. end
  118. end
  119. context 'no article body' do
  120. let(:object_structure) { load_article_json('customer_phone_no_body') }
  121. let(:zammad_structure) do
  122. {
  123. created_by_id: '3',
  124. updated_by_id: 1,
  125. ticket_id: '730',
  126. id: '3970',
  127. body: '',
  128. from: '"Betreuter Kunde" <kunde2@kunde.de>,',
  129. to: 'Postmaster',
  130. cc: '',
  131. content_type: 'text/plain',
  132. subject: 'test #3',
  133. in_reply_to: '',
  134. message_id: '',
  135. updated_at: '2014-11-21 00:21:08',
  136. created_at: '2014-11-21 00:17:41',
  137. type_id: 5,
  138. internal: false,
  139. sender_id: 2
  140. }
  141. end
  142. it 'creates' do
  143. creates_with(zammad_structure)
  144. end
  145. it 'updates' do
  146. updates_with(zammad_structure)
  147. end
  148. end
  149. context 'with article created from customer' do
  150. let(:object_structure) { load_article_json('customer_email') }
  151. let(:zammad_structure) do
  152. customer = User.find_by(email: 'kunde2@kunde.de')
  153. {
  154. created_by_id: customer.id,
  155. updated_by_id: customer.id,
  156. ticket_id: '999',
  157. id: '999',
  158. body: "Welcome!\n\nThank you for installing OTRS.\n\nYou will find updates and patches at http://www.otrs.com/open-source/.\nOnline documentation is available at http://doc.otrs.org/.\nYou can also use our mailing lists http://lists.otrs.org/\nor our forums at http://forums.otrs.org/\n\nRegards,\n\nThe OTRS Project\n",
  159. from: '"Betreuter Kunde" <kunde2@kunde.de>',
  160. to: 'Your OTRS System <otrs@localhost>',
  161. cc: nil,
  162. content_type: 'text/plain',
  163. subject: 'Welcome to OTRS!',
  164. in_reply_to: nil,
  165. message_id: '<007@localhost>',
  166. updated_at: '2014-06-24 09:32:14',
  167. created_at: '2010-08-02 14:00:00',
  168. type_id: 1,
  169. internal: false,
  170. sender_id: 2
  171. }
  172. end
  173. it 'creates' do
  174. creates_with(zammad_structure)
  175. end
  176. it 'updates' do
  177. updates_with(zammad_structure)
  178. end
  179. end
  180. context 'legacy article keys (lower then OTRS 6)' do
  181. let(:object_structure) { load_article_json('legacy_article_keys') }
  182. let(:zammad_structure) do
  183. {
  184. created_by_id: '3',
  185. updated_by_id: '3',
  186. ticket_id: '730',
  187. id: '3970',
  188. body: 'test #3',
  189. from: '"Betreuter Kunde" <kunde2@kunde.de>,',
  190. to: 'Postmaster',
  191. cc: '',
  192. content_type: 'text/plain',
  193. subject: 'test #3',
  194. in_reply_to: '',
  195. message_id: '',
  196. updated_at: '2015-11-21 00:21:08',
  197. created_at: '2015-11-21 00:17:41',
  198. type_id: 5,
  199. internal: false,
  200. sender_id: 2
  201. }
  202. end
  203. it 'creates' do
  204. creates_with(zammad_structure)
  205. end
  206. it 'updates' do
  207. updates_with(zammad_structure)
  208. end
  209. end
  210. end