user_spec.rb 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. require 'lib/sequencer/sequence/import/kayako/examples/object_custom_field_values_examples'
  4. RSpec.describe Sequencer::Sequence::Import::Kayako::User, db_strategy: :reset, sequencer: :sequence do
  5. context 'when importing users from Kayako' do
  6. let(:groups) do
  7. create_list(:group, 2)
  8. end
  9. let(:organization) { create(:organization) }
  10. let(:resource) do
  11. {
  12. 'id' => 80_000_602_705,
  13. 'uuid' => 'd4c85a6c-465f-5577-8240-276c0c7fe546',
  14. 'full_name' => 'John Doe',
  15. 'is_enabled' => true,
  16. 'role' => {
  17. 'id' => 2,
  18. 'title' => 'Agent',
  19. 'type' => 'AGENT',
  20. 'is_system' => true,
  21. 'agent_case_access' => 'ALL',
  22. 'created_at' => '2021-08-12T11:48:45+00:00',
  23. 'updated_at' => '2021-08-12T11:48:45+00:00',
  24. 'resource_type' => 'role',
  25. },
  26. 'agent_case_access' => 'INHERIT-FROM-ROLE',
  27. 'organization' => {
  28. 'id' => 1001,
  29. 'resource_type' => 'user'
  30. },
  31. 'teams' => [
  32. {
  33. 'id' => 1001,
  34. 'resource_type' => 'team',
  35. },
  36. {
  37. 'id' => 1002,
  38. 'resource_type' => 'team',
  39. }
  40. ],
  41. 'emails' => [
  42. {
  43. 'id' => 8,
  44. 'email' => 'kayako@example.com',
  45. 'is_primary' => true,
  46. 'is_validated' => false,
  47. 'is_notification_enabled' => false,
  48. 'created_at' => '2021-08-19T08:24:50+00:00',
  49. 'updated_at' => '2021-08-19T08:24:50+00:00',
  50. 'resource_type' => 'identity_email',
  51. },
  52. ],
  53. 'phones' => [
  54. {
  55. 'id' => 2,
  56. 'number' => '+49123456789',
  57. 'is_primary' => true,
  58. 'is_validated' => false,
  59. 'created_at' => '2021-08-19T10:16:26+00:00',
  60. 'updated_at' => '2021-08-19T10:16:33+00:00',
  61. 'resource_type' => 'identity_phone',
  62. }
  63. ],
  64. 'twitter' => [],
  65. 'facebook' => [],
  66. 'time_zone' => nil,
  67. 'time_zone_offset' => nil,
  68. 'last_seen_user_agent' => nil,
  69. 'last_seen_ip' => nil,
  70. 'last_seen_at' => nil,
  71. 'last_active_at' => '2021-08-19T13:16:23+00:00',
  72. 'avatar_updated_at' => nil,
  73. 'last_logged_in_at' => '2021-08-19T13:16:23+00:00',
  74. 'last_activity_at' => nil,
  75. 'created_at' => '2021-08-16T09:01:14+00:00',
  76. 'updated_at' => '2021-08-18T20:37:52+00:00',
  77. 'resource_type' => 'user',
  78. }
  79. end
  80. let(:id_map) do
  81. {
  82. 'Group' => {
  83. 1001 => groups[0].id,
  84. 1002 => groups[1].id,
  85. },
  86. 'Organization' => {
  87. 1001 => organization.id,
  88. },
  89. }
  90. end
  91. let(:process_payload) do
  92. {
  93. import_job: build_stubbed(:import_job, name: 'Import::Kayako', payload: {}),
  94. dry_run: false,
  95. resource: resource,
  96. field_map: {},
  97. id_map: id_map,
  98. default_language: 'en-us',
  99. }
  100. end
  101. let(:imported_user) do
  102. {
  103. firstname: 'John',
  104. lastname: 'Doe',
  105. login: 'kayako@example.com',
  106. email: 'kayako@example.com',
  107. phone: '+49123456789',
  108. active: true,
  109. last_login: DateTime.parse('2021-08-19T13:16:23+00:00'),
  110. }
  111. end
  112. it 'increased user count' do
  113. expect { process(process_payload) }.to change(User, :count).by(1)
  114. end
  115. it 'adds correct user data' do
  116. process(process_payload)
  117. expect(User.last).to have_attributes(imported_user)
  118. end
  119. it 'sets user roles correctly for initiator user' do
  120. Setting.set('import_kayako_endpoint_username', 'kayako@example.com')
  121. process(process_payload)
  122. expect(User.last.roles.sort.map(&:name)).to eq %w[Admin Agent]
  123. end
  124. it 'sets user roles correctly for non-admin user' do
  125. process(process_payload)
  126. expect(User.last.roles.sort.map(&:name)).to eq ['Agent']
  127. end
  128. it 'sets user groups correctly' do
  129. process(process_payload)
  130. expect(User.last.groups_access('full').sort).to eq groups
  131. end
  132. context 'when importing custom fields' do
  133. include_examples 'Object custom field values', object_name: 'User', klass: User
  134. end
  135. end
  136. end