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,30 +1,36 @@
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
# see https://github.com/zammad/zammad/pull/2243
context 'and logs exist for work performed by an agent who is also the customer of the ticket (#2243)' do
let(:ticket) { create(:ticket, customer: admin) }
let!(:time_log) { create(:ticket_time_accounting, ticket: ticket, created_by_id: admin.id) }
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
expect(json_response.first).not_to include('agent' => nil)
end end
let(:customer_user) do
create(:customer_user)
end end
let(:year) do
DateTime.now.utc.year
end
let(:month) do
DateTime.now.utc.month
end end
describe 'request handling' do context 'when requesting a log report download' do
it 'responds with an Excel spreadsheet' do
it 'does time account report' do
group = create(:group) group = create(:group)
ticket = create(:ticket, state: Ticket::State.lookup(name: 'open'), customer: customer_user ) ticket = create(:ticket, state: Ticket::State.lookup(name: 'open'), customer: customer )
article = create(:ticket_article, ticket_id: ticket.id, type: Ticket::Article::Type.lookup(name: 'note') ) article = create(:ticket_article, ticket: ticket, type: Ticket::Article::Type.lookup(name: 'note') )
create(:ticket_time_accounting, ticket_id: ticket.id, ticket_article_id: article.id) create(:ticket_time_accounting, ticket_id: ticket.id, ticket_article_id: article.id)
authenticated_as(admin_user) authenticated_as(admin)
get "/api/v1/time_accounting/log/by_ticket/#{year}/#{month}?download=true", params: {} get "/api/v1/time_accounting/log/by_ticket/#{year}/#{month}?download=true", params: {}
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
@ -34,3 +40,4 @@ RSpec.describe 'Time Accounting', type: :request do
end end
end end
end end
end