aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStruan Donald <struan@exo.org.uk>2011-05-10 17:23:20 +0100
committerStruan Donald <struan@exo.org.uk>2011-05-10 17:23:20 +0100
commit00177a5cd2ebbfeb08082712c1a1c392276c4a83 (patch)
tree8a27bed4bc193eaab663ffb623318d21eb4a3367
parenta1fa6aa37ce73e2a637e4bac565cd0a199f551e2 (diff)
send out alert subscription email confirmations
-rw-r--r--perllib/FixMyStreet/App/Controller/Alert.pm104
-rw-r--r--t/app/controller/alert_new.t96
-rw-r--r--templates/email/default/alert-confirm.txt14
3 files changed, 135 insertions, 79 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Alert.pm b/perllib/FixMyStreet/App/Controller/Alert.pm
index 3d2c86970..12624b8f2 100644
--- a/perllib/FixMyStreet/App/Controller/Alert.pm
+++ b/perllib/FixMyStreet/App/Controller/Alert.pm
@@ -27,7 +27,6 @@ Show the alerts page
sub index : Path('') : Args(0) {
my ( $self, $c ) = @_;
-
# my $q = shift;
# my $cobrand = Page::get_cobrand($q);
# my $error = shift;
@@ -67,7 +66,7 @@ sub index : Path('') : Args(0) {
sub list : Path('list') : Args(0) {
my ( $self, $c ) = @_;
- $c->stash->{rznvy} = $c->req->param('rznvy');
+ $c->stash->{rznvy} = $c->req->param('rznvy');
$c->stash->{selected_feed} = $c->req->param('feed');
# my ($q, @errors) = @_;
@@ -172,10 +171,11 @@ Target for subscribe form
sub subscribe : Path('subscribe') : Args(0) {
my ( $self, $c ) = @_;
- if ( $c->req->param( 'rss' ) ) {
- $c->detach( 'rss' );
- } elsif ( $c->req->param( 'rznvy' ) ) {
- $c->detach( 'subscribe_email' );
+ if ( $c->req->param('rss') ) {
+ $c->detach('rss');
+ }
+ elsif ( $c->req->param('rznvy') ) {
+ $c->detach('subscribe_email');
}
}
@@ -230,81 +230,109 @@ sub subscribe_email : Private {
$c->stash->{email_type} = 'alert';
my @errors;
- push @errors, _('Please enter a valid email address') unless is_valid_email($c->req->param('rznvy'));
- push @errors, _('Please select the type of alert you want') if $type && $type eq 'local' && !$c->req->param('feed');
+ push @errors, _('Please enter a valid email address')
+ unless is_valid_email( $c->req->param('rznvy') );
+ push @errors, _('Please select the type of alert you want')
+ if $type && $type eq 'local' && !$c->req->param('feed');
if (@errors) {
$c->stash->{errors} = \@errors;
$c->go('list');
-# return alert_updates_form($q, @errors) if $type && $type eq 'updates';
-# return alert_list($q, @errors) if $type && $type eq 'local';
-# return alert_front_page($q, @errors);
+
+ # return alert_updates_form($q, @errors) if $type && $type eq 'updates';
+ # return alert_list($q, @errors) if $type && $type eq 'local';
+ # return alert_front_page($q, @errors);
}
- my $alert_id;
+ my $alert;
my $email = $c->req->param('rznvy');
-# my $cobrand = Page::get_cobrand($q);
-# my $cobrand_data = Cobrand::extra_alert_data($cobrand, $q);
- if ($type eq 'updates') {
+
+ # my $cobrand = Page::get_cobrand($q);
+ # my $cobrand_data = Cobrand::extra_alert_data($cobrand, $q);
+ if ( $type eq 'updates' ) {
+
# my $id = $q->param('id');
# $alert_id = FixMyStreet::Alert::create($email, 'new_updates', $cobrand, $cobrand_data, $id);
- } elsif ($type eq 'problems') {
+ }
+ elsif ( $type eq 'problems' ) {
+
# $alert_id = FixMyStreet::Alert::create($email, 'new_problems', $cobrand, $cobrand_data);
- } elsif ($type eq 'local') {
+ }
+ elsif ( $type eq 'local' ) {
my $feed = $c->req->param('feed');
my ( $type, @params );
- if ($feed =~ /^area:(?:\d+:)?(\d+)/) {
+ if ( $feed =~ /^area:(?:\d+:)?(\d+)/ ) {
$type = 'area_problems';
push @params, $1;
- } elsif ($feed =~ /^council:(\d+)/) {
+ }
+ elsif ( $feed =~ /^council:(\d+)/ ) {
$type = 'council_problems';
push @params, $1, $1;
- } elsif ($feed =~ /^ward:(\d+):(\d+)/) {
+ }
+ elsif ( $feed =~ /^ward:(\d+):(\d+)/ ) {
$type = 'ward_problems';
push @params, $1, $2;
- } elsif ($feed =~ m{ \A local: ( [\+\-]? \d+ \.? \d* ) : ( [\+\-]? \d+ \.? \d* ) }xms ) {
+ }
+ elsif ( $feed =~
+ m{ \A local: ( [\+\-]? \d+ \.? \d* ) : ( [\+\-]? \d+ \.? \d* ) }xms
+ )
+ {
$type = 'local_problems';
push @params, $1, $2;
+
# my $lat = $1;
# my $lon = $2;
# $alert_id = FixMyStreet::Alert::create($email, 'local_problems', $cobrand, $cobrand_data, $lon, $lat);
}
my $options = {
- email => $email,
- alert_type => $type
+ email => $email,
+ alert_type => $type
};
if ( scalar @params == 1 ) {
$options->{parameter} = $params[0];
- } elsif ( scalar @params == 2 ) {
- $options->{parameter} = $params[0];
+ }
+ elsif ( scalar @params == 2 ) {
+ $options->{parameter} = $params[0];
$options->{parameter2} = $params[1];
}
- my $alert = $c->model('DB::Alert')->find(
- $options
- );
+ $alert = $c->model('DB::Alert')->find( $options );
- unless ( $alert ) {
- $options->{cobrand} = $c->cobrand->moniker();
+ unless ($alert) {
+ $options->{cobrand} = $c->cobrand->moniker();
$options->{cobrand_data} = $c->cobrand->extra_update_data();
- $alert = $c->model('DB::Alert')->new( $options );
+ $alert = $c->model('DB::Alert')->new($options);
$alert->insert();
}
$c->log->debug( 'created alert ' . $alert->id );
$c->stash->{template} = 'email_sent.html';
- } else {
+ }
+ else {
throw FixMyStreet::Alert::Error('Invalid type');
}
-#
-# my %h = ();
-# $h{url} = Page::base_url_with_lang($q, undef, 1) . '/A/'
-# . mySociety::AuthToken::store('alert', { id => $alert_id, type => 'subscribe', email => $email } );
-# dbh()->commit();
-# return Page::send_confirmation_email($q, $email, undef, 'alert', %h);
+
+ my $token = $c->model("DB::Token")->create(
+ {
+ scope => 'alert',
+ data => { id => $alert->id, type => 'subscribe', email => $email }
+ }
+ );
+
+ $c->stash->{token_url} = $c->uri_for_email( '/A', $token->token );
+
+ my $sender = mySociety::Config::get('CONTACT_EMAIL');
+
+ $c->send_email(
+ 'alert-confirm.txt',
+ {
+ to => $email,
+ from => $sender
+ }
+ );
}
=head2 prettify_pc
diff --git a/t/app/controller/alert_new.t b/t/app/controller/alert_new.t
index 3d1bfeabf..72467d31c 100644
--- a/t/app/controller/alert_new.t
+++ b/t/app/controller/alert_new.t
@@ -2,82 +2,96 @@ use strict;
use warnings;
use Test::More;
-
use FixMyStreet::TestMech;
my $mech = FixMyStreet::TestMech->new;
-foreach my $test (
+foreach my $test (
{
- email => 'test@example.com',
- type => 'area',
- content => 'your alert will not be activated',
+ 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',
+ 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 => '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',
+ 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 => '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',
+ 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 => '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',
+ 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';
+ )
+{
+ subtest "$test->{type} alert correctly created" => sub {
+ $mech->clear_emails_ok;
- # we don't want an alert
- my $alert = FixMyStreet::App->model('DB::Alert')->find( { email =>
- $test->{email}, alert_type => $type } );
- $alert->delete() if $alert;
+ my $type = $test->{type} . '_problems';
- $mech->get_ok($test->{uri});
- $mech->content_contains($test->{content});
+ # we don't want an alert
+ my $alert = FixMyStreet::App->model('DB::Alert')->find(
+ {
+ email => $test->{email},
+ alert_type => $type
+ }
+ );
+ $alert->delete() if $alert;
- $alert = FixMyStreet::App->model('DB::Alert')->find( { email =>
- $test->{email}, alert_type => $type, parameter =>
- $test->{param1}, parameter2 => $test->{param2} } );
+ $mech->get_ok( $test->{uri} );
+ $mech->content_contains( $test->{content} );
- ok $alert, "Found the alert";
+ $alert = FixMyStreet::App->model('DB::Alert')->find(
+ {
+ email => $test->{email},
+ alert_type => $type,
+ parameter => $test->{param1},
+ parameter2 => $test->{param2}
+ }
+ );
- SKIP: {
- skip 'sending email not yet implemented', 4;
+ 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) = $email->body =~ m{(http://\S+)};
+ 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';
};
- };
}
done_testing();
diff --git a/templates/email/default/alert-confirm.txt b/templates/email/default/alert-confirm.txt
new file mode 100644
index 000000000..fae3f2963
--- /dev/null
+++ b/templates/email/default/alert-confirm.txt
@@ -0,0 +1,14 @@
+Subject: Confirm your alert on FixMyStreet
+
+Hi,
+
+Please click on the link below to confirm the alert you just
+asked to subscribe to on FixMyStreet:
+
+ [% token_url %]
+
+If you can't click the link, please copy and paste it to the
+address bar of your web browser.
+
+Yours,
+The FixMyStreet team