aboutsummaryrefslogtreecommitdiffstats
path: root/t/app/controller/contact.t
blob: d6e56e7cc490f4da01b5135a56093c995b90a59c (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
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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
package FixMyStreet::Cobrand::AbuseOnly;

use base 'FixMyStreet::Cobrand::Default';

sub abuse_reports_only { 1; }

1;

package FixMyStreet::Cobrand::GeneralEnquiries;

use base 'FixMyStreet::Cobrand::Default';

sub abuse_reports_only { 1 }

sub setup_general_enquiries_stash { 1 }

1;

package main;

use FixMyStreet::TestMech;

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

$mech->get_ok('/contact');
my ($csrf) = $mech->content =~ /meta content="([^"]*)" name="csrf-token"/;
$mech->title_like(qr/Contact Us/);
$mech->content_contains("It's often quickest to ");

my $problem_main;

for my $test (
    {
        name      => 'A User',
        email     => 'problem_report_test@example.com',
        title     => 'Some problem or other',
        detail    => 'More detail on the problem',
        postcode  => 'EH99 1SP',
        confirmed => '2011-05-04 10:44:28.145168',
        anonymous => 0,
        meta      => 'Reported by A User at 10:44, Wed  4 May 2011',
        main      => 1,
    },
    {
        name      => 'A User',
        email     => 'problem_report_test@example.com',
        title     => 'A different problem',
        detail    => 'More detail on the different problem',
        postcode  => 'EH99 1SP',
        confirmed => '2011-05-03 13:24:28.145168',
        anonymous => 1,
        meta      => 'Reported anonymously at 13:24, Tue  3 May 2011',
    },
    {
        name      => 'A User',
        email     => 'problem_report_test@example.com',
        title     => 'A different problem',
        detail    => 'More detail on the different problem',
        postcode  => 'EH99 1SP',
        confirmed => '2011-05-03 13:24:28.145168',
        anonymous => 0,
        hidden    => 1,
        meta      => 'Reported anonymously at 13:24, Tue  3 May 2011',
    },
    {
        name      => 'A User',
        email     => 'problem_report_test@example.com',
        title     => 'A different problem',
        detail    => 'More detail on the different problem',
        postcode  => 'EH99 1SP',
        confirmed => '2011-05-03 13:24:28.145168',
        anonymous => 1,
        meta      => 'Reported anonymously at 13:24, Tue  3 May 2011',
        update    => {
            name  => 'Different User',
            email => 'commenter@example.com',
            text  => 'This is an update',
        },
    },
    {
        name      => 'A User',
        email     => 'problem_report_test@example.com',
        title     => 'A different problem',
        detail    => 'More detail on the different problem',
        postcode  => 'EH99 1SP',
        confirmed => '2011-05-03 13:24:28.145168',
        anonymous => 1,
        meta      => 'Reported anonymously at 13:24, Tue  3 May 2011',
        update    => {
            other_problem => 1,
            name  => 'Different User',
            email => 'commenter@example.com',
            text  => 'This is an update',
        },
    },
    {
        name      => 'A User',
        email     => 'problem_report_test@example.com',
        title     => 'A different problem',
        detail    => 'More detail on the different problem',
        postcode  => 'EH99 1SP',
        confirmed => '2011-05-03 13:24:28.145168',
        anonymous => 1,
        meta      => 'Reported anonymously at 13:24, Tue  3 May 2011',
        update    => {
            hidden => 1,
            name  => 'Different User',
            email => 'commenter@example.com',
            text  => 'This is an update',
        },
    },
  )
{
    subtest 'check reporting a problem displays correctly' => sub {
        my $user = $mech->create_user_ok($test->{email}, name => $test->{name});

        my $problem = FixMyStreet::DB->resultset('Problem')->create(
            {
                title     => $test->{title},
                detail    => $test->{detail},
                postcode  => $test->{postcode},
                confirmed => $test->{confirmed},
                name      => $test->{name},
                anonymous => $test->{anonymous},
                state     => $test->{hidden} ? 'hidden' : 'confirmed',
                user      => $user,
                latitude  => 0,
                longitude => 0,
                areas     => 0,
                used_map  => 0,
            }
        );

        my $update;

        if ( $test->{update} ) {
            my $update_info = $test->{update};
            my $update_user = $mech->create_user_ok($update_info->{email},
                name => $update_info->{name});

            $update = FixMyStreet::DB->resultset('Comment')->create(
                {
                    problem_id => $update_info->{other_problem} ? $problem_main->id : $problem->id,
                    user        => $update_user,
                    state       => $update_info->{hidden} ? 'hidden' : 'confirmed',
                    text        => $update_info->{text},
                    confirmed   => \'current_timestamp',
                    mark_fixed => 'f',
                    anonymous  => 'f',
                }
            );
        }

        ok $problem, 'succesfully create a problem';

        if ( $update ) {
            if ( $test->{update}->{hidden} ) {
                $mech->get( '/contact?id=' . $problem->id . '&update_id=' . $update->id );
                is $mech->res->code, 404, 'cannot report a hidden update';
            } elsif ( $test->{update}->{other_problem} ) {
                $mech->get( '/contact?id=' . $problem->id . '&update_id=' . $update->id );
                is $mech->res->code, 404, 'cannot view an update for another problem';
            } else {
                $mech->get_ok( '/contact?id=' . $problem->id . '&update_id=' . $update->id );
                $mech->content_contains('reporting the following update');
                $mech->content_contains( $test->{update}->{text} );
            }
        } elsif ( $test->{hidden} ) {
            $mech->get( '/contact?id=' . $problem->id );
            is $mech->res->code, 410, 'cannot report a hidden problem';
        } else {
            $mech->get_ok( '/contact?id=' . $problem->id );
            $mech->content_contains('reporting the following problem');
            $mech->content_contains( $test->{title} );
            $mech->content_contains( $test->{meta} );
        }

        $update->delete if $update;
        if ($test->{main}) {
            $problem_main = $problem;
        } else {
            $problem->delete;
        }
    };
}

