aboutsummaryrefslogtreecommitdiffstats
path: root/t/app/controller/contact.t
blob: fbf794b6b0c230f0039e895ab1a8a8eca9852d54 (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
use strict;
use warnings;
use Test::More;

use FixMyStreet::TestMech;

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

$mech->get_ok('/contact');
$mech->title_like(qr/Contact Us/);
$mech->content_contains("We'd love to hear what you think about this site");

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, Wednesday  4 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, Tuesday  3 May 2011',
    },
  )
{
    subtest 'check reporting a problem displays correctly' => sub {
        my $user = FixMyStreet::App->model('DB::User')->find_or_create(
            {
                name  => $test->{name},
                email => $test->{email}
            }
        );

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

        ok $problem, 'succesfully create a problem';

        $mech->get_ok( '/contact?id=' . $problem->id );
        $mech->content_contains('reporting the following problem');
        $mech->content_contains( $test->{title} );
        $mech->content_contains( $test->{meta} );

        $problem->delete;
    };
}

for my $test (
    {
        fields => {
            em      => ' ',
            name    => '',
            subject => '',
            message => '',
        },
        page_errors =>
          [ 'There were problems with your report. Please see below.', ],
        field_errors => [
            'Please give your name',
            'Please give your email',
            'Please give a subject',
            'Please write a message',
        ]
    },
    {
        fields => {
            em      => 'invalidemail',
            name    => '',
            subject => '',
            message => '',
        },
        page_errors =>
          [ 'There were problems with your report. Please see below.', ],
        field_errors => [
            'Please give your name',
            'Please give a valid email address',
            'Please give 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.', ],
        field_errors => [ 'Please give 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.', ],
        field_errors => [ '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.', ],
        field_errors => [ 'Please give 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.', ],
        field_errors => [ 'Please write a message', ]
    },
    {
        url    => '/contact?id=1',
        fields => {
            em      => 'test@example.com',
            name    => 'A name',
            subject => 'A subject',
            message => 'A message',
            id      => 'invalid',
        },
        page_errors  => [ 'Illegal ID' ],
        field_errors => []
    },
  )
{
    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';
        is_deeply $mech->form_errors, $test->{field_errors}, 'field_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';
        is_deeply $mech->visible_form_values, $test->{fields}, 'form values';
    };
}

for my $test (
    {
        fields => {
            em      => 'test@example.com',
            name    => 'A name',
            subject => 'A subject',
            message => 'A message',
        },
    },

  )
{
    subtest 'check email sent correctly' => sub {
        $mech->clear_emails_ok;
        $mech->get_ok('/contact');
        $mech->submit_form_ok( { with_fields => $test->{fields} } );
        $mech->content_contains('Thanks for your feedback');
        $mech->email_count_is(1);

        my $email = $mech->get_email;

        is $email->header('Subject'), 'FMS message: ' .  $test->{fields}->{subject}, 'subject';
        is $email->header('From'), "\"$test->{fields}->{name}\" <$test->{fields}->{em}>", 'from';
        like $email->body, qr/$test->{fields}->{message}/, 'body';
        like $email->body, qr/Sent by contact.cgi on \S+. IP address (?:\d{1,3}\.){3,}\d{1,3}/, 'body footer'
    };
}
done_testing();