aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDave Arter <davea@mysociety.org>2020-08-24 16:34:59 +0100
committerDave Arter <davea@mysociety.org>2020-08-24 17:14:05 +0100
commitd9cf470f321dc334391a35405a527521a3b605d5 (patch)
tree12744ac9a12117bb548f3911fb255173b6498045
parent51252c8ad919aa7fe0f8c8fe7ac8afadb6de5b18 (diff)
[Hackney] Allow editing & addition of contacts with special destination addresses
This adds a cobrand hook to validate the email address provided when editing a contact in the admin.
-rw-r--r--perllib/FixMyStreet/App/Controller/Admin/Bodies.pm4
-rw-r--r--perllib/FixMyStreet/Cobrand/Hackney.pm26
-rw-r--r--t/Mock/MapIt.pm2
-rw-r--r--t/cobrand/hackney.t27
4 files changed, 54 insertions, 5 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Admin/Bodies.pm b/perllib/FixMyStreet/App/Controller/Admin/Bodies.pm
index 52306af24..f88ad0051 100644
--- a/perllib/FixMyStreet/App/Controller/Admin/Bodies.pm
+++ b/perllib/FixMyStreet/App/Controller/Admin/Bodies.pm
@@ -255,7 +255,9 @@ sub update_contact : Private {
$email =~ s/\s+//g;
my $send_method = $c->get_param('send_method') || $contact->body->send_method || "";
my $email_unchanged = $contact->email && $email && $contact->email eq $email;
- unless ( $send_method eq 'Open311' || $email_unchanged ) {
+ my $cobrand = $contact->body->get_cobrand_handler;
+ my $cobrand_valid = $cobrand && $cobrand->call_hook(validate_contact_email => $email);
+ unless ( $send_method eq 'Open311' || $email_unchanged || $cobrand_valid ) {
$errors{email} = _('Please enter a valid email') unless is_valid_email_list($email) || $email eq 'REFUSED';
}
diff --git a/perllib/FixMyStreet/Cobrand/Hackney.pm b/perllib/FixMyStreet/Cobrand/Hackney.pm
index 234573e3e..459cf3e4d 100644
--- a/perllib/FixMyStreet/Cobrand/Hackney.pm
+++ b/perllib/FixMyStreet/Cobrand/Hackney.pm
@@ -3,7 +3,7 @@ use parent 'FixMyStreet::Cobrand::Whitelabel';
use strict;
use warnings;
-use mySociety::EmailUtil qw(is_valid_email);
+use mySociety::EmailUtil qw(is_valid_email is_valid_email_list);
sub council_area_id { return 2508; }
sub council_area { return 'Hackney'; }
@@ -155,9 +155,7 @@ sub get_body_sender {
my $contact = $body->contacts->search( { category => $problem->category } )->first;
- my $parts = join '\s*', qw(^ park : (.*?) ; estate : (.*?) ; other : (.*?) $);
- my $regex = qr/$parts/i;
- if (my ($park, $estate, $other) = $contact->email =~ $regex) {
+ if (my ($park, $estate, $other) = $self->_split_emails($contact->email)) {
my $to = $other;
if ($self->problem_is_within_area_type($problem, 'park')) {
$to = $park;
@@ -184,4 +182,24 @@ sub munge_sendreport_params {
}
}
+sub _split_emails {
+ my ($self, $email) = @_;
+
+ my $parts = join '\s*', qw(^ park : (.*?) ; estate : (.*?) ; other : (.*?) $);
+ my $regex = qr/$parts/i;
+
+ my ($park, $estate, $other) = $email =~ $regex;
+ return ($park, $estate, $other);
+}
+
+sub validate_contact_email {
+ my ( $self, $email ) = @_;
+
+ return 1 if is_valid_email_list($email);
+
+ my @emails = grep { $_ } $self->_split_emails($email);
+ return unless @emails;
+ return 1 if is_valid_email_list(join(",", @emails));
+}
+
1;
diff --git a/t/Mock/MapIt.pm b/t/Mock/MapIt.pm
index 760e0ba52..74e360974 100644
--- a/t/Mock/MapIt.pm
+++ b/t/Mock/MapIt.pm
@@ -122,6 +122,8 @@ sub dispatch_request {
$self->output({2326 => {parent_area => undef, id => 2326, name => "Cheltenham Borough Council", type => "DIS"}});
} elsif ($areas eq 'UTA') {
$self->output({2650 => {parent_area => undef, id => 2650, name => "Aberdeen Council", type => "UTA"}});
+ } elsif ($areas eq 'DIS,LBO,MTD,UTA,CTY,COI,LGD') {
+ $self->output({2508 => {parent_area => undef, id => 2508, name => "Hackney Council", type => "LBO"}});
} elsif ($areas eq 'GRE') {
$self->output({2493 => {parent_area => undef, id => 2493, name => "Greenwich Borough Council", type => "LBO"}});
} elsif ($areas eq 'LBO') {
diff --git a/t/cobrand/hackney.t b/t/cobrand/hackney.t
index 374bf2ea9..6ab47a7ce 100644
--- a/t/cobrand/hackney.t
+++ b/t/cobrand/hackney.t
@@ -48,6 +48,10 @@ $hackney_user->user_body_permissions->create({
body => $hackney,
permission_type => 'moderate',
});
+$hackney_user->user_body_permissions->create({
+ body => $hackney,
+ permission_type => 'category_edit',
+});
my $contact2 = $mech->create_contact_ok(
body_id => $hackney->id,
@@ -291,5 +295,28 @@ subtest "check category extra uses correct name" => sub {
like $extra_details->{category_extra}, qr/Hackney Council/, 'correct name in category extras';
};
+subtest "can edit special destination email addresses" => sub {
+ FixMyStreet::override_config {
+ MAPIT_URL => 'http://mapit.uk/',
+ ALLOWED_COBRANDS => ['hackney'],
+ COBRAND_FEATURES => { anonymous_account => { hackney => 'anonymous' } },
+ }, sub {
+ $contact2->update({ send_method => 'Email' });
+ $mech->log_in_ok( $hackney_user->email );
+ $mech->get_ok("/admin/body/" . $hackney->id . "/" . $contact2->category);
+ $mech->submit_form_ok( { with_fields => { email => 'park:parks@example.com;estate:estates@example;other:new@example.org' } },
+ "submit valid new email address");
+ $mech->content_lacks("Please enter a valid email");
+ $contact2->discard_changes;
+ is $contact2->email, 'park:parks@example.com;estate:estates@example;other:new@example.org', "New email addresses saved";
+
+ $mech->get_ok("/admin/body/" . $hackney->id . "/" . $contact2->category);
+ $mech->submit_form_ok( { with_fields => { email => 'invalid' } },
+ "submit invalid new email address");
+ $mech->content_contains("Please enter a valid email");
+ $contact2->discard_changes;
+ is $contact2->email, 'park:parks@example.com;estate:estates@example;other:new@example.org', "Invalid email addresses not saved";
+ };
+};
done_testing();