diff options
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Alert.pm | 31 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Comment.pm | 30 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Problem.pm | 32 | ||||
-rw-r--r-- | perllib/FixMyStreet/Roles/Abuser.pm | 29 | ||||
-rw-r--r-- | t/app/controller/alert_new.t | 79 |
5 files changed, 129 insertions, 72 deletions
diff --git a/perllib/FixMyStreet/DB/Result/Alert.pm b/perllib/FixMyStreet/DB/Result/Alert.pm index d780e91fd..e0017b94d 100644 --- a/perllib/FixMyStreet/DB/Result/Alert.pm +++ b/perllib/FixMyStreet/DB/Result/Alert.pm @@ -57,32 +57,10 @@ __PACKAGE__->belongs_to( # You can replace this text with custom code or comments, and it will be preserved on regeneration -# FIXME: this is more or less duplicated from problem. need to stick somewhere common +use Moose; +use namespace::clean -except => [ 'meta' ]; - -=head2 is_from_abuser - - $bool = $alert->is_from_abuser( ); - -Returns true if the user's email or its domain is listed in the 'abuse' table. - -=cut - -sub is_from_abuser { - my $self = shift; - - # get the domain - my $email = $self->user->email; - my ($domain) = $email =~ m{ @ (.*) \z }x; - - # search for an entry in the abuse table - my $abuse_rs = $self->result_source->schema->resultset('Abuse'); - - return - $abuse_rs->find( { email => $email } ) - || $abuse_rs->find( { email => $domain } ) - || undef; -} +with 'FixMyStreet::Roles::Abuser'; =head2 confirm @@ -103,4 +81,7 @@ sub confirm { return 1; } +# need the inline_constuctor bit as we don't inherit from Moose +__PACKAGE__->meta->make_immutable( inline_constructor => 0 ); + 1; diff --git a/perllib/FixMyStreet/DB/Result/Comment.pm b/perllib/FixMyStreet/DB/Result/Comment.pm index 9b5feb37d..4c70dd902 100644 --- a/perllib/FixMyStreet/DB/Result/Comment.pm +++ b/perllib/FixMyStreet/DB/Result/Comment.pm @@ -72,6 +72,11 @@ __PACKAGE__->belongs_to( # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:71bSUgPf3uW607g2EGl/Vw use DateTime::TimeZone; +use Moose; +use namespace::clean -except => [ 'meta' ]; + +with 'FixMyStreet::Roles::Abuser'; + my $tz = DateTime::TimeZone->new( name => "local" ); sub created_local { @@ -134,28 +139,7 @@ sub get_photo_params { return $photo; } -=head2 is_from_abuser - - $bool = $update->is_from_abuser( ); - -Returns true if the user's email or its domain is listed in the 'abuse' table. - -=cut - -sub is_from_abuser { - my $self = shift; - - # get the domain - my $email = $self->user->email; - my ($domain) = $email =~ m{ @ (.*) \z }x; - - # search for an entry in the abuse table - my $abuse_rs = $self->result_source->schema->resultset('Abuse'); - - return - $abuse_rs->find( { email => $email } ) - || $abuse_rs->find( { email => $domain } ) - || undef; -} +# we need the inline_constructor bit as we don't inherit from Moose +__PACKAGE__->meta->make_immutable( inline_constructor => 0 ); 1; diff --git a/perllib/FixMyStreet/DB/Result/Problem.pm b/perllib/FixMyStreet/DB/Result/Problem.pm index 25cf6e8b2..c1ecd6576 100644 --- a/perllib/FixMyStreet/DB/Result/Problem.pm +++ b/perllib/FixMyStreet/DB/Result/Problem.pm @@ -104,6 +104,11 @@ __PACKAGE__->has_many( # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:U3aYCRwE4etekKaHdhEkIw use DateTime::TimeZone; +use Moose; +use namespace::clean -except => [ 'meta' ]; + +with 'FixMyStreet::Roles::Abuser'; + my $tz = DateTime::TimeZone->new( name => "local" ); sub confirmed_local { @@ -178,30 +183,6 @@ sub check_for_errors { return \%errors; } -=head2 is_from_abuser - - $bool = $problem->is_from_abuser( ); - -Returns true if the user's email or its domain is listed in the 'abuse' table. - -=cut - -sub is_from_abuser { - my $self = shift; - - # get the domain - my $email = $self->user->email; - my ($domain) = $email =~ m{ @ (.*) \z }x; - - # search for an entry in the abuse table - my $abuse_rs = $self->result_source->schema->resultset('Abuse'); - - return - $abuse_rs->find( { email => $email } ) - || $abuse_rs->find( { email => $domain } ) - || undef; -} - =head2 confirm $bool = $problem->confirm( ); @@ -366,4 +347,7 @@ sub duration_string { ); } +# we need the inline_constructor bit as we don't inherit from Moose +__PACKAGE__->meta->make_immutable( inline_constructor => 0 ); + 1; diff --git a/perllib/FixMyStreet/Roles/Abuser.pm b/perllib/FixMyStreet/Roles/Abuser.pm new file mode 100644 index 000000000..b9e951305 --- /dev/null +++ b/perllib/FixMyStreet/Roles/Abuser.pm @@ -0,0 +1,29 @@ +package FixMyStreet::Roles::Abuser; + +use Moose::Role; + +=head2 is_from_abuser + + $bool = $alert->is_from_abuser( ); + +Returns true if the user's email or its domain is listed in the 'abuse' table. + +=cut + +sub is_from_abuser { + my $self = shift; + + # get the domain + my $email = $self->user->email; + my ($domain) = $email =~ m{ @ (.*) \z }x; + + # search for an entry in the abuse table + my $abuse_rs = $self->result_source->schema->resultset('Abuse'); + + return + $abuse_rs->find( { email => $email } ) + || $abuse_rs->find( { email => $domain } ) + || undef; +} + +1; diff --git a/t/app/controller/alert_new.t b/t/app/controller/alert_new.t index 4b23cb950..3d06594de 100644 --- a/t/app/controller/alert_new.t +++ b/t/app/controller/alert_new.t @@ -250,4 +250,83 @@ foreach my $test ( }; } +for my $test ( + { + email => 'test@example.com', + type => 'new_updates', + content => 'your alert will not be activated', + email_text => 'confirm the alert', + uri => '/alert/subscribe?type=updates&rznvy=test@example.com&id=1', + param1 => 1, + } + ) +{ + subtest "cannot sign up for alert if in abuse table" => sub { + $mech->clear_emails_ok; + + my $type = $test->{type}; + + 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); + } + + my $abuse = + FixMyStreet::App->model('DB::Abuse') + ->find_or_create( { email => $test->{email} } ); + + $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; + + $mech->get_ok("/A/$url_token"); + $mech->content_contains('error confirming'); + + $alert->discard_changes; + + ok !$alert->confirmed, 'alert not set to confirmed'; + + $abuse->delete; + }; +} done_testing(); |