aboutsummaryrefslogtreecommitdiffstats
path: root/t/app/controller/report_as_other.t
blob: 377de27bd219e17a7d9902608ef1058cd985d6be (plain)
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
use FixMyStreet::TestMech;
use FixMyStreet::Script::Reports;

# disable info logs for this test run
FixMyStreet::App->log->disable('info');
END { FixMyStreet::App->log->enable('info'); }

my $mech = FixMyStreet::TestMech->new;

my $body = $mech->create_body_ok(2237, 'Oxfordshire County Council');
my $contact1 = $mech->create_contact_ok( body_id => $body->id, category => 'Street lighting', email => 'highways@example.com' );
my $contact2 = $mech->create_contact_ok( body_id => $body->id, category => 'Potholes', email => 'potholes@example.com' );

my $test_email = 'body-user@example.net';
my $user = $mech->log_in_ok($test_email);
$user->update({ from_body => $body->id, name => 'Body User' });

my $superuser = $mech->create_user_ok('superuser@example.net', name => "Super", is_superuser => 1);

my ($report_to_update) = $mech->create_problems_for_body(1, $body->id, 'Title', { category => 'Potholes' });

subtest "Body user, no permissions, no special reporting tools shown" => sub {
    start_report();
    dropdown_shown(0);
    start_update();
    dropdown_shown(0, 'updateForm');
};

subtest "Body user, has permission to add report as council" => sub {
    my $report = add_report(
        'contribute_as_body',
        form_as => 'body',
        title => "Test Report",
        detail => 'Test report details.',
        category => 'Street lighting',
    );
    is $report->name, 'Oxfordshire County Council', 'report name is body';
    is $report->user->name, 'Body User', 'user name unchanged';
    is $report->user->id, $user->id, 'user matches';
    is $report->anonymous, 0, 'report not anonymous';
};

subtest "Body user, has permission to add report as another user with email" => sub {
    my $report = add_report(
        'contribute_as_another_user',
        form_as => 'another_user',
        title => "Test Report",
        detail => 'Test report details.',
        category => 'Potholes',
        name => 'Another User',
        username => 'another@example.net',
    );
    is $report->name, 'Another User', 'report name is given name';
    is $report->user->name, 'Another User', 'user name matches';
    is $report->user->email, 'another@example.net', 'user email correct';
    isnt $report->user->id, $user->id, 'user does not match';
    like $mech->get_text_body_from_email, qr/Your report to Oxfordshire County Council has been logged/;
};

subtest "Body user, has permission to add report as another user with mobile phone number" => sub {
    my $report = add_report(
        'contribute_as_another_user',
        form_as => 'another_user',
        title => "Test Report",
        detail => 'Test report details.',
        category => 'Potholes',
        name => 'Another User',
        username => '07906 111111',
    );
    is $report->name, 'Another User', 'report name is given name';
    is $report->user->name, 'Another User', 'user name matches';
    is $report->user->phone, '+447906111111', 'user phone correct';
    is $report->user->phone_verified, 1, 'user phone verified';
    is $report->user->email, undef, 'user email correct';
    is $report->user->email_verified, 0, 'user email not verified';
    isnt $report->user->id, $user->id, 'user does not match';
    $mech->email_count_is(0);
};

subtest "Body user, has permission to add report as another user with landline number" => sub {
    my $report = add_report(
        'contribute_as_another_user',
        form_as => 'another_user',
        title => "Test Report",
        detail => 'Test report details.',
        category => 'Potholes',
        name => 'Another User',
        username => '01685 222222',
    );
    is $report->name, 'Another User', 'report name is given name';
    is $report->user->name, 'Another User', 'user name matches';
    is $report->user->phone, '+441685222222', 'user phone correct';
    is $report->user->phone_verified, 1, 'user phone verified';
    is $report->user->email, undef, 'user email correct';
    is $report->user->email_verified, 0, 'user email not verified';
    isnt $report->user->id, $user->id, 'user does not match';
    $mech->email_count_is(0);
};

