forked from zammad/zammad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
time_accounting_spec.rb
144 lines (116 loc) · 6.63 KB
/
time_accounting_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
require 'rails_helper'
RSpec.describe 'Time Accounting API endpoints', type: :request do
let(:admin) { create(:admin) }
let(:agent) { create(:agent) }
let(:customer) { create(:customer) }
let(:year) { Time.current.year }
let(:month) { Time.current.month }
describe '/api/v1/time_accounting/log/by_activity' do
context 'when requesting a JSON response' do
before do
ticket = create(:ticket, customer: admin)
create(:ticket_time_accounting, ticket: ticket, created_by_id: admin.id)
create(:ticket_time_accounting, ticket: ticket, created_by_id: agent.id)
authenticated_as(admin)
end
it 'responds with an JSON' do
get "/api/v1/time_accounting/log/by_activity/#{year}/#{month}", as: :json
expect(json_response.first).to include('agent' => admin.fullname)
expect(json_response.first).to include('customer' => admin.fullname)
expect(json_response.second).to include('agent' => agent.fullname)
expect(json_response.second).to include('customer' => admin.fullname)
end
it 'respects :limit' do
get "/api/v1/time_accounting/log/by_activity/#{year}/#{month}?limit=1", as: :json
expect(json_response.count).to be 1
end
end
context 'when requesting a log report download' do
it 'responds with an Excel spreadsheet' do
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'))
create(:ticket_time_accounting, ticket_id: ticket.id, ticket_article_id: article.id)
authenticated_as(admin)
get "/api/v1/time_accounting/log/by_activity/#{year}/#{month}?download=true", params: {}
expect(response).to have_http_status(:ok)
expect(response['Content-Disposition']).to be_truthy
expect(response['Content-Disposition']).to eq("attachment; filename=\"by_activity-#{year}-#{month}.xlsx\"; filename*=UTF-8''by_activity-#{year}-#{month}.xlsx")
expect(response['Content-Type']).to eq(ExcelSheet::CONTENT_TYPE)
end
end
end
describe '/api/v1/time_accounting/log/by_ticket' do
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
end
context 'when requesting a log report download' do
it 'responds with an Excel spreadsheet' do
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'))
create(:ticket_time_accounting, ticket_id: ticket.id, ticket_article_id: article.id)
authenticated_as(admin)
get "/api/v1/time_accounting/log/by_ticket/#{year}/#{month}?download=true", params: {}
expect(response).to have_http_status(:ok)
expect(response['Content-Disposition']).to be_truthy
expect(response['Content-Disposition']).to eq("attachment; filename=\"by_ticket-#{year}-#{month}.xlsx\"; filename*=UTF-8''by_ticket-#{year}-#{month}.xlsx")
expect(response['Content-Type']).to eq(ExcelSheet::CONTENT_TYPE)
end
end
# Regression test for issue #2398 - Missing custom object in database causes error on export in time_accounting
# This test is identical to the above one, except with the added step of a pending migration in the beginning
context 'with pending attribute migrations, requesting a log report download' do
it 'responds with an Excel spreadsheet' do
ObjectManager::Attribute.add attributes_for :object_manager_attribute_select
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'))
create(:ticket_time_accounting, ticket_id: ticket.id, ticket_article_id: article.id)
authenticated_as(admin)
get "/api/v1/time_accounting/log/by_ticket/#{year}/#{month}?download=true", params: {}
expect(response).to have_http_status(:ok)
expect(response['Content-Disposition']).to be_truthy
expect(response['Content-Disposition']).to eq("attachment; filename=\"by_ticket-#{year}-#{month}.xlsx\"; filename*=UTF-8''by_ticket-#{year}-#{month}.xlsx")
expect(response['Content-Type']).to eq(ExcelSheet::CONTENT_TYPE)
end
end
end
describe 'Assign user to multiple organizations #1573' do
let(:organization1) { create(:organization) }
let(:organization2) { create(:organization) }
let(:customer) { create(:customer, organization: organization1, organizations: [organization2]) }
let(:ticket1) do
ticket = create(:ticket, state: Ticket::State.lookup(name: 'open'), customer: customer, organization: organization1)
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)
end
let(:ticket2) do
ticket = create(:ticket, state: Ticket::State.lookup(name: 'open'), customer: customer, organization: organization2)
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)
end
before do
ticket1 && ticket2
end
it 'does return results group by organization and customer so multi organization support is given' do
authenticated_as(admin)
get "/api/v1/time_accounting/log/by_customer/#{year}/#{month}", as: :json
expect(json_response.count).to eq(2)
expect(json_response[0]['organization']['id']).to eq(organization1.id)
expect(json_response[0]['time_unit']).to eq(ticket1.time_unit.to_s)
expect(json_response[1]['organization']['id']).to eq(organization2.id)
expect(json_response[1]['time_unit']).to eq(ticket2.time_unit.to_s)
end
end
end