tickets_spec.rb 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Sequencer::Unit::Import::Freshdesk::Tickets, db_strategy: 'reset', sequencer: :unit do
  4. context 'when importing ticket list from freshdesk' do
  5. let(:group) { create(:group) }
  6. let(:owner) { create(:agent, group_ids: [group.id]) }
  7. let(:id_map) do
  8. {
  9. 'User' => {
  10. 80_014_400_475 => owner.id,
  11. },
  12. 'Group' => {
  13. 80_000_374_718 => group.id,
  14. },
  15. }
  16. end
  17. let(:ticket_data) do
  18. {
  19. 'cc_emails' => [],
  20. 'fwd_emails' => [],
  21. 'reply_cc_emails' => [],
  22. 'ticket_cc_emails' => [],
  23. 'fr_escalated' => false,
  24. 'spam' => false,
  25. 'email_config_id' => nil,
  26. 'group_id' => 80_000_374_718,
  27. 'priority' => 1,
  28. 'requester_id' => 80_014_400_475,
  29. 'responder_id' => 80_014_400_475,
  30. 'source' => 3,
  31. 'company_id' => nil,
  32. 'status' => 2,
  33. 'subject' => 'Inline Images Failing?',
  34. 'association_type' => nil,
  35. 'support_email' => nil,
  36. 'to_emails' => ['info@zammad.org'],
  37. 'product_id' => nil,
  38. 'type' => nil,
  39. 'due_by' => '2021-05-17T12:29:27Z',
  40. 'fr_due_by' => '2021-05-15T12:29:27Z',
  41. 'is_escalated' => false,
  42. 'custom_fields' => {},
  43. 'created_at' => '2021-05-14T12:29:27Z',
  44. 'updated_at' => '2021-05-14T12:30:19Z',
  45. 'associated_tickets_count' => nil,
  46. 'tags' => [],
  47. }
  48. end
  49. let(:resources_payloud) do
  50. [
  51. ticket_data.merge(
  52. id: 10
  53. ),
  54. ticket_data.merge(
  55. id: 11
  56. ),
  57. ]
  58. end
  59. let(:process_payload) do
  60. {
  61. import_job: build_stubbed(:import_job, name: 'Import::Freshdesk', payload: {}),
  62. dry_run: false,
  63. request_params: {},
  64. field_map: {},
  65. id_map: id_map,
  66. time_entry_available: true,
  67. tickets_updated_since: nil,
  68. skip_initial_contacts: false,
  69. }
  70. end
  71. before do
  72. # We only want to test here the Ticket API, so disable other modules in the sequence
  73. # that make their own HTTP requests.
  74. custom_sequence = Sequencer::Sequence::Import::Freshdesk::Ticket.sequence.dup
  75. custom_sequence.delete('Import::Freshdesk::Ticket::Fetch')
  76. custom_sequence.delete('Import::Freshdesk::Ticket::TimeEntries')
  77. custom_sequence.delete('Import::Freshdesk::Ticket::Conversations')
  78. allow(Sequencer::Sequence::Import::Freshdesk::Ticket).to receive(:sequence) { custom_sequence }
  79. # Mock the groups get request
  80. stub_request(:get, 'https://yours.freshdesk.com/api/v2/tickets?order_by=updated_at&order_type=asc&page=1&per_page=100&updated_since=1970-01-01').to_return(status: 200, body: JSON.generate(resources_payloud), headers: {})
  81. end
  82. context 'when page limit is not reached' do
  83. it 'check ticket count' do
  84. expect { process(process_payload) }.to change(Ticket, :count).by(2)
  85. end
  86. end
  87. context 'when page limit is reached' do
  88. let(:resources_payloud_second_cycle) do
  89. [
  90. ticket_data.merge(
  91. id: 11
  92. ),
  93. ticket_data.merge(
  94. id: 12
  95. ),
  96. # Subject was changed during the import.
  97. ticket_data.merge(
  98. id: 10,
  99. subject: 'Different subject',
  100. status: 8,
  101. priority: 6
  102. ),
  103. ]
  104. end
  105. before do
  106. stub_request(:get, 'https://yours.freshdesk.com/api/v2/tickets?order_by=updated_at&order_type=asc&page=1&per_page=100&updated_since=1970-01-01').to_return(status: 200, body: JSON.generate(resources_payloud), headers: { link: 'second-page' })
  107. stub_request(:get, 'https://yours.freshdesk.com/api/v2/tickets?order_by=updated_at&order_type=asc&page=1&per_page=100&&updated_since=2021-05-14T12:30:19Z').to_return(status: 200, body: JSON.generate(resources_payloud_second_cycle), headers: {})
  108. end
  109. it 'check ticket count' do
  110. expect do
  111. process(process_payload) do |instance|
  112. # Set the `page_cycle` to zero, so that we trigger a new cycle (normally if more then 30.000 tickets exists)
  113. allow(instance).to receive(:page_cycle).and_return(0)
  114. end
  115. end.to change(Ticket, :count).by(3)
  116. end
  117. it 'check that ticket title was changed during import (and unknown status/priority not lead to any error)' do
  118. process(process_payload) do |instance|
  119. # Set the `page_cycle` to zero, so that we trigger a new cycle (normally if more then 30.000 tickets exists)
  120. allow(instance).to receive(:page_cycle).and_return(0)
  121. end
  122. expect(Ticket.find_by(number: 10).title).to eq('Different subject')
  123. end
  124. end
  125. end
  126. end