-
Notifications
You must be signed in to change notification settings - Fork 18
/
orders_controller_test.rb
514 lines (428 loc) · 18.8 KB
/
orders_controller_test.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
# Copyright (c) 2006-2017, Puzzle ITC GmbH. This file is part of
# PuzzleTime and licensed under the Affero General Public License version 3
# or later. See the COPYING file at the top-level directory or at
# https://github.com/puzzle/puzzletime.
# coding: utf-8
require 'test_helper'
class OrdersControllerTest < ActionController::TestCase
setup :login
test 'GET index sorted by order' do
get :index, params: { sort: 'order', status_id: '' }
assert_equal orders(:allgemein, :hitobito_demo, :puzzletime, :webauftritt), assigns(:orders)
end
test 'GET index sorted by kind' do
get :index, params: { sort: 'kind', status_id: '' }
assert_equal orders(:allgemein, :puzzletime, :hitobito_demo, :webauftritt), assigns(:orders)
end
test 'GET index sorted by department' do
get :index, params: { sort: 'department', status_id: '' }
assert_equal orders(:puzzletime, :webauftritt, :hitobito_demo, :allgemein), assigns(:orders)
end
test 'GET index sorted by responsible' do
get :index, params: { sort: 'responsible', status_id: '' }
assert_equal orders(:webauftritt, :allgemein, :hitobito_demo, :puzzletime), assigns(:orders)
end
test 'GET index sorted by status' do
get :index, params: { sort: 'status', status_id: '' }
assert_equal orders(:hitobito_demo, :puzzletime, :webauftritt, :allgemein), assigns(:orders)
end
test 'GET index sorted by major_chance_value' do
orders(:allgemein).order_chances.create!(name: 'Aare is above 18°', probability: :medium, impact: :low)
get :index, params: { sort: 'major_chance_value', status_id: '' }
assert_equal orders(:hitobito_demo, :webauftritt, :allgemein, :puzzletime), assigns(:orders)
end
test 'GET index sorted by major_risk_value' do
orders(:allgemein).order_risks.create!(name: 'Aare is below 18°', probability: :medium, impact: :low)
get :index, params: { sort: 'major_risk_value', status_id: '', sort_dir: 'desc' }
assert_equal orders(:allgemein, :puzzletime, :hitobito_demo, :webauftritt), assigns(:orders)
end
test 'GET index with default filter for manager' do
login_as(:mark)
get :index
assert_equal orders(:hitobito_demo, :puzzletime, :webauftritt), assigns(:orders)
assert_equal({ 'status_id' => [order_statuses(:bearbeitung).id] },
session[:list_params]['/orders'])
end
test 'GET index with default filter for user' do
login_as(:pascal)
get :index
assert_equal [orders(:hitobito_demo)], assigns(:orders)
assert_equal({ 'status_id' => [order_statuses(:bearbeitung).id],
'department_id' => departments(:devtwo).id },
session[:list_params]['/orders'])
end
test 'GET index with default filter for responsible' do
login_as(:lucien)
get :index
assert_equal orders(:hitobito_demo, :puzzletime), assigns(:orders)
assert_equal({ 'status_id' => [order_statuses(:bearbeitung).id],
'responsible_id' => employees(:lucien).id },
session[:list_params]['/orders'])
end
test 'GET index filtered by department' do
get :index, params: { department_id: departments(:devone).id }
assert_equal orders(:puzzletime, :webauftritt), assigns(:orders)
assert_equal({ 'department_id' => departments(:devone).id.to_s },
session[:list_params]['/orders'])
end
test 'GET index filtered by responsible' do
get :index, params: { responsible_id: employees(:lucien).id }
assert_equal orders(:hitobito_demo, :puzzletime), assigns(:orders)
assert_equal({ 'responsible_id' => employees(:lucien).id.to_s },
session[:list_params]['/orders'])
end
test 'GET index filtered by status' do
get :index, params: { status_id: order_statuses(:bearbeitung).id }
assert_equal orders(:hitobito_demo, :puzzletime, :webauftritt), assigns(:orders)
assert_equal({ 'status_id' => order_statuses(:bearbeitung).id.to_s },
session[:list_params]['/orders'])
end
test 'GET index filtered by multiple default statuses' do
offeriert = order_statuses(:offeriert)
offeriert.default = true
offeriert.save!
get :index
assert_equal(
{
'status_id' => [
order_statuses(:offeriert).id,
order_statuses(:bearbeitung).id
]
},
session[:list_params]['/orders']
)
end
test 'GET index filtered by kind' do
get :index, params: { kind_id: order_kinds(:projekt).id }
assert_equal orders(:hitobito_demo, :webauftritt), assigns(:orders)
assert_equal({ 'kind_id' => order_kinds(:projekt).id.to_s },
session[:list_params]['/orders'])
end
test 'GET index filtered by status and kind' do
get :index, params: { status_id: order_statuses(:bearbeitung).id, kind_id: order_kinds(:mandat).id }
assert_equal [orders(:puzzletime)], assigns(:orders)
assert_equal({ 'status_id' => order_statuses(:bearbeitung).id.to_s,
'kind_id' => order_kinds(:mandat).id.to_s },
session[:list_params]['/orders'])
end
test 'GET index filtered by department, responsible, status and kind' do
get :index, params: {
department_id: departments(:devtwo).id,
responsible_id: employees(:lucien).id,
status_id: order_statuses(:bearbeitung).id,
kind_id: order_kinds(:mandat).id
}
assert_equal [], assigns(:orders)
assert_equal({ 'department_id' => departments(:devtwo).id.to_s,
'responsible_id' => employees(:lucien).id.to_s,
'status_id' => order_statuses(:bearbeitung).id.to_s,
'kind_id' => order_kinds(:mandat).id.to_s },
session[:list_params]['/orders'])
end
test 'GET index uses remembered params if no params are given' do
session[:list_params] = {
'/orders' => {
'status_id' => order_statuses(:bearbeitung).id.to_s,
'kind_id' => order_kinds(:mandat).id.to_s
}
}
get :index
assert_equal [orders(:puzzletime)], assigns(:orders)
assert_equal({ 'status_id' => order_statuses(:bearbeitung).id.to_s,
'kind_id' => order_kinds(:mandat).id.to_s },
session[:list_params]['/orders'])
end
test 'GET index overwrites remembered params if params are given' do
session[:list_params] = {
'/orders' => {
'status_id' => order_statuses(:bearbeitung).id.to_s,
'kind_id' => order_kinds(:mandat).id.to_s
}
}
get :index, params: { status_id: order_statuses(:abgeschlossen).id, responsible_id: employees(:mark).id }
assert_equal [orders(:allgemein)], assigns(:orders)
assert_equal({ 'status_id' => order_statuses(:abgeschlossen).id.to_s,
'responsible_id' => employees(:mark).id.to_s },
session[:list_params]['/orders'])
end
test 'GET show' do
get :show, params: { id: orders(:hitobito_demo).id }
assert_template 'show'
end
test 'GET new presets some values' do
get :new
user = employees(:mark)
order = assigns(:order)
assert_nil order.department_id
assert_equal user, order.responsible
end
test 'POST create sets values' do
assert_difference('Order.count') do
post :create, params: {
client_work_item_id: clients(:swisstopo).work_item_id,
order: {
work_item_attributes: {
name: 'New Order',
shortname: 'NEO'
},
department_id: departments(:devtwo).id,
responsible_id: employees(:pascal).id,
kind_id: order_kinds(:projekt).id,
status_id: order_statuses(:bearbeitung).id,
order_team_members_attributes: {
'0' => { employee_id: employees(:half_year_maria).id, comment: 'rolle maria' },
'1' => { employee_id: employees(:next_year_pablo).id, comment: 'rolle pablo' }
},
order_contacts_attributes: {
'0' => { contact_id_or_crm: contacts(:swisstopo_1).id, comment: 'funktion 1' },
'1' => { contact_id_or_crm: contacts(:swisstopo_2).id, comment: 'funktion 2' }
}
}
}
end
assert_redirected_to edit_order_path(assigns(:order))
item = WorkItem.where(name: 'New Order').first
order = item.order
assert_equal 'NEO', item.shortname
assert_equal clients(:swisstopo).id, item.parent_id
assert_equal departments(:devtwo).id, order.department_id
assert_equal employees(:pascal).id, order.responsible_id
assert_equal order_statuses(:bearbeitung).id, order.status_id
assert_equal order_kinds(:projekt).id, order.kind_id
order_contacts = order.order_contacts.map { |oc| [oc.contact_id, oc.comment] }.sort
assert_equal [[contacts(:swisstopo_1).id, 'funktion 1'], [contacts(:swisstopo_2).id, 'funktion 2']].sort, order_contacts
order_team_members = order.order_team_members.map { |otm| [otm.employee.id, otm.comment] }.sort
assert_equal [[employees(:half_year_maria).id, 'rolle maria'], [employees(:next_year_pablo).id, 'rolle pablo']].sort, order_team_members
end
test 'POST create copies sub accounting posts and everything' do
source = orders(:hitobito_demo)
source.order_team_members.create!(employee: employees(:pascal), comment: 'Coder')
source.order_team_members.create!(employee: employees(:lucien), comment: 'PL')
source.order_contacts.create!(contact: contacts(:puzzle_rava), comment: 'BL')
source.create_contract!(number: 'hito1234', start_date: '2005-01-01', end_date: '2020-07-30')
post :create, params: {
client_work_item_id: clients(:puzzle).work_item_id,
copy_id: source.id,
order: {
work_item_attributes: {
parent_id: source.work_item.parent.id,
name: 'New Order',
shortname: 'NEO'
},
department_id: departments(:devtwo).id,
responsible_id: employees(:pascal).id,
kind_id: order_kinds(:projekt).id,
status_id: order_statuses(:bearbeitung).id,
order_team_members_attributes: {
'0' => { employee_id: employees(:half_year_maria).id, comment: 'rolle maria' },
'1' => { employee_id: employees(:next_year_pablo).id, comment: 'rolle pablo' }
},
order_contacts_attributes: {
'0' => { contact_id_or_crm: contacts(:puzzle_rava).id, comment: 'funktion 1' }
}
}
}
assert_equal [], assigns(:order).errors.full_messages
assert_redirected_to edit_order_path(assigns(:order))
item = WorkItem.where(name: 'New Order').first
order = item.order
assert_equal order_statuses(:bearbeitung).id, order.status_id
assert_equal order_kinds(:projekt).id, order.kind_id
order_contacts = order.order_contacts.map { |oc| [oc.contact_id, oc.comment] }.sort
assert_equal [[contacts(:puzzle_rava).id, 'funktion 1']], order_contacts
order_team_members = order.order_team_members.map { |otm| [otm.employee.id, otm.comment] }.sort
assert_equal [[employees(:half_year_maria).id, 'rolle maria'], [employees(:next_year_pablo).id, 'rolle pablo']].sort, order_team_members
assert_equal 'hito1234', order.contract.number
assert_not_equal source.contract_id, order.contract_id
assert_equal source.work_item.parent_id, item.parent_id
assert_equal 2, order.accounting_posts.count
assert_not_equal work_items(:hitobito_demo_app), order.accounting_posts.first
assert_equal work_items(:hitobito_demo_app).name, order.accounting_posts.first.name
end
test 'POST create copies same level accounting post' do
source = orders(:webauftritt)
post :create, params: {
client_work_item_id: clients(:swisstopo).work_item_id,
copy_id: source.id,
order: {
work_item_attributes: {
parent_id: source.work_item.parent.id,
name: 'New Order',
shortname: 'NEO'
},
department_id: departments(:devtwo).id,
responsible_id: employees(:pascal).id,
kind_id: order_kinds(:projekt).id,
status_id: order_statuses(:bearbeitung).id
}
}
assert_equal [], assigns(:order).errors.full_messages
assert_redirected_to edit_order_path(assigns(:order))
item = WorkItem.where(name: 'New Order').first
order = item.order
assert_equal order_statuses(:bearbeitung).id, order.status_id
assert_equal order_kinds(:projekt).id, order.kind_id
assert_equal [], order.order_contacts
assert_equal [], order.order_team_members
assert_not_equal source.contract_id, order.contract_id
assert_equal source.work_item.parent_id, item.parent_id
assert_equal 1, order.accounting_posts.count
post = order.accounting_posts.first
assert_not_equal work_items(:webauftritt), post.work_item
assert_equal order.work_item, post.work_item
assert_equal 140, post.offered_rate
end
test 'POST create copies same level accounting post with category' do
source = Fabricate(:order, work_item: Fabricate(:work_item, parent: work_items(:intern), name: 'test', shortname: 'tst'))
Fabricate(:accounting_post, work_item: source.work_item)
post :create, params: {
client_work_item_id: clients(:puzzle).work_item_id,
copy_id: source.id,
order: {
work_item_attributes: {
parent_id: source.work_item.parent.id,
name: 'New Order',
shortname: 'NEO'
},
department_id: departments(:devtwo).id,
responsible_id: employees(:pascal).id,
kind_id: order_kinds(:projekt).id,
status_id: order_statuses(:bearbeitung).id
}
}
assert_equal [], assigns(:order).errors.full_messages
assert_redirected_to edit_order_path(assigns(:order))
item = WorkItem.where(name: 'New Order').first
order = item.order
assert_equal order_statuses(:bearbeitung).id, order.status_id
assert_equal order_kinds(:projekt).id, order.kind_id
assert_equal [], order.order_contacts
assert_equal [], order.order_team_members
assert_nil order.contract_id
assert_equal source.work_item.parent_id, item.parent_id
assert_equal 1, order.accounting_posts.count
post = order.accounting_posts.first
assert_not_equal source.work_item, post.work_item
assert_equal order.work_item, post.work_item
end
test 'PATCH update sets values' do
order = orders(:puzzletime)
patch :update, params: {
id: order.id,
order: {
work_item_attributes: {
name: 'New Order',
shortname: 'NEO'
},
crm_key: 'puzzletime-crm-key',
department_id: departments(:devtwo).id,
responsible_id: employees(:pascal).id,
kind_id: order_kinds(:projekt).id,
status_id: order_statuses(:bearbeitung).id,
order_team_members_attributes: {
'0' => { employee_id: employees(:half_year_maria).id, comment: 'rolle maria' },
'1' => { employee_id: employees(:next_year_pablo).id, comment: 'rolle pablo' }
},
order_contacts_attributes: {
'0' => { contact_id_or_crm: contacts(:puzzle_rava), comment: 'funktion 1' },
'1' => { contact_id_or_crm: contacts(:puzzle_hauswart), comment: 'funktion 2' }
}
}
}
assert_redirected_to edit_order_path(order)
order.reload
item = order.work_item
assert_equal 'New Order', item.name
assert_equal 'NEO', item.shortname
assert_equal clients(:puzzle).id, item.parent_id
assert_equal departments(:devtwo).id, order.department_id
assert_equal employees(:pascal).id, order.responsible_id
assert_equal order_statuses(:bearbeitung).id, order.status_id
assert_equal order_kinds(:projekt).id, order.kind_id
assert_equal 'puzzletime-crm-key', order.crm_key
order_contacts = order.order_contacts.map { |oc| [oc.contact_id, oc.comment] }.sort
assert_equal [[contacts(:puzzle_rava).id, 'funktion 1'], [contacts(:puzzle_hauswart).id, 'funktion 2']].sort, order_contacts
order_team_members = order.order_team_members.map { |otm| [otm.employee.id, otm.comment] }.sort
assert_equal [[employees(:half_year_maria).id, 'rolle maria'], [employees(:next_year_pablo).id, 'rolle pablo']].sort, order_team_members
end
test 'DELETE destroys order and work item' do
order = orders(:puzzletime)
order.worktimes.destroy_all
assert_difference 'WorkItem.count', -1 do
assert_difference 'Order.count', -1 do
assert_difference 'AccountingPost.count', -1 do
delete :destroy, params: { id: order.id }
end
end
end
assert_redirected_to orders_path(returning: true)
end
test 'DELETE destroys order and all accounting posts' do
order = orders(:hitobito_demo)
order.worktimes.destroy_all
assert_difference 'WorkItem.count', -3 do
assert_difference 'Order.count', -1 do
assert_difference 'AccountingPost.count', -2 do
delete :destroy, params: { id: order.id }
end
end
end
assert_redirected_to orders_path(returning: true)
end
test 'ajax GET #employees includes only those with worktimes in given period' do
order = orders(:webauftritt)
lucien = employees(:lucien)
get :employees,
xhr: true,
params: { id: order.id, period_from: '11.12.2006', period_to: '01.03.2007' }
empls = JSON.parse(response.body)
assert_equal 1, empls.size
empl = empls.first
assert_equal lucien.id, empl['id']
assert_equal lucien.lastname, empl['lastname']
assert_equal lucien.firstname, empl['firstname']
end
test 'ajax GET #employees includes all with worktimes if no period specified' do
order = orders(:webauftritt)
lucien = employees(:lucien)
mark = employees(:mark)
get :employees, xhr: true, params: { id: order.id }
empls = JSON.parse(response.body)
assert_equal 2, empls.size
assert empls.any? { |e| e['id'] == lucien.id }
assert empls.any? { |e| e['id'] == mark.id }
end
test 'ajax GET #employees empty if no worktimes for given period' do
order = orders(:webauftritt)
get :employees,
xhr: true,
params: { id: order.id, period_from: '11.12.2007', period_to: '01.03.2008' }
empls = JSON.parse(response.body)
assert_equal 0, empls.size
end
test 'GET #edit redirects to #show if no write access on given order' do
login_as(:lucien)
order = orders(:webauftritt)
get :edit, params: { id: order.id }
assert_redirected_to order_path(order)
end
test 'GET #edit shows edit form if write access' do
order = orders(:webauftritt)
get :edit, params: { id: order.id }
assert_template :edit
end
test 'GET #search returns list of orders filtered by query' do
order = orders(:webauftritt)
get :search, params: { q: order.path_shortnames }, format: :json
assert find_in_body(response.body, 'id', order.id)
end
test 'GET #search accepts param to only return open orders' do
order = orders(:allgemein)
get :search, params: { q: order.path_shortnames, only_open: true }, format: :json
refute find_in_body(response.body, 'id', order.id)
end
private
def find_in_body(body, field, element)
JSON.parse(body).find { |w| w[field] == element }
end
end