time_accounting_spec.rb 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Time Accounting API endpoints', type: :request do
  4. let(:admin) { create(:admin) }
  5. let(:agent) { create(:agent) }
  6. let(:customer) { create(:customer) }
  7. let(:year) { Time.current.year }
  8. let(:month) { Time.current.month }
  9. describe '/api/v1/time_accounting/log/by_activity', authenticated_as: :admin do
  10. context 'when requesting a JSON response' do
  11. let(:ticket) { create(:ticket, customer: admin) }
  12. let(:time_accounting1) { create(:ticket_time_accounting, :for_type, ticket: ticket, created_by_id: admin.id) }
  13. let(:time_accounting2) { create(:ticket_time_accounting, ticket: ticket, created_by_id: agent.id) }
  14. before do
  15. time_accounting1 && time_accounting2
  16. end
  17. context 'with time_accounting_types switched on' do
  18. before do
  19. Setting.set('time_accounting_types', true)
  20. end
  21. it 'responds with an JSON' do
  22. get "/api/v1/time_accounting/log/by_activity/#{year}/#{month}", as: :json
  23. expect(json_response.first).to include(
  24. 'time_unit' => time_accounting1.time_unit.to_s,
  25. 'type' => time_accounting1.type.name,
  26. 'customer' => admin.fullname,
  27. 'organization' => '-',
  28. 'agent' => admin.fullname,
  29. )
  30. expect(json_response.second).to include(
  31. 'time_unit' => time_accounting2.time_unit.to_s,
  32. 'type' => '-',
  33. 'customer' => admin.fullname,
  34. 'organization' => '-',
  35. 'agent' => agent.fullname,
  36. )
  37. end
  38. end
  39. context 'with time_accounting_types switched off' do
  40. it 'responds with an JSON' do
  41. get "/api/v1/time_accounting/log/by_activity/#{year}/#{month}", as: :json
  42. expect(json_response.first).to include(
  43. 'time_unit' => time_accounting1.time_unit.to_s,
  44. 'customer' => admin.fullname,
  45. 'organization' => '-',
  46. 'agent' => admin.fullname,
  47. )
  48. expect(json_response.first).not_to have_key('type')
  49. end
  50. end
  51. it 'respects :limit' do
  52. get "/api/v1/time_accounting/log/by_activity/#{year}/#{month}?limit=1", as: :json
  53. expect(json_response.count).to be 1
  54. end
  55. end
  56. context 'when requesting a log report download' do
  57. it 'responds with an Excel spreadsheet' do
  58. create(:group)
  59. ticket = create(:ticket, state: Ticket::State.lookup(name: 'open'), customer: customer)
  60. article = create(:ticket_article, ticket: ticket, type: Ticket::Article::Type.lookup(name: 'note'))
  61. create(:ticket_time_accounting, ticket_id: ticket.id, ticket_article_id: article.id)
  62. get "/api/v1/time_accounting/log/by_activity/#{year}/#{month}?download=true", params: {}
  63. expect(response).to have_http_status(:ok)
  64. expect(response['Content-Disposition']).to be_truthy
  65. expect(response['Content-Disposition']).to eq("attachment; filename=\"by_activity-#{year}-#{month}.xlsx\"; filename*=UTF-8''by_activity-#{year}-#{month}.xlsx")
  66. expect(response['Content-Type']).to eq(ExcelSheet::CONTENT_TYPE)
  67. end
  68. end
  69. end
  70. describe '/api/v1/time_accounting/log/by_ticket' do
  71. context 'when requesting a JSON response' do
  72. # see https://github.com/zammad/zammad/pull/2243
  73. context 'and logs exist for work performed by an agent who is also the customer of the ticket (#2243)' do
  74. let(:ticket) { create(:ticket, customer: admin) }
  75. let!(:time_log) { create(:ticket_time_accounting, ticket: ticket, created_by_id: admin.id) }
  76. it 'responds with a non-nil value for each :agent key' do
  77. authenticated_as(admin)
  78. get "/api/v1/time_accounting/log/by_ticket/#{year}/#{month}", as: :json
  79. expect(json_response.first).not_to include('agent' => nil)
  80. end
  81. end
  82. end
  83. context 'when requesting a log report download' do
  84. it 'responds with an Excel spreadsheet' do
  85. create(:group)
  86. ticket = create(:ticket, state: Ticket::State.lookup(name: 'open'), customer: customer)
  87. article = create(:ticket_article, ticket: ticket, type: Ticket::Article::Type.lookup(name: 'note'))
  88. create(:ticket_time_accounting, ticket_id: ticket.id, ticket_article_id: article.id)
  89. authenticated_as(admin)
  90. get "/api/v1/time_accounting/log/by_ticket/#{year}/#{month}?download=true", params: {}
  91. expect(response).to have_http_status(:ok)
  92. expect(response['Content-Disposition']).to be_truthy
  93. expect(response['Content-Disposition']).to eq("attachment; filename=\"by_ticket-#{year}-#{month}.xlsx\"; filename*=UTF-8''by_ticket-#{year}-#{month}.xlsx")
  94. expect(response['Content-Type']).to eq(ExcelSheet::CONTENT_TYPE)
  95. end
  96. end
  97. # Regression test for issue #2398 - Missing custom object in database causes error on export in time_accounting
  98. # This test is identical to the above one, except with the added step of a pending migration in the beginning
  99. context 'with pending attribute migrations, requesting a log report download' do
  100. it 'responds with an Excel spreadsheet' do
  101. ObjectManager::Attribute.add attributes_for :object_manager_attribute_select
  102. create(:group)
  103. ticket = create(:ticket, state: Ticket::State.lookup(name: 'open'), customer: customer)
  104. article = create(:ticket_article, ticket: ticket, type: Ticket::Article::Type.lookup(name: 'note'))
  105. create(:ticket_time_accounting, ticket_id: ticket.id, ticket_article_id: article.id)
  106. authenticated_as(admin)
  107. get "/api/v1/time_accounting/log/by_ticket/#{year}/#{month}?download=true", params: {}
  108. expect(response).to have_http_status(:ok)
  109. expect(response['Content-Disposition']).to be_truthy
  110. expect(response['Content-Disposition']).to eq("attachment; filename=\"by_ticket-#{year}-#{month}.xlsx\"; filename*=UTF-8''by_ticket-#{year}-#{month}.xlsx")
  111. expect(response['Content-Type']).to eq(ExcelSheet::CONTENT_TYPE)
  112. end
  113. end
  114. end
  115. describe 'Assign user to multiple organizations #1573' do
  116. let(:organization1) { create(:organization) }
  117. let(:organization2) { create(:organization) }
  118. let(:customer) { create(:customer, organization: organization1, organizations: [organization2]) }
  119. let(:ticket1) do
  120. ticket = create(:ticket, state: Ticket::State.lookup(name: 'open'), customer: customer, organization: organization1)
  121. article = create(:ticket_article, ticket: ticket, type: Ticket::Article::Type.lookup(name: 'note'))
  122. create(:ticket_time_accounting, ticket_id: ticket.id, ticket_article_id: article.id)
  123. end
  124. let(:ticket2) do
  125. ticket = create(:ticket, state: Ticket::State.lookup(name: 'open'), customer: customer, organization: organization2)
  126. article = create(:ticket_article, ticket: ticket, type: Ticket::Article::Type.lookup(name: 'note'))
  127. create(:ticket_time_accounting, ticket_id: ticket.id, ticket_article_id: article.id)
  128. end
  129. before do
  130. ticket1 && ticket2
  131. end
  132. it 'does return results group by organization and customer so multi organization support is given' do
  133. authenticated_as(admin)
  134. get "/api/v1/time_accounting/log/by_customer/#{year}/#{month}", as: :json
  135. expect(json_response.count).to eq(2)
  136. expect(json_response[0]['organization']['id']).to eq(organization1.id)
  137. expect(json_response[0]['time_unit']).to eq(ticket1.time_unit.to_s)
  138. expect(json_response[1]['organization']['id']).to eq(organization2.id)
  139. expect(json_response[1]['time_unit']).to eq(ticket2.time_unit.to_s)
  140. end
  141. end
  142. end