diff options
author | Dave Arter <davea@mysociety.org> | 2016-04-12 11:54:55 +0100 |
---|---|---|
committer | Dave Arter <davea@mysociety.org> | 2016-07-08 15:28:21 +0100 |
commit | 7f7e030db5f676e8a89abde5f62f84f084609381 (patch) | |
tree | 37b4712d4e67391985e391214e479c3233eb0703 | |
parent | a145cd1dd0613cc141e541dfc9a3c9a1756161f5 (diff) |
Refactor sending of problem confirmation email
Replaced duplicate code blocks that are responsible for sending
confirmation of problem email with a function. Should make it
easier to do two-tier conditional messages later on. Also fixes a
minor bug where the wrong confirmation email would be sent for
reports from the mobile app to unresponsive councils.
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Report/New.pm | 61 | ||||
-rw-r--r-- | t/app/controller/report_new.t | 38 |
2 files changed, 67 insertions, 32 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Report/New.pm b/perllib/FixMyStreet/App/Controller/Report/New.pm index c4a2162a8..e81dc719f 100644 --- a/perllib/FixMyStreet/App/Controller/Report/New.pm +++ b/perllib/FixMyStreet/App/Controller/Report/New.pm @@ -143,22 +143,11 @@ sub report_new_ajax : Path('mobile') : Args(0) { $c->forward('save_user_and_report'); my $report = $c->stash->{report}; - my $data = $c->stash->{token_data} || {}; - my $token = $c->model("DB::Token")->create( { - scope => 'problem', - data => { - %$data, - id => $report->id - } - } ); if ( $report->confirmed ) { $c->forward( 'create_reporter_alert' ); $c->stash->{ json_response } = { success => 1, report => $report->id }; } else { - $c->stash->{token_url} = $c->uri_for_email( '/P', $token->token ); - $c->send_email( 'problem-confirm.txt', { - to => [ $report->name ? [ $report->user->email, $report->name ] : $report->user->email ], - } ); + $c->forward( 'send_problem_confirm_email' ); $c->stash->{ json_response } = { success => 1 }; } @@ -1021,6 +1010,31 @@ sub tokenize_user : Private { if $c->get_param('oauth_need_email') && $c->session->{oauth}{twitter_id}; } +sub send_problem_confirm_email : Private { + my ( $self, $c ) = @_; + my $data = $c->stash->{token_data} || {}; + my $report = $c->stash->{report}; + my $token = $c->model("DB::Token")->create( { + scope => 'problem', + data => { + %$data, + id => $report->id + } + } ); + + my $template = 'problem-confirm.txt'; + $template = 'problem-confirm-not-sending.txt' unless $report->bodies_str; + + $c->stash->{token_url} = $c->uri_for_email( '/P', $token->token ); + if ($c->cobrand->can('problem_confirm_email_extras')) { + $c->cobrand->problem_confirm_email_extras($report); + } + + $c->send_email( $template, { + to => [ $report->name ? [ $report->user->email, $report->name ] : $report->user->email ], + } ); +} + =head2 save_user_and_report Save the user and the report. @@ -1178,30 +1192,13 @@ sub redirect_or_confirm_creation : Private { return 1; } - my $template = 'problem-confirm.txt'; - $template = 'problem-confirm-not-sending.txt' unless $report->bodies_str; - - # otherwise create a confirm token and email it to them. - my $data = $c->stash->{token_data} || {}; - my $token = $c->model("DB::Token")->create( { - scope => 'problem', - data => { - %$data, - id => $report->id - } - } ); - $c->stash->{token_url} = $c->uri_for_email( '/P', $token->token ); - if ($c->cobrand->can('problem_confirm_email_extras')) { - $c->cobrand->problem_confirm_email_extras($report); - } - $c->send_email( $template, { - to => [ $report->name ? [ $report->user->email, $report->name ] : $report->user->email ], - } ); + # otherwise email a confirm token to them. + $c->forward( 'send_problem_confirm_email' ); # tell user that they've been sent an email $c->stash->{template} = 'email_sent.html'; $c->stash->{email_type} = 'problem'; - $c->log->info($report->user->id . ' created ' . $report->id . ', email sent, ' . ($data->{password} ? 'password set' : 'password not set')); + $c->log->info($report->user->id . ' created ' . $report->id . ', email sent, ' . ($c->stash->{token_data}->{password} ? 'password set' : 'password not set')); } sub create_reporter_alert : Private { diff --git a/t/app/controller/report_new.t b/t/app/controller/report_new.t index ba550193e..94d34a93d 100644 --- a/t/app/controller/report_new.t +++ b/t/app/controller/report_new.t @@ -1542,6 +1542,44 @@ subtest "unresponsive body handling works" => sub { like $email->body, qr/despite not being sent/i, "correct email sent"; $user->problems->delete; + $mech->clear_emails_ok; + + # Make sure the same behaviour occurs for reports from the mobile app + $mech->log_out_ok; + $mech->post_ok( '/report/new/mobile', { + title => "Test Report at café", + detail => 'Test report details.', + photo1 => '', + name => 'Joe Bloggs', + email => $test_email, + may_show_name => '1', + phone => '07903 123 456', + category => 'Trees', + service => 'iOS', + lat => 55.9, + lon => -3.2, + pc => '', + used_map => '1', + submit_register => '1', + password_register => '', + }); + my $res = $mech->response; + ok $res->header('Content-Type') =~ m{^application/json\b}, 'response should be json'; + + $user = FixMyStreet::App->model('DB::User')->find( { email => $test_email } ); + ok $user, "test user does exist"; + + $report = $user->problems->first; + ok $report, "Found the report"; + is $report->bodies_str, undef, "Report not going anywhere"; + + $email = $mech->get_email; + ok $email, "got an email"; + like $email->body, qr/despite not being sent/i, "correct email sent"; + + $user->problems->delete; + $mech->clear_emails_ok; + $contact1->body->update( { send_method => $old_send } ); # And test per-category refusing |