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
|
use CGI::Simple;
use Test::MockModule;
use Test::MockTime qw(:all);
use Test::Output;
use FixMyStreet::TestMech;
use FixMyStreet::Script::Reports;
my $mech = FixMyStreet::TestMech->new;
# Mock fetching bank holidays
my $uk = Test::MockModule->new('FixMyStreet::Cobrand::UK');
$uk->mock('_fetch_url', sub { '{}' });
# Create test data
my $user = $mech->create_user_ok( 'bromley@example.com', name => 'Bromley' );
my $body = $mech->create_body_ok( 2482, 'Bromley Council',
{ can_be_devolved => 1, send_extended_statuses => 1, comment_user => $user });
my $contact = $mech->create_contact_ok(
body_id => $body->id,
category => 'Other',
email => 'LIGHT',
);
$contact->set_extra_metadata(id_field => 'service_request_id_ext');
$contact->set_extra_fields(
{ code => 'easting', datatype => 'number', },
{ code => 'northing', datatype => 'number', },
{ code => 'service_request_id_ext', datatype => 'number', },
{ code => 'service_sub_code', values => [ { key => 'RED', name => 'Red' }, { key => 'BLUE', name => 'Blue' } ], },
);
$contact->update;
my $tfl = $mech->create_body_ok( 2482, 'TfL');
$mech->create_contact_ok(
body_id => $tfl->id,
category => 'Traffic Lights',
email => 'tfl@example.org',
);
my $waste = $mech->create_contact_ok(
body => $body,
category => 'Report missed collection',
email => 'missed',
send_method => 'Open311',
endpoint => 'waste-endpoint',
);
$waste->set_extra_metadata(group => ['Waste']);
$waste->update;
my @reports = $mech->create_problems_for_body( 1, $body->id, 'Test', {
latitude => 51.402096,
longitude => 0.015784,
cobrand => 'bromley',
user => $user,
});
my $report = $reports[0];
for my $update ('in progress', 'unable to fix') {
FixMyStreet::DB->resultset('Comment')->find_or_create( {
problem_state => $update,
problem_id => $report->id,
user_id => $user->id,
name => 'User',
mark_fixed => 'f',
text => "This update marks it as $update",
state => 'confirmed',
confirmed => 'now()',
anonymous => 'f',
} );
}
# Test Bromley special casing of 'unable to fix'
$mech->get_ok( '/report/' . $report->id );
$mech->content_contains( 'marks it as in progress' );
$mech->content_contains( 'State changed to: In progress' );
$mech->content_contains( 'marks it as unable to fix' );
$mech->content_contains( 'State changed to: No further action' );
for my $test (
{
desc => 'testing special Open311 behaviour',
updates => {},
expected => {
'attribute[easting]' => 540315,
'attribute[northing]' => 168935,
'attribute[service_request_id_ext]' => $report->id,
'attribute[report_title]' => 'Test Test 1 for ' . $body->id,
'jurisdiction_id' => 'FMS',
address_id => undef,
},
},
{
desc => 'testing Open311 behaviour with no map click or postcode',
updates => {
used_map => 0,
postcode => ''
},
expected => {
'attribute[easting]' => 540315,
'attribute[northing]' => 168935,
'attribute[service_request_id_ext]' => $report->id,
'jurisdiction_id' => 'FMS',
'address_id' => '#NOTPINPOINTED#',
},
},
{
desc => 'asset ID',
feature_id => '1234',
expected => {
'attribute[service_request_id_ext]' => $report->id,
'attribute[report_title]' => 'Test Test 1 for ' . $body->id . ' | ID: 1234',
},
},
) {
subtest $test->{desc}, sub {
$report->$_($test->{updates}->{$_}) for keys %{$test->{updates}};
$report->$_(undef) for qw/ whensent send_method_used external_id /;
$report->set_extra_fields({ name => 'feature_id', value => $test->{feature_id} })
if $test->{feature_id};
$report->update;
$body->update( { send_method => 'Open311', endpoint => 'http://bromley.endpoint.example.com', jurisdiction => 'FMS', api_key => 'test', send_comments => 1 } );
my $test_data;
FixMyStreet::override_config {
STAGING_FLAGS => { send_reports => 1 },
ALLOWED_COBRANDS => [ 'fixmystreet', 'bromley' ],
MAPIT_URL => 'http://mapit.uk/',
}, sub {
$test_data = FixMyStreet::Script::Reports::send();
};
$report->discard_changes;
ok $report->whensent, 'Report marked as sent';
is $report->send_method_used, 'Open311', 'Report sent via Open311';
is $report->external_id, 248, 'Report has right external ID';
my $req = $test_data->{test_req_used};
my $c = CGI::Simple->new($req->content);
is $c->param($_), $test->{expected}->{$_}, "Request had correct $_"
for keys %{$test->{expected}};
};
}
for my $test (
{
cobrand => 'bromley',
fields => {
submit_update => 1,
username_register => 'unregistered@example.com',
update => 'Update from an unregistered user',
add_alert => undef,
first_name => 'Unreg',
last_name => 'User',
fms_extra_title => 'DR',
may_show_name => undef,
}
},
{
cobrand => 'fixmystreet',
fields => {
submit_update => 1,
username_register => 'unregistered@example.com',
update => 'Update from an unregistered user',
add_alert => undef,
name => 'Unreg User',
fms_extra_title => 'DR',
may_show_name => undef,
}
},
)
{
subtest 'check Bromley update emails via ' . $test->{cobrand} . ' cobrand are correct' => sub {
$mech->log_out_ok();
$mech->clear_emails_ok();
my $report_id = $report->id;
FixMyStreet::override_config {
ALLOWED_COBRANDS => [ $test->{cobrand} ],
}, sub {
$mech->get_ok("/report/$report_id");
$mech->submit_form_ok(
{
with_fields => $test->{fields}
},
'submit update'
);
};
$mech->content_contains('Nearly done! Now check your email');
my $body = $mech->get_text_body_from_email;
like $body, qr/This update will be sent to Bromley Council/i, "Email indicates problem will be sent to Bromley";
unlike $body, qr/Note that we do not send updates to/i, "Email does not say updates aren't sent to Bromley";
my $unreg_user = FixMyStreet::DB->resultset('User')->find( { email => 'unregistered@example.com' } );
ok $unreg_user, 'found user';
$mech->delete_user( $unreg_user );
};
}
subtest 'check display of TfL and waste reports' => sub {
$mech->create_problems_for_body( 1, $tfl->id, 'TfL Test', {
latitude => 51.402096,
longitude => 0.015784,
cobrand => 'bromley',
user => $user,
});
$mech->get_ok( '/report/' . $report->id );
FixMyStreet::override_config {
ALLOWED_COBRANDS => 'bromley',
MAPIT_URL => 'http://mapit.uk/',
}, sub {
$mech->follow_link_ok({ text_regex => qr/Back to all reports/i });
};
$mech->content_like(qr{<a title="TfL Test[^>]*www.example.org[^>]*><img[^>]*grey});
$mech->content_like(qr{<a title="Test Test[^>]*href="/[^>]*><img[^>]*yellow});
$mech->content_lacks('Report missed collection');
};
subtest 'check geolocation overrides' => sub {
my $cobrand = FixMyStreet::Cobrand::Bromley->new;
foreach my $test (
{ query => 'Main Rd, BR1', town => 'Bromley', string => 'Main Rd' },
{ query => 'Main Rd, BR3', town => 'Beckenham', string => 'Main Rd' },
{ query => 'Main Rd, BR4', town => 'West Wickham', string => 'Main Rd' },
{ query => 'Main Rd, BR5', town => 'Orpington', string => 'Main Rd' },
{ query => 'Main Rd, BR7', town => 'Chislehurst', string => 'Main Rd' },
{ query => 'Main Rd, BR8', town => 'Swanley', string => 'Main Rd' },
{ query => 'Old Priory Avenue', town => 'BR6 0PL', string => 'Old Priory Avenue' },
) {
my $res = $cobrand->disambiguate_location($test->{query});
is $res->{town}, $test->{town}, "Town matches $test->{town}";
is $res->{string}, $test->{string}, "String matches $test->{string}";
}
};
subtest 'check special subcategories in admin' => sub {
$mech->create_user_ok('superuser@example.com', is_superuser => 1);
$mech->log_in_ok('superuser@example.com');
$user->update({ from_body => $body->id });
FixMyStreet::override_config {
ALLOWED_COBRANDS => 'bromley',
MAPIT_URL => 'http://mapit.uk/',
}, sub {
$mech->get_ok('/admin/templates/' . $body->id . '/new');
$mech->get_ok('/admin/users/' . $user->id);
$mech->submit_form_ok({ with_fields => { 'contacts['.$contact->id.']' => 1, 'contacts[BLUE]' => 1 } });
};
$user->discard_changes;
is_deeply $user->get_extra_metadata('categories'), [ $contact->id ];
is_deeply $user->get_extra_metadata('subcategories'), [ 'BLUE' ];
};
subtest 'check heatmap page' => sub {
$user->update({ area_ids => [ 60705 ] });
FixMyStreet::override_config {
ALLOWED_COBRANDS => 'bromley',
MAPIT_URL => 'http://mapit.uk/',
COBRAND_FEATURES => { category_groups => { bromley => 1 }, heatmap => { bromley => 1 } },
}, sub {
$mech->log_in_ok($user->email);
$mech->get_ok('/dashboard/heatmap?end_date=2018-12-31');
$mech->get_ok('/dashboard/heatmap?filter_category=RED&ajax=1');
};
};
subtest 'test open enquiries' => sub {
set_fixed_time('2020-05-19T12:00:00Z'); # After sample food waste collection
FixMyStreet::override_config {
ALLOWED_COBRANDS => 'bromley',
COBRAND_FEATURES => {
echo => { bromley => { sample_data => 1 } },
waste => { bromley => 1 }
},
}, sub {
$mech->get_ok('/waste/uprn/12345');
$mech->follow_link_ok({ text => 'Report a problem with a food waste collection' });
$mech->content_contains('Waste spillage');
$mech->content_lacks('Gate not closed');
};
restore_time();
};
subtest 'test reporting before/after completion' => sub {
FixMyStreet::override_config {
ALLOWED_COBRANDS => 'bromley',
COBRAND_FEATURES => {
echo => { bromley => { sample_data => 1 } },
waste => { bromley => 1 }
},
}, sub {
set_fixed_time('2020-05-27T11:00:00Z');
$mech->get_ok('/waste/uprn/12345');
$mech->content_contains('(completed at 10:00am)');
$mech->content_contains('Wrong Bin Out');
$mech->content_lacks('Report a paper & cardboard collection');
$mech->content_contains('Report a refuse collection');
set_fixed_time('2020-05-28T12:00:00Z');
$mech->get_ok('/waste/uprn/12345');
$mech->content_contains('Report a refuse collection');
set_fixed_time('2020-05-29T12:00:00Z');
$mech->get_ok('/waste/uprn/12345');
$mech->content_contains('Report a refuse collection');
set_fixed_time('2020-05-30T12:00:00Z');
$mech->get_ok('/waste/uprn/12345');
$mech->content_lacks('Report a refuse collection');
};
restore_time();
};
subtest 'test waste max-per-day' => sub {
FixMyStreet::override_config {
ALLOWED_COBRANDS => 'bromley',
COBRAND_FEATURES => {
echo => { bromley => { max_per_day => 1, sample_data => 1 } },
waste => { bromley => 1 }
},
}, sub {
SKIP: {
skip( "No memcached", 2 ) unless Memcached::increment('bromley-test');
Memcached::delete("bromley-test");
$mech->get_ok('/waste/uprn/12345');
$mech->get('/waste/uprn/12345');
is $mech->res->code, 403, 'Now forbidden';
}
};
};
package SOAP::Result;
sub result { return $_[0]->{result}; }
sub new { my $c = shift; bless { @_ }, $c; }
package main;
subtest 'updating of waste reports' => sub {
my $integ = Test::MockModule->new('SOAP::Lite');
$integ->mock(call => sub {
my ($cls, @args) = @_;
my $method = $args[0]->name;
if ($method eq 'GetEvent') {
my ($key, $type, $value) = ${$args[3]->value}->value;
my $external_id = ${$value->value}->value->value;
my ($waste, $event_state_id, $resolution_code) = split /-/, $external_id;
return SOAP::Result->new(result => {
EventStateId => $event_state_id,
EventTypeId => '2104',
LastUpdatedDate => { OffsetMinutes => 60, DateTime => '2020-06-24T14:00:00Z' },
ResolutionCodeId => $resolution_code,
});
} elsif ($method eq 'GetEventType') {
return SOAP::Result->new(result => {
Workflow => { States => { State => [
{ CoreState => 'New', Name => 'New', Id => 15001 },
{ CoreState => 'Pending', Name => 'Unallocated', Id => 15002 },
{ CoreState => 'Pending', Name => 'Allocated to Crew', Id => 15003 },
{ CoreState => 'Closed', Name => 'Completed', Id => 15004,
ResolutionCodes => { StateResolutionCode => [
{ ResolutionCodeId => 201, Name => '' },
{ ResolutionCodeId => 202, Name => 'Spillage on Arrival' },
] } },
{ CoreState => 'Closed', Name => 'Not Completed', Id => 15005,
ResolutionCodes => { StateResolutionCode => [
{ ResolutionCodeId => 203, Name => 'Nothing Found' },
{ ResolutionCodeId => 204, Name => 'Too Heavy' },
{ ResolutionCodeId => 205, Name => 'Inclement Weather' },
] } },
{ CoreState => 'Closed', Name => 'Rejected', Id => 15006,
ResolutionCodes => { StateResolutionCode => [
{ ResolutionCodeId => 206, Name => 'Out of Time' },
{ ResolutionCodeId => 207, Name => 'Duplicate' },
] } },
] } },
});
} else {
is $method, 'UNKNOWN';
}
});
FixMyStreet::override_config {
ALLOWED_COBRANDS => 'bromley',
COBRAND_FEATURES => {
echo => { bromley => { url => 'https://www.example.org/' } },
waste => { bromley => 1 }
},
}, sub {
@reports = $mech->create_problems_for_body(2, $body->id, 'Report missed collection', {
category => 'Report missed collection',
cobrand_data => 'waste',
});
$reports[1]->update({ external_id => 'something-else' }); # To test loop
$report = $reports[0];
my $cobrand = FixMyStreet::Cobrand::Bromley->new;
$report->update({ external_id => 'waste-15001-' });
stdout_like {
$cobrand->waste_fetch_events(1);
} qr/Fetching data for report/;
$report->discard_changes;
is $report->comments->count, 0, 'No new update';
is $report->state, 'confirmed', 'No state change';
$report->update({ external_id => 'waste-15003-' });
stdout_like {
$cobrand->waste_fetch_events(1);
} qr/Updating report to state action scheduled, Allocated to Crew/;
$report->discard_changes;
is $report->comments->count, 1, 'A new update';
is $report->state, 'action scheduled', 'A state change';
$report->update({ external_id => 'waste-15003-' });
stdout_like {
$cobrand->waste_fetch_events(1);
} qr/Latest update matches fetched state/;
$report->discard_changes;
is $report->comments->count, 1, 'No new update';
is $report->state, 'action scheduled', 'State unchanged';
$report->update({ external_id => 'waste-15004-201' });
stdout_like {
$cobrand->waste_fetch_events(1);
} qr/Updating report to state fixed - council, Completed/;
$report->discard_changes;
is $report->comments->count, 2, 'A new update';
is $report->state, 'fixed - council', 'Changed to fixed';
$reports[1]->update({ state => 'fixed - council' });
stdout_like {
$cobrand->waste_fetch_events(1);
} qr/^$/, 'No open reports';
$report->update({ external_id => 'waste-15005-205', state => 'confirmed' });
stdout_like {
$cobrand->waste_fetch_events(1);
} qr/Updating report to state unable to fix, Inclement Weather/;
$report->discard_changes;
is $report->comments->count, 3, 'A new update';
is $report->state, 'unable to fix', 'A state change';
};
FixMyStreet::override_config {
ALLOWED_COBRANDS => 'bromley',
COBRAND_FEATURES => {
echo => { bromley => {
url => 'https://www.example.org/',
receive_action => 'action',
receive_username => 'un',
receive_password => 'password',
} },
waste => { bromley => 1 }
},
}, sub {
FixMyStreet::App->log->disable('info');
$mech->get('/waste/echo');
is $mech->res->code, 405, 'Cannot GET';
$mech->post('/waste/echo', Content_Type => 'text/xml');
is $mech->res->code, 400, 'No body';
my $in = '<Envelope><Header><Action>bad-action</Action></Header><Body></Body></Envelope>';
$mech->post('/waste/echo', Content_Type => 'text/xml', Content => $in);
is $mech->res->code, 400, 'Bad action';
$in = '<Envelope><Header><Action>action</Action><Security><UsernameToken><Username></Username><Password></Password></UsernameToken></Security></Header><Body></Body></Envelope>';
$mech->post('/waste/echo', Content_Type => 'text/xml', Content => $in);
is $mech->res->code, 400, 'Bad auth';
$in = <<EOF;
<?xml version="1.0" encoding="UTF-8"?>
<Envelope>
<Header>
<Action>action</Action>
<Security><UsernameToken><Username>un</Username><Password>password</Password></UsernameToken></Security>
</Header>
<Body>
<NotifyEventUpdated>
<event>
<Guid>waste-15005-XXX</Guid>
<EventTypeId>2104</EventTypeId>
<EventStateId>15006</EventStateId>
<ResolutionCodeId>207</ResolutionCodeId>
</event>
</NotifyEventUpdated>
</Body>
</Envelope>
EOF
$mech->post('/waste/echo', Content_Type => 'text/xml', Content => $in);
is $mech->res->code, 200, 'OK response, even though event does not exist';
is $report->comments->count, 3, 'No new update';
$in = <<EOF;
<?xml version="1.0" encoding="UTF-8"?>
<Envelope>
<Header>
<Action>action</Action>
<Security><UsernameToken><Username>un</Username><Password>password</Password></UsernameToken></Security>
</Header>
<Body>
<NotifyEventUpdated>
<event>
<Guid>waste-15005-205</Guid>
<EventTypeId>2104</EventTypeId>
<EventStateId>15006</EventStateId>
<ResolutionCodeId>207</ResolutionCodeId>
</event>
</NotifyEventUpdated>
</Body>
</Envelope>
EOF
$mech->post('/waste/echo', Content_Type => 'text/xml', Content => $in);
#$report->update({ external_id => 'waste-15005-205', state => 'confirmed' });
is $report->comments->count, 4, 'A new update';
$report->discard_changes;
is $report->state, 'closed', 'A state change';
FixMyStreet::App->log->enable('info');
};
};
done_testing();
|