ticket_spec.rb 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Import::OTRS::Ticket 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. allow(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_ticket_json(file)
  17. json_fixture("import/otrs/ticket/#{file}")
  18. end
  19. def import_backend_expectations
  20. expect(Import::OTRS::ArticleCustomerFactory).to receive(:import)
  21. expect(Import::OTRS::ArticleFactory).to receive(:import)
  22. expect(Import::OTRS::HistoryFactory).to receive(:import)
  23. allow(User).to receive(:find_by).and_return(nil)
  24. # needed, otherwise 'ActiveRecord::UnknownAttributeError' for
  25. # DynamicFields will arise
  26. allow(Import::OTRS::DynamicFieldFactory).to receive('skip_field?').and_return(true)
  27. end
  28. let(:import_object) { Ticket }
  29. let(:existing_object) { instance_double(import_object) }
  30. let(:start_import_test) { described_class.new(object_structure) }
  31. context 'default' do
  32. let(:object_structure) { load_ticket_json('default') }
  33. let(:zammad_structure) do
  34. {
  35. title: 'test #3',
  36. owner_id: 1,
  37. customer_id: 1,
  38. created_by_id: '3',
  39. updated_by_id: 1,
  40. updated_at: '2014-11-21 00:21:08',
  41. created_at: '2014-11-21 00:17:40',
  42. number: '20141121305000012',
  43. group_id: '1',
  44. state_id: '2',
  45. priority_id: '3',
  46. id: '730',
  47. close_at: '2014-11-21 00:21:08'
  48. }
  49. end
  50. it 'creates' do
  51. import_backend_expectations
  52. creates_with(zammad_structure)
  53. end
  54. it 'updates' do
  55. import_backend_expectations
  56. updates_with(zammad_structure)
  57. end
  58. end
  59. context 'no title' do
  60. let(:object_structure) { load_ticket_json('no_title') }
  61. let(:zammad_structure) do
  62. {
  63. title: '**EMPTY**',
  64. owner_id: 1,
  65. customer_id: 1,
  66. created_by_id: '3',
  67. updated_by_id: 1,
  68. updated_at: '2014-11-21 00:21:08',
  69. created_at: '2014-11-21 00:17:40',
  70. number: '20141121305000012',
  71. group_id: '1',
  72. state_id: '2',
  73. priority_id: '3',
  74. id: '730',
  75. close_at: '2014-11-21 00:21:08'
  76. }
  77. end
  78. it 'creates' do
  79. import_backend_expectations
  80. creates_with(zammad_structure)
  81. end
  82. it 'updates' do
  83. import_backend_expectations
  84. updates_with(zammad_structure)
  85. end
  86. end
  87. context 'unknown customer' do
  88. let(:object_structure) { load_ticket_json('unknown_customer') }
  89. let(:zammad_structure) do
  90. {
  91. owner_id: 1,
  92. customer_id: 1337,
  93. created_by_id: 1337,
  94. updated_by_id: 1,
  95. updated_at: '2014-07-17 14:29:44',
  96. created_at: '2014-07-17 14:29:43',
  97. number: '2014071754002471',
  98. group_id: '2',
  99. state_id: '1',
  100. priority_id: '3',
  101. title: 'Ask me about performance',
  102. id: '540'
  103. }
  104. end
  105. def article_based_customer_expectation
  106. user = instance_double(User)
  107. allow(user).to receive(:id).and_return(1337)
  108. allow(Import::OTRS::ArticleCustomer).to receive(:find).with(hash_including('From' => '458@company-sales.com')).and_return(user)
  109. end
  110. it 'creates' do
  111. import_backend_expectations
  112. article_based_customer_expectation
  113. creates_with(zammad_structure)
  114. end
  115. it 'updates' do
  116. import_backend_expectations
  117. article_based_customer_expectation
  118. updates_with(zammad_structure)
  119. end
  120. end
  121. context 'pending state' do
  122. let(:object_structure) { load_ticket_json('pending_state') }
  123. let(:zammad_structure) do
  124. {
  125. title: 'test #3',
  126. owner_id: 1,
  127. customer_id: 1,
  128. created_by_id: '3',
  129. updated_by_id: 1,
  130. updated_at: '2014-11-21 00:21:08',
  131. created_at: '2014-11-21 00:17:40',
  132. number: '20141121305000012',
  133. group_id: '1',
  134. state_id: Ticket::State.find_by(name: 'pending close').id.to_s,
  135. pending_time: Time.zone.at(Time.zone.parse('2014-12-06 12:50:11').to_i),
  136. priority_id: '3',
  137. id: '730',
  138. close_at: '2014-11-21 00:21:08'
  139. }
  140. end
  141. it 'creates' do
  142. import_backend_expectations
  143. creates_with(zammad_structure)
  144. end
  145. it 'updates' do
  146. import_backend_expectations
  147. updates_with(zammad_structure)
  148. end
  149. end
  150. context 'with removed state' do
  151. let(:object_structure) { load_ticket_json('removed_state') }
  152. it 'skips', :aggregate_failures do
  153. expect(Import::OTRS::ArticleCustomerFactory).not_to receive(:import)
  154. expect(Import::OTRS::ArticleFactory).not_to receive(:import)
  155. expect(Import::OTRS::HistoryFactory).not_to receive(:import)
  156. expect(import_object).not_to receive(:new)
  157. start_import_test
  158. expect(Ticket.find_by(title: 'test #3 - removed')).to be_nil
  159. end
  160. end
  161. end