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
|
use strict;
use warnings;
use Test::More;
use FixMyStreet::TestMech;
my $mech = FixMyStreet::TestMech->new;
foreach my $test (
{
email => 'test@example.com',
type => 'area',
content => 'your alert will not be activated',
email_text => 'confirm the alert',
uri =>
'/alert/subscribe?type=local&rznvy=test@example.com&feed=area:1000:A_Location',
param1 => 1000
},
{
email => 'test@example.com',
type => 'council',
content => 'your alert will not be activated',
email_text => 'confirm the alert',
uri =>
'/alert/subscribe?type=local&rznvy=test@example.com&feed=council:1000:A_Location',
param1 => 1000,
param2 => 1000,
},
{
email => 'test@example.com',
type => 'ward',
content => 'your alert will not be activated',
email_text => 'confirm the alert',
uri =>
'/alert/subscribe?type=local&rznvy=test@example.com&feed=ward:1000:1001:A_Location:Diff_Location',
param1 => 1000,
param2 => 1001,
},
{
email => 'test@example.com',
type => 'local',
content => 'your alert will not be activated',
email_text => 'confirm the alert',
uri =>
'/alert/subscribe?type=local&rznvy=test@example.com&feed=local:10.2:20.1',
param1 => 10.2,
param2 => 20.1,
}
)
{
subtest "$test->{type} alert correctly created" => sub {
$mech->clear_emails_ok;
my $type = $test->{type} . '_problems';
my $user = FixMyStreet::App->model('DB::User')->find(
{
email => $test->{email}
}
);
# we don't want an alert
my $alert;
if ( $user ) {
$mech->delete_user( $user );
}
$mech->get_ok( $test->{uri} );
$mech->content_contains( $test->{content} );
$user = FixMyStreet::App->model('DB::User')->find(
{
email => $test->{email}
}
);
ok $user, 'user created for alert';
$alert = FixMyStreet::App->model('DB::Alert')->find(
{
user => $user,
alert_type => $type,
parameter => $test->{param1},
parameter2 => $test->{param2},
confirmed => 0,
}
);
ok $alert, "Found the alert";
my $email = $mech->get_email;
ok $email, "got an email";
like $email->body, qr/$test->{email_text}/i, "Correct email text";
my ( $url, $url_token ) = $email->body =~ m{(http://\S+/A/)(\S+)};
ok $url, "extracted confirm url '$url'";
my $token = FixMyStreet::App->model('DB::Token')->find(
{
token => $url_token,
scope => 'alert'
}
);
ok $token, 'Token found in database';
ok $alert->id == $token->data->{id}, 'token alertid matches alert id';
$mech->clear_emails_ok;
my $existing_id = $alert->id;
my $existing_token = $url_token;
$mech->get_ok( $test->{uri} );
$email = $mech->get_email;
ok $email, 'got a second email';
($url_token) = $email->body =~ m{http://\S+/A/(\S+)};
ok $url_token ne $existing_token, 'sent out a new token';
$token = FixMyStreet::App->model('DB::Token')->find(
{
token => $url_token,
scope => 'alert'
}
);
ok $token, 'new token found in database';
ok $token->data->{id} == $existing_id, 'subscribed to exsiting alert';
$mech->get_ok("/A/$url_token");
$mech->content_contains('successfully confirmed');
$alert =
FixMyStreet::App->model('DB::Alert')->find( { id => $existing_id, } );
ok $alert->confirmed, 'alert set to confirmed';
};
}
foreach my $test (
{
email => 'test-new@example.com',
type => 'area',
content => 'your alert will not be activated',
email_text => 'confirm the alert',
uri =>
'/alert/subscribe?type=local&rznvy=test-new@example.com&feed=area:1000:A_Location',
param1 => 1000
}
) {
subtest "use existing user in a alert" => sub {
my $type = $test->{type} . '_problems';
my $user = FixMyStreet::App->model('DB::User')->find_or_create(
{
email => $test->{email}
}
);
my $alert;
if ( $user ) {
$alert = FixMyStreet::App->model('DB::Alert')->find(
{
user => $user,
alert_type => $type
}
);
# clear existing data so we can be sure we're creating it
$alert->delete() if $alert;
}
$mech->get_ok( $test->{uri} );
$alert = FixMyStreet::App->model('DB::Alert')->find(
{
user => $user,
alert_type => $type,
parameter => $test->{param1},
parameter2 => $test->{param2},
confirmed => 0,
}
);
ok $alert, 'New alert created with existing user';
};
}
done_testing();
|