time_entries_spec.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Sequencer::Sequence::Import::Freshdesk::TimeEntries, db_strategy: 'reset', sequencer: :sequence do
  4. let(:time_entry_available) { true }
  5. let(:ticket) { create(:ticket) }
  6. let(:process_payload) do
  7. {
  8. import_job: build_stubbed(:import_job, name: 'Import::Freshdesk', payload: {}),
  9. dry_run: false,
  10. object: 'TimeEntry',
  11. request_params: {
  12. ticket: {
  13. 'id' => 1001,
  14. },
  15. },
  16. field_map: {},
  17. id_map: {
  18. 'Ticket' => {
  19. 1001 => ticket.id,
  20. },
  21. 'User' => {
  22. 80_014_400_475 => 1,
  23. }
  24. },
  25. skipped_resource_id: nil,
  26. time_entry_available: time_entry_available,
  27. skip_initial_contacts: false,
  28. }
  29. end
  30. context 'when time entry feature is available' do
  31. let(:resources_payloud) do
  32. [
  33. {
  34. 'id' => 80_027_218_656,
  35. 'billable' => true,
  36. 'note' => 'Example Preparation',
  37. 'timer_running' => false,
  38. 'agent_id' => 80_014_400_475,
  39. 'ticket_id' => 1001,
  40. 'time_spent' => '01:20',
  41. 'created_at' => '2021-05-14T12:29:27Z',
  42. 'updated_at' => '2021-05-14T12:29:27Z',
  43. 'start_time' => '2021-05-14T12:29:27Z',
  44. 'executed_at' => '2021-05-14T12:29:27Z'
  45. },
  46. {
  47. 'id' => 80_027_218_657,
  48. 'billable' => true,
  49. 'note' => 'Example Preparation 2',
  50. 'timer_running' => false,
  51. 'agent_id' => 80_014_400_475,
  52. 'ticket_id' => 1001,
  53. 'time_spent' => '02:20',
  54. 'created_at' => '2021-05-15T12:29:27Z',
  55. 'updated_at' => '2021-05-15T12:29:27Z',
  56. 'start_time' => '2021-05-15T12:29:27Z',
  57. 'executed_at' => '2021-05-15T12:29:27Z'
  58. }
  59. ]
  60. end
  61. let(:imported_time_entry) do
  62. {
  63. ticket_id: ticket.id,
  64. created_by_id: 1,
  65. time_unit: 140,
  66. }
  67. end
  68. before do
  69. # Mock the groups get request
  70. stub_request(:get, 'https://yours.freshdesk.com/api/v2/tickets/1001/time_entries?per_page=100').to_return(status: 200, body: JSON.generate(resources_payloud), headers: {})
  71. end
  72. it 'add time entry for ticket' do
  73. expect { process(process_payload) }.to change(Ticket::TimeAccounting, :count).by(2)
  74. end
  75. it 'check last time unit for ticket' do
  76. process(process_payload)
  77. expect(Ticket::TimeAccounting.last).to have_attributes(imported_time_entry)
  78. end
  79. context 'with empty time entries' do
  80. let(:resources_payloud) { [] }
  81. it 'do not change time entry for ticket' do
  82. expect { process(process_payload) }.not_to change(Ticket::TimeAccounting, :count)
  83. end
  84. end
  85. end
  86. context 'when time entry feature is not available' do
  87. let(:time_entry_available) { false }
  88. it 'add time entry for ticket' do
  89. expect { process(process_payload) }.not_to change(Ticket::TimeAccounting, :count)
  90. end
  91. end
  92. end