aboutsummaryrefslogtreecommitdiffstats
path: root/t/app/controller/contact_enquiry.t
blob: af249ec6cc80ee6a8b7b8f0aabb385fc6075a033 (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
use utf8;
use Encode;
use FixMyStreet::TestMech;
use Path::Tiny;
use File::Temp 'tempdir';
use FixMyStreet::Script::Reports;
use Test::MockModule;

# 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( 2483, 'Hounslow Borough Council', {
    send_method => 'Open311',
    endpoint => 'endpoint',
    api_key => 'key',
    jurisdiction => 'hounslow',
});
my $contact = $mech->create_contact_ok(
    body_id => $body->id,
    category => 'General Enquiry',
    email => 'genenq@example.com',
    non_public => 1,
);
my $contact2 = $mech->create_contact_ok(
    body_id => $body->id,
    category => 'FOI Request',
    email => 'foi@example.com',
    non_public => 1,
);
my $contact3 = $mech->create_contact_ok(
    body_id => $body->id,
    category => 'Other',
    email => 'other@example.com',
);
my $contact4 = $mech->create_contact_ok(
    body_id => $body->id,
    category => 'Carriageway Defect',
    email => 'potholes@example.com',
);
$contact->update( { extra => { group => 'General Enquiries' } } );
$contact2->update( { extra => { group => 'General Enquiries' } } );
$contact3->update( { extra => { group => 'Other' } } );

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

FixMyStreet::override_config {
    ALLOWED_COBRANDS => ['hounslow'],
    MAPIT_URL => 'http://mapit.uk/',
}, sub {
    subtest 'Non-general enquiries category not shown' => sub {
        $mech->get_ok( '/contact/enquiry' );
        $mech->content_lacks('Carriageway Defect');
        $mech->content_contains('FOI Request');
    };

    subtest 'Enquiry can be submitted when logged out' => sub {
        my $problems = FixMyStreet::DB->resultset('Problem')->to_body( $body->id );

        $mech->get_ok( '/contact/enquiry' );
        $mech->submit_form_ok( {
            with_fields => {
                name => 'Test User',
                username => 'testuser@example.org',
                category => 'Other',
                detail => 'This is a general enquiry',
            }
        } );
        is $mech->uri->path, '/contact/enquiry/submit';
        $mech->content_contains("Thank you for your enquiry");

        is $problems->count, 1, 'problem created';
        my $problem = $problems->first;
        is $problem->category, 'Other', 'problem has correct category';
        is $problem->detail, 'This is a general enquiry', 'problem has correct detail';
        is $problem->non_public, 1, 'problem created non_public';
        is $problem->postcode, '';
        is $problem->used_map, 0;
        is $problem->latitude, 51.469, 'Problem has correct latitude';
        is $problem->longitude, -0.35, 'Problem has correct longitude';
        ok $problem->confirmed, 'problem confirmed';
        is $problem->user->name, undef, 'User created without name';
        is $problem->name, 'Test User', 'Report created with correct name';
        is $problem->user->email, 'testuser@example.org', 'User created with correct email';
    };

    subtest 'Enquiry can be submitted when logged in' => sub {
        my $problems = FixMyStreet::DB->resultset('Problem')->to_body( $body->id );
        my $prob_user = $problems->first->user;
        $problems->delete_all;

        my $user = $mech->log_in_ok( $prob_user->email );

        $mech->get_ok( '/contact/enquiry' );
        $mech->submit_form_ok( {
            with_fields => {
                name => 'Test User',
                category => 'FOI Request',
                detail => 'This is a general enquiry',
            }
        } );
        is $mech->uri->path, '/contact/enquiry/submit';
        $mech->content_contains("Thank you for your enquiry");

        is $problems->count, 1, 'problem created';
        my $problem = $problems->first;
        is $problem->category, 'FOI Request', 'problem has correct category';
        is $problem->detail, 'This is a general enquiry', 'problem has correct detail';
        is $problem->non_public, 1, 'problem created non_public';
        is $problem->postcode, '';
        is $problem->used_map, 0;
        is $problem->latitude, 51.469, 'Problem has correct latitude';
        is $problem->longitude, -0.35, 'Problem has correct longitude';
        ok $problem->confirmed, 'problem confirmed';
        is $problem->name, 'Test User', 'Report created with correct name';
        is $problem->user->name, 'Test User', 'User name updated in DB';
        is $problem->user->email, $user->email, 'Report user has correct email';

        $mech->log_out_ok;
    };

    subtest 'User name not changed if logged out when making report' => sub {
        my $problems = FixMyStreet::DB->resultset('Problem')->to_body( $body->id );
        my $user = $problems->first->user;
        $problems->delete_all;

        is $user->name, 'Test User', 'User has correct name';

        $mech->get_ok( '/contact/enquiry' );
        $mech->submit_form_ok( {
            with_fields => {
                name => 'Simon Neil',
                username => $user->email,
                category => 'General Enquiry',
                detail => 'This is a general enquiry',
            }
        } );
        is $mech->uri->path, '/contact/enquiry/submit';
        $mech->content_contains("Thank you for your enquiry");

        is $problems->count, 1, 'problem created';
        my $problem = $problems->first;
        is $problem->category, 'General Enquiry', 'problem has correct category';
        is $problem->detail, 'This is a general enquiry', 'problem has correct detail';
        is $problem->non_public, 1, 'problem created non_public';
        is $problem->postcode, '';
        is $problem->used_map, 0;
        is $problem->latitude, 51.469, 'Problem has correct latitude';
        is $problem->longitude, -0.35, 'Problem has correct longitude';
        ok $problem->confirmed, 'problem confirmed';
        is $problem->name, 'Simon Neil', 'Report created with correct name';
        is $problem->user->email, $user->email, 'Report user has correct email';
        $user->discard_changes;
        is $user->name, 'Test User', 'User name in DB not changed';

        $mech->log_out_ok;
    };

};

