diff options
Diffstat (limited to 't/app/controller/json.t')
-rw-r--r-- | t/app/controller/json.t | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/t/app/controller/json.t b/t/app/controller/json.t new file mode 100644 index 000000000..d6820e1be --- /dev/null +++ b/t/app/controller/json.t @@ -0,0 +1,109 @@ +use strict; +use warnings; + +use Test::More; + +use FixMyStreet::TestMech; +my $mech = FixMyStreet::TestMech->new; + +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 = ( + '?' => $bad_date, + '?foo=bar' => $bad_date, + '?start_date=&end_date=' => $bad_date, + '?start_date=bad&end_date=2000-02-01' => $bad_date, + '?start_date=2000-01-01&end_date=bad' => $bad_date, + '?start_date=2000-02-31&end_date=2000-02-01' => $bad_date, + '?start_date=2000-01-01&end_date=2000-02-31' => $bad_date, + + '?start_date=2000-02-01&end_date=2000-01-01' => $mad_date, + + '?start_date=2000-01-01&end_date=2000-02-01' => $bad_type, + '/foo?type=foo&start_date=2000-01-01&end_date=2000-02-01' => $bad_type, + ); + + foreach my $q ( sort keys %tests ) { + is_deeply # + $mech->get_ok_json("/json/problems$q"), # + { error => $tests{$q} }, # + "correct error for query '$q'"; + } + +}; + +is_deeply # + $mech->get_ok_json( + "/json/problems/new?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, confirmed => '2000-01-01 12:02:00' } ); + +ok $problem, "created normal test problem"; +ok $anon_problem, "created anon test problem"; + +is_deeply # + $mech->get_ok_json( + "/json/problems/new?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:02: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(); |