aboutsummaryrefslogtreecommitdiffstats
path: root/t/app/controller/report_import.t
blob: c8cbcf412490e2f61e3c4441ca9d9febef604b2d (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
use strict;
use warnings;
use Test::More;
use LWP::Protocol::PSGI;

use t::Mock::MapIt;
use FixMyStreet::TestMech;
use FixMyStreet::App;
use Web::Scraper;
use Path::Class;

my $mech = FixMyStreet::TestMech->new;
$mech->get_ok('/import');

my $sample_file = file(__FILE__)->parent->file("sample.jpg")->stringify;
ok -e $sample_file, "sample file $sample_file exists";

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

my $body = $mech->create_body_ok(2245, 'Wiltshire Council');
$mech->create_contact_ok(
    body_id => $body->id,
    category => 'Street lighting',
    email => 'streetlighting@example.com',
);
$mech->create_contact_ok(
    body_id => $body->id,
    category => 'Potholes',
    email => 'highways@example.com',
);

# submit an empty report to import - check we get all errors
subtest "Test creating bad partial entries" => sub {

    foreach my $test (
        {
            fields => { email => 'bob', },
            errors => [
                'You must supply a service',
                'Please enter a subject',
                'Please enter your name',
                'Please enter a valid email',
                'Either a location or a photo must be provided.',
            ],
        },
        {
            fields => { email => 'bob@example.com' },
            errors => [
                'You must supply a service',
                'Please enter a subject',
                'Please enter your name',
                'Either a location or a photo must be provided.',
            ],
        },
        {
            fields => { lat => 1, lon => 1, },
            errors => [
                'You must supply a service',
                'Please enter a subject',
                'Please enter your name',
                'Please enter your email',
'We had a problem with the supplied co-ordinates - outside the UK?',
            ],
        },
        {
            fields => { photo => $sample_file, },
            errors => [
                'You must supply a service',
                'Please enter a subject',
                'Please enter your name',
                'Please enter your email',
            ],
        },
      )
    {
        $mech->get_ok('/import');

        FixMyStreet::override_config {
            ALLOWED_COBRANDS => [ { 'fixmystreet' => '.' } ],
        }, sub {
            $mech->submit_form_ok(    #
                { with_fields => $test->{fields} },
                "fill in form"
            );
        };

        is_deeply( $mech->import_errors, $test->{errors}, "expected errors" );
    }

};

subtest "Submit a correct entry" => sub {
    LWP::Protocol::PSGI->register(t::Mock::MapIt->run_if_script, host => 'mapit.uk');

    $mech->get_ok('/import');

    $mech->submit_form_ok(    #
        {
            with_fields => {
                service => 'test-script',
                name    => 'Test User',
                email   => 'Test@example.com',
                subject => 'Test report',
                detail  => 'This is a test report',
                photo   => $sample_file,
            }
        },
        "fill in form"
    );

    is_deeply( $mech->import_errors, [], "got no errors" );
    is $mech->content, 'SUCCESS', "Got success response";

    # check that we have received the email
    my $token_url = $mech->get_link_from_email;
    $mech->clear_emails_ok;
    ok $token_url, "Found a token url $token_url";

    # go to the token url
    FixMyStreet::override_config {
        MAPIT_URL => 'http://mapit.uk/',
    }, sub {
        $mech->get_ok($token_url);
    };

    # check that we are on '/around'
    is $mech->uri->path, '/around', "sent to /around";

    # check that we are not shown anything as we don't have a location yet
    is_deeply $mech->visible_form_values, { pc => '' },
      "check only pc field is shown";

    FixMyStreet::override_config {
        ALLOWED_COBRANDS => [ 'fixmystreet' ],
        MAPIT_URL => 'http://mapit.uk/',
    }, sub {
        $mech->submit_form_ok(
            { with_fields => { pc => 'SN15 5NG' } },
            "fill in postcode"
        );
    };

    is $mech->uri->path, '/report/new', "sent to report page";

    # check that fields are prefilled for us
    is_deeply $mech->visible_form_values,
      {
        name          => 'Test User',
        title         => 'Test report',
        detail        => 'This is a test report',
        photo1        => '',
        photo2        => '',
        photo3        => '',
        phone         => '',
        may_show_name => '1',
        category      => '-- Pick a category --',
        gender => undef,
      },
      "check imported fields are shown";

    # Check photo present, and still there after map submission (testing bug #18)
    $mech->content_contains( '<img align="right" src="/photo/' );
    $mech->content_contains('latitude" value="51.5"', 'Check latitude');
    $mech->content_contains('longitude" value="-2.1"', 'Check longitude');
    FixMyStreet::override_config {
        ALLOWED_COBRANDS => [ { 'fixmystreet' => '.' } ],
        MAPIT_URL => 'http://mapit.uk/',
    }, sub {
        $mech->submit_form_ok(
            {
                button => 'tile_16192.10896',
                x => 10,
                y => 10,
            },
            "New map location"
        );
    };
    $mech->content_contains( '<img align="right" src="/photo/' );
    $mech->content_contains('latitude" value="51.508475"', 'Check latitude');
    $mech->content_contains('longitude" value="-2.108946"', 'Check longitude');

    # check that fields haven't changed at all
    is_deeply $mech->visible_form_values,
      {
        name          => 'Test User',
        title         => 'Test report',
        detail        => 'This is a test report',
        photo1        => '',
        photo2        => '',
        photo3        => '',
        phone         => '',
        may_show_name => '1',
        category      => '-- Pick a category --',
        gender => undef,
      },
      "check imported fields are shown";

    # change the details
    FixMyStreet::override_config {
        ALLOWED_COBRANDS => [ { 'fixmystreet' => '.' } ],
        MAPIT_URL => 'http://mapit.uk/',
    }, sub {
        $mech->submit_form_ok(
            {
                with_fields => {
                    name          => 'New Test User',
                    title         => 'New Test report',
                    detail        => 'This is a test report',
                    phone         => '01234 567 890',
                    may_show_name => '1',
                    category      => 'Street lighting',
                }
            },
            "Update details and save"
        );
    };

    # check that report has been created
    my $user =
      FixMyStreet::App->model('DB::User')
      ->find( { email => 'test@example.com' } );
    ok $user, "Found a user";

    my $report = $user->problems->first;
    is $report->state, 'confirmed',       'is confirmed';
    is $report->title, 'New Test report', 'title is correct';

    $mech->delete_user($user);
};

subtest "Submit a correct entry (with location)" => sub {

    $mech->get_ok('/import');

    $mech->submit_form_ok(    #
        {
            with_fields => {
                service => 'test-script',
                lat     => '51.5',
                lon     => '-2.1',
                name    => 'Test User ll',
                email   => 'test-ll@example.com',
                subject => 'Test report ll',
                detail  => 'This is a test report ll',
                photo   => $sample_file,
            }
        },
        "fill in form"
    );

    is_deeply( $mech->import_errors, [], "got no errors" );
    is $mech->content, 'SUCCESS', "Got success response";

    # check that we have received the email
    my $token_url = $mech->get_link_from_email;
    $mech->clear_emails_ok;
    ok $token_url, "Found a token url $token_url";

    # go to the token url
    FixMyStreet::override_config {
        ALLOWED_COBRANDS => [ { 'fixmystreet' => '.' } ],
        MAPIT_URL => 'http://mapit.mysociety.org/',
    }, sub {
        $mech->get_ok($token_url);
    };

    # check that we are on '/report/new'
    is $mech->uri->path, '/report/new', "sent to /report/new";

    # check that fields are prefilled for us
    is_deeply $mech->visible_form_values,
      {
        name          => 'Test User ll',
        title         => 'Test report ll',
        detail        => 'This is a test report ll',
        photo1        => '',
        photo2        => '',
        photo3        => '',
        phone         => '',
        may_show_name => '1',
        category      => '-- Pick a category --',
        gender => undef,
      },
      "check imported fields are shown";

    # change the details
    FixMyStreet::override_config {
        ALLOWED_COBRANDS => [ { 'fixmystreet' => '.' } ],
        MAPIT_URL => 'http://mapit.mysociety.org/',
    }, sub {
        $mech->submit_form_ok(    #
            {
                with_fields => {
                    name          => 'New Test User ll',
                    title         => 'New Test report ll',
                    detail        => 'This is a test report ll',
                    phone         => '01234 567 890',
                    may_show_name => '1',
                    category      => 'Street lighting',
                }
            },
            "Update details and save"
        );
    };

    # check that report has been created
    my $user =
      FixMyStreet::App->model('DB::User')
      ->find( { email => 'test-ll@example.com' } );
    ok $user, "Found a user";

    my $report = $user->problems->first;
    is $report->state, 'confirmed',          'is confirmed';
    is $report->title, 'New Test report ll', 'title is correct';

    $mech->delete_user($user);
};

subtest "Submit a correct entry (with location) to cobrand" => sub {
  FixMyStreet::override_config {
    ALLOWED_COBRANDS => [ 'zurich' ],
    MAPIT_URL => 'http://global.mapit.mysociety.org/',
    MAPIT_TYPES => [ 'O08' ],
    MAPIT_ID_WHITELIST => [],
    MAP_TYPE => 'Zurich,OSM',
  }, sub {
    ok $mech->host("zurich.example.org"), 'change host to zurich';

    $mech->get_ok('/import');

    $mech->submit_form_ok(    #
        {
            with_fields => {
                service => 'test-script',
                lat     => '47.4',
                lon     => '8.5',
                name    => 'Test User ll',
                email   => 'test-ll@example.com',
                subject => 'Test report ll',
                detail  => 'This is a test report ll',
                photo   => $sample_file,
            }
        },
        "fill in form"
    );

    is_deeply( $mech->import_errors, [], "got no errors" );
    is $mech->content, 'SUCCESS', "Got success response";

    # check that we have received the email
    my $token_url = $mech->get_link_from_email;
    $mech->clear_emails_ok;
    ok $token_url, "Found a token url $token_url";

    # go to the token url
    $mech->get_ok($token_url);

    # check that we are on '/report/new'
    is $mech->uri->path, '/report/new', "sent to /report/new";

    # check that fields are prefilled for us
    is_deeply $mech->visible_form_values,
      {
        name          => 'Test User ll',
        detail        => 'This is a test report ll',
        photo1         => '',
        photo2         => '',
        photo3         => '',
        phone         => '',
        email => 'test-ll@example.com',
      },
      "check imported fields are shown"
          or diag Dumper( $mech->visible_form_values ); use Data::Dumper;

    my $user =
      FixMyStreet::App->model('DB::User')
      ->find( { email => 'test-ll@example.com' } );
    ok $user, "Found a user";

    my $report = $user->problems->first;
    is $report->state, 'partial',        'is still partial';
    is $report->title, 'Test report ll', 'title is correct';
    is $report->lang, 'de-ch',           'language is correct';

    $mech->delete_user($user);
  };
};

done_testing();

END {
    $mech->delete_body($body);
}