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
|
package FixMyStreet::Cobrand::AnonAllowedByCategory;
use parent 'FixMyStreet::Cobrand::UKCouncils';
sub council_url { 'anonbycategory' }
sub council_name { 'Aberdeen City Council' }
sub council_area { 'Aberdeen' }
sub council_area_id { 2650 }
sub anonymous_account { { email => 'anoncategory@example.org', name => 'Anonymous Category' } }
package main;
use FixMyStreet::TestMech;
my $mech = FixMyStreet::TestMech->new;
my $superuser = $mech->create_user_ok('superuser@example.com', name => 'Super User', is_superuser => 1);
$mech->log_in_ok( $superuser->email );
my $body = $mech->create_body_ok(2650, 'Aberdeen City Council');
my $body2 = $mech->create_body_ok(2237, 'Oxfordshire County Council');
my $user = $mech->create_user_ok('user@example.com', name => 'OCC User', from_body => $body2);
$user->user_body_permissions->create({ body => $body2, permission_type => 'category_edit' });
# This override is wrapped around ALL the /admin/body tests
FixMyStreet::override_config {
MAPIT_URL => 'http://mapit.uk/',
MAPIT_TYPES => [ 'UTA' ],
BASE_URL => 'http://www.example.org',
}, sub {
$mech->get_ok('/admin/body/' . $body->id);
$mech->content_contains('Aberdeen City Council');
$mech->content_like(qr{AB\d\d});
$mech->content_contains("http://www.example.org/around");
subtest 'check contact creation' => sub {
$mech->get_ok('/admin/body/' . $body->id);
$mech->submit_form_ok( { with_fields => {
category => 'test category',
email => 'test@example.com',
note => 'test note',
non_public => undef,
state => 'unconfirmed',
} } );
$mech->content_contains( 'test category' );
$mech->content_contains( 'test@example.com' );
$mech->content_contains( '<td>test note' );
$mech->content_like( qr/<td>\s*unconfirmed\s*<\/td>/ ); # No private
$mech->submit_form_ok( { with_fields => {
category => 'private category',
email => 'test@example.com',
note => 'test note',
non_public => 'on',
} } );
$mech->content_contains( 'private category' );
$mech->content_like( qr{test\@example.com\s*</td>\s*<td>\s*confirmed\s*<br>\s*<small>\s*Private\s*</small>\s*</td>} );
$mech->submit_form_ok( { with_fields => {
category => 'test/category',
email => 'test@example.com',
note => 'test/note',
non_public => 'on',
} } );
$mech->get_ok('/admin/body/' . $body->id . '/test/category');
$mech->content_contains('test/category');
};
subtest 'check contact editing' => sub {
$mech->get_ok('/admin/body/' . $body->id .'/test%20category');
$mech->content_lacks( 'group</strong> is used for the top-level category' );
$mech->submit_form_ok( { with_fields => {
email => 'test2@example.com',
note => 'test2 note',
non_public => undef,
} } );
$mech->content_contains( 'test category' );
$mech->content_like( qr{test2\@example.com\s*</td>\s*<td>\s*unconfirmed\s*</td>} );
$mech->content_contains( '<td>test2 note' );
$mech->get_ok('/admin/body/' . $body->id . '/test%20category');
$mech->submit_form_ok( { with_fields => {
email => 'test2@example.com, test3@example.com',
note => 'test3 note',
} } );
$mech->content_contains( 'test2@example.com,test3@example.com' );
$mech->get_ok('/admin/body/' . $body->id . '/test%20category');
$mech->content_contains( '<td><strong>test2@example.com,test3@example.com' );
$mech->submit_form_ok( { with_fields => {
email => 'test2@example.com',
note => 'test2 note',
non_public => 'on',
} } );
$mech->content_like( qr{test2\@example.com\s*</td>\s*<td>\s*unconfirmed\s*<br>\s*<small>\s*Private\s*</small>\s*</td>} );
$mech->get_ok('/admin/body/' . $body->id . '/test%20category');
$mech->content_contains( '<td><strong>test2@example.com' );
};
subtest 'check contact renaming' => sub {
my ($report) = $mech->create_problems_for_body(1, $body->id, 'Title', { category => 'test category' });
$mech->get_ok('/admin/body/' . $body->id .'/test%20category');
$mech->submit_form_ok( { with_fields => { category => 'private category' } } );
$mech->content_contains('You cannot rename');
$mech->submit_form_ok( { with_fields => { category => 'testing category' } } );
$mech->content_contains( 'testing category' );
$mech->get('/admin/body/' . $body->id . '/test%20category');
is $mech->res->code, 404;
$mech->get_ok('/admin/body/' . $body->id . '/testing%20category');
$mech->content_contains('<td><strong>test2@example.com</strong></td>');
$report->discard_changes;
is $report->category, 'testing category';
$mech->submit_form_ok( { with_fields => { category => 'test category' } } );
};
subtest 'check contact updating' => sub {
$mech->get_ok('/admin/body/' . $body->id . '/test%20category');
$mech->content_like(qr{test2\@example.com</strong>[^<]*</td>[^<]*<td>unconfirmed}s);
$mech->get_ok('/admin/body/' . $body->id);
$mech->form_number( 1 );
$mech->tick( 'confirmed', 'test category' );
$mech->submit_form_ok({form_number => 1});
$mech->content_like(qr'test2@example.com</td>[^<]*<td>\s*confirmed's);
$mech->get_ok('/admin/body/' . $body->id . '/test%20category');
$mech->content_like(qr{test2\@example.com[^<]*</td>[^<]*<td><strong>confirmed}s);
};
$body->update({ send_method => undef });
subtest 'check open311 configuring' => sub {
$mech->get_ok('/admin/body/' . $body->id);
$mech->content_lacks('Council contacts configured via Open311');
$mech->form_number(3);
$mech->submit_form_ok(
{
with_fields => {
api_key => 'api key',
endpoint => 'http://example.com/open311',
jurisdiction => 'mySociety',
send_comments => 0,
send_method => 'Open311',
}
}
);
$mech->content_contains('Council contacts configured via Open311');
$mech->content_contains('Values updated');
my $conf = FixMyStreet::DB->resultset('Body')->find( $body->id );
is $conf->endpoint, 'http://example.com/open311', 'endpoint configured';
is $conf->api_key, 'api key', 'api key configured';
is $conf->jurisdiction, 'mySociety', 'jurisdiction configures';
$mech->form_number(3);
$mech->submit_form_ok(
{
with_fields => {
api_key => 'new api key',
endpoint => 'http://example.org/open311',
jurisdiction => 'open311',
send_comments => 0,
send_method => 'Open311',
}
}
);
$mech->content_contains('Values updated');
$conf = FixMyStreet::DB->resultset('Body')->find( $body->id );
is $conf->endpoint, 'http://example.org/open311', 'endpoint updated';
is $conf->api_key, 'new api key', 'api key updated';
is $conf->jurisdiction, 'open311', 'jurisdiction configures';
ok !$conf->get_extra_metadata('fetch_all_problems'), 'fetch all problems unset';
$mech->form_number(3);
$mech->submit_form_ok(
{
with_fields => {
api_key => 'new api key',
endpoint => 'http://example.org/open311',
jurisdiction => 'open311',
send_comments => 0,
send_method => 'Open311',
'extra[fetch_all_problems]' => 1,
}
}
);
$mech->content_contains('Values updated');
$conf = FixMyStreet::DB->resultset('Body')->find( $body->id );
ok $conf->get_extra_metadata('fetch_all_problems'), 'fetch all problems set';
$mech->form_number(3);
$mech->submit_form_ok(
{
with_fields => {
api_key => 'new api key',
endpoint => 'http://example.org/open311',
jurisdiction => 'open311',
send_comments => 0,
send_method => 'Open311',
'extra[fetch_all_problems]' => 0,
can_be_devolved => 1, # for next test
}
}
);
$mech->content_contains('Values updated');
$conf = FixMyStreet::DB->resultset('Body')->find( $body->id );
ok !$conf->get_extra_metadata('fetch_all_problems'), 'fetch all problems unset';
};
subtest 'check open311 devolved editing' => sub {
$mech->get_ok('/admin/body/' . $body->id . '/test%20category');
$mech->content_contains("name=\"category\"\n size=\"30\" value=\"test category\"\n readonly>", 'Cannot edit Open311 category name');
$mech->submit_form_ok( { with_fields => {
send_method => 'Email',
email => 'testing@example.org',
note => 'Updating contact to email',
} } );
$mech->content_contains('Values updated');
$mech->get_ok('/admin/body/' . $body->id . '/test%20category');
$mech->content_contains("name=\"category\"\n size=\"30\" value=\"test category\"\n required>", 'Can edit as now devolved');
$mech->submit_form_ok( { with_fields => {
send_method => '',
email => 'open311 code',
note => 'Removing email send method',
} } );
$mech->content_contains('open311 code');
$mech->content_contains('Values updated');
};
subtest 'check text output' => sub {
$mech->get_ok('/admin/body/' . $body->id . '?text=1');
is $mech->content_type, 'text/plain';
$mech->content_contains('test category');
$mech->content_lacks('<body');
};
subtest 'disable form message editing' => sub {
$mech->get_ok('/admin/body/' . $body->id . '/test%20category');
$mech->submit_form_ok( { with_fields => {
disable => 1,
disable_message => '<em>Please</em> <u>ring</u> us on <a href="tel:01234">01234</a>, click <a href="javascript:bad">bad</a>',
note => 'Adding emergency message',
} } );
$mech->content_contains('Values updated');
my $contact = $body->contacts->find({ category => 'test category' });
is_deeply $contact->get_extra_fields, [{
description => '<em>Please</em> ring us on <a href="tel:01234">01234</a>, click <a>bad</a>',
code => '_fms_disable_',
protected => 'true',
variable => 'false',
disable_form => 'true',
}], 'right message added';
};
subtest 'open311 protection editing' => sub {
$mech->get_ok('/admin/body/' . $body->id . '/test%20category');
$mech->submit_form_ok( { with_fields => {
open311_protect => 1,
note => 'Protected from Open311 changes',
} } );
$mech->content_contains('Values updated');
my $contact = $body->contacts->find({ category => 'test category' });
is $contact->get_extra_metadata('open311_protect'), 1, 'Open311 protect flag set';
};
subtest 'test assigned_users_only setting' => sub {
$mech->get_ok('/admin/body/' . $body->id . '/test%20category');
$mech->submit_form_ok( { with_fields => {
assigned_users_only => 1,
} } );
$mech->content_contains('Values updated');
my $contact = $body->contacts->find({ category => 'test category' });
is $contact->get_extra_metadata('assigned_users_only'), 1;
};
subtest 'updates disabling' => sub {
$mech->get_ok('/admin/body/' . $body->id . '/test%20category');
$mech->submit_form_ok( { with_fields => {
updates_disallowed => 1,
note => 'Disabling updates',
} } );
$mech->content_contains('Values updated');
my $contact = $body->contacts->find({ category => 'test category' });
is $contact->get_extra_metadata('updates_disallowed'), 1, 'Updates disallowed flag set';
};
subtest 'reopen disabling' => sub {
$mech->get_ok('/admin/body/' . $body->id . '/test%20category');
$mech->submit_form_ok( { with_fields => {
reopening_disallowed => 1,
note => 'Disabling reopening',
} } );
$mech->content_contains('Values updated');
my $contact = $body->contacts->find({ category => 'test category' });
is $contact->get_extra_metadata('reopening_disallowed'), 1, 'Reopening disallowed flag set';
};
subtest 'allow anonymous reporting' => sub {
$mech->get_ok('/admin/body/' . $body->id . '/test%20category');
$mech->content_lacks('Allow anonymous reports');
};
}; # END of override wrap
FixMyStreet::override_config {
MAPIT_URL => 'http://mapit.uk/',
MAPIT_TYPES => [ 'UTA' ],
BASE_URL => 'http://www.example.org',
ALLOWED_COBRANDS => [ "fixmystreet", "anonallowedbycategory" ],
}, sub {
subtest 'allow anonymous reporting' => sub {
$mech->get_ok('/admin/body/' . $body->id . '/test%20category');
$mech->submit_form_ok( { with_fields => {
anonymous_allowed => 1,
note => 'Anonymous Allowed',
} } );
$mech->content_contains('Values updated');
my $contact = $body->contacts->find({ category => 'test category' });
is $contact->get_extra_metadata('anonymous_allowed'), 1, 'Anonymous reports allowed flag set';
};
};
FixMyStreet::override_config {
MAPIT_URL => 'http://mapit.uk/',
MAPIT_TYPES => [ 'UTA' ],
BASE_URL => 'http://www.example.org',
COBRAND_FEATURES => {
category_groups => { default => 1 },
}
}, sub {
subtest 'group editing works' => sub {
$mech->get_ok('/admin/body/' . $body->id);
$mech->content_contains('Parent categories');
$mech->submit_form_ok( { with_fields => {
category => 'grouped category',
email => 'test@example.com',
note => 'test note',
group => 'group a',
non_public => undef,
state => 'unconfirmed',
} } );
my $contact = $body->contacts->find({ category => 'grouped category' });
is_deeply $contact->get_extra_metadata('group'), ['group a'], "group stored correctly";
};
subtest 'group can be unset' => sub {
$mech->get_ok('/admin/body/' . $body->id);
$mech->content_contains('Parent categories');
$mech->submit_form_ok( { with_fields => {
category => 'grouped category',
email => 'test@example.com',
note => 'test note',
group => undef,
non_public => undef,
state => 'unconfirmed',
} } );
my $contact = $body->contacts->find({ category => 'grouped category' });
is $contact->get_extra_metadata('group'), undef, "group unset correctly";
};
};
FixMyStreet::override_config {
MAPIT_URL => 'http://mapit.uk/',
MAPIT_TYPES => [ 'UTA' ],
BASE_URL => 'http://www.example.org',
COBRAND_FEATURES => {
category_groups => { default => 1 },
}
}, sub {
subtest 'multi group editing works' => sub {
$mech->get_ok('/admin/body/' . $body->id);
$mech->content_contains('Parent categories');
# have to do this as a post as adding a second group requires
# javascript
$mech->post_ok( '/admin/body/' . $body->id, {
posted => 'new',
token => $mech->form_id('category_edit')->value('token'),
category => 'grouped category',
email => 'test@example.com',
note => 'test note',
'group' => [ 'group a', 'group b'],
non_public => undef,
state => 'unconfirmed',
} );
my $contact = $body->contacts->find({ category => 'grouped category' });
is_deeply $contact->get_extra_metadata('group'), ['group a', 'group b'], "group stored correctly";
};
};
subtest 'check log of the above' => sub {
$mech->get_ok('/admin/users/' . $superuser->id . '/log');
$mech->content_contains('Added category <a href="/admin/body/' . $body->id . '/test/category">test/category</a>');
$mech->content_contains('Edited category <a href="/admin/body/' . $body->id . '/test category">test category</a>');
$mech->content_contains('Edited body <a href="/admin/body/' . $body->id . '">Aberdeen City Council</a>');
};
subtest 'check update disallowed message' => sub {
FixMyStreet::override_config {
MAPIT_URL => 'http://mapit.uk/',
ALLOWED_COBRANDS => 'bathnes',
COBRAND_FEATURES => { updates_allowed => { bathnes => 'open' } }
}, sub {
$mech->get_ok('/admin/body/' . $body->id .'/test%20category');
$mech->content_contains('even if this is unticked, only open reports can have updates left on them.');
};
FixMyStreet::override_config {
MAPIT_URL => 'http://mapit.uk/',
ALLOWED_COBRANDS => 'bathnes',
COBRAND_FEATURES => { updates_allowed => { bathnes => 'staff' } }
}, sub {
$mech->get_ok('/admin/body/' . $body->id .'/test%20category');
$mech->content_contains('even if this is unticked, only staff will be able to leave updates.');
};
FixMyStreet::override_config {
MAPIT_URL => 'http://mapit.uk/',
ALLOWED_COBRANDS => 'bathnes',
COBRAND_FEATURES => { updates_allowed => { bathnes => 'reporter' } }
}, sub {
$mech->get_ok('/admin/body/' . $body->id .'/test%20category');
$mech->content_contains('even if this is unticked, only the problem reporter will be able to leave updates');
};
};
subtest 'check hardcoded contact renaming' => sub {
FixMyStreet::override_config {
MAPIT_URL => 'http://mapit.uk/',
'ALLOWED_COBRANDS' => [ 'oxfordshire' ],
}, sub {
my $contact = FixMyStreet::DB->resultset('Contact')->create(
{
body_id => $body2->id,
category => 'protected category',
state => 'confirmed',
editor => $0,
whenedited => \'current_timestamp',
note => 'protected contact',
email => 'protected@example.org',
}
);
$contact->set_extra_metadata( 'hardcoded', 1 );
$contact->update;
$mech->get_ok('/admin/body/' . $body2->id .'/protected%20category');
$mech->content_contains( 'name="hardcoded"' );
$mech->content_like( qr'value="protected category"[^>]*readonly's );
$mech->submit_form_ok( { with_fields => { category => 'non protected category', note => 'rename category' } } );
$mech->content_contains( 'protected category' );
$mech->content_lacks( 'non protected category' );
$mech->get('/admin/body/' . $body2->id . '/non%20protected%20category');
is $mech->res->code, 404;
$mech->get_ok('/admin/body/' . $body2->id .'/protected%20category');
$mech->submit_form_ok( { with_fields => { hardcoded => 0, note => 'remove hardcoding' } } );
$mech->get_ok('/admin/body/' . $body2->id .'/protected%20category');
$mech->content_unlike( qr'value="protected category"[^>]*readonly's );
$mech->submit_form_ok( { with_fields => { category => 'non protected category', note => 'rename category' } } );
$mech->content_contains( 'non protected category' );
$mech->get_ok('/admin/body/' . $body2->id . '/non%20protected%20category');
$mech->get('/admin/body/' . $body2->id . '/protected%20category');
is $mech->res->code, 404;
$contact->discard_changes;
$contact->set_extra_metadata( 'hardcoded', 1 );
$contact->update;
$mech->log_out_ok( $superuser->email );
$mech->log_in_ok( $user->email );
$mech->get_ok('/admin/body/' . $body2->id . '/non%20protected%20category');
$mech->content_lacks( 'name="hardcoded"' );
$user->update( { is_superuser => 1 } );
$mech->get_ok('/admin/body/' . $body2->id . '/non%20protected%20category');
$mech->content_contains('name="hardcoded"' );
$user->update( { is_superuser => 0 } );
$mech->submit_form_ok( { with_fields => { hardcoded => 0, note => 'remove hardcoding' } } );
$mech->content_lacks( 'name="hardcoded"' );
$contact->discard_changes;
is $contact->get_extra_metadata('hardcoded'), 1, "non superuser can't remove hardcoding";
$mech->log_out_ok( $user->email );
};
};
done_testing();
|