time_accounting_spec.rb 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # Copyright (C) 2012-2022 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' do
  10. context 'when requesting a JSON response' do
  11. it 'responds with an JSON' do
  12. ticket = create(:ticket, customer: admin)
  13. create(:ticket_time_accounting, ticket: ticket, created_by_id: admin.id)
  14. create(:ticket_time_accounting, ticket: ticket, created_by_id: agent.id)
  15. authenticated_as(admin)
  16. get "/api/v1/time_accounting/log/by_activity/#{year}/#{month}", as: :json
  17. expect(json_response.first).to include('agent' => admin.fullname)
  18. expect(json_response.first).to include('customer' => admin.fullname)
  19. expect(json_response.second).to include('agent' => agent.fullname)
  20. expect(json_response.second).to include('customer' => admin.fullname)
  21. end
  22. end
  23. context 'when requesting a log report download' do
  24. it 'responds with an Excel spreadsheet' do
  25. create(:group)
  26. ticket = create(:ticket, state: Ticket::State.lookup(name: 'open'), customer: customer)
  27. article = create(:ticket_article, ticket: ticket, type: Ticket::Article::Type.lookup(name: 'note'))
  28. create(:ticket_time_accounting, ticket_id: ticket.id, ticket_article_id: article.id)
  29. authenticated_as(admin)
  30. get "/api/v1/time_accounting/log/by_activity/#{year}/#{month}?download=true", params: {}
  31. expect(response).to have_http_status(:ok)
  32. expect(response['Content-Disposition']).to be_truthy
  33. expect(response['Content-Disposition']).to eq("attachment; filename=\"by_activity-#{year}-#{month}.xls\"; filename*=UTF-8''by_activity-#{year}-#{month}.xls")
  34. expect(response['Content-Type']).to eq('application/vnd.ms-excel')
  35. end
  36. end
  37. end
  38. describe '/api/v1/time_accounting/log/by_ticket' do
  39. context 'when requesting a JSON response' do
  40. # see https://github.com/zammad/zammad/pull/2243
  41. context 'and logs exist for work performed by an agent who is also the customer of the ticket (#2243)' do
  42. let(:ticket) { create(:ticket, customer: admin) }
  43. let!(:time_log) { create(:ticket_time_accounting, ticket: ticket, created_by_id: admin.id) }
  44. it 'responds with a non-nil value for each :agent key' do
  45. authenticated_as(admin)
  46. get "/api/v1/time_accounting/log/by_ticket/#{year}/#{month}", as: :json
  47. expect(json_response.first).not_to include('agent' => nil)
  48. end
  49. end
  50. end
  51. context 'when requesting a log report download' do
  52. it 'responds with an Excel spreadsheet' do
  53. create(:group)
  54. ticket = create(:ticket, state: Ticket::State.lookup(name: 'open'), customer: customer)
  55. article = create(:ticket_article, ticket: ticket, type: Ticket::Article::Type.lookup(name: 'note'))
  56. create(:ticket_time_accounting, ticket_id: ticket.id, ticket_article_id: article.id)
  57. authenticated_as(admin)
  58. get "/api/v1/time_accounting/log/by_ticket/#{year}/#{month}?download=true", params: {}
  59. expect(response).to have_http_status(:ok)
  60. expect(response['Content-Disposition']).to be_truthy
  61. expect(response['Content-Disposition']).to eq("attachment; filename=\"by_ticket-#{year}-#{month}.xls\"; filename*=UTF-8''by_ticket-#{year}-#{month}.xls")
  62. expect(response['Content-Type']).to eq('application/vnd.ms-excel')
  63. end
  64. end
  65. # Regression test for issue #2398 - Missing custom object in database causes error on export in time_accounting
  66. # This test is identical to the above one, except with the added step of a pending migration in the beginning
  67. context 'with pending attribute migrations, requesting a log report download' do
  68. it 'responds with an Excel spreadsheet' do
  69. ObjectManager::Attribute.add attributes_for :object_manager_attribute_select
  70. create(:group)
  71. ticket = create(:ticket, state: Ticket::State.lookup(name: 'open'), customer: customer)
  72. article = create(:ticket_article, ticket: ticket, type: Ticket::Article::Type.lookup(name: 'note'))
  73. create(:ticket_time_accounting, ticket_id: ticket.id, ticket_article_id: article.id)
  74. authenticated_as(admin)
  75. get "/api/v1/time_accounting/log/by_ticket/#{year}/#{month}?download=true", params: {}
  76. expect(response).to have_http_status(:ok)
  77. expect(response['Content-Disposition']).to be_truthy
  78. expect(response['Content-Disposition']).to eq("attachment; filename=\"by_ticket-#{year}-#{month}.xls\"; filename*=UTF-8''by_ticket-#{year}-#{month}.xls")
  79. expect(response['Content-Type']).to eq('application/vnd.ms-excel')
  80. end
  81. end
  82. end
  83. describe 'Assign user to multiple organizations #1573' do
  84. let(:organization1) { create(:organization) }
  85. let(:organization2) { create(:organization) }
  86. let(:customer) { create(:customer, organization: organization1, organizations: [organization2]) }
  87. let(:ticket1) do
  88. ticket = create(:ticket, state: Ticket::State.lookup(name: 'open'), customer: customer, organization: organization1)
  89. article = create(:ticket_article, ticket: ticket, type: Ticket::Article::Type.lookup(name: 'note'))
  90. create(:ticket_time_accounting, ticket_id: ticket.id, ticket_article_id: article.id)
  91. end
  92. let(:ticket2) do
  93. ticket = create(:ticket, state: Ticket::State.lookup(name: 'open'), customer: customer, organization: organization2)
  94. article = create(:ticket_article, ticket: ticket, type: Ticket::Article::Type.lookup(name: 'note'))
  95. create(:ticket_time_accounting, ticket_id: ticket.id, ticket_article_id: article.id)
  96. end
  97. before do
  98. ticket1 && ticket2
  99. end
  100. it 'does return results group by organization and customer so multi organization support is given' do
  101. authenticated_as(admin)
  102. get "/api/v1/time_accounting/log/by_customer/#{year}/#{month}", as: :json
  103. expect(json_response.count).to eq(2)
  104. expect(json_response[0]['organization']['id']).to eq(organization1.id)
  105. expect(json_response[0]['time_unit']).to eq(ticket1.time_unit.to_s)
  106. expect(json_response[1]['organization']['id']).to eq(organization2.id)
  107. expect(json_response[1]['time_unit']).to eq(ticket2.time_unit.to_s)
  108. end
  109. end
  110. end