article_spec.rb 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. references: '',
  42. updated_at: '2014-11-21 00:21:08',
  43. created_at: '2014-11-21 00:17:41',
  44. type_id: 5,
  45. internal: false,
  46. sender_id: 2
  47. }
  48. end
  49. it 'creates' do
  50. expect(Import::OTRS::Article::AttachmentFactory).to receive(:import)
  51. creates_with(zammad_structure)
  52. end
  53. it 'updates' do
  54. expect(Import::OTRS::Article::AttachmentFactory).to receive(:import)
  55. updates_with(zammad_structure)
  56. end
  57. end
  58. context 'content type with comma' do
  59. let(:object_structure) { load_article_json('content_type_comma') }
  60. let(:zammad_structure) do
  61. {
  62. created_by_id: '3',
  63. updated_by_id: '3',
  64. ticket_id: '730',
  65. id: '3970',
  66. body: 'test #3',
  67. from: '"Betreuter Kunde" <kunde2@kunde.de>,',
  68. to: 'Postmaster',
  69. cc: '',
  70. content_type: 'text/plain',
  71. subject: 'test #3',
  72. in_reply_to: '',
  73. message_id: '',
  74. references: '',
  75. updated_at: '2014-11-21 00:21:08',
  76. created_at: '2014-11-21 00:17:41',
  77. type_id: 5,
  78. internal: false,
  79. sender_id: 2
  80. }
  81. end
  82. it 'creates' do
  83. expect(Import::OTRS::Article::AttachmentFactory).to receive(:import)
  84. creates_with(zammad_structure)
  85. end
  86. it 'updates' do
  87. expect(Import::OTRS::Article::AttachmentFactory).to receive(:import)
  88. updates_with(zammad_structure)
  89. end
  90. end
  91. context 'no content type' do
  92. let(:object_structure) { load_article_json('no_content_type') }
  93. let(:zammad_structure) do
  94. customer = User.find_by(email: 'feedback@otrs.org')
  95. {
  96. created_by_id: customer.id,
  97. updated_by_id: customer.id,
  98. ticket_id: '999',
  99. id: '999',
  100. 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",
  101. from: 'OTRS Feedback <feedback@otrs.org>',
  102. to: 'Your OTRS System <otrs@localhost>',
  103. cc: nil,
  104. subject: 'Welcome to OTRS!',
  105. in_reply_to: nil,
  106. message_id: '<007@localhost>',
  107. references: nil,
  108. updated_at: '2014-06-24 09:32:14',
  109. created_at: '2010-08-02 14:00:00',
  110. type_id: 1,
  111. internal: false,
  112. sender_id: 2
  113. }
  114. end
  115. it 'creates' do
  116. creates_with(zammad_structure)
  117. end
  118. it 'updates' do
  119. updates_with(zammad_structure)
  120. end
  121. end
  122. context 'no article body' do
  123. let(:object_structure) { load_article_json('customer_phone_no_body') }
  124. let(:zammad_structure) do
  125. {
  126. created_by_id: '3',
  127. updated_by_id: 1,
  128. ticket_id: '730',
  129. id: '3970',
  130. body: '',
  131. from: '"Betreuter Kunde" <kunde2@kunde.de>,',
  132. to: 'Postmaster',
  133. cc: '',
  134. content_type: 'text/plain',
  135. subject: 'test #3',
  136. in_reply_to: '',
  137. message_id: '',
  138. references: '',
  139. updated_at: '2014-11-21 00:21:08',
  140. created_at: '2014-11-21 00:17:41',
  141. type_id: 5,
  142. internal: false,
  143. sender_id: 2
  144. }
  145. end
  146. it 'creates' do
  147. creates_with(zammad_structure)
  148. end
  149. it 'updates' do
  150. updates_with(zammad_structure)
  151. end
  152. end
  153. context 'with article created from customer' do
  154. let(:object_structure) { load_article_json('customer_email') }
  155. let(:zammad_structure) do
  156. customer = User.find_by(email: 'kunde2@kunde.de')
  157. {
  158. created_by_id: customer.id,
  159. updated_by_id: customer.id,
  160. ticket_id: '999',
  161. id: '999',
  162. 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",
  163. from: '"Betreuter Kunde" <kunde2@kunde.de>',
  164. to: 'Your OTRS System <otrs@localhost>',
  165. cc: nil,
  166. content_type: 'text/plain',
  167. subject: 'Welcome to OTRS!',
  168. in_reply_to: nil,
  169. message_id: '<007@localhost>',
  170. references: nil,
  171. updated_at: '2014-06-24 09:32:14',
  172. created_at: '2010-08-02 14:00:00',
  173. type_id: 1,
  174. internal: false,
  175. sender_id: 2
  176. }
  177. end
  178. it 'creates' do
  179. creates_with(zammad_structure)
  180. end
  181. it 'updates' do
  182. updates_with(zammad_structure)
  183. end
  184. end
  185. context 'legacy article keys (lower then OTRS 6)' do
  186. let(:object_structure) { load_article_json('legacy_article_keys') }
  187. let(:zammad_structure) do
  188. {
  189. created_by_id: '3',
  190. updated_by_id: '3',
  191. ticket_id: '730',
  192. id: '3970',
  193. body: 'test #3',
  194. from: '"Betreuter Kunde" <kunde2@kunde.de>,',
  195. to: 'Postmaster',
  196. cc: '',
  197. content_type: 'text/plain',
  198. subject: 'test #3',
  199. in_reply_to: '',
  200. message_id: '',
  201. references: '',
  202. updated_at: '2015-11-21 00:21:08',
  203. created_at: '2015-11-21 00:17:41',
  204. type_id: 5,
  205. internal: false,
  206. sender_id: 2
  207. }
  208. end
  209. it 'creates' do
  210. creates_with(zammad_structure)
  211. end
  212. it 'updates' do
  213. updates_with(zammad_structure)
  214. end
  215. end
  216. end