diff options
Diffstat (limited to 't/app/controller/admin.t')
-rw-r--r-- | t/app/controller/admin.t | 755 |
1 files changed, 550 insertions, 205 deletions
diff --git a/t/app/controller/admin.t b/t/app/controller/admin.t index 71a391c59..5f8abe5a6 100644 --- a/t/app/controller/admin.t +++ b/t/app/controller/admin.t @@ -6,28 +6,23 @@ use FixMyStreet::TestMech; my $mech = FixMyStreet::TestMech->new; -my $secret = FixMyStreet::App->model('DB::Secret')->search(); +my $user = $mech->create_user_ok('test@example.com', name => 'Test User'); -# don't explode if there's nothing in the secret table -if ( $secret == 0 ) { - diag "You need to put an entry in the secret table for the admin tests to run"; - plan skip_all => 'No entry in secret table'; -} +my $user2 = $mech->create_user_ok('test2@example.com', name => 'Test User 2'); + +my $superuser = $mech->create_user_ok('superuser@example.com', name => 'Super User', is_superuser => 1); -my $user = - FixMyStreet::App->model('DB::User') - ->find_or_create( { email => 'test@example.com', name => 'Test User' } ); -ok $user, "created test user"; +my $oxfordshire = $mech->create_body_ok(2237, 'Oxfordshire County Council', id => 2237); +my $oxfordshirecontact = $mech->create_contact_ok( body_id => $oxfordshire->id, category => 'Potholes', email => 'potholes@example.com' ); +$mech->create_contact_ok( body_id => $oxfordshire->id, category => 'Traffic lights', email => 'lights@example.com' ); +my $oxfordshireuser = $mech->create_user_ok('counciluser@example.com', name => 'Council User', from_body => $oxfordshire); -my $user2 = - FixMyStreet::App->model('DB::User') - ->find_or_create( { email => 'test2@example.com', name => 'Test User 2' } ); -ok $user2, "created second test user"; +my $oxford = $mech->create_body_ok(2421, 'Oxford City Council'); +$mech->create_contact_ok( body_id => $oxford->id, category => 'Graffiti', email => 'graffiti@example.net' ); +my $bromley = $mech->create_body_ok(2482, 'Bromley Council', id => 2482); -my $user3 = - FixMyStreet::App->model('DB::User') - ->find( { email => 'test3@example.com', name => 'Test User 2' } ); +my $user3 = $mech->create_user_ok('test3@example.com', name => 'Test User 2'); if ( $user3 ) { $mech->delete_user( $user3 ); @@ -45,7 +40,7 @@ my $dt = DateTime->new( my $report = FixMyStreet::App->model('DB::Problem')->find_or_create( { postcode => 'SW1A 1AA', - council => '2504', + bodies_str => '2504', areas => ',105255,11806,11828,2247,2504,', category => 'Other', title => 'Report to Edit', @@ -53,6 +48,7 @@ my $report = FixMyStreet::App->model('DB::Problem')->find_or_create( used_map => 't', name => 'Test User', anonymous => 'f', + external_id => '13', state => 'confirmed', confirmed => $dt->ymd . ' ' . $dt->hms, lang => 'en-gb', @@ -69,13 +65,15 @@ my $report = FixMyStreet::App->model('DB::Problem')->find_or_create( my $alert = FixMyStreet::App->model('DB::Alert')->find_or_create( { - alert_type => 'new_updates', - parameter => $report->id, + alert_type => 'area_problems', + parameter => 2482, confirmed => 1, user => $user, }, ); +$mech->log_in_ok( $superuser->email ); + subtest 'check summary counts' => sub { my $problems = FixMyStreet::App->model('DB::Problem')->search( { state => { -in => [qw/confirmed fixed closed investigating planned/, 'in progress', 'fixed - user', 'fixed - council'] } } ); @@ -84,16 +82,20 @@ subtest 'check summary counts' => sub { my $problem_count = $problems->count; $problems->update( { cobrand => '' } ); - FixMyStreet::App->model('DB::Problem')->search( { council => 2489 } )->update( { council => 1 } ); + FixMyStreet::App->model('DB::Problem')->search( { bodies_str => 2489 } )->update( { bodies_str => 1 } ); my $q = FixMyStreet::App->model('DB::Questionnaire')->find_or_new( { problem => $report, }); - $q->whensent( \'ms_current_timestamp()' ); + $q->whensent( \'current_timestamp' ); $q->in_storage ? $q->update : $q->insert; my $alerts = FixMyStreet::App->model('DB::Alert')->search( { confirmed => { '>' => 0 } } ); my $a_count = $alerts->count; - $mech->get_ok('/admin'); + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'fixmystreet' ], + }, sub { + $mech->get_ok('/admin'); + }; $mech->title_like(qr/Summary/); @@ -105,11 +107,10 @@ subtest 'check summary counts' => sub { $mech->content_contains( "$q_count questionnaires sent" ); - SKIP: { - skip( "Need 'barnet' in ALLOWED_COBRANDS config", 7 ) - unless FixMyStreet::Cobrand->exists('barnet'); - - ok $mech->host('barnet.fixmystreet.com'); + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'oxfordshire' ], + }, sub { + ok $mech->host('oxfordshire.fixmystreet.com'); $mech->get_ok('/admin'); $mech->title_like(qr/Summary/); @@ -118,11 +119,11 @@ subtest 'check summary counts' => sub { my ($num_alerts) = $mech->content =~ /(\d+) confirmed alerts/; my ($num_qs) = $mech->content =~ /(\d+) questionnaires sent/; - $report->council(2489); - $report->cobrand('barnet'); + $report->bodies_str(2237); + $report->cobrand('oxfordshire'); $report->update; - $alert->cobrand('barnet'); + $alert->cobrand('oxfordshire'); $alert->update; $mech->get_ok('/admin'); @@ -131,48 +132,56 @@ subtest 'check summary counts' => sub { $mech->content_contains( ($num_alerts+1) . " confirmed alerts" ); $mech->content_contains( ($num_qs+1) . " questionnaires sent" ); - $report->council(2504); + $report->bodies_str(2504); $report->cobrand(''); $report->update; $alert->cobrand(''); $alert->update; - } + }; - FixMyStreet::App->model('DB::Problem')->search( { council => 1 } )->update( { council => 2489 } ); - ok $mech->host('fixmystreet.com'); + FixMyStreet::App->model('DB::Problem')->search( { bodies_str => 1 } )->update( { bodies_str => 2489 } ); + ok $mech->host('www.fixmystreet.com'); }; -my $host = FixMyStreet->config('BASE_URL'); -$mech->get_ok('/admin/council_contacts/2650'); +# This override is wrapped around ALL the /admin/body tests +FixMyStreet::override_config { + MAPIT_URL => 'http://mapit.uk/', + MAPIT_TYPES => [ 'UTA' ], + BASE_URL => 'http://www.example.org', +}, sub { + +my $body = $mech->create_body_ok(2650, 'Aberdeen City Council'); +$mech->get_ok('/admin/body/' . $body->id); $mech->content_contains('Aberdeen City Council'); $mech->content_like(qr{AB\d\d}); -$mech->content_contains("$host/around"); +$mech->content_contains("http://www.example.org/around"); subtest 'check contact creation' => sub { my $contact = FixMyStreet::App->model('DB::Contact')->search( - { body_id => 2650, category => [ 'test category', 'test/category' ] } + { body_id => $body->id, category => [ 'test category', 'test/category' ] } ); $contact->delete_all; my $history = FixMyStreet::App->model('DB::ContactsHistory')->search( - { body_id => 2650, category => [ 'test category', 'test/category' ] } + { body_id => $body->id, category => [ 'test category', 'test/category' ] } ); $history->delete_all; - $mech->get_ok('/admin/council_contacts/2650'); + $mech->get_ok('/admin/body/' . $body->id); $mech->submit_form_ok( { with_fields => { category => 'test category', email => 'test@example.com', note => 'test note', non_public => undef, + confirmed => 0, } } ); $mech->content_contains( 'test category' ); - $mech->content_contains( '<td>test@example.com' ); + $mech->content_contains( 'test@example.com' ); $mech->content_contains( '<td>test note' ); - $mech->content_contains( '<td>Public' ); + $mech->content_contains( 'Private: No' ); $mech->submit_form_ok( { with_fields => { category => 'private category', @@ -182,7 +191,7 @@ subtest 'check contact creation' => sub { } } ); $mech->content_contains( 'private category' ); - $mech->content_contains( '<td>Non Public' ); + $mech->content_contains( 'Private: Yes' ); $mech->submit_form_ok( { with_fields => { category => 'test/category', @@ -190,57 +199,67 @@ subtest 'check contact creation' => sub { note => 'test/note', non_public => 'on', } } ); - $mech->get_ok('/admin/council_edit/2650/test/category'); + $mech->get_ok('/admin/body/' . $body->id . '/test/category'); }; subtest 'check contact editing' => sub { - $mech->get_ok('/admin/council_edit/2650/test%20category'); + $mech->get_ok('/admin/body/' . $body->id .'/test%20category'); - $mech->submit_form_ok( { with_fields => { + $mech->submit_form_ok( { with_fields => { email => 'test2@example.com', note => 'test2 note', non_public => undef, } } ); $mech->content_contains( 'test category' ); - $mech->content_contains( '<td>test2@example.com' ); + $mech->content_contains( 'test2@example.com' ); $mech->content_contains( '<td>test2 note' ); - $mech->content_contains( '<td>Public' ); + $mech->content_contains( 'Private: No' ); - $mech->submit_form_ok( { with_fields => { + $mech->get_ok('/admin/body/' . $body->id . '/test%20category'); + $mech->submit_form_ok( { with_fields => { + email => 'test2@example.com, test3@example.com', + note => 'test3 note', + } } ); + + $mech->content_contains( 'test2@example.com,test3@example.com' ); + + $mech->get_ok('/admin/body/' . $body->id . '/test%20category'); + $mech->content_contains( '<td><strong>test2@example.com,test3@example.com' ); + + $mech->get_ok('/admin/body/' . $body->id . '/test%20category'); + $mech->submit_form_ok( { with_fields => { email => 'test2@example.com', note => 'test2 note', non_public => 'on', } } ); - $mech->content_contains( '<td>Non Public' ); + $mech->content_contains( 'Private: Yes' ); - $mech->get_ok('/admin/council_edit/2650/test%20category'); + $mech->get_ok('/admin/body/' . $body->id . '/test%20category'); $mech->content_contains( '<td><strong>test2@example.com' ); }; subtest 'check contact updating' => sub { - $mech->get_ok('/admin/council_edit/2650/test%20category'); + $mech->get_ok('/admin/body/' . $body->id . '/test%20category'); $mech->content_like(qr{test2\@example.com</strong>[^<]*</td>[^<]*<td>No}s); - $mech->get_ok('/admin/council_contacts/2650'); + $mech->get_ok('/admin/body/' . $body->id); $mech->form_number( 1 ); $mech->tick( 'confirmed', 'test category' ); $mech->submit_form_ok({form_number => 1}); - $mech->content_like(qr'test2@example.com</td>[^<]*<td>Yes's); - $mech->get_ok('/admin/council_edit/2650/test%20category'); + $mech->content_like(qr'test2@example.com</td>[^<]*<td>\s*Confirmed: Yes's); + $mech->get_ok('/admin/body/' . $body->id . '/test%20category'); $mech->content_like(qr{test2\@example.com[^<]*</td>[^<]*<td><strong>Yes}s); }; -my $open311 = - FixMyStreet::App->model('DB::Body')->search( { area_id => 2650 } ); -$open311->delete if $open311; +$body->update({ send_method => undef }); subtest 'check open311 configuring' => sub { - $mech->get_ok('/admin/council_contacts/2650/'); + $mech->get_ok('/admin/body/' . $body->id); $mech->content_lacks('Council contacts configured via Open311'); $mech->form_number(3); @@ -256,13 +275,9 @@ subtest 'check open311 configuring' => sub { } ); $mech->content_contains('Council contacts configured via Open311'); - $mech->content_contains('Configuration updated - contacts will be generated automatically later'); - - $open311 = - FixMyStreet::App->model('DB::Body')->search( { area_id => 2650 } ); + $mech->content_contains('Values updated'); - is $open311->count, 1, 'only one configuration'; - my $conf = $open311->first; + my $conf = FixMyStreet::App->model('DB::Body')->find( $body->id ); is $conf->endpoint, 'http://example.com/open311', 'endpoint configured'; is $conf->api_key, 'api key', 'api key configured'; is $conf->jurisdiction, 'mySociety', 'jurisdiction configures'; @@ -280,24 +295,24 @@ subtest 'check open311 configuring' => sub { } ); - $mech->content_contains('Configuration updated'); + $mech->content_contains('Values updated'); - $open311 = - FixMyStreet::App->model('DB::Body')->search( { area_id => 2650 } ); - - is $open311->count, 1, 'only one configuration'; - $conf = $open311->first; + $conf = FixMyStreet::App->model('DB::Body')->find( $body->id ); is $conf->endpoint, 'http://example.org/open311', 'endpoint updated'; is $conf->api_key, 'new api key', 'api key updated'; is $conf->jurisdiction, 'open311', 'jurisdiction configures'; }; subtest 'check text output' => sub { - $mech->get_ok('/admin/council_contacts/2650?text=1'); + $mech->get_ok('/admin/body/' . $body->id . '?text=1'); is $mech->content_type, 'text/plain'; $mech->content_contains('test category'); }; + +}; # END of override wrap + + my $log_entries = FixMyStreet::App->model('DB::AdminLog')->search( { object_type => 'problem', @@ -327,7 +342,6 @@ foreach my $test ( non_public => undef, }, changes => { title => 'Edited Report', }, - log_count => 1, log_entries => [qw/edit/], resend => 0, }, @@ -344,7 +358,6 @@ foreach my $test ( non_public => undef, }, changes => { detail => 'Edited Detail', }, - log_count => 2, log_entries => [qw/edit edit/], resend => 0, }, @@ -361,7 +374,6 @@ foreach my $test ( non_public => undef, }, changes => { name => 'Edited User', }, - log_count => 3, log_entries => [qw/edit edit edit/], resend => 0, user => $user, @@ -381,7 +393,6 @@ foreach my $test ( changes => { flagged => 'on', }, - log_count => 4, log_entries => [qw/edit edit edit edit/], resend => 0, user => $user, @@ -399,7 +410,6 @@ foreach my $test ( non_public => undef, }, changes => { email => $user2->email, }, - log_count => 5, log_entries => [qw/edit edit edit edit edit/], resend => 0, user => $user2, @@ -417,8 +427,7 @@ foreach my $test ( non_public => undef, }, changes => { state => 'unconfirmed' }, - log_count => 6, - log_entries => [qw/state_change edit edit edit edit edit/], + log_entries => [qw/edit state_change edit edit edit edit edit/], resend => 0, }, { @@ -434,8 +443,7 @@ foreach my $test ( non_public => undef, }, changes => { state => 'confirmed' }, - log_count => 7, - log_entries => [qw/state_change state_change edit edit edit edit edit/], + log_entries => [qw/edit state_change edit state_change edit edit edit edit edit/], resend => 0, }, { @@ -451,9 +459,8 @@ foreach my $test ( non_public => undef, }, changes => { state => 'fixed' }, - log_count => 8, log_entries => - [qw/state_change state_change state_change edit edit edit edit edit/], + [qw/edit state_change edit state_change edit state_change edit edit edit edit edit/], resend => 0, }, { @@ -469,9 +476,8 @@ foreach my $test ( non_public => undef, }, changes => { state => 'hidden' }, - log_count => 9, log_entries => [ - qw/state_change state_change state_change state_change edit edit edit edit edit/ + qw/edit state_change edit state_change edit state_change edit state_change edit edit edit edit edit/ ], resend => 0, }, @@ -491,9 +497,8 @@ foreach my $test ( state => 'confirmed', anonymous => 1, }, - log_count => 11, log_entries => [ - qw/edit state_change state_change state_change state_change state_change edit edit edit edit edit/ + qw/edit state_change edit state_change edit state_change edit state_change edit state_change edit edit edit edit edit/ ], resend => 0, }, @@ -510,9 +515,8 @@ foreach my $test ( non_public => undef, }, changes => {}, - log_count => 12, log_entries => [ - qw/resend edit state_change state_change state_change state_change state_change edit edit edit edit edit/ + qw/resend edit state_change edit state_change edit state_change edit state_change edit state_change edit edit edit edit edit/ ], resend => 1, }, @@ -531,9 +535,8 @@ foreach my $test ( changes => { non_public => 'on', }, - log_count => 13, log_entries => [ - qw/edit resend edit state_change state_change state_change state_change state_change edit edit edit edit edit/ + qw/edit resend edit state_change edit state_change edit state_change edit state_change edit state_change edit edit edit edit edit/ ], resend => 0, }, @@ -543,6 +546,7 @@ foreach my $test ( $log_entries->reset; $mech->get_ok("/admin/report_edit/$report_id"); + @{$test->{fields}}{'external_id', 'external_body', 'external_team', 'category'} = (13, "", "", "Other"); is_deeply( $mech->visible_form_values(), $test->{fields}, 'initial form values' ); my $new_fields = { @@ -557,13 +561,13 @@ foreach my $test ( } is_deeply( $mech->visible_form_values(), $new_fields, 'changed form values' ); - is $log_entries->count, $test->{log_count}, 'log entry count'; + is $log_entries->count, scalar @{$test->{log_entries}}, 'log entry count'; is $log_entries->next->action, $_, 'log entry added' for @{ $test->{log_entries} }; $report->discard_changes; - if ( $report->state eq 'confirmed' ) { - $mech->content_contains( 'type="submit" name="resend"', 'no resend button' ); + if ($report->state eq 'confirmed' && $report->whensent) { + $mech->content_contains( 'type="submit" name="resend"', 'resend button' ); } else { $mech->content_lacks( 'type="submit" name="resend"', 'no resend button' ); } @@ -584,6 +588,33 @@ foreach my $test ( }; } +FixMyStreet::override_config { + ALLOWED_COBRANDS => 'fixmystreet', +}, sub { + +subtest 'change report category' => sub { + my ($ox_report) = $mech->create_problems_for_body(1, $oxfordshire->id, 'Unsure', { + category => 'Potholes', + areas => ',2237,2421,', # Cached used by categories_for_point... + latitude => 51.7549262252, + longitude => -1.25617899435, + whensent => \'current_timestamp', + }); + $mech->get_ok("/admin/report_edit/" . $ox_report->id); + + $mech->submit_form_ok( { with_fields => { category => 'Traffic lights' } }, 'form_submitted' ); + $ox_report->discard_changes; + is $ox_report->category, 'Traffic lights'; + isnt $ox_report->whensent, undef; + + $mech->submit_form_ok( { with_fields => { category => 'Graffiti' } }, 'form_submitted' ); + $ox_report->discard_changes; + is $ox_report->category, 'Graffiti'; + is $ox_report->whensent, undef; +}; + +}; + subtest 'change email to new user' => sub { $log_entries->delete; $mech->get_ok("/admin/report_edit/$report_id"); @@ -593,9 +624,13 @@ subtest 'change email to new user' => sub { state => $report->state, name => $report->name, email => $report->user->email, + category => 'Other', anonymous => 1, flagged => 'on', non_public => 'on', + external_id => '13', + external_body => '', + external_team => '', }; is_deeply( $mech->visible_form_values(), $fields, 'initial form values' ); @@ -845,6 +880,10 @@ for my $test ( }; } +my $westminster = $mech->create_body_ok(2504, 'Westminster City Council'); +$report->bodies_str($westminster->id); +$report->update; + for my $test ( { desc => 'user is problem owner', @@ -853,18 +892,18 @@ for my $test ( update_fixed => 0, update_reopen => 0, update_state => undef, - user_council => undef, + user_body => undef, content => 'user is problem owner', }, { - desc => 'user is council user', + desc => 'user is body user', problem_user => $user, update_user => $user2, update_fixed => 0, update_reopen => 0, update_state => undef, - user_council => 2504, - content => 'user is from same council as problem - 2504', + user_body => $westminster->id, + content => 'user is from same council as problem - ' . $westminster->id, }, { desc => 'update changed problem state', @@ -873,7 +912,7 @@ for my $test ( update_fixed => 0, update_reopen => 0, update_state => 'planned', - user_council => 2504, + user_body => $westminster->id, content => 'Update changed problem state to planned', }, { @@ -883,7 +922,7 @@ for my $test ( update_fixed => 1, update_reopen => 0, update_state => undef, - user_council => undef, + user_body => undef, content => 'Update marked problem as fixed', }, { @@ -893,7 +932,7 @@ for my $test ( update_fixed => 0, update_reopen => 1, update_state => undef, - user_council => undef, + user_body => undef, content => 'Update reopened problem', }, ) { @@ -907,7 +946,7 @@ for my $test ( $update->mark_open( $test->{update_reopen} ); $update->update; - $test->{update_user}->from_body( $test->{user_council} ); + $test->{update_user}->from_body( $test->{user_body} ); $test->{update_user}->update; $mech->get_ok('/admin/update_edit/' . $update->id ); @@ -1033,41 +1072,47 @@ subtest 'report search' => sub { $update->user($report->user); $update->update; - $mech->get_ok('/admin/search_reports'); - $mech->get_ok('/admin/search_reports?search=' . $report->id ); + $mech->get_ok('/admin/reports'); + $mech->get_ok('/admin/reports?search=' . $report->id ); $mech->content_contains( $report->title ); my $r_id = $report->id; - $mech->content_like( qr{href="http://[^/]*[^.]/report/$r_id/">$r_id</a>} ); + $mech->content_like( qr{href="http://[^/]*[^.]/report/$r_id"[^>]*>$r_id</a>} ); - $mech->get_ok('/admin/search_reports?search=' . $report->user->email); + $mech->get_ok('/admin/reports?search=' . $report->external_id); + $mech->content_like( qr{href="http://[^/]*[^.]/report/$r_id"[^>]*>$r_id</a>} ); + + $mech->get_ok('/admin/reports?search=ref:' . $report->external_id); + $mech->content_like( qr{href="http://[^/]*[^.]/report/$r_id"[^>]*>$r_id</a>} ); + + $mech->get_ok('/admin/reports?search=' . $report->user->email); my $u_id = $update->id; - $mech->content_like( qr{href="http://[^/]*[^.]/report/$r_id/">$r_id</a>} ); - $mech->content_like( qr{href="http://[^/]*[^.]/report/$r_id/#update_$u_id">$u_id</a>} ); + $mech->content_like( qr{href="http://[^/]*[^.]/report/$r_id"[^>]*>$r_id</a>} ); + $mech->content_like( qr{href="http://[^/]*[^.]/report/$r_id#update_$u_id"[^>]*>$u_id</a>} ); $update->state('hidden'); $update->update; - $mech->get_ok('/admin/search_reports?search=' . $report->user->email); + $mech->get_ok('/admin/reports?search=' . $report->user->email); $mech->content_like( qr{<tr [^>]*hidden[^>]*> \s* <td> \s* $u_id \s* </td>}xs ); $report->state('hidden'); $report->update; - $mech->get_ok('/admin/search_reports?search=' . $report->user->email); - $mech->content_like( qr{<tr [^>]*hidden[^>]*> \s* <td> \s* $r_id \s* </td>}xs ); + $mech->get_ok('/admin/reports?search=' . $report->user->email); + $mech->content_like( qr{<tr [^>]*hidden[^>]*> \s* <td[^>]*> \s* $r_id \s* </td>}xs ); $report->state('fixed - user'); $report->update; - $mech->get_ok('/admin/search_reports?search=' . $report->user->email); - $mech->content_like( qr{href="http://[^/]*[^.]/report/$r_id/">$r_id</a>} ); + $mech->get_ok('/admin/reports?search=' . $report->user->email); + $mech->content_like( qr{href="http://[^/]*[^.]/report/$r_id"[^>]*>$r_id</a>} ); }; subtest 'search abuse' => sub { - $mech->get_ok( '/admin/search_users?search=example' ); - $mech->content_like(qr/test4\@example.com.*\n.*\n.*Email in abuse table/); + $mech->get_ok( '/admin/users?search=example' ); + $mech->content_like(qr{test4\@example.com.*</td>\s*<td>.*?</td>\s*<td>\(Email in abuse table}s); }; subtest 'show flagged entries' => sub { @@ -1077,27 +1122,53 @@ subtest 'show flagged entries' => sub { $user->flagged( 1 ); $user->update; - $mech->get_ok('/admin/list_flagged'); + $mech->get_ok('/admin/flagged'); $mech->content_contains( $report->title ); $mech->content_contains( $user->email ); }; +my $haringey = $mech->create_body_ok(2509, 'Haringey Borough Council'); + subtest 'user search' => sub { - $mech->get_ok('/admin/search_users'); - $mech->get_ok('/admin/search_users?search=' . $user->name); + $mech->get_ok('/admin/users'); + $mech->get_ok('/admin/users?search=' . $user->name); $mech->content_contains( $user->name); my $u_id = $user->id; $mech->content_like( qr{user_edit/$u_id">Edit</a>} ); - $mech->get_ok('/admin/search_users?search=' . $user->email); + $mech->get_ok('/admin/users?search=' . $user->email); $mech->content_like( qr{user_edit/$u_id">Edit</a>} ); - $user->from_body(2509); + $user->from_body($haringey->id); $user->update; - $mech->get_ok('/admin/search_users?search=2509' ); - $mech->content_contains(2509); + $mech->get_ok('/admin/users?search=' . $haringey->id ); + $mech->content_contains('Haringey'); +}; + +subtest 'search does not show user from another council' => sub { + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'oxfordshire' ], + }, sub { + $mech->get_ok('/admin/users'); + $mech->get_ok('/admin/users?search=' . $user->name); + + $mech->content_contains( "Searching found no users." ); + + $mech->get_ok('/admin/users?search=' . $user->email); + $mech->content_contains( "Searching found no users." ); + }; +}; + +subtest 'user_edit does not show user from another council' => sub { + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'oxfordshire' ], + }, sub { + $mech->get('/admin/user_edit/' . $user->id); + ok !$mech->res->is_success(), "want a bad response"; + is $mech->res->code, 404, "got 404"; + }; }; $log_entries = FixMyStreet::App->model('DB::AdminLog')->search( @@ -1115,101 +1186,375 @@ is $log_entries->count, 0, 'no admin log entries'; $user->flagged( 0 ); $user->update; -for my $test ( - { - desc => 'edit user name', - fields => { - name => 'Test User', - email => 'test@example.com', - council => 2509, - flagged => undef, - }, - changes => { - name => 'Changed User', - }, - log_count => 1, - log_entries => [qw/edit/], - }, - { - desc => 'edit user email', - fields => { - name => 'Changed User', - email => 'test@example.com', - council => 2509, - flagged => undef, - }, - changes => { - email => 'changed@example.com', +my $southend = $mech->create_body_ok(2607, 'Southend-on-Sea Borough Council'); + +my %default_perms = ( + "permissions[moderate]" => undef, + "permissions[planned_reports]" => undef, + "permissions[report_edit]" => undef, + "permissions[report_edit_category]" => undef, + "permissions[report_edit_priority]" => undef, + "permissions[report_inspect]" => undef, + "permissions[report_instruct]" => undef, + "permissions[contribute_as_another_user]" => undef, + "permissions[contribute_as_body]" => undef, + "permissions[view_body_contribute_details]" => undef, + "permissions[user_edit]" => undef, + "permissions[user_manage_permissions]" => undef, + "permissions[user_assign_body]" => undef, + "permissions[user_assign_areas]" => undef, + "permissions[template_edit]" => undef, + "permissions[responsepriority_edit]" => undef, + "permissions[category_edit]" => undef, + trusted_bodies => undef, +); + +FixMyStreet::override_config { + MAPIT_URL => 'http://mapit.uk/', +}, sub { + for my $test ( + { + desc => 'edit user name', + fields => { + name => 'Test User', + email => 'test@example.com', + body => $haringey->id, + phone => '', + flagged => undef, + is_superuser => undef, + area_id => '', + %default_perms, + }, + changes => { + name => 'Changed User', + }, + log_count => 1, + log_entries => [qw/edit/], }, - log_count => 2, - log_entries => [qw/edit edit/], - }, - { - desc => 'edit user council', - fields => { - name => 'Changed User', - email => 'changed@example.com', - council => 2509, - flagged => undef, + { + desc => 'edit user email', + fields => { + name => 'Changed User', + email => 'test@example.com', + body => $haringey->id, + phone => '', + flagged => undef, + is_superuser => undef, + area_id => '', + %default_perms, + }, + changes => { + email => 'changed@example.com', + }, + log_count => 2, + log_entries => [qw/edit edit/], }, - changes => { - council => 2607, + { + desc => 'edit user body', + fields => { + name => 'Changed User', + email => 'changed@example.com', + body => $haringey->id, + phone => '', + flagged => undef, + is_superuser => undef, + area_id => '', + %default_perms, + }, + changes => { + body => $southend->id, + }, + log_count => 3, + log_entries => [qw/edit edit edit/], }, - log_count => 3, - log_entries => [qw/edit edit edit/], - }, - { - desc => 'edit user flagged', - fields => { - name => 'Changed User', - email => 'changed@example.com', - council => 2607, - flagged => undef, + { + desc => 'edit user flagged', + fields => { + name => 'Changed User', + email => 'changed@example.com', + body => $southend->id, + phone => '', + flagged => undef, + is_superuser => undef, + area_id => '', + %default_perms, + }, + changes => { + flagged => 'on', + }, + log_count => 4, + log_entries => [qw/edit edit edit edit/], }, - changes => { - flagged => 'on', + { + desc => 'edit user remove flagged', + fields => { + name => 'Changed User', + email => 'changed@example.com', + body => $southend->id, + phone => '', + flagged => 'on', + is_superuser => undef, + area_id => '', + %default_perms, + }, + changes => { + flagged => undef, + }, + log_count => 4, + log_entries => [qw/edit edit edit edit/], }, - log_count => 4, - log_entries => [qw/edit edit edit edit/], - }, - { - desc => 'edit user remove flagged', - fields => { - name => 'Changed User', - email => 'changed@example.com', - council => 2607, - flagged => 'on', + { + desc => 'edit user add is_superuser', + fields => { + name => 'Changed User', + email => 'changed@example.com', + body => $southend->id, + phone => '', + flagged => undef, + is_superuser => undef, + area_id => '', + %default_perms, + }, + changes => { + is_superuser => 'on', + }, + removed => [ + keys %default_perms, + ], + log_count => 5, + log_entries => [qw/edit edit edit edit edit/], }, - changes => { - flagged => undef, + { + desc => 'edit user remove is_superuser', + fields => { + name => 'Changed User', + email => 'changed@example.com', + body => $southend->id, + phone => '', + flagged => undef, + is_superuser => 'on', + area_id => '', + }, + changes => { + is_superuser => undef, + }, + added => { + %default_perms, + }, + log_count => 5, + log_entries => [qw/edit edit edit edit edit/], }, - log_count => 4, - log_entries => [qw/edit edit edit edit/], - }, -) { - subtest $test->{desc} => sub { - $mech->get_ok( '/admin/user_edit/' . $user->id ); + ) { + subtest $test->{desc} => sub { + $mech->get_ok( '/admin/user_edit/' . $user->id ); - my $visible = $mech->visible_form_values; - is_deeply $visible, $test->{fields}, 'expected user'; + my $visible = $mech->visible_form_values; + is_deeply $visible, $test->{fields}, 'expected user'; - my $expected = { - %{ $test->{fields} }, - %{ $test->{changes} } + my $expected = { + %{ $test->{fields} }, + %{ $test->{changes} } + }; + + $mech->submit_form_ok( { with_fields => $expected } ); + + # Some actions cause visible fields to be added/removed + foreach my $x (@{ $test->{removed} }) { + delete $expected->{$x}; + } + if ( $test->{added} ) { + $expected = { + %$expected, + %{ $test->{added} } + }; + } + + $visible = $mech->visible_form_values; + is_deeply $visible, $expected, 'user updated'; + + $mech->content_contains( 'Updated!' ); }; + } +}; - $mech->submit_form_ok( { with_fields => $expected } ); +subtest "Test setting a report from unconfirmed to something else doesn't cause a front end error" => sub { + $report->update( { confirmed => undef, state => 'unconfirmed', non_public => 0 } ); + $mech->get_ok("/admin/report_edit/$report_id"); + $mech->submit_form_ok( { with_fields => { state => 'investigating' } } ); + $report->discard_changes; + ok( $report->confirmed, 'report has a confirmed timestamp' ); + $mech->get_ok("/report/$report_id"); +}; + +subtest "Check admin_base_url" => sub { + my $rs = FixMyStreet::App->model('DB::Problem'); + my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker($report->cobrand)->new(); + + is ($report->admin_url($cobrand), + (sprintf 'http://www.example.org/admin/report_edit/%d', $report_id), + 'get_admin_url OK'); +}; + +# Finished with the superuser tests +$mech->log_out_ok; + +subtest "Users without from_body can't access admin" => sub { + $user->from_body( undef ); + $user->update; + + $mech->log_in_ok( $user->email ); - $visible = $mech->visible_form_values; - is_deeply $visible, $expected, 'user updated'; + ok $mech->get('/admin'); + is $mech->res->code, 403, "got 403"; - $mech->content_contains( 'Updated!' ); + $mech->log_out_ok; +}; + +subtest "Users with from_body can access their own council's admin" => sub { + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'oxfordshire' ], + }, sub { + $mech->log_in_ok( $oxfordshireuser->email ); + + $mech->get_ok('/admin'); + $mech->content_contains( 'FixMyStreet admin:' ); + + $mech->log_out_ok; }; -} +}; + +subtest "Users with from_body can't access another council's admin" => sub { + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'bristol' ], + }, sub { + $mech->log_in_ok( $oxfordshireuser->email ); + + ok $mech->get('/admin'); + is $mech->res->code, 403, "got 403"; -$mech->delete_user( $user ); -$mech->delete_user( $user2 ); -$mech->delete_user( $user3 ); -$mech->delete_user( 'test4@example.com' ); + $mech->log_out_ok; + }; +}; + +subtest "Users with from_body can't access fixmystreet.com admin" => sub { + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'fixmystreet' ], + }, sub { + $mech->log_in_ok( $oxfordshireuser->email ); + + ok $mech->get('/admin'); + is $mech->res->code, 403, "got 403"; + + $mech->log_out_ok; + }; +}; + +subtest "response templates can be added" => sub { + is $oxfordshire->response_templates->count, 0, "No response templates yet"; + $mech->log_in_ok( $superuser->email ); + $mech->get_ok( "/admin/templates/" . $oxfordshire->id . "/new" ); + + my $fields = { + title => "Report acknowledgement", + text => "Thank you for your report. We will respond shortly.", + auto_response => undef, + "contacts[".$oxfordshirecontact->id."]" => 1, + }; + $mech->submit_form_ok( { with_fields => $fields } ); + + is $oxfordshire->response_templates->count, 1, "Response template was added"; +}; + +subtest "response templates are included on page" => sub { + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'oxfordshire' ], + }, sub { + $report->update({ category => $oxfordshirecontact->category, bodies_str => $oxfordshire->id }); + $mech->log_in_ok( $oxfordshireuser->email ); + + $mech->get_ok("/report/" . $report->id); + $mech->content_contains( $oxfordshire->response_templates->first->text ); + + $mech->log_out_ok; + }; +}; + +$mech->log_in_ok( $superuser->email ); -done_testing(); +subtest "response priorities can be added" => sub { + is $oxfordshire->response_priorities->count, 0, "No response priorities yet"; + $mech->get_ok( "/admin/responsepriorities/" . $oxfordshire->id . "/new" ); + + my $fields = { + name => "Cat 1A", + description => "Fixed within 24 hours", + deleted => undef, + "contacts[".$oxfordshirecontact->id."]" => 1, + }; + $mech->submit_form_ok( { with_fields => $fields } ); + + is $oxfordshire->response_priorities->count, 1, "Response template was added to body"; + is $oxfordshirecontact->response_priorities->count, 1, "Response template was added to contact"; +}; + +subtest "response priorities can be listed" => sub { + $mech->get_ok( "/admin/responsepriorities/" . $oxfordshire->id ); + + $mech->content_contains( $oxfordshire->response_priorities->first->name ); + $mech->content_contains( $oxfordshire->response_priorities->first->description ); +}; + +subtest "response priorities are limited by body" => sub { + my $bromleypriority = $bromley->response_priorities->create( { + deleted => 0, + name => "Bromley Cat 0", + } ); + + is $bromley->response_priorities->count, 1, "Response template was added to Bromley"; + is $oxfordshire->response_priorities->count, 1, "Response template wasn't added to Oxfordshire"; + + $mech->get_ok( "/admin/responsepriorities/" . $oxfordshire->id ); + $mech->content_lacks( $bromleypriority->name ); + + $mech->get_ok( "/admin/responsepriorities/" . $bromley->id ); + $mech->content_contains( $bromleypriority->name ); +}; + +$mech->log_out_ok; + +subtest "response priorities can't be viewed across councils" => sub { + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'oxfordshire' ], + }, sub { + $oxfordshireuser->user_body_permissions->create({ + body => $oxfordshire, + permission_type => 'responsepriority_edit', + }); + $mech->log_in_ok( $oxfordshireuser->email ); + $mech->get_ok( "/admin/responsepriorities/" . $oxfordshire->id ); + $mech->content_contains( $oxfordshire->response_priorities->first->name ); + + + $mech->get( "/admin/responsepriorities/" . $bromley->id ); + ok !$mech->res->is_success(), "want a bad response"; + is $mech->res->code, 404, "got 404"; + + my $bromley_priority_id = $bromley->response_priorities->first->id; + $mech->get( "/admin/responsepriorities/" . $bromley->id . "/" . $bromley_priority_id ); + ok !$mech->res->is_success(), "want a bad response"; + is $mech->res->code, 404, "got 404"; + }; +}; + +END { + $mech->delete_user( $user ); + $mech->delete_user( $user2 ); + $mech->delete_user( $user3 ); + $mech->delete_user( $superuser ); + $mech->delete_user( 'test4@example.com' ); + $mech->delete_body( $oxfordshire ); + $mech->delete_body( $oxford ); + $mech->delete_body( $bromley ); + $mech->delete_body( $westminster ); + done_testing(); +} |