subtest "Body user, has permission to add report as another user with only name" => sub {
    my $report = add_report(
        'contribute_as_another_user',
        form_as => 'another_user',
        title => "Test Report",
        detail => 'Test report details.',
        category => 'Potholes',
        name => 'Another User',
        username => '',
        may_show_name => undef,
    );
    is $report->name, 'Another User', 'report name is name given';
    is $report->user->name, 'Body User', 'user name unchanged';
    is $report->user->id, $user->id, 'user matches';
    is $report->anonymous, 1, 'report anonymous';
};

subtest "Body user, has permission to add report as another (existing) user with email" => sub {
    FixMyStreet::Script::Reports::send();
    $mech->clear_emails_ok;

    my $existing = $mech->create_user_ok('existing@example.net', name => 'Existing User');
    my $report = add_report(
        'contribute_as_another_user',
        form_as => 'another_user',
        title => "Test Report",
        detail => 'Test report details.',
        category => 'Potholes',
        name => 'Existing Yooser',
        username => $existing->email,
    );
    is $report->name, 'Existing Yooser', 'report name is given name';
    is $report->user->name, 'Existing User', 'user name remains same';
    is $report->user->email, $existing->email, 'user email correct';
    isnt $report->user->id, $user->id, 'user does not match';
    like $mech->get_text_body_from_email, qr/Your report to Oxfordshire County Council has been logged/;

    my $send_confirmation_mail_override = Sub::Override->new(
        "FixMyStreet::Cobrand::Default::report_sent_confirmation_email",
        sub { return 'external_id'; }
    );
    FixMyStreet::Script::Reports::send();
    $mech->email_count_is(2);
    $mech->clear_emails_ok;
    $send_confirmation_mail_override->restore();
};

subtest "Body user, has permission to add report as another (existing) user with phone" => sub {
    $mech->create_user_ok('+447906333333', name => 'Existing User');
    my $report = add_report(
        'contribute_as_another_user',
        form_as => 'another_user',
        title => "Test Report",
        detail => 'Test report details.',
        category => 'Potholes',
        name => 'Existing Yooser',
        username => '07906 333333',
    );
    is $report->name, 'Existing Yooser', 'report name is given name';
    is $report->user->name, 'Existing User', 'user name remains same';
    is $report->user->phone, '+447906333333', 'user phone correct';
    isnt $report->user->id, $user->id, 'user does not match';
    $mech->email_count_is(0);
};

subtest "Superuser, can add report as anonymous user" => sub {
    FixMyStreet::Script::Reports::send();
    $mech->clear_emails_ok;

    my $user = $mech->log_in_ok($superuser->email);
    my $report = add_report(
        'contribute_as_anonymous_user',
        form_as => 'anonymous_user',
        title => "Test Report",
        detail => 'Test report details.',
        category => 'Street lighting',
    );
    is $report->name, 'an administrator', 'report name is admin';
    is $report->user->name, 'Super', 'user name unchanged';
    is $report->user->id, $user->id, 'user matches';
    is $report->anonymous, 1, 'report anonymous';
    is $report->get_extra_metadata('contributed_as'), 'anonymous_user';
    is $report->get_extra_metadata('contributed_by'), undef;

    my $send_confirmation_mail_override = Sub::Override->new(
        "FixMyStreet::Cobrand::Default::report_sent_confirmation_email",
        sub { return 'external_id'; }
    );

    FixMyStreet::Script::Reports::send();
    my $email = $mech->get_email;
    like $email->header('Subject'), qr/Problem Report: Test Report/, 'report email title correct';
    $mech->clear_emails_ok;
    $send_confirmation_mail_override->restore();

    $mech->log_in_ok($test_email);
};

