aboutsummaryrefslogtreecommitdiffstats
path: root/t/app/controller/report_new_open311.t
blob: 7d4f5429fe3df1660bcb13d6627c4642670c2b14 (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
use FixMyStreet::TestMech;
use FixMyStreet::App;
use Test::LongString;
use Web::Scraper;

# 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(2245, 'Wiltshire Council');
$body->update({
    endpoint => 'http://example.com/open311',
    jurisdiction => 'mySociety',
    api_key => 'apikey',
});

# Let's make some contacts to send things to!
my $contact1 = $mech->create_contact_ok(
    body_id => $body->id, # Edinburgh
    category => 'Street lighting',
    email => '100',
    extra => [ { description => 'Lamppost number', code => 'number', required => 'True' },
               { description => 'Lamppost type', code => 'type', required => 'False', values =>
                   { value => [ { name => ['Gas'], key => ['old'] }, { name => [ 'Yellow' ], key => [ 'modern' ] } ] }
               }
             ],
);
my $contact1b = $mech->create_contact_ok(
    body_id => $body->id, # Edinburgh
    category => 'Moon lighting',
    email => '100b',
    extra => [ { description => 'Moon type', code => 'type', required => 'False', values =>
                   [ { name => 'Full', key => 'full' }, { name => 'New', key => 'new' } ] }
             ],
);
my $contact2 = $mech->create_contact_ok(
    body_id => $body->id, # Edinburgh
    category => 'Graffiti Removal',
    email => '101',
);
$mech->create_contact_ok(
    body_id => $body->id, # Edinburgh
    category => 'Ball lighting',
    email => '102',
    extra => { _fields => [
        { description => 'Size', code => 'size', required => 'True', automated => '' },
        { description => 'Speed', code => 'speed', required => 'True', automated => 'server_set' },
        { description => 'Colour', code => 'colour', required => 'True', automated => 'hidden_field' },
    ] },
);

my $body2 = $mech->create_body_ok(2651, 'Edinburgh Council');
my $contact4 = $mech->create_contact_ok(
    body_id => $body2->id, # Edinburgh
    category => 'Pothole',
    email => '103',
    extra => { _fields => [
        { description => 'USRN', code => 'usrn', required => 'true', automated => 'hidden_field', variable => 'true', order => '1' },
        { description => 'Asset ID', code => 'central_asset_id', required => 'true', automated => 'hidden_field', variable => 'true', order => '2' },
    ] },
);

# test that the various bit of form get filled in and errors correctly
# generated.
my $empty_form = {
    title         => '',
    detail        => '',
    photo1        => '',
    photo2        => '',
    photo3        => '',
    name          => '',
    may_show_name => '1',
    username      => '',
    email         => '',
    phone         => '',
    category      => '',
    password_sign_in => '',
    password_register => '',
    remember_me => undef,
};
foreach my $test (
    {
        msg    => 'all fields empty',
        pc     => 'EH99 1SP',
        fields => {
            %$empty_form,
            category => 'Street lighting',
        },
        changes => {
            number => '',
            type   => '',
        },
        errors  => [
            'This information is required',
            'Please enter a subject',
            'Please enter some details',
            'Please enter your email',
            'Please enter your name',
        ],
        submit_with => {
            title => 'test',
            detail => 'test detail',
            name => 'Test User',
            username => 'testopen311@example.com',
            category => 'Street lighting',
            number => 27,
            type => 'old',
        },
        extra => [
            {
                name => 'number',
                value => 27,
                description => 'Lamppost number',
            },
            {
                name => 'type',
                value => 'old',
                description => 'Lamppost type',
            }
        ]
    },
    {
        msg    => 'automated things',
        pc     => 'EH99 1SP',
        fields => {
            %$empty_form,
            category => 'Ball lighting',
        },
        changes => {
            size => '',
        },
        hidden => [ 'colour' ],
        errors  => [
            'This information is required',
            'Please enter a subject',
            'Please enter some details',
            'Please enter your email',
            'Please enter your name',
        ],
        submit_with => {
            title => 'test',
            detail => 'test detail',
            name => 'Test User',
            username => 'testopen311@example.com',
            size => 'big',
            colour => 'red',
        },
        extra => [
            {
                name => 'size',
                value => 'big',
                description => 'Size',
            },
            {
                name => 'colour',
                value => 'red',
                description => 'Colour',
            }
        ]
    },
  )
{
    subtest "check form errors where $test->{msg}" => sub {
        $mech->log_out_ok;
        $mech->clear_emails_ok;

        # check that the user does not exist
        my $test_email = $test->{submit_with}->{username};
        my $user = FixMyStreet::App->model('DB::User')->find( { email => $test_email } );
        if ( $user ) {
            $user->problems->delete;
            $user->comments->delete;
            $user->delete;
        }

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

        # submit initial pc form
        FixMyStreet::override_config {
            ALLOWED_COBRANDS => [ { 'fixmystreet' => '.' } ],
            MAPIT_URL => 'http://mapit.uk/',
        }, sub {
            $mech->submit_form_ok( { with_fields => { pc => $test->{pc} } },
                "submit location" );
            is_deeply $mech->page_errors, [], "no errors for pc '$test->{pc}'";

            # click through to the report page
            $mech->follow_link_ok( { text_regex => qr/skip this step/i, },
                "follow 'skip this step' link" );

            # submit the main form
            $mech->submit_form_ok( { with_fields => $test->{fields} },
                "submit form" );
        };

        # check that we got the errors expected
        is_deeply $mech->page_errors, $test->{errors}, "check errors";

        # check that fields have changed as expected
        my $new_values = {
            %{ $test->{fields} },     # values added to form
            %{ $test->{changes} },    # changes we expect
        };
        is_deeply $mech->visible_form_values, $new_values,
          "values correctly changed";
        if ($test->{hidden}) {
            my %hidden_fields = map { $_->name => 1 } grep { $_->type eq 'hidden' } ($mech->forms)[0]->inputs;
            foreach (@{$test->{hidden}}) {
                is $hidden_fields{$_}, 1;
            }
        }

        if ( $test->{fields}->{category} eq 'Street lighting' ) {
            my $result = scraper {
                process 'select#form_type option', 'option[]' => '@value';
            }
            ->scrape( $mech->response );

            is_deeply $result->{option}, [ "", qw/old modern/], 'displayed streetlight type select';
        }

        $new_values = {
            %{ $test->{fields} },
            %{ $test->{submit_with} },
        };
        FixMyStreet::override_config {
            ALLOWED_COBRANDS => [ { 'fixmystreet' => '.' } ],
            MAPIT_URL => 'http://mapit.uk/',
        }, sub {
            $mech->submit_form_ok( { with_fields => $new_values } );
        };

        $user = FixMyStreet::App->model('DB::User')->find( { email => $test_email } );
        ok $user, 'created user';
        my $prob = $user->problems->first;
        ok $prob, 'problem created';

        is_deeply $prob->get_extra_fields, $test->{extra}, 'extra open311 data added to problem';

        $user->problems->delete;
        $user->delete;
    };
}

subtest "Category extras omits description label when all fields are hidden" => sub {
    FixMyStreet::override_config {
        ALLOWED_COBRANDS => [ { fixmystreet => '.' } ],
        MAPIT_URL => 'http://mapit.uk/',
    }, sub {
        for (
          { url => '/report/new/ajax?' },
          { url => '/report/new/category_extras?category=Pothole' },
        ) {
            my $json = $mech->get_ok_json($_->{url} . '&latitude=55.952055&longitude=-3.189579');
            my $category_extra = $json->{by_category} ? $json->{by_category}{Pothole}{category_extra} : $json->{category_extra};
            contains_string($category_extra, "usrn");
            contains_string($category_extra, "central_asset_id");
            lacks_string($category_extra, "USRN", "Lacks 'USRN' label");
            lacks_string($category_extra, "Asset ID", "Lacks 'Asset ID' label");
            lacks_string($category_extra, "resolve your problem quicker, by providing some extra detail", "Lacks description text");
        }
    };
};

subtest "Category extras includes description label for user" => sub {
    FixMyStreet::override_config {
        ALLOWED_COBRANDS => [ { fixmystreet => '.' } ],
        MAPIT_URL => 'http://mapit.uk/',
    }, sub {
        $contact4->push_extra_fields({ description => 'Size?', code => 'size', required => 'true', automated => '', variable => 'true', order => '3' });
        $contact4->update;
        for (
          { url => '/report/new/ajax?' },
          { url => '/report/new/category_extras?category=Pothole' },
        ) {
            my $json = $mech->get_ok_json($_->{url} . '&latitude=55.952055&longitude=-3.189579');
            my $category_extra = $json->{by_category} ? $json->{by_category}{Pothole}{category_extra} : $json->{category_extra};
            contains_string($category_extra, "usrn");
            contains_string($category_extra, "central_asset_id");
            lacks_string($category_extra, "USRN", "Lacks 'USRN' label");
            lacks_string($category_extra, "Asset ID", "Lacks 'Asset ID' label");
            contains_string($category_extra, "Size?");
            contains_string($category_extra, "resolve your problem quicker, by providing some extra detail", "Contains description text");
        }
    };
};

done_testing();