for my $test (
    {
        fields => {
            em      => ' ',
            name    => '',
            subject => '',
            message => '',
        },
        page_errors =>
          [ 'There were problems with your report. Please see below.',
            'Please enter your name',
            'Please enter your email',
            'Please enter a subject',
            'Please write a message',
        ]
    },
    {
        fields => {
            em      => 'invalidemail',
            name    => '',
            subject => '',
            message => '',
        },
        page_errors =>
          [ 'There were problems with your report. Please see below.',
            'Please enter your name',
            'Please enter a valid email address',
            'Please enter a subject',
            'Please write a message',
        ]
    },
    {
        fields => {
            em      => 'test@example.com',
            name    => 'A name',
            subject => '',
            message => '',
        },
        page_errors => [
            'There were problems with your report. Please see below.',
            'Please enter a subject', 'Please write a message',
        ]
    },
    {
        fields => {
            em      => 'test@example.com',
            name    => 'A name',
            subject => 'A subject',
            message => '',
        },
        page_errors => [
            'There were problems with your report. Please see below.',
            'Please write a message',
        ]
    },
    {
        fields => {
            em      => 'test@example.com',
            name    => 'A name',
            subject => '  ',
            message => '',
        },
        page_errors => [
            'There were problems with your report. Please see below.',
            'Please enter a subject',
            'Please write a message',
        ]
    },
    {
        fields => {
            em      => 'test@example.com',
            name    => 'A name',
            subject => 'A subject',
            message => ' ',
        },
        page_errors => [
            'There were problems with your report. Please see below.',
            'Please write a message',
        ]
    },
    {
        url    => '/contact?id=' . $problem_main->id,
        fields => {
            em      => 'test@example.com',
            name    => 'A name',
            subject => 'A subject',
            message => 'A message',
            id      => 'invalid',
        },
        page_errors  => [ 'Illegal ID' ],
    },
  )
{
    subtest 'check submit page error handling' => sub {
        $mech->get_ok( $test->{url} ? $test->{url} : '/contact' );
        $mech->submit_form_ok( { with_fields => $test->{fields} } );
        is_deeply $mech->page_errors, $test->{page_errors}, 'page errors';

        # we santise this when we submit so need to remove it
        delete $test->{fields}->{id}
          if $test->{fields}->{id} and $test->{fields}->{id} eq 'invalid';
        $test->{fields}->{'extra.phone'} = '';
        is_deeply $mech->visible_form_values, $test->{fields}, 'form values';
    };
}

my %common = (
    em => 'test@example.com',
    name => 'A name',
    subject => 'A subject',
    message => 'A message',
);

