aboutsummaryrefslogtreecommitdiffstats
path: root/t/app/controller/photo.t
blob: 69c2ae8660f9d04ead711328f44657db5482854d (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
use strict;
use utf8; # sign in error message has – in it
use warnings;
use feature 'say';
use Test::More;
use utf8;

use FixMyStreet::TestMech;
use FixMyStreet::App;
use Web::Scraper;
use Path::Tiny;
use File::Temp 'tempdir';

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

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

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

my $westminster = $mech->create_body_ok(2527, 'Liverpool City Council');

subtest "Check multiple upload worked" => sub {
    $mech->get_ok('/around');

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

    # submit initial pc form
    FixMyStreet::override_config {
        ALLOWED_COBRANDS => [ { fixmystreet => '.' } ],
        MAPIT_URL => 'http://mapit.mysociety.org/',
        UPLOAD_DIR => $UPLOAD_DIR,
    }, sub {

        $mech->log_in_ok('test@example.com');


        # submit the main form
        # can't post_ok as we lose the Content_Type header
        # (TODO rewrite with HTTP::Request::Common and request_ok)
        $mech->get_ok('/report/new?lat=53.4031156&lon=-2.9840579');
        my ($csrf) = $mech->content =~ /name="token" value="([^"]*)"/;

        $mech->post( '/report/new',
            Content_Type => 'form-data',
            Content =>
            {
            submit_problem => 1,
            token => $csrf,
            title         => 'Test',
            lat => 53.4031156, lon => -2.9840579, # in Liverpool
            pc            => 'L1 4LN',
            detail        => 'Detail',
            photo1         => [ $sample_file, undef, Content_Type => 'application/octet-stream' ],
            photo2         => [ $sample_file, undef, Content_Type => 'application/octet-stream' ],
            photo3         => [ $sample_file, undef, Content_Type => 'application/octet-stream' ],
            name          => 'Bob Jones',
            may_show_name => '1',
            email         => 'test@example.com',
            phone         => '',
            category      => 'Street lighting',
            }
        );
        ok $mech->success, 'Made request with multiple photo upload';
        $mech->base_is('http://localhost/report/new');
        $mech->content_like(
            qr[(<img align="right" src="/photo/temp.74e3362283b6ef0c48686fb0e161da4043bbcc97.jpeg" alt="">\s*){3}],
            'Three uploaded pictures are all shown, safe');
        $mech->content_contains(
            'name="upload_fileid" value="74e3362283b6ef0c48686fb0e161da4043bbcc97.jpeg,74e3362283b6ef0c48686fb0e161da4043bbcc97.jpeg,74e3362283b6ef0c48686fb0e161da4043bbcc97.jpeg"',
            'Returned upload_fileid contains expected hash, 3 times');
        my $image_file = path($UPLOAD_DIR, '74e3362283b6ef0c48686fb0e161da4043bbcc97.jpeg');
        ok $image_file->exists, 'File uploaded to temp';
    };
};

done_testing();