subtest "Body user, can add report as anonymous user" => sub {
    FixMyStreet::Script::Reports::send();
    $mech->clear_emails_ok;

    my $user = $mech->log_in_ok($user->email);
    my $report = add_report(
        'contribute_as_anonymous_user',
        form_as => 'anonymous_user',
        title => "Test Report",
        detail => 'Test report details.',
        category => 'Street lighting',
    );
    is $report->name, $body->name, 'report name is OK';
    is $report->user->name, 'Body User', 'user name unchanged';
    is $report->user->id, $user->id, 'user matches';
    is $report->anonymous, 1, 'report anonymous';
    is $report->get_extra_metadata('contributed_as'), 'anonymous_user';
    is $report->get_extra_metadata('contributed_by'), $user->id;

    $mech->log_in_ok($test_email);
};

subtest "Body user, has permission to add update as council" => sub {
    my $update = add_update(
        'contribute_as_body',
        form_as => 'body',
        update => 'Test Update',
    );
    is $update->name, 'Oxfordshire County Council', 'update name is body';
    is $update->user->name, 'Body User', 'user name unchanged';
    is $update->user->id, $user->id, 'user matches';
    is $update->anonymous, 0, 'update not anonymous';
};

subtest "Body user, has permission to add update as another user with email" => sub {
    my $update = add_update(
        'contribute_as_another_user',
        form_as => 'another_user',
        update => 'Test Update',
        name => 'Another User',
        username => 'another2@example.net',
    );
    is $update->name, 'Another User', 'update name is given name';
    is $update->user->name, 'Another User', 'user name matches';
    is $update->user->email, 'another2@example.net', 'user email correct';
    isnt $update->user->id, $user->id, 'user does not match';
    like $mech->get_text_body_from_email, qr/Your update has been logged/;
};

subtest "Body user, has permission to add update as another user with mobile phone" => sub {
    my $update = add_update(
        'contribute_as_another_user',
        form_as => 'another_user',
        update => 'Test Update',
        name => 'Another User',
        username => '07906 444444',
    );
    is $update->name, 'Another User', 'update name is given name';
    is $update->user->name, 'Another User', 'user name matches';
    is $update->user->phone, '+447906444444', 'user phone correct';
    isnt $update->user->id, $user->id, 'user does not match';
    $mech->email_count_is(0);
};

subtest "Body user, has permission to add update as another user with landline phone" => sub {
    my $update = add_update(
        'contribute_as_another_user',
        form_as => 'another_user',
        update => 'Test Update',
        name => 'Another User',
        username => '01685 555555',
    );
    is $update->name, 'Another User', 'update name is given name';
    is $update->user->name, 'Another User', 'user name matches';
    is $update->user->phone, '+441685555555', 'user phone correct';
    isnt $update->user->id, $user->id, 'user does not match';
    $mech->email_count_is(0);
};

subtest "Body user, has permission to add update as another (existing) user with email" => sub {
    my $existing = $mech->create_user_ok('existing@example.net', name => 'Existing User');
    my $update = add_update(
        'contribute_as_another_user',
        form_as => 'another_user',
        update => 'Test Update',
        name => 'Existing Yooser',
        username => $existing->email,
    );
    is $update->name, 'Existing Yooser', 'update name is given name';
    is $update->user->name, 'Existing User', 'user name remains same';
    is $update->user->email, $existing->email, 'user email correct';
    isnt $update->user->id, $user->id, 'user does not match';
    like $mech->get_text_body_from_email, qr/Your update has been logged/;
};

subtest "Body user, has permission to add update as another (existing) user with phone" => sub {
    my $update = add_update(
        'contribute_as_another_user',
        form_as => 'another_user',
        update => 'Test Update',
        name => 'Existing Yooser',
        username => '07906 333333',
    );
    is $update->name, 'Existing Yooser', 'update name is given name';
    is $update->user->name, 'Existing User', 'user name remains same';
    is $update->user->phone, '+447906333333', 'user phone correct';
    isnt $update->user->id, $user->id, 'user does not match';
    $mech->email_count_is(0);
};

