diff options
-rw-r--r-- | perllib/FixMyStreet/App/Controller/JSON.pm | 53 | ||||
-rw-r--r-- | perllib/FixMyStreet/TestMech.pm | 25 | ||||
-rw-r--r-- | t/app/controller/json.t | 75 |
3 files changed, 138 insertions, 15 deletions
diff --git a/perllib/FixMyStreet/App/Controller/JSON.pm b/perllib/FixMyStreet/App/Controller/JSON.pm index a0e5490cb..c437aafc0 100644 --- a/perllib/FixMyStreet/App/Controller/JSON.pm +++ b/perllib/FixMyStreet/App/Controller/JSON.pm @@ -47,23 +47,56 @@ sub json : Path : Args(0) { return; } + # check that the dates are sane + if ( $start_dt > $end_dt ) { + $c->stash->{error} = 'Start date after end date'; + return; + } + # check that the type is supported unless ( $type eq 'new_problems' || $type eq 'fixed_problems' ) { $c->stash->{error} = 'Invalid type supplied'; return; } - my $response = $c->stash->{response} ||= {}; - - # elsif ( $type eq 'new_problems' ) { - # $problems = Problems::created_in_interval( $start_date, $end_date ); - # } - # elsif ( $type eq 'fixed_problems' ) { - # $problems = Problems::fixed_in_interval( $start_date, $end_date ); - # } - + # query the database + $c->stash->{response} = + $type eq 'new_problems' + ? Problems::created_in_interval( $start_date, $end_date ) + : Problems::fixed_in_interval( $start_date, $end_date ); } +# If we convert this code to be fully DBIC based then the following snippet is a +# good start. The roadblock to doing it fully is the 'site_restriction' in the +# SQL which is currently provided as SQL, rather than something that could be +# easily added to the DBIC query. The hardest cobrand to change would be the +# cities - so perhaps do it after we know wether that needs to be kept or not. +# +# my $state = +# $type eq 'new_problems' ? 'confirmed' +# : $type eq 'fixed_problems' ? 'fixed_problems' +# : die; +# +# my $one_day = DateTime::Duration->new( days => 1 ); +# +# my $problems = $c->model('DB::Problem')->search( +# { +# created => { +# '>=' => $start_dt, +# '<=' => $end_dt + $one_day, +# }, +# state => $state, +# # ------ add is site_restriction here ------- +# }, +# { +# columns => [ +# 'id', 'title', 'council', 'category', +# 'detail', 'name', 'anonymous', 'confirmed', +# 'whensent', 'service', +# ] +# } +# ); + sub end : Private { my ( $self, $c ) = @_; @@ -73,7 +106,7 @@ sub end : Private { : $c->stash->{response}; $c->res->content_type('application/json; charset=utf-8'); - $c->res->body( encode_json($response) ); + $c->res->body( encode_json( $response || {} ) ); } __PACKAGE__->meta->make_immutable; diff --git a/perllib/FixMyStreet/TestMech.pm b/perllib/FixMyStreet/TestMech.pm index 2ed6c2c61..4c4a3b3eb 100644 --- a/perllib/FixMyStreet/TestMech.pm +++ b/perllib/FixMyStreet/TestMech.pm @@ -54,6 +54,26 @@ sub logged_in_ok { "logged in" ); } +=head2 create_user_ok + + $user = $mech->create_user_ok( $email ); + +Create a test user (or find it and return if it already exists). + +=cut + +sub create_user_ok { + my $self = shift; + my ($email) = @_; + + my $user = + FixMyStreet::App->model('DB::User') + ->find_or_create( { email => $email } ); + ok $user, "found/created user for $email"; + + return $user; +} + =head2 log_in_ok $user = $mech->log_in_ok( $email_address ); @@ -66,10 +86,7 @@ sub log_in_ok { my $mech = shift; my $email = shift; - my $user = - FixMyStreet::App->model('DB::User') - ->find_or_create( { email => $email } ); - ok $user, "found/created user for $email"; + my $user = $mech->create_user_ok($email); # store the old password and then change it my $old_password_sha1 = $user->password; diff --git a/t/app/controller/json.t b/t/app/controller/json.t index e5fda5d27..2c9ff4a61 100644 --- a/t/app/controller/json.t +++ b/t/app/controller/json.t @@ -6,9 +6,10 @@ use Test::More; use FixMyStreet::TestMech; my $mech = FixMyStreet::TestMech->new; -subtest "check that a bad request produces the right response" => sub { +subtest "check that a bad request produces the appropriate response" => sub { my $bad_date = "Invalid dates supplied"; + my $mad_date = "Start date after end date"; my $bad_type = "Invalid type supplied"; my %tests = ( @@ -20,6 +21,8 @@ subtest "check that a bad request produces the right response" => sub { 'type=&start_date=2000-02-31&end_date=2000-02-01' => $bad_date, 'type=&start_date=2000-01-01&end_date=2000-02-31' => $bad_date, + 'type=&start_date=2000-02-01&end_date=2000-01-01' => $mad_date, + 'type=&start_date=2000-01-01&end_date=2000-02-01' => $bad_type, 'type=foo&start_date=2000-01-01&end_date=2000-02-01' => $bad_type, ); @@ -33,4 +36,74 @@ subtest "check that a bad request produces the right response" => sub { }; +is_deeply # + $mech->get_ok_json( + "/json?type=new_problems&start_date=2000-01-01&end_date=2000-02-01"), # + [], # + "correct response"; + +# put an entry in the database for this test +my $user = $mech->create_user_ok('test@example.com'); + +my $problem_args = { + postcode => 'sw1a 1aa', + council => '2501', + areas => ',105164,11806,11827,2247,2501,34817,42011,66045,70786,8519,', + category => 'test category', + title => 'Test title', + detail => 'Test detail', + used_map => 't', + name => 'Test Name', + created => '2000-01-01 12:00:00', + confirmed => '2000-01-01 12:01:00', + state => 'confirmed', + lang => 'en-gb', + service => '', + cobrand => '', + cobrand_data => '', + lastupdate => '2000-01-01 12:00:00', + whensent => undef, + send_questionnaire => 't', + latitude => '51.4531988729771', + longitude => '-0.23021896608596', +}; +my $problem = $user->add_to_problems( { %$problem_args, anonymous => 0 } ); +my $anon_problem = $user->add_to_problems( { %$problem_args, anonymous => 1 } ); + +ok $problem, "created normal test problem"; +ok $anon_problem, "created anon test problem"; + +is_deeply # + $mech->get_ok_json( + "/json?type=new_problems&start_date=2000-01-01&end_date=2000-02-01"), # + [ + { + 'anonymous' => 0, + 'category' => 'test category', + 'confirmed' => '2000-01-01 12:01:00', + 'council' => 'Wandsworth Borough Council', + 'detail' => 'Test detail', + 'id' => $problem->id, + 'name' => 'Test Name', + 'service' => 'Web interface', + 'title' => 'Test title', + 'whensent' => undef + }, + { + 'anonymous' => 1, + 'category' => 'test category', + 'confirmed' => '2000-01-01 12:01:00', + 'council' => 'Wandsworth Borough Council', + 'detail' => 'Test detail', + 'id' => $anon_problem->id, + 'name' => '', + 'service' => 'Web interface', + 'title' => 'Test title', + 'whensent' => undef + } + ], + "correct response"; + +$mech->delete_user($user); + done_testing(); |