diff options
author | Dave Arter <davea@mysociety.org> | 2019-04-05 18:49:32 +0100 |
---|---|---|
committer | Dave Arter <davea@mysociety.org> | 2019-06-06 11:59:21 +0100 |
commit | ff3b747698f577876bff25512ac137c6677ccab7 (patch) | |
tree | 8d11c4376cbb2d7a60b79c94fc9827ef3c417d81 /t/app/controller | |
parent | 1a1be49646218b2217e25e82e6666749f78dc612 (diff) |
[Hounslow] Add general enquiries functionality
This functionality allows a cobrand to replace the /contact form
with a form that creates hidden reports which are sent via Open311.
The form also allows file uploads in addition to photos.
This functionality is currently enabled for the Hounslow cobrand and
others cobrands can enable it by defining setup_general_enquiries_stash
which primarily sets up the appropriate categories and default values
for the report.
Diffstat (limited to 't/app/controller')
-rw-r--r-- | t/app/controller/contact.t | 31 | ||||
-rw-r--r-- | t/app/controller/contact_enquiry.t | 226 | ||||
-rw-r--r-- | t/app/controller/sample.pdf | bin | 0 -> 12501 bytes |
3 files changed, 257 insertions, 0 deletions
diff --git a/t/app/controller/contact.t b/t/app/controller/contact.t index 842f27dd5..c74aaa5ff 100644 --- a/t/app/controller/contact.t +++ b/t/app/controller/contact.t @@ -6,6 +6,16 @@ sub abuse_reports_only { 1; } 1; +package FixMyStreet::Cobrand::GeneralEnquiries; + +use base 'FixMyStreet::Cobrand::Default'; + +sub abuse_reports_only { 1 } + +sub setup_general_enquiries_stash { 1 } + +1; + package main; use FixMyStreet::TestMech; @@ -492,6 +502,27 @@ subtest 'check can limit contact to abuse reports' => sub { } }; +subtest 'check redirected to correct form for general enquiries cobrand' => sub { + FixMyStreet::override_config { + 'ALLOWED_COBRANDS' => [ 'generalenquiries' ], + }, sub { + $mech->get( '/contact' ); + is $mech->res->code, 200, "got 200 for final destination"; + is $mech->res->previous->code, 302, "got 302 for redirect"; + is $mech->uri->path, '/contact/enquiry'; + + $mech->get_ok( '/contact?id=' . $problem_main->id, 'can visit for abuse report' ); + + my $token = FixMyStreet::App->model("DB::Token")->create({ + scope => 'moderation', + data => { id => $problem_main->id } + }); + + $mech->get_ok( '/contact?m=' . $token->token, 'can visit for moderation complaint' ); + + } +}; + $problem_main->delete; done_testing(); diff --git a/t/app/controller/contact_enquiry.t b/t/app/controller/contact_enquiry.t new file mode 100644 index 000000000..fe3962c8e --- /dev/null +++ b/t/app/controller/contact_enquiry.t @@ -0,0 +1,226 @@ +use FixMyStreet::TestMech; +use Path::Tiny; +use File::Temp 'tempdir'; + +# disable info logs for this test run +FixMyStreet::App->log->disable('info'); +END { FixMyStreet::App->log->enable('info'); } + +my $mech = FixMyStreet::TestMech->new; + +my $body = $mech->create_body_ok( 2483, 'Hounslow Borough Council' ); +my $contact = $mech->create_contact_ok( + body_id => $body->id, + category => 'General Enquiry', + email => 'genenq@example.com', + non_public => 1, +); +my $contact2 = $mech->create_contact_ok( + body_id => $body->id, + category => 'FOI Request', + email => 'foi@example.com', + non_public => 1, +); +my $contact3 = $mech->create_contact_ok( + body_id => $body->id, + category => 'Other', + email => 'other@example.com', +); +my $contact4 = $mech->create_contact_ok( + body_id => $body->id, + category => 'Carriageway Defect', + email => 'potholes@example.com', +); +$contact->update( { extra => { group => 'General Enquiries' } } ); +$contact2->update( { extra => { group => 'General Enquiries' } } ); +$contact3->update( { extra => { group => 'Other' } } ); + +FixMyStreet::override_config { ALLOWED_COBRANDS => ['bromley'], }, sub { + subtest 'redirected to / if general enquiries not enabled' => sub { + $mech->get( '/contact/enquiry' ); + is $mech->res->code, 200, "got 200 for final destination"; + is $mech->res->previous->code, 302, "got 302 for redirect"; + is $mech->uri->path, '/'; + }; +}; + +FixMyStreet::override_config { + ALLOWED_COBRANDS => ['hounslow'], + MAPIT_URL => 'http://mapit.uk/', +}, sub { + subtest 'Non-general enquiries category not shown' => sub { + $mech->get_ok( '/contact/enquiry' ); + $mech->content_lacks('Carriageway Defect'); + $mech->content_contains('FOI Request'); + }; + + subtest 'Enquiry can be submitted when logged out' => sub { + my $problems = FixMyStreet::App->model('DB::Problem')->to_body( $body->id ); + + $mech->get_ok( '/contact/enquiry' ); + $mech->submit_form_ok( { + with_fields => { + name => 'Test User', + username => 'testuser@example.org', + category => 'Other', + detail => 'This is a general enquiry', + } + } ); + is $mech->uri->path, '/contact/enquiry/submit'; + $mech->content_contains("Thank you for your enquiry"); + + is $problems->count, 1, 'problem created'; + my $problem = $problems->first; + is $problem->category, 'Other', 'problem has correct category'; + is $problem->detail, 'This is a general enquiry', 'problem has correct detail'; + is $problem->non_public, 1, 'problem created non_public'; + is $problem->postcode, ''; + is $problem->used_map, 0; + is $problem->latitude, 51.469, 'Problem has correct latitude'; + is $problem->longitude, -0.35, 'Problem has correct longitude'; + ok $problem->confirmed, 'problem confirmed'; + is $problem->user->name, undef, 'User created without name'; + is $problem->name, 'Test User', 'Report created with correct name'; + is $problem->user->email, 'testuser@example.org', 'User created with correct email'; + }; + + subtest 'Enquiry can be submitted when logged in' => sub { + my $problems = FixMyStreet::App->model('DB::Problem')->to_body( $body->id ); + my $user = $problems->first->user; + $problems->delete_all; + + $mech->log_in_ok( $user->email ); + + $mech->get_ok( '/contact/enquiry' ); + $mech->submit_form_ok( { + with_fields => { + name => 'Test User', + category => 'FOI Request', + detail => 'This is a general enquiry', + } + } ); + is $mech->uri->path, '/contact/enquiry/submit'; + $mech->content_contains("Thank you for your enquiry"); + + is $problems->count, 1, 'problem created'; + my $problem = $problems->first; + is $problem->category, 'FOI Request', 'problem has correct category'; + is $problem->detail, 'This is a general enquiry', 'problem has correct detail'; + is $problem->non_public, 1, 'problem created non_public'; + is $problem->postcode, ''; + is $problem->used_map, 0; + is $problem->latitude, 51.469, 'Problem has correct latitude'; + is $problem->longitude, -0.35, 'Problem has correct longitude'; + ok $problem->confirmed, 'problem confirmed'; + is $problem->name, 'Test User', 'Report created with correct name'; + is $problem->user->name, 'Test User', 'User name updated in DB'; + is $problem->user->email, 'testuser@example.org', 'Report user has correct email'; + + $mech->log_out_ok; + }; + + subtest 'User name not changed if logged out when making report' => sub { + my $problems = FixMyStreet::App->model('DB::Problem')->to_body( $body->id ); + my $user = $problems->first->user; + $problems->delete_all; + + is $user->name, 'Test User', 'User has correct name'; + + $mech->get_ok( '/contact/enquiry' ); + $mech->submit_form_ok( { + with_fields => { + name => 'Simon Neil', + username => 'testuser@example.org', + category => 'General Enquiry', + detail => 'This is a general enquiry', + } + } ); + is $mech->uri->path, '/contact/enquiry/submit'; + $mech->content_contains("Thank you for your enquiry"); + + is $problems->count, 1, 'problem created'; + my $problem = $problems->first; + is $problem->category, 'General Enquiry', 'problem has correct category'; + is $problem->detail, 'This is a general enquiry', 'problem has correct detail'; + is $problem->non_public, 1, 'problem created non_public'; + is $problem->postcode, ''; + is $problem->used_map, 0; + is $problem->latitude, 51.469, 'Problem has correct latitude'; + is $problem->longitude, -0.35, 'Problem has correct longitude'; + ok $problem->confirmed, 'problem confirmed'; + is $problem->name, 'Simon Neil', 'Report created with correct name'; + is $problem->user->email, 'testuser@example.org', 'Report user has correct email'; + $user->discard_changes; + is $user->name, 'Test User', 'User name in DB not changed'; + + $mech->log_out_ok; + }; + +}; + +my $sample_jpeg = path(__FILE__)->parent->child("sample.jpg"); +ok $sample_jpeg->exists, "sample image $sample_jpeg exists"; +my $sample_pdf = path(__FILE__)->parent->child("sample.pdf"); +ok $sample_pdf->exists, "sample PDF $sample_pdf exists"; + +subtest "Check photo & file upload works" => sub { + my $UPLOAD_DIR = tempdir( CLEANUP => 1 ); + + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'hounslow' ], + MAPIT_URL => 'http://mapit.uk/', + PHOTO_STORAGE_BACKEND => 'FileSystem', + PHOTO_STORAGE_OPTIONS => { + UPLOAD_DIR => $UPLOAD_DIR, + }, + }, sub { + my $problems = FixMyStreet::App->model('DB::Problem')->to_body( $body->id ); + $problems->delete_all; + + + $mech->get_ok('/contact/enquiry'); + my ($csrf) = $mech->content =~ /name="token" value="([^"]*)"/; + + $mech->post( '/contact/enquiry/submit', + Content_Type => 'form-data', + Content => + { + submit_problem => 1, + token => $csrf, + name => 'Test User', + username => 'testuser@example.org', + category => 'Other', + detail => 'This is a general enquiry', + photo1 => [ $sample_jpeg, undef, Content_Type => 'image/jpeg' ], + photo2 => [ $sample_pdf, undef, Content_Type => 'application/pdf' ], + } + ); + ok $mech->success, 'Made request with two files uploaded'; + + is $problems->count, 1, 'problem created'; + my $problem = $problems->first; + is $problem->detail, 'This is a general enquiry', 'problem has correct detail'; + is $problem->non_public, 1, 'problem created non_public'; + is $problem->postcode, ''; + is $problem->used_map, 0; + is $problem->latitude, 51.469, 'Problem has correct latitude'; + is $problem->longitude, -0.35, 'Problem has correct longitude'; + ok $problem->confirmed, 'problem confirmed'; + + my $image_file = path($UPLOAD_DIR, '74e3362283b6ef0c48686fb0e161da4043bbcc97.jpeg'); + ok $image_file->exists, 'Photo uploaded to temp'; + + my $photoset = $problem->get_photoset(); + is $photoset->num_images, 1, 'Found just 1 image'; + is $photoset->data, '74e3362283b6ef0c48686fb0e161da4043bbcc97.jpeg'; + + my $pdf_file = path($UPLOAD_DIR, 'enquiry_files', '90f7a64043fb458d58de1a0703a6355e2856b15e.pdf'); + ok $pdf_file->exists, 'PDF uploaded to temp'; + + is_deeply $problem->get_extra_metadata('enquiry_files'), { + '90f7a64043fb458d58de1a0703a6355e2856b15e.pdf' => 'sample.pdf' + }, 'enquiry file info stored OK'; + + }; +}; +done_testing(); diff --git a/t/app/controller/sample.pdf b/t/app/controller/sample.pdf Binary files differnew file mode 100644 index 000000000..9ac5be44b --- /dev/null +++ b/t/app/controller/sample.pdf |