use Test::MockTime ':all';
package FixMyStreet::Cobrand::No2FA;
use parent 'FixMyStreet::Cobrand::FixMyStreet';
sub must_have_2fa { 0 }
package FixMyStreet::Cobrand::Tester;
use parent 'FixMyStreet::Cobrand::Default';
# Allow access if CSV export for a body, otherwise deny
sub dashboard_permission {
my $self = shift;
my $c = $self->{c};
return 0 unless $c->get_param('export');
return $c->get_param('body') || 0;
}
package main;
use strict;
use warnings;
use FixMyStreet::TestMech;
use File::Temp 'tempdir';
use Path::Tiny;
use Web::Scraper;
set_absolute_time('2014-02-01T12:00:00');
my $mech = FixMyStreet::TestMech->new;
my $other_body = $mech->create_body_ok(1234, 'Some Other Council');
my $body = $mech->create_body_ok(2651, 'City of Edinburgh Council');
my @cats = ('Litter', 'Other', 'Potholes', 'Traffic lights');
for my $contact ( @cats ) {
my $c = $mech->create_contact_ok(body_id => $body->id, category => $contact, email => "$contact\@example.org");
if ($contact eq 'Potholes') {
$c->set_extra_metadata(group => ['Road']);
$c->update;
}
}
my $superuser = $mech->create_user_ok('superuser@example.com', name => 'Super User', is_superuser => 1);
my $counciluser = $mech->create_user_ok('counciluser@example.com', name => 'Council User', from_body => $body);
my $normaluser = $mech->create_user_ok('normaluser@example.com', name => 'Normal User');
my $body_id = $body->id;
my $area_id = '60705';
my $alt_area_id = '62883';
my $last_month = DateTime->now->subtract(months => 2);
$mech->create_problems_for_body(2, $body->id, 'Title', { areas => ",$area_id,2651,", category => 'Potholes', cobrand => 'no2fat' });
$mech->create_problems_for_body(3, $body->id, 'Title', { areas => ",$area_id,2651,", category => 'Traffic lights', cobrand => 'no2fa', dt => $last_month });
$mech->create_problems_for_body(1, $body->id, 'Title', { areas => ",$alt_area_id,2651,", category => 'Litter', cobrand => 'no2fa' });
my @scheduled_problems = $mech->create_problems_for_body(7, $body->id, 'Title', { areas => ",$area_id,2651,", category => 'Traffic lights', cobrand => 'no2fa' });
my @fixed_problems = $mech->create_problems_for_body(4, $body->id, 'Title', { areas => ",$area_id,2651,", category => 'Potholes', cobrand => 'no2fa' });
my @closed_problems = $mech->create_problems_for_body(3, $body->id, 'Title', { areas => ",$area_id,2651,", category => 'Traffic lights', cobrand => 'no2fa' });
my $first_problem_id;
my $first_update_id;
foreach my $problem (@scheduled_problems) {
$problem->update({ state => 'action scheduled' });
my ($update) = $mech->create_comment_for_problem($problem, $counciluser, 'Title', 'text', 0, 'confirmed', 'action scheduled');
$first_problem_id = $problem->id unless $first_problem_id;
$first_update_id = $update->id unless $first_update_id;
}
foreach my $problem (@fixed_problems) {
$problem->update({ state => 'fixed - council' });
$mech->create_comment_for_problem($problem, $counciluser, 'Title', 'text', 0, 'confirmed', 'fixed');
}
foreach my $problem (@closed_problems) {
$problem->update({ state => 'closed' });
$mech->create_comment_for_problem($problem, $counciluser, 'Name', 'in progress text', 0, 'confirmed', 'in progress');
$mech->create_comment_for_problem($problem, $counciluser, 'Title', 'text', 0, 'confirmed', 'closed');
}
my $categories = scraper {
process "select[name=category] option", 'cats[]' => 'TEXT',
process "table[id=overview] > tr", 'rows[]' => scraper {
process 'td', 'cols[]' => 'TEXT'
},
};
my $UPLOAD_DIR = tempdir( CLEANUP => 1 );
FixMyStreet::override_config {
ALLOWED_COBRANDS => 'no2fa',
COBRAND_FEATURES => { category_groups => { no2fa => 1 } },
MAPIT_URL => 'http://mapit.uk/',
PHOTO_STORAGE_OPTIONS => {
UPLOAD_DIR => $UPLOAD_DIR,
},
}, sub {
subtest 'not logged in, redirected to login' => sub {
$mech->not_logged_in_ok;
$mech->get_ok('/dashboard');
$mech->content_contains( 'sign in' );
};
subtest 'normal user, 404' => sub {
$mech->log_in_ok( $normaluser->email );
$mech->get('/dashboard');
is $mech->status, '404', 'If not council user get 404';
};
subtest 'superuser, body list' => sub {
$mech->log_in_ok( $superuser->email );
$mech->get_ok('/dashboard');
# Contains body name, in list of bodies
$mech->content_contains('Some Other Council');
$mech->content_contains('Edinburgh Council');
$mech->content_lacks('Category:');
$mech->get_ok('/dashboard?body=' . $body->id);
$mech->content_lacks('Some Other Council');
$mech->content_contains('Edinburgh Council');
$mech->content_contains('Trowbridge');
$mech->content_contains('Category:');
};
subtest 'council user, ward list' => sub {
$mech->log_in_ok( $counciluser->email );
$mech->get_ok('/dashboard');
$mech->content_lacks('Some Other Council');
$mech->content_contains('Edinburgh Council');
$mech->content_contains('Trowbridge');
$mech->content_contains('Category:');
};
subtest 'area user can only see their area' => sub {
$counciluser->update({area_ids => [ $area_id ]});
$mech->get_ok("/dashboard");
$mech->content_contains('
Trowbridge
');
$mech->get_ok("/dashboard?body=" . $other_body->id);
$mech->content_contains('Trowbridge
');
$mech->get_ok("/dashboard?ward=$alt_area_id");
$mech->content_contains('Trowbridge
');
$counciluser->update({area_ids => [ $area_id, $alt_area_id ]});
$mech->get_ok("/dashboard");
$mech->content_contains('Bradford-on-Avon / Trowbridge
');
$counciluser->update({area_ids => undef});
};
subtest 'The correct categories and totals shown by default' => sub {
$mech->get_ok("/dashboard");
my $expected_cats = [ 'All', 'Litter', 'Other', 'Traffic lights', 'Potholes' ];
my $res = $categories->scrape( $mech->content );
$mech->content_contains('