/
service_dialogs_spec.rb
577 lines (483 loc) · 20.2 KB
/
service_dialogs_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
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
#
# REST API Request Tests - Service Dialogs specs
#
# - Refresh dialog fields /api/service_dialogs/:id "refresh_dialog_fields"
#
describe "Service Dialogs API" do
let(:dialog1) { FactoryGirl.create(:dialog, :label => "ServiceDialog1") }
let(:dialog2) { FactoryGirl.create(:dialog, :label => "ServiceDialog2") }
let(:ra1) { FactoryGirl.create(:resource_action, :dialog => dialog1) }
let(:ra2) { FactoryGirl.create(:resource_action, :dialog => dialog2) }
let(:template) { FactoryGirl.create(:service_template, :name => "ServiceTemplate") }
let(:service) { FactoryGirl.create(:service, :name => "Service1") }
context "Service Dialogs collection" do
before { template.resource_actions = [ra1, ra2] }
it "query only returns href" do
api_basic_authorize collection_action_identifier(:service_dialogs, :read, :get)
get api_service_dialogs_url
expected = {
"name" => "service_dialogs",
"count" => Dialog.count,
"subcount" => Dialog.count,
"resources" => Array.new(Dialog.count) { {"href" => anything} }
}
expect(response.parsed_body).to include(expected)
expect(response).to have_http_status(:ok)
end
it "query with expanded resources to include content" do
api_basic_authorize collection_action_identifier(:service_dialogs, :read, :get)
get api_service_dialogs_url, :params => { :expand => "resources" }
expect_query_result(:service_dialogs, Dialog.count, Dialog.count)
expect_result_resources_to_include_keys("resources", %w(id href label content))
end
it "query single dialog to include content" do
api_basic_authorize action_identifier(:service_dialogs, :read, :resource_actions, :get)
get api_service_dialog_url(nil, dialog1)
expect_single_resource_query(
"id" => dialog1.id.to_s,
"href" => api_service_dialog_url(nil, dialog1),
"label" => dialog1.label
)
expect_result_to_have_keys(%w(content))
end
it "query single dialog to include content with target and resource action specified" do
api_basic_authorize action_identifier(:service_dialogs, :read, :resource_actions, :get)
service_template = FactoryGirl.create(:service_template)
get(api_service_dialog_url(nil, dialog1), :params => { :resource_action_id => ra1.id, :target_id => service_template.id, :target_type => 'service_template' })
expect_single_resource_query(
"id" => dialog1.id.to_s,
"href" => api_service_dialog_url(nil, dialog1),
"label" => dialog1.label
)
expect_result_to_have_keys(%w(content))
end
it "requires all of target_id, target_type, and resource_action" do
api_basic_authorize action_identifier(:service_dialogs, :read, :resource_actions, :get)
get(api_service_dialog_url(nil, dialog1), :params => { :target_id => 'id' })
expected = {
'error' => a_hash_including('message' => a_string_including('Must specify all of'))
}
expect(response).to have_http_status(:bad_request)
expect(response.parsed_body).to include(expected)
end
it "query single dialog to exclude content when attributes are asked for" do
api_basic_authorize action_identifier(:service_dialogs, :read, :resource_actions, :get)
get api_service_dialog_url(nil, dialog1), :params => { :attributes => "id,label" }
expect_result_to_have_only_keys(%w(href id label))
end
context 'Delete Service Dialogs' do
it 'DELETE /api/service_dialogs/:id' do
dialog = FactoryGirl.create(:dialog)
api_basic_authorize collection_action_identifier(:service_dialogs, :delete)
expect do
delete(api_service_dialog_url(nil, dialog))
end.to change(Dialog, :count).by(-1)
expect(response).to have_http_status(:no_content)
end
it 'POST /api/service_dialogs/:id deletes a single service dialog' do
dialog = FactoryGirl.create(:dialog)
api_basic_authorize collection_action_identifier(:service_dialogs, :delete)
expect do
post(api_service_dialog_url(nil, dialog), :params => { 'action' => 'delete' })
end.to change(Dialog, :count).by(-1)
expect(response).to have_http_status(:ok)
end
it 'POST /api/service_dialogs deletes a single service dialog' do
dialog = FactoryGirl.create(:dialog)
api_basic_authorize collection_action_identifier(:service_dialogs, :delete)
expect do
post(api_service_dialogs_url, :params => { 'action' => 'delete', 'resources' => [{ 'id' => dialog.id }] })
end.to change(Dialog, :count).by(-1)
expect(response).to have_http_status(:ok)
end
it 'POST /api/service_dialogs deletes multiple service dialogs' do
dialog_a, dialog_b = FactoryGirl.create_list(:dialog, 2)
api_basic_authorize collection_action_identifier(:service_dialogs, :delete)
expect do
post(
api_service_dialogs_url,
:params => {
'action' => 'delete',
'resources' => [{'id' => dialog_a.id}, {'id' => dialog_b.id}]
}
)
end.to change(Dialog, :count).by(-2)
expect(response).to have_http_status(:ok)
end
end
context 'Edit Service Dialogs' do
let(:dialog) { FactoryGirl.create(:dialog_with_tab_and_group_and_field) }
it 'POST /api/service_dialogs/:id rejects a request without appropriate role' do
api_basic_authorize
post(api_service_dialog_url(nil, dialog), :params => gen_request(:edit, :label => 'updated label'))
expect(response).to have_http_status(:forbidden)
end
it 'POST /api/service_dialogs/:id updates a service dialog' do
api_basic_authorize collection_action_identifier(:service_dialogs, :edit)
dialog_tab = dialog.dialog_tabs.first
dialog_group = dialog_tab.dialog_groups.first
dialog_field = dialog_group.dialog_fields.first
updated_dialog = {
'label' => 'updated label',
'content' => {
'dialog_tabs' => [
'id' => dialog_tab.id.to_s,
'label' => 'updated tab label',
'dialog_groups' => [
{
'id' => dialog_group.id.to_s,
'dialog_fields' => [
{ 'id' => dialog_field.id.to_s }
]
}
]
]
}
}
expected = {
'href' => a_string_including(api_service_dialog_url(nil, dialog)),
'id' => dialog.id.to_s,
'label' => 'updated label',
'dialog_tabs' => a_collection_including(
a_hash_including('label' => 'updated tab label')
)
}
expect do
post(api_service_dialog_url(nil, dialog), :params => gen_request(:edit, updated_dialog))
dialog.reload
end.to change(dialog, :content)
expect(response).to have_http_status(:ok)
expect(response.parsed_body).to include(expected)
end
it 'POST /api/service_dialogs updates multiple service dialog' do
dialog2 = FactoryGirl.create(:dialog_with_tab_and_group_and_field)
api_basic_authorize collection_action_identifier(:service_dialogs, :edit)
post(
api_service_dialogs_url,
:params => {
:action => 'edit',
:resources => [
{:id => dialog.id, 'label' => 'foo bar'},
{:id => dialog2.id, :label => 'bar'}
]
}
)
expected = {
'results' => a_collection_containing_exactly(
a_hash_including(
'id' => dialog.id.to_s,
'label' => 'foo bar'
),
a_hash_including(
'id' => dialog2.id.to_s,
'label' => 'bar'
)
)
}
expect(response.parsed_body).to include(expected)
expect(response).to have_http_status(:ok)
end
end
context 'Service Dialogs Copy' do
it 'forbids blueprint copy without an appropriate role' do
dialog = FactoryGirl.create(:dialog_with_tab_and_group_and_field)
api_basic_authorize
post(api_service_dialog_url(nil, dialog), :params => { :action => 'copy' })
expect(response).to have_http_status(:forbidden)
end
it 'Can copy multiple service dialogs' do
dialog1 = FactoryGirl.create(:dialog_with_tab_and_group_and_field, :label => 'foo')
dialog2 = FactoryGirl.create(:dialog_with_tab_and_group_and_field, :label => 'bar')
api_basic_authorize collection_action_identifier(:service_dialogs, :copy)
expected = {
'results' => a_collection_containing_exactly(
a_hash_including(
'label' => "Copy of foo"
),
a_hash_including(
'label' => "Copy of bar"
)
)
}
expect do
post(
api_service_dialogs_url,
:params => {
:action => 'copy',
:resources => [
{:id => dialog1.id},
{:id => dialog2.id}
]
}
)
end.to change(Dialog, :count).by(2)
expect(response.parsed_body).to include(expected)
expect(response).to have_http_status(:ok)
end
it 'Can copy a single service dialog' do
dialog = FactoryGirl.create(:dialog_with_tab_and_group_and_field, :label => 'foo')
api_basic_authorize collection_action_identifier(:service_dialogs, :copy)
expected = {
'label' => "Copy of foo"
}
expect do
post(api_service_dialog_url(nil, dialog), :params => { :action => 'copy' })
end.to change(Dialog, :count).by(1)
expect(response.parsed_body).to include(expected)
expect(response).to have_http_status(:ok)
end
it 'Can copy a service dialog with a new label' do
dialog = FactoryGirl.create(:dialog_with_tab_and_group_and_field, :label => 'bar')
api_basic_authorize collection_action_identifier(:service_dialogs, :copy)
expected = {
'label' => 'foo'
}
expect do
post(api_service_dialog_url(nil, dialog), :params => { :action => 'copy', 'label' => 'foo' })
end.to change(Dialog, :count).by(1)
expect(response.parsed_body).to include(expected)
expect(response).to have_http_status(:ok)
end
end
end
context "Service Dialogs subcollection" do
before do
template.resource_actions = [ra1, ra2]
api_basic_authorize
end
it "query all service dialogs of a Service Template" do
get(api_service_template_service_dialogs_url(nil, template), :params => { :expand => "resources" })
dialogs = template.dialogs
expect_query_result(:service_dialogs, dialogs.count, dialogs.count)
expect_result_resources_to_include_data("resources", "label" => dialogs.pluck(:label))
end
it "query all service dialogs of a Service" do
service.update_attributes!(:service_template_id => template.id)
get(api_service_service_dialogs_url(nil, service), :params => { :expand => "resources" })
dialogs = service.dialogs
expect_query_result(:service_dialogs, dialogs.count, dialogs.count)
expect_result_resources_to_include_data("resources", "label" => dialogs.pluck(:label))
end
it "queries service dialogs content with the template and related resource action specified and returns IDs" do
get(api_service_template_service_dialog_url(nil, template, dialog1), :params => { :attributes => "content" })
expected = {
'content' => a_collection_including(
a_hash_including('id' => dialog1.id.to_s)
)}
expect(response).to have_http_status(:ok)
expect(response.parsed_body).to include(expected)
end
end
describe "Service Dialogs refresh dialog fields" do
let(:dialog1) { FactoryGirl.create(:dialog, :label => "Dialog1") }
let(:tab1) { FactoryGirl.create(:dialog_tab, :label => "Tab1") }
let(:group1) { FactoryGirl.create(:dialog_group, :label => "Group1") }
let(:text1) { FactoryGirl.create(:dialog_field_text_box, :label => "TextBox1", :name => "text1") }
def init_dialog
dialog1.dialog_tabs << tab1
tab1.dialog_groups << group1
group1.dialog_fields << text1
end
it "rejects refresh dialog fields requests without appropriate role" do
api_basic_authorize
post(api_service_dialog_url(nil, dialog1), :params => gen_request(:refresh_dialog_fields, "fields" => %w(test1)))
expect(response).to have_http_status(:forbidden)
end
it "rejects refresh dialog fields with unspecified fields" do
api_basic_authorize action_identifier(:service_dialogs, :refresh_dialog_fields)
init_dialog
post(api_service_dialog_url(nil, dialog1), :params => gen_request(:refresh_dialog_fields))
expect_single_action_result(:success => false, :message => /must specify fields/i)
end
it "rejects refresh dialog fields of invalid fields" do
api_basic_authorize action_identifier(:service_dialogs, :refresh_dialog_fields)
init_dialog
post(api_service_dialog_url(nil, dialog1), :params => gen_request(
:refresh_dialog_fields,
"fields" => %w(bad_field),
"resource_action_id" => ra1.id,
"target_id" => template.id,
"target_type" => "service_template"
))
expect_single_action_result(:success => false, :message => /unknown dialog field bad_field/i)
end
it "requires all of resource_action_id, target_id, and target_type" do
api_basic_authorize action_identifier(:service_dialogs, :refresh_dialog_fields)
init_dialog
post(api_service_dialog_url(nil, dialog1), :params => gen_request(
:refresh_dialog_fields,
"fields" => %w(text1)
))
expect_single_action_result(:success => false, :message => a_string_including('Must specify all of'))
end
it "supports refresh when passing in resource_action_id, target_id, and target_type" do
api_basic_authorize action_identifier(:service_dialogs, :refresh_dialog_fields)
init_dialog
post(api_service_dialog_url(nil, dialog1), :params => gen_request(
:refresh_dialog_fields,
"fields" => %w(text1),
"resource_action_id" => ra1.id,
"target_id" => template.id,
"target_type" => "service_template"
))
expect(response.parsed_body).to include(
"success" => true,
"message" => a_string_matching(/refreshing dialog fields/i),
"href" => api_service_dialog_url(nil, dialog1),
"result" => hash_including("text1")
)
end
it "supports refresh dialog fields of valid fields" do
api_basic_authorize action_identifier(:service_dialogs, :refresh_dialog_fields)
init_dialog
post(api_service_dialog_url(nil, dialog1), :params => gen_request(
:refresh_dialog_fields,
"fields" => %w(text1),
"resource_action_id" => ra1.id,
"target_id" => template.id,
"target_type" => "service_template"
))
expect(response.parsed_body).to include(
"success" => true,
"message" => a_string_matching(/refreshing dialog fields/i),
"href" => api_service_dialog_url(nil, dialog1),
"result" => hash_including("text1")
)
end
end
context 'Creates service dialogs' do
let(:dialog_request) do
{
:description => 'Dialog',
:label => 'dialog_label',
:dialog_tabs => [
{
:description => 'Dialog tab',
:position => 0,
:label => 'dialog_tab_label',
:dialog_groups => [
{
:description => 'Dialog group',
:label => 'group_label',
:dialog_fields => [
{
:name => 'A dialog field',
:label => 'dialog_field_label'
}
]
}
]
}
]
}
end
it 'rejects service dialog creation without appropriate role' do
api_basic_authorize
post(api_service_dialogs_url, :params => dialog_request)
expect(response).to have_http_status(:forbidden)
end
it 'rejects service dialog creation with an href specified' do
api_basic_authorize collection_action_identifier(:service_dialogs, :create)
post(api_service_dialogs_url, :params => dialog_request.merge!("href" => api_service_dialog_url(nil, 123)))
expected = {
"error" => a_hash_including(
"kind" => "bad_request",
"message" => a_string_matching(/id or href should not be specified/)
)
}
expect(response.parsed_body).to include(expected)
expect(response).to have_http_status(:bad_request)
end
it 'rejects service dialog creation with an id specified' do
api_basic_authorize collection_action_identifier(:service_dialogs, :create)
post(api_service_dialogs_url, :params => dialog_request.merge!("id" => 123))
expected = {
"error" => a_hash_including(
"kind" => "bad_request",
"message" => a_string_matching(/id or href should not be specified/)
)
}
expect(response.parsed_body).to include(expected)
expect(response).to have_http_status(:bad_request)
end
it 'supports single service dialog creation' do
api_basic_authorize collection_action_identifier(:service_dialogs, :create)
expected = {
"results" => [
a_hash_including(
"description" => "Dialog",
"label" => "dialog_label",
"dialog_tabs" => a_collection_including(a_hash_including("description" => "Dialog tab"))
)
]
}
expect do
post(api_service_dialogs_url, :params => dialog_request)
end.to change(Dialog, :count).by(1)
expect(response.parsed_body).to include(expected)
expect(response).to have_http_status(:ok)
end
it 'supports multiple service dialog creation' do
api_basic_authorize collection_action_identifier(:service_dialogs, :create)
dialog_request_2 = {
:description => 'Dialog 2',
:label => 'dialog_2_label',
:dialog_tabs => [
{
:description => 'Dialog 2 tab',
:position => 0,
:label => 'dialog_2_label',
:dialog_groups => [
{
:description => 'a new dialog group',
:label => 'dialog_2_group_label',
:dialog_fields => [
{
:name => 'a new dialog field',
:label => 'dialog_field_label'
}
]
}
]
}
]
}
expected = {
"results" => [
a_hash_including(
"description" => "Dialog",
"label" => "dialog_label"
),
a_hash_including(
"description" => "Dialog 2",
"label" => "dialog_2_label"
)
]
}
expect do
post(api_service_dialogs_url, :params => gen_request(:create, [dialog_request, dialog_request_2]))
end.to change(Dialog, :count).by(2)
expect(response.parsed_body).to include(expected)
expect(response).to have_http_status(:ok)
end
it 'returns dialog import service errors' do
api_basic_authorize collection_action_identifier(:service_dialogs, :create)
invalid_request = {
'description' => 'Dialog',
'label' => 'a_dialog'
}
expected = {
'error' => a_hash_including(
'kind' => 'bad_request',
'message' => a_string_including('Failed to create a new dialog'),
'klass' => 'Api::BadRequestError'
)
}
expect do
post(api_service_dialogs_url, :params => invalid_request)
end.to change(Dialog, :count).by(0)
expect(response.parsed_body).to include(expected)
expect(response).to have_http_status(:bad_request)
end
end
end