time_accounting_spec.rb 3.0 KB

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