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
|
use FixMyStreet::TestMech;
use Test::MockModule;
my $mech = FixMyStreet::TestMech->new;
$mech->get_ok('/my/planned');
is $mech->uri->path, '/auth', "got sent to the sign in page";
my $body = $mech->create_body_ok(2237, 'Oxfordshire County Council');
my $body2 = $mech->create_body_ok(2421, 'Oxford City Council');
my ($problem) = $mech->create_problems_for_body(1, $body->id, 'Test Title');
$mech->get_ok($problem->url);
$mech->content_lacks('Shortlist');
$mech->content_lacks('Shortlisted');
my $user = $mech->log_in_ok( 'test@example.com' );
$user->update({ from_body => $body });
$user->user_body_permissions->find_or_create({
body => $body,
permission_type => 'planned_reports',
});
$mech->get_ok('/my/planned');
$mech->content_lacks('Test Title');
$user->add_to_planned_reports($problem);
$mech->get_ok('/my/planned');
$mech->content_contains('Test Title');
$user->remove_from_planned_reports($problem);
$mech->get_ok('/my/planned');
$mech->content_lacks('Test Title');
$user->add_to_planned_reports($problem);
$mech->get_ok('/my/planned');
$mech->content_contains('Test Title');
$mech->get_ok($problem->url);
$mech->text_contains('Shortlisted');
$mech->submit_form_ok({ with_fields => { 'shortlist-remove' => 1 } });
$mech->text_contains('Shortlist');
$mech->submit_form_ok({ with_fields => { 'shortlist-add' => 1 } });
$mech->text_contains('Shortlisted');
$mech->get_ok('/my/planned?sort=shortlist&ajax=1');
$mech->content_contains('shortlist-up');
$mech->content_contains('shortlist-down');
$mech->get_ok('/my/planned?sort=created-desc&ajax=1');
$mech->content_lacks('shortlist-up');
$mech->content_lacks('shortlist-down');
$mech->get_ok('/my/planned?ajax=1');
$mech->content_contains('shortlist-up');
$mech->content_contains('shortlist-down');
subtest "POSTing multiple problems to my/planned/change adds all to shortlist" => sub {
my ($problem1, $problem2, $problem3) = $mech->create_problems_for_body(3, $body->id, 'New Problem');
# Grab CSRF token
$mech->get_ok($problem1->url);
my ($csrf) = $mech->content =~ /meta content="([^"]*)" name="csrf-token"/;
$mech->post_ok( '/my/planned/change_multiple', {
'ids[]' => [
$problem1->id,
$problem2->id,
$problem3->id,
],
token => $csrf,
}
);
$mech->get_ok($problem1->url);
$mech->text_contains('Shortlisted');
$mech->get_ok($problem2->url);
$mech->text_contains('Shortlisted');
$mech->get_ok($problem3->url);
$mech->text_contains('Shortlisted');
};
subtest "re-ordering shortlist on non shortlist page redirect to shortlist" => sub {
$user->user_planned_reports->remove();
my ($problem1) = $mech->create_problems_for_body(1, $body->id, 'New Problem');
$mech->get_ok($problem1->url);
my ($csrf) = $mech->content =~ /meta content="([^"]*)" name="csrf-token"/;
$mech->post_ok( '/my/planned/change', {
id => $problem1->id,
'shortlist-up' => 1,
token => $csrf,
},
);
$mech->content_contains('Your shortlist');
};
subtest "shortlist with no action is forbidden" => sub {
$user->user_planned_reports->remove();
my ($problem1) = $mech->create_problems_for_body(1, $body->id, 'New Problem');
$mech->get_ok($problem1->url);
my ($csrf) = $mech->content =~ /meta content="([^"]*)" name="csrf-token"/;
my $result = $mech->post( '/my/planned/change', {
id => $problem1->id,
token => $csrf,
},
);
is $result->code, 403, '403 response if no action';
};
subtest "cannot remove non-existant problems from shortlist" => sub {
$user->user_planned_reports->remove();
my ($problem1) = $mech->create_problems_for_body(1, $body->id, 'New Problem');
$mech->get_ok($problem1->url);
my ($csrf) = $mech->content =~ /meta content="([^"]*)" name="csrf-token"/;
my $result = $mech->post( '/my/planned/change', {
id => 999,
'shortlist-remove' => 1,
token => $csrf,
},
);
is $result->code, 404, 'removing missing report returns 404';
};
subtest "can remove problems from shortlist" => sub {
$user->user_planned_reports->remove();
my ($problem1, $problem2) = $mech->create_problems_for_body(2, $body->id, 'New Problem');
$mech->get_ok($problem1->url);
my ($csrf) = $mech->content =~ /meta content="([^"]*)" name="csrf-token"/;
$mech->post_ok( '/my/planned/change_multiple', {
'ids[]' => [
$problem1->id,
$problem2->id,
],
token => $csrf,
}
);
$mech->get_ok($problem1->url);
$mech->text_contains('Shortlisted');
($csrf) = $mech->content =~ /meta content="([^"]*)" name="csrf-token"/;
$mech->post_ok( '/my/planned/change', {
id => $problem1->id,
'shortlist-remove' => 1,
token => $csrf,
},
'Removed problem from shortlist');
$mech->get_ok($problem1->url);
$mech->text_lacks('Shortlisted');
$mech->text_contains('Shortlist');
# check cases where problem has changed body due
# to e.g. change of category
$problem2->update({
bodies_str => $body2->id,
title => 'Other body problem',
});
$mech->get_ok('/my/planned');
$mech->text_contains('Other body problem');
($csrf) = $mech->content =~ /meta content="([^"]*)" name="csrf-token"/;
$mech->post_ok( '/my/planned/change', {
id => $problem2->id,
'shortlist-remove' => 1,
token => $csrf,
},
'Removed problem for other body from shortlist');
$mech->get_ok('/my/planned');
$mech->text_lacks('Other body problem');
};
FixMyStreet::override_config {
ALLOWED_COBRANDS => [ 'oxfordshire' ],
BASE_URL => 'http://oxfordshire.fixmystreet.site',
}, sub {
my $cobrand = Test::MockModule->new('FixMyStreet::Cobrand::Oxfordshire');
$cobrand->mock('available_permissions', sub {
my $self = shift;
return FixMyStreet::Cobrand::Default->available_permissions;
});
subtest "can remove problems not displayed in cobrand from shortlist" => sub {
$user->user_planned_reports->remove();
my ($problem1) = $mech->create_problems_for_body(2, $body->id, 'New Problem');
$mech->get_ok($problem1->url);
my ($csrf) = $mech->content =~ /meta content="([^"]*)" name="csrf-token"/;
$mech->post_ok( '/my/planned/change_multiple', {
'ids[]' => [
$problem1->id,
],
token => $csrf,
}
);
$mech->get_ok('/my/planned');
$mech->text_contains('New Problem');
$mech->content_contains('Remove from shortlist');
$problem1->update({
bodies_str => $body2->id,
});
$mech->get_ok('/my/planned');
$mech->text_contains('New Problem');
$mech->content_contains('Remove from shortlist');
($csrf) = $mech->content =~ /meta content="([^"]*)" name="csrf-token"/;
$mech->post_ok( '/my/planned/change', {
id => $problem1->id,
'shortlist-remove' => 1,
token => $csrf,
ajax => 1,
},
'Removed problem not displayed in this cobrand');
$mech->get_ok('/my/planned');
$mech->text_lacks('New Problem', 'Problem no longer in shortlist');
};
};
done_testing();
|