aboutsummaryrefslogtreecommitdiffstats
path: root/t/app/controller/admin/templates.t
blob: ad5b3e77bf68b446fcc1025edbaa6b20b2fb2090 (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
use FixMyStreet::TestMech;

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

my $user = $mech->create_user_ok('test@example.com', name => 'Test User');

my $superuser = $mech->create_user_ok('superuser@example.com', name => 'Super User', is_superuser => 1);

my $oxfordshire = $mech->create_body_ok(2237, 'Oxfordshire County Council');
my $oxfordshirecontact = $mech->create_contact_ok( body_id => $oxfordshire->id, category => 'Potholes', email => 'potholes@example.com' );
my $oxfordshirecontact2 = $mech->create_contact_ok( body_id => $oxfordshire->id, category => 'Flytipping', email => 'flytipping@example.com' );
my $oxfordshireuser = $mech->create_user_ok('counciluser@example.com', name => 'Council User', from_body => $oxfordshire);

my $bromley = $mech->create_body_ok(2482, 'Bromley Borough Council');
my $bromleycontact = $mech->create_contact_ok( body_id => $bromley->id, category => 'Potholes', email => 'potholes@example.com' );
my $bromleyuser = $mech->create_user_ok('bromleyuser@example.com', name => 'Council User', from_body => $bromley);
$bromleyuser->user_body_permissions->find_or_create({
    body => $bromley,
    permission_type => 'report_inspect',
});
my $bromleytemplate = $bromley->response_templates->create({
    title => "Bromley-specific response template.",
    text => "This template will only appear on the Bromley cobrand.",
});

my $tfl = $mech->create_body_ok(2482, 'TfL');
my $tflcontact = $mech->create_contact_ok( body_id => $tfl->id, category => 'Potholes', email => 'potholes@example.com' );
my $tfluser = $mech->create_user_ok('tfluser@example.com', name => 'Council User', from_body => $tfl);
$tfluser->user_body_permissions->find_or_create({
    body => $tfl,
    permission_type => 'report_inspect',
});
my $tfltemplate = $tfl->response_templates->create({
    title => "TfL-specific response template.",
    text => "This template will only appear on the TfL cobrand.",
});

my $dt = DateTime->now();

my $report = FixMyStreet::DB->resultset('Problem')->find_or_create(
    {
        postcode           => 'SW1A 1AA',
        bodies_str         => '2504',
        areas              => ',105255,11806,11828,2247,2504,',
        category           => 'Other',
        title              => 'Report to Edit',
        detail             => 'Detail for Report to Edit',
        used_map           => 't',
        name               => 'Test User',
        anonymous          => 'f',
        external_id        => '13',
        state              => 'confirmed',
        confirmed          => $dt->ymd . ' ' . $dt->hms,
        lang               => 'en-gb',
        service            => '',
        cobrand            => '',
        cobrand_data       => '',
        send_questionnaire => 't',
        latitude           => '51.5016605453401',
        longitude          => '-0.142497580865087',
        user_id            => $user->id,
        whensent           => $dt->ymd . ' ' . $dt->hms,
    }
);

my $report_id = $report->id;
ok $report, "created test report - $report_id";

subtest "response templates can be added" => sub {
    is $oxfordshire->response_templates->count, 0, "No response templates yet";
    $mech->log_in_ok( $superuser->email );
    $mech->get_ok( "/admin/templates/" . $oxfordshire->id . "/new" );

    my $fields = {
        title => "Report acknowledgement",
        text => "Thank you for your report. We will respond shortly.",
        auto_response => undef,
        "contacts[".$oxfordshirecontact->id."]" => 1,
    };
    $mech->submit_form_ok( { with_fields => $fields } );

    is $oxfordshire->response_templates->count, 1, "Response template was added";
};

subtest 'check log of the above' => sub {
    my $template_id = $oxfordshire->response_templates->first->id;
    $mech->get_ok('/admin/users/' . $superuser->id . '/log');
    $mech->content_contains('Added template <a href="/admin/templates/' . $oxfordshire->id . '/' . $template_id . '">Report acknowledgement</a>');
};

subtest "but not another with the same title" => sub {
    my $fields = {
        title => "Report acknowledgement",
        text => "Another report acknowledgement.",
        auto_response => undef,
        "contacts[".$oxfordshirecontact->id."]" => 1,
    };
    my $list_url = "/admin/templates/" . $oxfordshire->id;
    $mech->get_ok( "$list_url/new" );
    $mech->submit_form_ok( { with_fields => $fields } );
    is $mech->uri->path, "$list_url/new", 'not redirected';
    $mech->content_contains( 'Please correct the errors below' );
    $mech->content_contains( 'There is already a template with that title.' );

    my @ts = $oxfordshire->response_templates->all;
    is @ts, 1, "No new response template was added";

    my $url = "$list_url/" . $ts[0]->id;
    $mech->get_ok($url);
    $mech->submit_form_ok( { with_fields => $fields } );
    is $mech->uri->path, $list_url, 'redirected';
    is $oxfordshire->response_templates->count, 1, "No new response template was added";
};

subtest "response templates are included on page" => sub {
    FixMyStreet::override_config {
        ALLOWED_COBRANDS => [ 'oxfordshire' ],
    }, sub {
        $report->update({ category => $oxfordshirecontact->category, bodies_str => $oxfordshire->id });
        $mech->log_in_ok( $oxfordshireuser->email );

        $mech->get_ok("/report/" . $report->id);
        $mech->content_contains( $oxfordshire->response_templates->first->text );

        $mech->log_out_ok;
    };
};

subtest "auto-response templates that duplicate a single category can't be added" => sub {
    $mech->delete_response_template($_) for $oxfordshire->response_templates;
    my $template = $oxfordshire->response_templates->create({
        title => "Report fixed - potholes",
        text => "Thank you for your report. This problem has been fixed.",
        auto_response => 1,
        state => 'fixed - council',
    });
    $template->contact_response_templates->find_or_create({
        contact_id => $oxfordshirecontact->id,
    });
    is $oxfordshire->response_templates->count, 1, "Initial response template was created";


    $mech->log_in_ok( $superuser->email );
    $mech->get_ok( "/admin/templates/" . $oxfordshire->id . "/new" );

    # This response template has the same category & state as an existing one
    # so won't be allowed.
    my $fields = {
        title => "Report marked fixed - potholes",
        text => "Thank you for your report. This pothole has been fixed.",
        auto_response => 'on',
        state => 'fixed - council',
        "contacts[".$oxfordshirecontact->id."]" => 1,
    };
    $mech->submit_form_ok( { with_fields => $fields } );
    is $mech->uri->path, '/admin/templates/' . $oxfordshire->id . '/new', 'not redirected';
    $mech->content_contains( 'Please correct the errors below' );
    $mech->content_contains( 'There is already an auto-response template for this category/state.' );

    is $oxfordshire->response_templates->count, 1, "Duplicate response template wasn't added";
};

subtest "auto-response templates that duplicate all categories can't be added" => sub {
    $mech->delete_response_template($_) for $oxfordshire->response_templates;
    $oxfordshire->response_templates->create({
        title => "Report investigating - all cats",
        text => "Thank you for your report. This problem has been fixed.",
        auto_response => 1,
        state => 'fixed - council',
    });
    is $oxfordshire->response_templates->count, 1, "Initial response template was created";


    $mech->log_in_ok( $superuser->email );
    $mech->get_ok( "/admin/templates/" . $oxfordshire->id . "/new" );

    # There's already a response template for all categories and this state, so
    # this new template won't be allowed.
    my $fields = {
        title => "Report investigating - single cat",
        text => "Thank you for your report. This problem has been fixed.",
        auto_response => 'on',
        state => 'fixed - council',
        "contacts[".$oxfordshirecontact->id."]" => 1,
    };
    $mech->submit_form_ok( { with_fields => $fields } );
    is $mech->uri->path, '/admin/templates/' . $oxfordshire->id . '/new', 'not redirected';
    $mech->content_contains( 'Please correct the errors below' );
    $mech->content_contains( 'There is already an auto-response template for this category/state.' );


    is $oxfordshire->response_templates->count, 1, "Duplicate response template wasn't added";
};

subtest "all-category auto-response templates that duplicate a single category can't be added" => sub {
    $mech->delete_response_template($_) for $oxfordshire->response_templates;
    my $template = $oxfordshire->response_templates->create({
        title => "Report fixed - potholes",
        text => "Thank you for your report. This problem has been fixed.",
        auto_response => 1,
        state => 'fixed - council',
    });
    $template->contact_response_templates->find_or_create({
        contact_id => $oxfordshirecontact->id,
    });
    is $oxfordshire->response_templates->count, 1, "Initial response template was created";


    $mech->log_in_ok( $superuser->email );
    $mech->get_ok( "/admin/templates/" . $oxfordshire->id . "/new" );

    # This response template is implicitly for all categories, but there's
    # already a template for a specific category in this state, so it won't be
    # allowed.
    my $fields = {
        title => "Report marked fixed - all cats",
        text => "Thank you for your report. This problem has been fixed.",
        auto_response => 'on',
        state => 'fixed - council',
    };
    $mech->submit_form_ok( { with_fields => $fields } );
    is $mech->uri->path, '/admin/templates/' . $oxfordshire->id . '/new', 'not redirected';
    $mech->content_contains( 'Please correct the errors below' );
    $mech->content_contains( 'There is already an auto-response template for this category/state.' );

    is $oxfordshire->response_templates->count, 1, "Duplicate response template wasn't added";
};

subtest "auto-response templates that duplicate external_status_code can't be added" => sub {
    $mech->delete_response_template($_) for $oxfordshire->response_templates;
    my $template = $oxfordshire->response_templates->create({
        title => "Report fixed - potholes",
        text => "Thank you for your report. This problem has been fixed.",
        auto_response => 1,
        external_status_code => '100',
    });
    $template->contact_response_templates->find_or_create({
        contact_id => $oxfordshirecontact->id,
    });
    is $oxfordshire->response_templates->count, 1, "Initial response template was created";

    $mech->log_in_ok( $superuser->email );
    $mech->get_ok( "/admin/templates/" . $oxfordshire->id . "/new" );

    my $fields = {
        title => "Report marked fixed - all cats",
        text => "Thank you for your report. This problem has been fixed.",
        auto_response => 'on',
        external_status_code => '100',
    };
    $mech->submit_form_ok( { with_fields => $fields } );
    is $mech->uri->path, '/admin/templates/' . $oxfordshire->id . '/new', 'not redirected';
    $mech->content_contains( 'Please correct the errors below' );
    $mech->content_contains( 'There is already an auto-response template for this category/state.' );

    is $oxfordshire->response_templates->count, 1, "Duplicate response template wasn't added";
};

subtest "templates that set state and external_status_code can't be added" => sub {
    $mech->delete_response_template($_) for $oxfordshire->response_templates;
    $mech->log_in_ok( $superuser->email );
    $mech->get_ok( "/admin/templates/" . $oxfordshire->id . "/new" );

    my $fields = {
        title => "Report marked fixed - all cats",
        text => "Thank you for your report. This problem has been fixed.",
        auto_response => 'on',
        state => 'fixed - council',
        external_status_code => '100',
    };
    $mech->submit_form_ok( { with_fields => $fields } );
    is $mech->uri->path, '/admin/templates/' . $oxfordshire->id . '/new', 'not redirected';
    $mech->content_contains( 'Please correct the errors below' );
    $mech->content_contains( 'State and external status code cannot be used simultaneously.' );

    is $oxfordshire->response_templates->count, 0, "Invalid response template wasn't added";
};

subtest "category groups are shown" => sub {
    FixMyStreet::override_config {
        ALLOWED_COBRANDS => [ 'oxfordshire' ],
        COBRAND_FEATURES => {
            category_groups => {
                oxfordshire => 1,
            },
            multiple_category_groups => {
                oxfordshire => 1,
            },
        },
    }, sub {

        $mech->log_in_ok( $superuser->email );

        $mech->get_ok( "/admin/templates/" . $oxfordshire->id . "/new" );
        $mech->content_contains("No Group") or diag $mech->content;
        $mech->content_lacks("Multiple Groups");
        $mech->content_lacks("These categories appear in more than one group:");

        $oxfordshirecontact->set_extra_metadata( group => [ 'Highways' ] );
        $oxfordshirecontact->update;
        $oxfordshirecontact2->set_extra_metadata( group => [ 'Street Cleaning' ] );
        $oxfordshirecontact2->update;
        $mech->get_ok( "/admin/templates/" . $oxfordshire->id . "/new" );
        $mech->content_lacks("No Group");
        $mech->content_lacks("Multiple Groups");
        $mech->content_lacks("These categories appear in more than one group:");
        $mech->content_contains("Highways");
        $mech->content_contains("Street Cleaning");

        $oxfordshirecontact->set_extra_metadata( group => [ 'Highways', 'Roads & Pavements' ] );
        $oxfordshirecontact->update;
        $oxfordshirecontact2->set_extra_metadata( group => [ 'Street Cleaning' ] );
        $oxfordshirecontact2->update;
        $mech->get_ok( "/admin/templates/" . $oxfordshire->id . "/new" );
        $mech->content_lacks("No Group");
        $mech->content_contains("Multiple Groups");
        $mech->content_contains("These categories appear in more than one group:");
        $mech->content_contains("Highways; Roads &amp; Pavements");
        $mech->content_contains("Street Cleaning");
    };
};

subtest "TfL cobrand only shows TfL templates" => sub {
    FixMyStreet::override_config {
        ALLOWED_COBRANDS => [ 'tfl' ],
        COBRAND_FEATURES => { internal_ips => { tfl => [ '127.0.0.1' ] } },
    }, sub {
        $report->update({
            category => $tflcontact->category,
            bodies_str => $tfl->id,
            latitude => 51.402096,
            longitude => 0.015784,
            state => 'confirmed',
            areas => ',2482,',
        });
        $mech->log_in_ok( $tfluser->email );

        $mech->get_ok("/report/" . $report->id);
        $mech->content_contains( $tfltemplate->text );
        $mech->content_contains( $tfltemplate->title );
        $mech->content_lacks( $bromleytemplate->text );
        $mech->content_lacks( $bromleytemplate->title );

        $mech->log_out_ok;
    };
};

subtest "Bromley cobrand only shows Bromley templates" => sub {
    FixMyStreet::override_config {
        ALLOWED_COBRANDS => [ 'bromley', 'tfl' ],
    }, sub {
        $report->update({ category => $bromleycontact->category, bodies_str => $bromley->id });
        $mech->log_in_ok( $bromleyuser->email );

        $mech->get_ok("/report/" . $report->id);
        $mech->content_contains( $bromleytemplate->text );
        $mech->content_contains( $bromleytemplate->title );
        $mech->content_lacks( $tfltemplate->text );
        $mech->content_lacks( $tfltemplate->title );

        $mech->log_out_ok;
    };
};

done_testing();