Test and fix time accounting API endpoint bug (replaces #2243)

This commit is contained in:
Ryan Lue 2018-09-28 08:49:06 +02:00
parent 8e99a010ca
commit 97b17caea4
3 changed files with 35 additions and 31 deletions

View file

@ -46,7 +46,7 @@ class TimeAccountingsController < ApplicationController
end end
end end
end end
if !customers[local_time_unit[:agent_id]] if !agents[local_time_unit[:agent_id]]
agent_user = User.lookup(id: local_time_unit[:agent_id]) agent_user = User.lookup(id: local_time_unit[:agent_id])
agent = '-' agent = '-'
if agent_user if agent_user

View file

@ -1,10 +1,7 @@
FactoryBot.define do FactoryBot.define do
factory :ticket_time_accounting, class: Ticket::TimeAccounting do factory :ticket_time_accounting, class: Ticket::TimeAccounting do
ticket_id { FactoryBot.create(:ticket).id } ticket
ticket_article_id { FactoryBot.create(:ticket_article).id } time_unit { rand(100) }
time_unit 200
created_by_id 1 created_by_id 1
created_at Time.zone.now
updated_at Time.zone.now
end end
end end

View file

@ -1,36 +1,43 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe 'Time Accounting', type: :request do RSpec.describe 'Time Accounting API endpoints', type: :request do
let(:admin) { create(:admin_user) }
let(:customer) { create(:customer_user) }
let(:year) { Time.current.year }
let(:month) { Time.current.month }
let(:admin_user) do describe '/api/v1/time_accounting/log/by_ticket' do
create(:admin_user) context 'when requesting a JSON response' do
end # see https://github.com/zammad/zammad/pull/2243
let(:customer_user) do context 'and logs exist for work performed by an agent who is also the customer of the ticket (#2243)' do
create(:customer_user) let(:ticket) { create(:ticket, customer: admin) }
end let!(:time_log) { create(:ticket_time_accounting, ticket: ticket, created_by_id: admin.id) }
let(:year) do
DateTime.now.utc.year
end
let(:month) do
DateTime.now.utc.month
end
describe 'request handling' do it 'responds with a non-nil value for each :agent key' do
authenticated_as(admin)
get "/api/v1/time_accounting/log/by_ticket/#{year}/#{month}", as: :json
it 'does time account report' do expect(json_response.first).not_to include('agent' => nil)
group = create(:group) end
ticket = create(:ticket, state: Ticket::State.lookup(name: 'open'), customer: customer_user ) end
article = create(:ticket_article, ticket_id: ticket.id, type: Ticket::Article::Type.lookup(name: 'note') ) end
create(:ticket_time_accounting, ticket_id: ticket.id, ticket_article_id: article.id) context 'when requesting a log report download' do
it 'responds with an Excel spreadsheet' do
group = create(:group)
ticket = create(:ticket, state: Ticket::State.lookup(name: 'open'), customer: customer )
article = create(:ticket_article, ticket: ticket, type: Ticket::Article::Type.lookup(name: 'note') )
authenticated_as(admin_user) create(:ticket_time_accounting, ticket_id: ticket.id, ticket_article_id: article.id)
get "/api/v1/time_accounting/log/by_ticket/#{year}/#{month}?download=true", params: {}
expect(response).to have_http_status(200) authenticated_as(admin)
expect(response['Content-Disposition']).to be_truthy get "/api/v1/time_accounting/log/by_ticket/#{year}/#{month}?download=true", params: {}
expect(response['Content-Disposition']).to eq("attachment; filename=\"by_ticket-#{year}-#{month}.xls\"")
expect(response['Content-Type']).to eq('application/vnd.ms-excel') expect(response).to have_http_status(200)
expect(response['Content-Disposition']).to be_truthy
expect(response['Content-Disposition']).to eq("attachment; filename=\"by_ticket-#{year}-#{month}.xls\"")
expect(response['Content-Type']).to eq('application/vnd.ms-excel')
end
end end
end end
end end