user_spec.rb 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Import::OTRS::User do
  4. def creates_with(zammad_structure)
  5. allow(import_object).to receive(:find_by).and_return(nil)
  6. allow(import_object).to receive(:new).with(zammad_structure).and_call_original
  7. expect_any_instance_of(import_object).to receive(:save)
  8. expect_any_instance_of(described_class).to receive(:reset_primary_key_sequence)
  9. start_import_test
  10. end
  11. def updates_with(zammad_structure)
  12. allow(import_object).to receive(:find_by).and_return(existing_object)
  13. # we delete the :role_ids from the zammad_structure to make sure that
  14. # a) role_ids call returns the initial role_ids
  15. # b) and update! gets called without them
  16. allow(existing_object).to receive(:role_ids).and_return(zammad_structure.delete(:role_ids))
  17. expect(existing_object).to receive(:update!).with(zammad_structure)
  18. expect(import_object).not_to receive(:new)
  19. start_import_test
  20. end
  21. def role_delete_expecations(role_ids); end
  22. def load_user_json(file)
  23. json_fixture("import/otrs/user/#{file}")
  24. end
  25. def prepare_expectations
  26. requester_expectations
  27. user_expectations
  28. end
  29. def user_expectations
  30. allow(User).to receive(:where).and_return([])
  31. end
  32. # this is really bad and should get improved!
  33. # these are integration tests that will likely fail
  34. # soon - sorry :)
  35. def requester_expectations
  36. queue_list = [
  37. {
  38. 'QueueID' => '1',
  39. 'GroupID' => '2',
  40. }
  41. ]
  42. allow(Import::OTRS::Requester).to receive(:load).with('Queue').and_return(queue_list)
  43. group_list = [
  44. {
  45. 'ID' => '2',
  46. 'Name' => 'user',
  47. },
  48. {
  49. 'ID' => '3',
  50. 'Name' => 'admin',
  51. },
  52. ]
  53. allow(Import::OTRS::Requester).to receive(:load).with('Group').and_return(group_list)
  54. role_list = [{ 'ID' => '3', 'GroupIDs' => { '2' => ['rw'], '3' => ['rw'] } }]
  55. allow(Import::OTRS::Requester).to receive(:load).with('Role').and_return(role_list)
  56. end
  57. let(:import_object) { User }
  58. let(:existing_object) { instance_double(import_object) }
  59. let(:start_import_test) { described_class.new(object_structure) }
  60. context 'default' do
  61. let(:object_structure) { load_user_json('default') }
  62. let(:zammad_structure) do
  63. {
  64. created_by_id: 1,
  65. updated_by_id: 1,
  66. active: true,
  67. source: 'OTRS Import',
  68. role_ids: [2, 1],
  69. group_ids: ['1'],
  70. password: '{sha2}9faaba2ab242a99bbb6992e9424386375f6757c17e6484ae570f39d9cad9f28ea',
  71. updated_at: '2014-04-28 10:53:18',
  72. created_at: '2014-04-28 10:53:18',
  73. id: '1',
  74. email: 'root@localhost',
  75. firstname: 'Admin',
  76. lastname: 'OTRS',
  77. login: 'root@localhost'
  78. }
  79. end
  80. it 'creates' do
  81. prepare_expectations
  82. creates_with(zammad_structure)
  83. end
  84. it 'updates' do
  85. prepare_expectations
  86. updates_with(zammad_structure)
  87. end
  88. end
  89. context 'no groups' do
  90. let(:object_structure) { load_user_json('no_groups') }
  91. let(:zammad_structure) do
  92. {
  93. created_by_id: 1,
  94. updated_by_id: 1,
  95. active: true,
  96. source: 'OTRS Import',
  97. role_ids: [2, 1],
  98. group_ids: [],
  99. password: '{sha2}9edb001ad7900daea0622d89225c9ca729749fd12ae5ea044f072d1b7c56c8cc',
  100. updated_at: '2014-11-14 00:53:20',
  101. created_at: '2014-11-14 00:53:20',
  102. id: '6',
  103. email: 'agent-2-for-role-2@example.com',
  104. firstname: 'agent-2-for-role-2',
  105. lastname: 'agent-2-for-role-2',
  106. login: 'agent-2-for-role-2'
  107. }
  108. end
  109. it 'creates' do
  110. prepare_expectations
  111. creates_with(zammad_structure)
  112. end
  113. it 'updates' do
  114. prepare_expectations
  115. updates_with(zammad_structure)
  116. end
  117. end
  118. context 'capital email' do
  119. let(:object_structure) { load_user_json('capital_email') }
  120. let(:zammad_structure) do
  121. {
  122. created_by_id: 1,
  123. updated_by_id: 1,
  124. active: true,
  125. source: 'OTRS Import',
  126. role_ids: [2, 1],
  127. group_ids: ['1'],
  128. password: '{sha2}9faaba2ab242a99bbb6992e9424386375f6757c17e6484ae570f39d9cad9f28ea',
  129. updated_at: '2014-04-28 10:53:18',
  130. created_at: '2014-04-28 10:53:18',
  131. id: '1',
  132. email: 'root@localhost',
  133. firstname: 'Admin',
  134. lastname: 'OTRS',
  135. login: 'root@localhost'
  136. }
  137. end
  138. it 'creates' do
  139. prepare_expectations
  140. creates_with(zammad_structure)
  141. end
  142. it 'updates' do
  143. prepare_expectations
  144. updates_with(zammad_structure)
  145. end
  146. end
  147. context 'regular user with camel case login' do
  148. let(:object_structure) { load_user_json('camel_case_login') }
  149. let(:zammad_structure) do
  150. {
  151. created_by_id: 1,
  152. updated_by_id: 1,
  153. active: true,
  154. source: 'OTRS Import',
  155. role_ids: [2, 1],
  156. group_ids: ['1'],
  157. password: '{sha2}9faaba2ab242a99bbb6992e9424386375f6757c17e6484ae570f39d9cad9f28ea',
  158. updated_at: '2014-04-28 10:53:18',
  159. created_at: '2014-04-28 10:53:18',
  160. id: '1',
  161. email: 'root@localhost',
  162. firstname: 'Admin',
  163. lastname: 'OTRS',
  164. login: 'root@localhost'
  165. }
  166. end
  167. it 'creates' do
  168. prepare_expectations
  169. creates_with(zammad_structure)
  170. end
  171. it 'updates' do
  172. prepare_expectations
  173. updates_with(zammad_structure)
  174. end
  175. end
  176. end