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
|
use FixMyStreet::TestMech;
use t::Mock::Twilio;
my $twilio = t::Mock::Twilio->new;
LWP::Protocol::PSGI->register($twilio->to_psgi_app, host => 'api.twilio.com');
my $mech = FixMyStreet::TestMech->new;
my $user = $mech->create_user_ok('test@example.com', name => 'Test User');
my $user2 = $mech->create_user_ok('commenter@example.com', name => 'Commenter');
my $body = $mech->create_body_ok(2504, 'Westminster City Council');
my $dt = DateTime->new(
year => 2011,
month => 04,
day => 16,
hour => 15,
minute => 47,
second => 23
);
my $report = FixMyStreet::App->model('DB::Problem')->find_or_create(
{
postcode => 'SW1A 1AA',
bodies_str => $body->id,
areas => ',105255,11806,11828,2247,2504,',
category => 'Other',
title => 'Test 2',
detail => 'Test 2 Detail',
used_map => 't',
name => 'Test User',
anonymous => 'f',
state => 'confirmed',
confirmed => $dt->ymd . ' ' . $dt->hms,
lang => 'en-gb',
service => '',
cobrand => 'default',
cobrand_data => '',
send_questionnaire => 't',
latitude => '51.5016605453401',
longitude => '-0.142497580865087',
user_id => $user->id,
}
);
my $report_id = $report->id;
ok $report, "created test report - $report_id";
my $comment = FixMyStreet::App->model('DB::Comment')->find_or_create( {
problem_id => $report_id,
user_id => $user2->id,
name => 'Other User',
mark_fixed => 'false',
text => 'This is some update text',
state => 'confirmed',
confirmed => $dt->ymd . ' ' . $dt->hms,
anonymous => 'f',
});
my $comment_id = $comment->id;
ok $comment, "created test update - $comment_id";
for my $test (
{
desc => 'Invalid phone',
fields => {
username => '01214960000000',
update => 'Update',
name => 'Name',
photo1 => '',
photo2 => '',
photo3 => '',
fixed => undef,
add_alert => 1,
may_show_name => undef,
remember_me => undef,
password_sign_in => '',
password_register => '',
},
changes => {},
field_errors => [ 'Please check your phone number is correct' ]
},
{
desc => 'landline number',
fields => {
username => '01214960000',
update => 'Update',
name => 'Name',
photo1 => '',
photo2 => '',
photo3 => '',
fixed => undef,
add_alert => 1,
may_show_name => undef,
remember_me => undef,
password_register => '',
password_sign_in => '',
},
changes => {
username => '0121 496 0000',
},
field_errors => [ 'Please enter a mobile number' ]
},
)
{
subtest "submit an update - $test->{desc}" => sub {
$mech->get_ok("/report/$report_id");
FixMyStreet::override_config {
SMS_AUTHENTICATION => 1,
PHONE_COUNTRY => 'GB',
}, sub {
$mech->submit_form_ok( { with_fields => $test->{fields} }, 'submit update' );
};
is_deeply $mech->page_errors, $test->{field_errors}, 'field errors';
my $values = {
%{ $test->{fields} },
%{ $test->{changes} },
};
is_deeply $mech->visible_form_values('updateForm'), $values, 'form changes';
};
}
my $test_phone = '+61491570156';
for my $test (
{
desc => 'submit an update, unregistered, logged out',
form_values => {
submit_update => 1,
username => $test_phone,
update => 'Update from an unregistered user',
add_alert => undef,
name => 'Unreg User',
may_show_name => undef,
},
},
{
desc => 'submit an update, unregistered, logged out, sign up for alerts',
form_values => {
submit_update => 1,
username => $test_phone,
update => 'Update from an unregistered user',
add_alert => 1,
name => 'Unreg User',
may_show_name => undef,
},
},
{
desc => 'submit an update, registered, logged out, confirming by text',
registered => 1,
form_values => {
submit_update => 1,
username => $test_phone,
update => 'Update from a registered user',
add_alert => undef,
name => 'Reg User',
password_register => 'new_secret',
},
},
) {
subtest $test->{desc} => sub {
$mech->log_out_ok();
my $user;
if ($test->{registered}) {
$user = $mech->create_user_ok( $test_phone );
$user->update( { name => 'Mr Reg', password => 'secret2' } );
}
$mech->get_ok("/report/$report_id");
FixMyStreet::override_config {
SMS_AUTHENTICATION => 1,
TWILIO_ACCOUNT_SID => 'AC123',
}, sub {
$mech->submit_form_ok( { with_fields => $test->{form_values} }, 'submit update');
};
$mech->content_contains('Nearly done! Now check your phone');
if ($user) {
$user->discard_changes;
ok $user->check_password( 'secret2' ), 'password unchanged';
is $user->name, 'Mr Reg', 'name unchanged';
}
my ($token) = $mech->content =~ /name="token" value="([^"]*)"/;
$token = FixMyStreet::App->model('DB::Token')->find({
token => $token,
scope => 'comment'
});
ok $token, 'Token found in database';
my $update_id = $token->data->{id};
my $add_alerts = $token->data->{add_alert};
my $update = FixMyStreet::App->model('DB::Comment')->find( { id => $update_id } );
ok $update, 'found update in database';
is $update->state, 'unconfirmed', 'update unconfirmed';
my $details = $test->{form_values};
is $update->user->phone, $details->{username}, 'update phone';
is $update->user->phone_verified, 1;
is $update->text, $details->{update}, 'update text';
is $add_alerts, $details->{add_alert} ? 1 : 0, 'do not sign up for alerts';
my $code = $twilio->get_text_code;
$mech->submit_form_ok( { with_fields => { code => '00000' } });
$mech->content_contains('Try again');
$mech->submit_form_ok( { with_fields => { code => $code } });
$mech->content_contains("/report/$report_id#update_$update_id");
if ($user) {
$user->discard_changes;
ok $user->check_password( 'new_secret' ), 'password changed';
is $user->name, 'Reg User', 'name changed';
} else {
$user = FixMyStreet::App->model( 'DB::User' )->find( { phone => $details->{username} } );
ok $user, 'found user';
}
my $alert = FixMyStreet::App->model( 'DB::Alert' )->find(
{ user => $user, alert_type => 'new_updates', confirmed => 1, }
);
ok $details->{add_alert} ? defined( $alert ) : !defined( $alert ), 'sign up for alerts';
$update->discard_changes;
is $update->state, 'confirmed', 'update confirmed';
$mech->delete_user( $user );
};
}
for my $test (
{
desc => 'submit an update for a registered user, signing in with wrong password',
form_values => {
submit_update => 1,
username => $test_phone,
update => 'Update from a user',
add_alert => undef,
password_sign_in => 'secret',
},
field_errors => [
"There was a problem with your login information. If you cannot remember your password, or do not have one, please fill in the \x{2018}No\x{2019} section of the form.",
'Please enter your name', # FIXME Not really necessary error
],
},
{
desc => 'submit an update for a registered user and sign in',
form_values => {
submit_update => 1,
username => $test_phone,
update => 'Update from a user',
add_alert => undef,
password_sign_in => 'secret2',
},
message => 'You have successfully signed in; please check and confirm your details are accurate:',
}
) {
subtest $test->{desc} => sub {
# Set things up
my $user = $mech->create_user_ok( $test->{form_values}->{username} );
my $pw = 'secret2';
$user->update( { name => 'Mr Reg', password => $pw } );
$report->comments->delete;
$mech->log_out_ok();
$mech->clear_emails_ok();
$mech->get_ok("/report/$report_id");
FixMyStreet::override_config {
SMS_AUTHENTICATION => 1,
}, sub {
$mech->submit_form_ok(
{
button => 'submit_sign_in',
with_fields => $test->{form_values}
},
'submit update'
);
};
$mech->content_contains($test->{message}) if $test->{message};
is_deeply $mech->page_errors, $test->{field_errors}, 'check there were errors'
if $test->{field_errors};
SKIP: {
skip( "Incorrect password", 4 ) unless $test->{form_values}{password_sign_in} eq $pw;
# Now submit with a name
$mech->submit_form_ok(
{ with_fields => { name => 'Joe Bloggs', } },
"submit good details"
);
$mech->content_contains('Thank you for updating this issue');
my $update = $report->comments->first;
ok $update, 'found update';
is $update->text, $test->{form_values}->{update}, 'update text';
is $update->user->phone, $test->{form_values}->{username}, 'update user';
is $update->state, 'confirmed', 'update confirmed';
$mech->delete_user( $update->user );
}
};
}
done_testing();
|