agent_spec.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Sequencer::Sequence::Import::Freshdesk::Agent, sequencer: :sequence do
  4. context 'when importing agents from Freshdesk' do
  5. let(:groups) do
  6. create_list(:group, 3)
  7. end
  8. let(:resource) do
  9. {
  10. 'available' => false,
  11. 'occasional' => false,
  12. 'id' => 1001,
  13. 'ticket_scope' => 1,
  14. 'created_at' => '2021-04-09T13:23:58Z',
  15. 'updated_at' => '2021-05-10T09:14:20Z',
  16. 'last_active_at' => '2021-05-10T09:14:20Z',
  17. 'available_since' => nil,
  18. 'type' => 'support_agent',
  19. 'contact' => {
  20. 'active' => false,
  21. 'email' => 'freshdesk@example.com',
  22. 'job_title' => nil,
  23. 'language' => 'en',
  24. 'last_login_at' => '2021-05-10T07:52:58Z',
  25. 'mobile' => nil,
  26. 'name' => 'John Doe',
  27. 'phone' => nil,
  28. 'time_zone' => 'Eastern Time (US & Canada)',
  29. 'created_at' => '2021-04-09T13:23:58Z',
  30. 'updated_at' => '2021-04-09T13:31:00Z'
  31. },
  32. 'signature' => nil,
  33. 'group_ids' => [1001, 1002, 1003]
  34. }
  35. end
  36. let(:id_map) do
  37. {
  38. 'Group' => {
  39. 1001 => groups[0].id,
  40. 1002 => groups[1].id,
  41. 1003 => groups[2].id,
  42. }
  43. }
  44. end
  45. let(:process_payload) do
  46. {
  47. import_job: build_stubbed(:import_job, name: 'Import::Freshdesk', payload: {}),
  48. dry_run: false,
  49. resource: resource,
  50. field_map: {},
  51. id_map: id_map,
  52. }
  53. end
  54. let(:imported_user) do
  55. {
  56. firstname: 'John',
  57. lastname: 'Doe',
  58. login: 'freshdesk@example.com',
  59. email: 'freshdesk@example.com',
  60. active: true,
  61. last_login: DateTime.parse('2021-05-10T07:52:58Z'),
  62. }
  63. end
  64. before do
  65. stub_request(:get, 'https://yours.freshdesk.com/api/v2/agents/me').to_return(status: 200, body: '', headers: {})
  66. end
  67. it 'imports user correctly (increased user count)' do
  68. expect { process(process_payload) }.to change(User, :count).by(1)
  69. end
  70. it 'imports user data correctly' do
  71. process(process_payload)
  72. expect(User.last).to have_attributes(imported_user)
  73. end
  74. it 'sets user roles correctly for admin user' do
  75. allow(Sequencer::Unit::Import::Freshdesk::Agent::Mapping).to receive(:admin_id).and_return(1001)
  76. process(process_payload)
  77. expect(User.last.roles.sort.map(&:name)).to eq %w[Admin Agent]
  78. end
  79. it 'sets user roles correctly for non-admin user' do
  80. process(process_payload)
  81. expect(User.last.roles.sort.map(&:name)).to eq ['Agent']
  82. end
  83. it 'sets user groups correctly' do
  84. process(process_payload)
  85. expect(User.last.groups_access('full').sort).to eq groups
  86. end
  87. context 'with deleted flag in resource' do
  88. before do
  89. resource['contact']['deleted'] = true
  90. imported_user[:active] = false
  91. end
  92. it 'imports user data correctly' do
  93. process(process_payload)
  94. expect(User.last).to have_attributes(imported_user)
  95. end
  96. end
  97. end
  98. end