user_spec.rb 5.3 KB

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