for my $test (
    { fields => \%common },
    { fields => { %common, id => $problem_main->id } },
  )
{
    subtest 'check email sent correctly' => sub {
        $problem_main->discard_changes;
        ok !$problem_main->flagged, 'problem not flagged';

        $mech->clear_emails_ok;
        if ($test->{fields}{id}) {
            $mech->get_ok('/contact?id=' . $test->{fields}{id});
        } else {
            $mech->get_ok('/contact');
        }
        $mech->submit_form_ok( { with_fields => $test->{fields} } );
        $mech->content_contains('Thank you for your enquiry');

        my $email = $mech->get_email;

        is $email->header('Subject'), 'FixMyStreet message: ' .  $test->{fields}->{subject}, 'subject';
        is $email->header('From'), "\"$test->{fields}->{name}\" <$test->{fields}->{em}>", 'from';
        my $body = $mech->get_text_body_from_email($email);
        like $body, qr/$test->{fields}->{message}/, 'body';
        like $body, qr/Sent by contact form on \S+.\s+IP address (?:\d{1,3}\.){3,}\d{1,3}, user agent ./, 'body footer';
        my $problem_id = $test->{fields}{id};
        like $body, qr/Complaint about report $problem_id/, 'reporting a report'
            if $test->{fields}{id};

        $problem_main->discard_changes;
        if ( $problem_id ) {
            ok $problem_main->flagged, 'problem flagged';
        } else {
            ok !$problem_main->flagged, 'problem not flagged';
        }

    };
}

for my $test (
    { fields => \%common }
  )
{
    subtest 'check email contains user details' => sub {
        my $user = $mech->create_user_ok(
            $test->{fields}->{em},
            name => $test->{fields}->{name}
        );

        my $older_unhidden_problem = FixMyStreet::DB->resultset('Problem')->create(
            {
                title     => 'Some problem or other',
                detail    => 'More detail on the problem',
                postcode  => 'EH99 1SP',
                confirmed => '2011-05-04 10:44:28.145168',
                anonymous => 0,
                name      => $test->{fields}->{name},
                state     => 'confirmed',
                user      => $user,
                latitude  => 0,
                longitude => 0,
                areas     => 0,
                used_map  => 0,
            }
        );

        my $newer_hidden_problem = FixMyStreet::DB->resultset('Problem')->create(
            {
                title     => 'A hidden problem',
                detail    => 'Shhhh secret',
                postcode  => 'EH99 1SP',
                confirmed => '2012-06-05 10:44:28.145168',
                anonymous => 0,
                name      => $test->{fields}->{name},
                state     => 'hidden',
                user      => $user,
                latitude  => 0,
                longitude => 0,
                areas     => 0,
                used_map  => 0,
            }
        );

        $mech->clear_emails_ok;
        $mech->get_ok('/contact');
        $test->{fields}{em} = $user->email;
        $mech->submit_form_ok( { with_fields => $test->{fields} } );

        my $email = $mech->get_email;
        my $body = $mech->get_text_body_from_email($email);

        my $user_id = $user->id;
        my $user_email = $user->email;
        my $older_unhidden_problem_id = $older_unhidden_problem->id;
        my $newer_hidden_problem_id = $newer_hidden_problem->id;

        like $body, qr/admin\/users\/$user_id/, 'email contains admin link to edit user';
        like $body, qr/admin\/reports\?search=$user_email/, 'email contains admin link to show users reports';
        like $body, qr/admin\/report_edit\/$older_unhidden_problem_id/, 'email contains admin link for users latest unhidden report';
        unlike $body, qr/admin\/report_edit\/$newer_hidden_problem_id/, 'email does not link to hidden reports';

    };
}

for my $test (
    {
        fields => { %common, dest => undef },
        page_errors =>
          [ 'There were problems with your report. Please see below.',
            'Please enter who your message is for',
            'You can only contact the team behind FixMyStreet using our contact form', # The JS-hidden one
        ]
    },
    {
        fields => { %common, dest => 'council' },
        page_errors =>
          [ 'There were problems with your report. Please see below.',
            'You can only contact the team behind FixMyStreet using our contact form',
        ]
    },
    {
        fields => { %common, dest => 'update' },
        page_errors =>
          [ 'There were problems with your report. Please see below.',
            'You can only contact the team behind FixMyStreet using our contact form',
        ]
    },
  )
{
    subtest 'check submit page incorrect destination handling' => sub {
        FixMyStreet::override_config {
            ALLOWED_COBRANDS => [ 'fixmystreet' ],
        }, sub {
            $mech->host('www.fixmystreet.com');
            $mech->get_ok( $test->{url} ? $test->{url} : '/contact' );
            $mech->submit_form_ok( { with_fields => $test->{fields} } );
            is_deeply $mech->page_errors, $test->{page_errors}, 'page errors';

            # we santise this when we submit so need to remove it
            delete $test->{fields}->{id}
              if $test->{fields}->{id} and $test->{fields}->{id} eq 'invalid';
            $test->{fields}->{'extra.phone'} = '';
            is_deeply $mech->visible_form_values, $test->{fields}, 'form values';

            # Ugh, but checking div not hidden; text always shown and hidden with CSS
            if ( $test->{fields}->{dest} and $test->{fields}->{dest} eq 'update' ) {
                $mech->content_contains('<div class="form-error__box form-error--update">');
            } elsif ( $test->{fields}->{dest} and $test->{fields}->{dest} eq 'council' ) {
                # Ugh, but checking div not hidden
                $mech->content_contains('<div class="form-error__box form-error--council">');
            }
        }
    };
}

