time_accounting_spec.rb 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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(:customer) { create(:customer) }
  6. let(:year) { Time.current.year }
  7. let(:month) { Time.current.month }
  8. describe '/api/v1/time_accounting/log/by_ticket' do
  9. context 'when requesting a JSON response' do
  10. # see https://github.com/zammad/zammad/pull/2243
  11. context 'and logs exist for work performed by an agent who is also the customer of the ticket (#2243)' do
  12. let(:ticket) { create(:ticket, customer: admin) }
  13. let!(:time_log) { create(:ticket_time_accounting, ticket: ticket, created_by_id: admin.id) }
  14. it 'responds with a non-nil value for each :agent key' do
  15. authenticated_as(admin)
  16. get "/api/v1/time_accounting/log/by_ticket/#{year}/#{month}", as: :json
  17. expect(json_response.first).not_to include('agent' => nil)
  18. end
  19. end
  20. end
  21. context 'when requesting a log report download' do
  22. it 'responds with an Excel spreadsheet' do
  23. create(:group)
  24. ticket = create(:ticket, state: Ticket::State.lookup(name: 'open'), customer: customer)
  25. article = create(:ticket_article, ticket: ticket, type: Ticket::Article::Type.lookup(name: 'note'))
  26. create(:ticket_time_accounting, ticket_id: ticket.id, ticket_article_id: article.id)
  27. authenticated_as(admin)
  28. get "/api/v1/time_accounting/log/by_ticket/#{year}/#{month}?download=true", params: {}
  29. expect(response).to have_http_status(:ok)
  30. expect(response['Content-Disposition']).to be_truthy
  31. expect(response['Content-Disposition']).to eq("attachment; filename=\"by_ticket-#{year}-#{month}.xls\"; filename*=UTF-8''by_ticket-#{year}-#{month}.xls")
  32. expect(response['Content-Type']).to eq('application/vnd.ms-excel')
  33. end
  34. end
  35. # Regression test for issue #2398 - Missing custom object in database causes error on export in time_accounting
  36. # This test is identical to the above one, except with the added step of a pending migration in the beginning
  37. context 'with pending attribute migrations, requesting a log report download' do
  38. it 'responds with an Excel spreadsheet' do
  39. ObjectManager::Attribute.add attributes_for :object_manager_attribute_select
  40. create(:group)
  41. ticket = create(:ticket, state: Ticket::State.lookup(name: 'open'), customer: customer)
  42. article = create(:ticket_article, ticket: ticket, type: Ticket::Article::Type.lookup(name: 'note'))
  43. create(:ticket_time_accounting, ticket_id: ticket.id, ticket_article_id: article.id)
  44. authenticated_as(admin)
  45. get "/api/v1/time_accounting/log/by_ticket/#{year}/#{month}?download=true", params: {}
  46. expect(response).to have_http_status(:ok)
  47. expect(response['Content-Disposition']).to be_truthy
  48. expect(response['Content-Disposition']).to eq("attachment; filename=\"by_ticket-#{year}-#{month}.xls\"; filename*=UTF-8''by_ticket-#{year}-#{month}.xls")
  49. expect(response['Content-Type']).to eq('application/vnd.ms-excel')
  50. end
  51. end
  52. end
  53. end