subtest "Body user, has permission to add update as anonymous user" => sub {
    my $update = add_update(
        'contribute_as_anonymous_user',
        form_as => 'anonymous_user',
        update => 'Test Update',
    );
    is $update->name, 'Oxfordshire County Council', 'update name is body';
    is $update->user->name, 'Body User', 'user name unchanged';
    is $update->user->id, $user->id, 'user matches';
    is $update->anonymous, 1, 'update anonymous';
};

for my $test_permission ( qw/planned_reports default_to_body/ ) {
    subtest "$test_permission user defaults to reporting as body" => sub {
        $_->delete for $user->user_body_permissions;
        for my $permission ( 'contribute_as_another_user', 'contribute_as_anonymous_user', 'contribute_as_body', $test_permission ) {
            $user->user_body_permissions->create({ body => $body, permission_type => $permission })
        }
        FixMyStreet::override_config {
            ALLOWED_COBRANDS => [ 'fixmystreet' ],
            MAPIT_URL => 'http://mapit.uk/',
            PHONE_COUNTRY => 'GB',
        }, sub {
            $mech->get_ok('/report/new?latitude=51.7549262252&longitude=-1.25617899435');
        };

        is $mech->visible_form_values()->{form_as}, 'body', 'report as body is default';
    };
}

done_testing();

sub start_report {
    my $permission = shift;
    $_->delete for $user->user_body_permissions;
    $user->user_body_permissions->create({ body => $body, permission_type => $permission })
        if $permission;
    FixMyStreet::override_config {
        ALLOWED_COBRANDS => [ 'fixmystreet' ],
        MAPIT_URL => 'http://mapit.uk/',
        PHONE_COUNTRY => 'GB',
    }, sub {
        $mech->get_ok('/report/new?latitude=51.7549262252&longitude=-1.25617899435');
    };
}

sub add_report {
    my ($permission, %fields) = @_;
    start_report($permission);
    FixMyStreet::override_config {
        ALLOWED_COBRANDS => [ 'fixmystreet' ],
        MAPIT_URL => 'http://mapit.uk/',
        PHONE_COUNTRY => 'GB',
    }, sub {
        dropdown_shown(1);
        $mech->submit_form_ok({
            with_fields => \%fields,
        }, "submit details");
    };
    # Anonymous test done as superuser, which redirects
    $mech->content_contains('Thank you for reporting this issue')
        unless $permission eq 'contribute_as_anonymous_user';
    my $report = FixMyStreet::DB->resultset("Problem")->search(undef, { order_by => { -desc => 'id' } })->first;
    ok $report, "Found the report";
    is $report->state, 'confirmed', "report is now confirmed";
    return $report;
}

sub start_update {
    my $permission = shift;
    $_->delete for $user->user_body_permissions;
    $user->user_body_permissions->create({ body => $body, permission_type => $permission })
        if $permission;
    FixMyStreet::override_config {
        ALLOWED_COBRANDS => [ 'fixmystreet' ],
        MAPIT_URL => 'http://mapit.uk/',
        PHONE_COUNTRY => 'GB',
    }, sub {
        $mech->get_ok('/report/' . $report_to_update->id);
    };
}

sub add_update {
    my ($permission, %fields) = @_;
    start_update($permission);
    FixMyStreet::override_config {
        ALLOWED_COBRANDS => [ 'fixmystreet' ],
        MAPIT_URL => 'http://mapit.uk/',
        PHONE_COUNTRY => 'GB',
    }, sub {
        dropdown_shown(1, 'updateForm');
        $mech->submit_form_ok({
            with_fields => \%fields,
        }, "submit details");
    };
    $mech->content_contains('Thank you for updating this issue');
    my $update = FixMyStreet::DB->resultset("Comment")->search(undef, { order_by => { -desc => 'id' } })->first;
    ok $update, "Found the update";
    is $update->state, 'confirmed', "update is now confirmed";
    return $update;
}

sub dropdown_shown {
    my ($shown, $name) = @_;
    is grep({ $_ eq 'form_as' } keys %{$mech->visible_form_values($name)}), $shown, "Dropdown shown = $shown";
}