aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet
diff options
context:
space:
mode:
authorEdmund von der Burg <evdb@mysociety.org>2011-05-20 16:05:15 +0100
committerEdmund von der Burg <evdb@mysociety.org>2011-05-20 16:05:15 +0100
commitcfcda98c6859672b843c15407e0c55d39da98b83 (patch)
tree52dd274da375d9370aa17007f6c27f699c2da35d /perllib/FixMyStreet
parent0518f68fcdc68241ddf7a0d083c450136a9e5efa (diff)
Added tests for bad requests
Diffstat (limited to 'perllib/FixMyStreet')
-rw-r--r--perllib/FixMyStreet/App/Controller/JSON.pm81
-rw-r--r--perllib/FixMyStreet/TestMech.pm37
2 files changed, 110 insertions, 8 deletions
diff --git a/perllib/FixMyStreet/App/Controller/JSON.pm b/perllib/FixMyStreet/App/Controller/JSON.pm
new file mode 100644
index 000000000..a0e5490cb
--- /dev/null
+++ b/perllib/FixMyStreet/App/Controller/JSON.pm
@@ -0,0 +1,81 @@
+package FixMyStreet::App::Controller::JSON;
+use Moose;
+use namespace::autoclean;
+
+BEGIN { extends 'Catalyst::Controller'; }
+
+use JSON;
+use DateTime;
+use DateTime::Format::ISO8601;
+
+=head1 NAME
+
+FixMyStreet::App::Controller::JSON - Catalyst Controller
+
+=head1 DESCRIPTION
+
+Provide information as JSON
+
+=head1 METHODS
+
+=head2 json
+
+=cut
+
+sub json : Path : Args(0) {
+ my ( $self, $c ) = @_;
+
+ # gather the parameters
+ my $type = $c->req->param('type') || '';
+ my $start_date = $c->req->param('start_date') || '';
+ my $end_date = $c->req->param('end_date') || '';
+
+ my $yyyy_mm_dd = qr{^\d{4}-\d\d-\d\d$};
+ if ( $start_date !~ $yyyy_mm_dd
+ || $end_date !~ $yyyy_mm_dd )
+ {
+ $c->stash->{error} = 'Invalid dates supplied';
+ return;
+ }
+
+ # convert the dates to datetimes and trap errors
+ my $iso8601 = DateTime::Format::ISO8601->new;
+ my $start_dt = eval { $iso8601->parse_datetime($start_date); };
+ my $end_dt = eval { $iso8601->parse_datetime($end_date); };
+ unless ( $start_dt && $end_dt ) {
+ $c->stash->{error} = 'Invalid dates supplied';
+ 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 );
+ # }
+
+}
+
+sub end : Private {
+ my ( $self, $c ) = @_;
+
+ my $response =
+ $c->stash->{error}
+ ? { error => $c->stash->{error} }
+ : $c->stash->{response};
+
+ $c->res->content_type('application/json; charset=utf-8');
+ $c->res->body( encode_json($response) );
+}
+
+__PACKAGE__->meta->make_immutable;
+
+1;
diff --git a/perllib/FixMyStreet/TestMech.pm b/perllib/FixMyStreet/TestMech.pm
index 35f934299..2ed6c2c61 100644
--- a/perllib/FixMyStreet/TestMech.pm
+++ b/perllib/FixMyStreet/TestMech.pm
@@ -15,6 +15,7 @@ use Web::Scraper;
use Carp;
use Email::Send::Test;
use Digest::SHA1 'sha1_hex';
+use JSON;
=head1 NAME
@@ -129,14 +130,11 @@ sub delete_user {
$mech->log_out_ok;
for my $p ( $user->problems ) {
- ok( $_->delete, "delete comment " . $_->text )
- for $p->comments;
+ ok( $_->delete, "delete comment " . $_->text ) for $p->comments;
ok( $p->delete, "delete problem " . $p->title );
}
- ok( $_->delete, "delete comment " . $_->text )
- for $user->comments;
- ok( $_->delete, "delete alert " . $_->alert_type )
- for $user->alerts;
+ ok( $_->delete, "delete comment " . $_->text ) for $user->comments;
+ ok( $_->delete, "delete alert " . $_->alert_type ) for $user->alerts;
ok $user->delete, "delete test user " . $user->email;
return 1;
@@ -223,7 +221,7 @@ arrayref of TEXTs. If none found return empty arrayref.
sub page_errors {
my $mech = shift;
my $result = scraper {
- process 'p.error', 'errors[]', 'TEXT';
+ process 'p.error', 'errors[]', 'TEXT';
process 'ul.error', 'errors[]', 'TEXT';
}
->scrape( $mech->response );
@@ -332,7 +330,6 @@ sub extract_problem_title {
return $result->{title};
}
-
=head2 extract_problem_banner
$banner = $mech->extract_problem_banner;
@@ -434,4 +431,28 @@ sub session_cookie_expiry {
return $expires || 0;
}
+=head2 get_ok_json
+
+ $decoded = $mech->get_ok_json( $url );
+
+Get the url, check that it was JSON and then decode and return the body.
+
+=cut
+
+sub get_ok_json {
+ my $mech = shift;
+ my $url = shift;
+
+ # try to get the response
+ $mech->get_ok($url)
+ || return undef;
+ my $res = $mech->response;
+
+ # check that the content-type of response is correct
+ croak "Response was not JSON"
+ unless $res->header('Content-Type') =~ m{^application/json\b};
+
+ return decode_json( $res->content );
+}
+
1;