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

use FixMyStreet::TestMech;
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";

# 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');

        $mech->submit_form_ok(    #
            { with_fields => $test->{fields} },
            "fill in form"
        );

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

};

# submit an empty report to import - check we get all errors
subtest "Submit a correct entry" => sub {

    $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
    $mech->email_count_is(1);
    my $email = $mech->get_email;
    $mech->clear_emails_ok;

    my ($token_url) = $email->body =~ m{(http://\S+)};
    ok $token_url, "Found a token url $token_url";

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

    # 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";

    $mech->submit_form_ok(    #
        { with_fields => { pc => 'SW1A 1AA' } },
        "fill in postcode"
    );

    # check that we are not shown anything as we don't have a location yet
    is_deeply $mech->visible_form_values,
      {
        name          => 'Test User',
        email         => 'test@example.com',
        title         => 'Test report',
        detail        => 'This is a test report',
        photo         => '',
        phone         => '',
        may_show_name => '1',
      },
      "check imported fields are shown";

  TODO: {
        local $TODO = "'/report/123' urls not srved by catalyst yet";

        # change the details
        $mech->submit_form_ok(    #
            {
                with_fields => {
                    name          => 'New Test User',
                    email         => 'test@example.com',
                    title         => 'New Test report',
                    detail        => 'This is a test report',
                    phone         => '01234 567 890',
                    may_show_name => '1',
                }
            },
            "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);
};

done_testing();