my $sample_jpeg = path(__FILE__)->parent->child("sample.jpg");
ok $sample_jpeg->exists, "sample image $sample_jpeg exists";
my $sample_pdf = path(__FILE__)->parent->child("sample.pdf");
ok $sample_pdf->exists, "sample PDF $sample_pdf exists";

my $UPLOAD_DIR = tempdir( CLEANUP => 1 );

FixMyStreet::override_config {
    ALLOWED_COBRANDS => [ 'hounslow' ],
    STAGING_FLAGS => { send_reports => 1 },
    MAPIT_URL => 'http://mapit.uk/',
    PHOTO_STORAGE_BACKEND => 'FileSystem',
    PHOTO_STORAGE_OPTIONS => {
        UPLOAD_DIR => $UPLOAD_DIR,
    },
}, sub {

    my $pdf_hash = '90f7a64043fb458d58de1a0703a6355e2856b15e.pdf';
    my $image_hash = '74e3362283b6ef0c48686fb0e161da4043bbcc97.jpeg';

    subtest "Check photo & file upload works" => sub {
        my $problems = FixMyStreet::DB->resultset('Problem')->to_body( $body->id );
        $problems->delete_all;


        $mech->get_ok('/contact/enquiry');
        my ($csrf) = $mech->content =~ /name="token" value="([^"]*)"/;

        $mech->post( '/contact/enquiry/submit',
            Content_Type => 'form-data',
            Content =>
            {
            submit_problem => 1,
            token => $csrf,
            name => 'Test User',
            username => 'testuser@example.org',
            category => 'Other',
            detail => encode_utf8('This is a general enquiry‽'),
            photo1         => [ $sample_jpeg, undef, Content_Type => 'image/jpeg' ],
            photo2         => [ $sample_pdf, undef, Content_Type => 'application/pdf' ],
            }
        );
        ok $mech->success, 'Made request with two files uploaded';

        is $problems->count, 1, 'problem created';
        my $problem = $problems->first;
        is $problem->detail, 'This is a general enquiry‽', 'problem has correct detail';
        is $problem->non_public, 1, 'problem created non_public';
        is $problem->postcode, '';
        is $problem->used_map, 0;
        is $problem->latitude, 51.469, 'Problem has correct latitude';
        is $problem->longitude, -0.35, 'Problem has correct longitude';
        ok $problem->confirmed, 'problem confirmed';

        my $image_file = path($UPLOAD_DIR, $image_hash);
        ok $image_file->exists, 'Photo uploaded to temp';

        my $photoset = $problem->get_photoset();
        is $photoset->num_images, 1, 'Found just 1 image';
        is $photoset->data, $image_hash;

        my $pdf_file = path($UPLOAD_DIR, 'enquiry_files', $pdf_hash);
        ok $pdf_file->exists, 'PDF uploaded to temp';

        is_deeply $problem->get_extra_metadata('enquiry_files'), {
            $pdf_hash => 'sample.pdf'
        }, 'enquiry file info stored OK';
    };

    subtest 'Check Open311 sending of the above report' => sub {
        my $module = Test::MockModule->new('FixMyStreet::Cobrand::UKCouncils');
        $module->mock(get => sub ($) { '{}' });
        my $test_data = FixMyStreet::Script::Reports::send();
        my $req = $test_data->{test_req_used};
        my $found = 0;
        foreach ($req->parts) {
            my $cd = $_->header('Content-Disposition');
            if ($cd =~ /attribute\[description\]/) {
                is decode_utf8($_->content), 'This is a general enquiry‽', 'Correct description';
                $found++;
            }
            if ($cd =~ /sample.pdf/) {
                is $cd, 'form-data; name="file_' . $pdf_hash . '"; filename="sample.pdf"', 'Correct PDF header';
                is $_->header('Content-Type'), 'application/pdf', 'Correct PDF content type';
                $found++;
            }
            if ($cd =~ /jpeg/) {
                is $cd, 'form-data; name="photo1"; filename="' . $image_hash . '"', 'Correct image header';
                is $_->header('Content-Type'), 'image/jpeg', 'Correct image content type';
                $found++;
            }
        }
        is $found, 3, 'Found all tested headers';
    };

};

done_testing();