for my $test (
    { fields => { %common, dest => 'help' } },
    { fields => { %common, dest => 'feedback' } },
    { fields => { %common, dest => 'from_council' } },
  )
{
    subtest 'check email sent correctly with dest field set to us' => sub {
        FixMyStreet::override_config {
            ALLOWED_COBRANDS => [ 'fixmystreet' ],
        }, sub {
            $mech->clear_emails_ok;
            $mech->get_ok('/contact');
            $mech->submit_form_ok( { with_fields => $test->{fields} } );
            $mech->content_contains('Thank you for your enquiry');
            $mech->email_count_is(1);
        }
    };
}

for my $test (
    {
        fields => { %common, dest => undef },
        page_errors =>
          [ 'There were problems with your report. Please see below.',
            'Please enter a topic of your message',
            'You can only use this form to report inappropriate content', # The JS-hidden one
        ]
    },
    {
        fields => { %common, dest => 'council' },
        page_errors =>
          [ 'There were problems with your report. Please see below.',
            'You can only use this form to report inappropriate content',
        ]
    },
    {
        fields => { %common, dest => 'update' },
        page_errors =>
          [ 'There were problems with your report. Please see below.',
            'You can only use this form to report inappropriate content',
        ]
    },
  )
{
    subtest 'check Bucks submit page incorrect destination handling' => sub {
        FixMyStreet::override_config {
            ALLOWED_COBRANDS => [ 'buckinghamshire' ],
        }, sub {
            $mech->get_ok( '/contact?id=' . $problem_main->id, 'can visit for abuse report' );
            $mech->submit_form_ok( { with_fields => $test->{fields} } );
            is_deeply $mech->page_errors, $test->{page_errors}, 'page errors';

            $test->{fields}->{'extra.phone'} = '';
            is_deeply $mech->visible_form_values, $test->{fields}, 'form values';

            if ( $test->{fields}->{dest} and $test->{fields}->{dest} eq 'update' ) {
                $mech->content_contains('please leave an update');
            } elsif ( $test->{fields}->{dest} and $test->{fields}->{dest} eq 'council' ) {
                $mech->content_contains('should find other contact details');
            }
        }
    };
}

for my $test (
    {
        fields => {
            %common,
            token => $csrf,
            dest        => 'from_council',
            success_url => '/faq',
            s => "",
        },
        url_should_be => 'http://localhost/faq',
    },
    {
        fields => {
            %common,
            token => $csrf,
            dest        => 'from_council',
            success_url => 'http://www.example.com',
            s => "",
        },
        url_should_be => 'http://www.example.com',
    },
  )
{
    subtest 'check user can be redirected to a custom URL after contact form is submitted' => sub {
        FixMyStreet::override_config {
            ALLOWED_COBRANDS => [ 'fixmystreet' ],
        }, sub {
            $mech->post('/contact/submit', $test->{fields});
            is $mech->uri->as_string, $test->{url_should_be};
        }
    };
}

subtest 'check can limit contact to abuse reports' => sub {
    FixMyStreet::override_config {
        'ALLOWED_COBRANDS' => [ 'abuseonly' ],
    }, sub {
        $mech->get( '/contact' );
        is $mech->res->code, 404, 'cannot visit contact page';
        $mech->get_ok( '/contact?id=' . $problem_main->id, 'can visit for abuse report' );

        my $token = FixMyStreet::DB->resultset("Token")->create({
            scope => 'moderation',
            data => { id => $problem_main->id }
        });

        $mech->get_ok( '/contact?m=' . $token->token, 'can visit for moderation complaint' );

    }
};

subtest 'check redirected to correct form for general enquiries cobrand' => sub {
    FixMyStreet::override_config {
        'ALLOWED_COBRANDS' => [ 'generalenquiries' ],
    }, sub {
        $mech->get( '/contact' );
        is $mech->res->code, 200, "got 200 for final destination";
        is $mech->res->previous->code, 302, "got 302 for redirect";
        is $mech->uri->path, '/contact/enquiry';

        $mech->get_ok( '/contact?id=' . $problem_main->id, 'can visit for abuse report' );

        my $token = FixMyStreet::DB->resultset("Token")->create({
            scope => 'moderation',
            data => { id => $problem_main->id }
        });

        $mech->get_ok( '/contact?m=' . $token->token, 'can visit for moderation complaint' );

    }
};

$problem_main->delete;

done_testing();