user_spec.rb 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. require 'zendesk_api'
  4. RSpec.describe Sequencer::Sequence::Import::Zendesk::User, db_strategy: :reset, sequencer: :sequence do
  5. context 'when importing users from Zendesk' do
  6. let(:groups) do
  7. create_list(:group, 3)
  8. end
  9. let(:merge_resource) do
  10. {}
  11. end
  12. let(:resource) do
  13. ZendeskAPI::User.new(
  14. nil,
  15. {
  16. 'id' => 1_150_734_731,
  17. 'name' => 'Bob Smith',
  18. 'email' => 'zendesk-user@example.com',
  19. 'created_at' => '2015-07-19 22:41:41 UTC',
  20. 'updated_at' => '2021-08-19 13:40:25 UTC',
  21. 'time_zone' => 'Berlin',
  22. 'iana_time_zone' => 'Europe/Berlin',
  23. 'phone' => '00114124',
  24. 'shared_phone_number' => true,
  25. 'photo' => nil,
  26. 'locale_id' => 1,
  27. 'locale' => 'en-US',
  28. 'organization_id' => 154_755_561,
  29. 'role' => 'admin',
  30. 'verified' => true,
  31. 'external_id' => nil,
  32. 'tags' => ['2'],
  33. 'alias' => '',
  34. 'active' => true,
  35. 'shared' => false,
  36. 'shared_agent' => false,
  37. 'last_login_at' => '2021-08-19 13:40:25 UTC',
  38. 'two_factor_auth_enabled' => false,
  39. 'signature' => '',
  40. 'details' => '',
  41. 'notes' => '',
  42. 'role_type' => nil,
  43. 'custom_role_id' => nil,
  44. 'moderator' => true,
  45. 'ticket_restriction' => nil,
  46. 'only_private_comments' => false,
  47. 'restricted_agent' => false,
  48. 'suspended' => false,
  49. 'default_group_id' => 1002,
  50. 'report_csv' => true,
  51. 'user_fields' => {
  52. 'custom_dropdown' => '2',
  53. 'lieblingstier' => 'Hundä',
  54. 'test::example' => '1',
  55. }
  56. }.merge(merge_resource)
  57. )
  58. end
  59. let(:group_map) do
  60. {
  61. 1001 => groups[0].id,
  62. 1002 => groups[1].id,
  63. 1003 => groups[2].id,
  64. }
  65. end
  66. let(:user_group_map) do
  67. {
  68. 1_150_734_731 => [1001, 1002, 1003]
  69. }
  70. end
  71. let(:organization_map) do
  72. {}
  73. end
  74. let(:field_map) do
  75. {
  76. 'User' => {
  77. 'custom_dropdown' => 'custom_dropdown',
  78. 'lieblingstier' => 'lieblingstier',
  79. 'test::example' => 'test_example',
  80. }
  81. }
  82. end
  83. let(:process_payload) do
  84. {
  85. import_job: build_stubbed(:import_job, name: 'Import::Zendesk', payload: {}),
  86. dry_run: false,
  87. resource: resource,
  88. group_map: group_map,
  89. user_group_map: user_group_map,
  90. organization_map: organization_map,
  91. field_map: field_map
  92. }
  93. end
  94. let(:merge_imported_user) { {} }
  95. let(:imported_user) do
  96. {
  97. firstname: 'Bob',
  98. lastname: 'Smith',
  99. login: 'zendesk-user@example.com',
  100. email: 'zendesk-user@example.com',
  101. active: true,
  102. last_login: DateTime.parse('2021-08-19T13:40:25Z'),
  103. custom_dropdown: '2',
  104. lieblingstier: 'Hundä',
  105. test_example: '1',
  106. }.merge(merge_imported_user)
  107. end
  108. before do
  109. create(:object_manager_attribute_select, object_name: 'User', name: 'custom_dropdown')
  110. create(:object_manager_attribute_text, object_name: 'User', name: 'lieblingstier')
  111. create(:object_manager_attribute_text, object_name: 'User', name: 'test_example')
  112. ObjectManager::Attribute.migration_execute
  113. end
  114. context 'with admin user' do
  115. it 'imports user correctly (increased user count)' do
  116. expect { process(process_payload) }.to change(User, :count).by(1)
  117. end
  118. it 'imports user data correctly' do
  119. process(process_payload)
  120. expect(User.last).to have_attributes(imported_user)
  121. end
  122. it 'sets user roles correctly for admin user' do
  123. process(process_payload)
  124. expect(User.last.roles.sort.map(&:name)).to eq %w[Admin Agent]
  125. end
  126. end
  127. context 'with agent user' do
  128. let(:merge_resource) do
  129. {
  130. 'role' => 'agent',
  131. 'restricted_agent' => true,
  132. }
  133. end
  134. it 'sets user roles correctly for non-admin user' do
  135. process(process_payload)
  136. expect(User.last.roles.sort.map(&:name)).to eq ['Agent']
  137. end
  138. it 'sets user groups correctly' do
  139. process(process_payload)
  140. expect(User.last.groups_access('full').sort).to eq groups
  141. end
  142. end
  143. context 'with inactive user' do
  144. let(:merge_resource) do
  145. {
  146. 'active' => false,
  147. }
  148. end
  149. let(:merge_imported_user) do
  150. {
  151. active: false,
  152. }
  153. end
  154. it 'imports user data correctly' do
  155. process(process_payload)
  156. expect(User.last).to have_attributes(imported_user)
  157. end
  158. end
  159. context 'with suspended user' do
  160. let(:merge_resource) do
  161. {
  162. 'suspended' => true,
  163. }
  164. end
  165. let(:merge_imported_user) do
  166. {
  167. active: false,
  168. }
  169. end
  170. it 'imports user data correctly' do
  171. process(process_payload)
  172. expect(User.last).to have_attributes(imported_user)
  173. end
  174. end
  175. end
  176. end