From 159890995896ccaeeeb6898f679631161e5d4dd6 Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Sun, 23 Apr 2017 16:59:35 +0100 Subject: [PATCH] Started on user day entry --- cpanfile | 2 +- lib/Pear/LocalLoop.pm | 11 +++++ lib/Pear/LocalLoop/Controller/Api/User.pm | 29 ++++++++++++ lib/Pear/LocalLoop/Plugin/Validators.pm | 7 +++ lib/Test/Pear/LocalLoop.pm | 38 +++++++++++++++ t/api/user.t | 56 +++++++++++++++++++++++ 6 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 t/api/user.t diff --git a/cpanfile b/cpanfile index 8444dd6..01c7fc2 100644 --- a/cpanfile +++ b/cpanfile @@ -14,5 +14,5 @@ requires 'DBIx::Class::TimeStamp'; requires 'DBIx::Class::Schema::Loader'; requires 'SQL::Translator'; requires 'DateTime'; +requires 'DateTime::Format::Strptime'; requires 'DateTime::Format::SQLite'; -requires 'TryCatch'; diff --git a/lib/Pear/LocalLoop.pm b/lib/Pear/LocalLoop.pm index 4c54816..a43e3cb 100644 --- a/lib/Pear/LocalLoop.pm +++ b/lib/Pear/LocalLoop.pm @@ -69,6 +69,16 @@ sub startup { } }); + $self->helper( datetime_formatter => sub { + my $c = shift; + + return DateTime::Format::Strptime->new( + pattern => '%FT%T%z', + strict => 1, + on_error => 'undef', + ); + }); + $self->helper( get_path_from_uuid => sub { my $c = shift; my $uuid = shift; @@ -120,6 +130,7 @@ sub startup { }); $api->post('/upload')->to('api-upload#post_upload'); $api->post('/search')->to('api-upload#post_search'); + $api->post('/user/day')->to('api-user#post_day'); $api->post('/edit')->to('api-api#post_edit'); $api->post('/fetchuser')->to('api-api#post_fetchuser'); $api->post('/user-history')->to('api-user#post_user_history'); diff --git a/lib/Pear/LocalLoop/Controller/Api/User.pm b/lib/Pear/LocalLoop/Controller/Api/User.pm index 757ff2a..abbeddd 100644 --- a/lib/Pear/LocalLoop/Controller/Api/User.pm +++ b/lib/Pear/LocalLoop/Controller/Api/User.pm @@ -1,5 +1,34 @@ package Pear::LocalLoop::Controller::Api::User; use Mojo::Base 'Mojolicious::Controller'; +use Mojo::JSON; + +has error_messages => sub { + return { + day => { + is_iso_datetime => { message => 'Invalid ISO8601 Datetime', status => 400 }, + }, + }; +}; + +sub post_day { + my $c = shift; + + my $validation = $c->validation; + + $validation->input( $c->stash->{api_json} ); + + $validation->optional('day')->is_iso_datetime; + + return $c->api_validation_error if $validation->has_error; + + $c->render( json => { + success => Mojo::JSON->true, + }); +} + +1; + +__END__ use Data::Dumper; use Mojo::JSON; use DateTime; diff --git a/lib/Pear/LocalLoop/Plugin/Validators.pm b/lib/Pear/LocalLoop/Plugin/Validators.pm index b938a2c..282f8c1 100644 --- a/lib/Pear/LocalLoop/Plugin/Validators.pm +++ b/lib/Pear/LocalLoop/Plugin/Validators.pm @@ -5,6 +5,7 @@ use Email::Valid; use Geo::UK::Postcode; use Scalar::Util qw/ looks_like_number /; use File::Basename; +use DateTime::Format::Strptime; sub register { my ( $plugin, $app, $conf ) = @_; @@ -45,6 +46,12 @@ sub register { $extension =~ s/^\.//; return $app->types->type($extension) eq $filetype ? undef : 1; }); + + $app->validator->add_check( is_iso_datetime => sub { + my ( $validation, $name, $value ) = @_; + $value = $app->datetime_formatter->parse_datetime( $value ); + return defined $value ? undef : 1; + }); } 1; diff --git a/lib/Test/Pear/LocalLoop.pm b/lib/Test/Pear/LocalLoop.pm index 0640649..606f109 100644 --- a/lib/Test/Pear/LocalLoop.pm +++ b/lib/Test/Pear/LocalLoop.pm @@ -1,8 +1,10 @@ package Test::Pear::LocalLoop; use Mojo::Base -base; +use Test::More; use File::Temp; use Test::Mojo; +use DateTime::Format::Strptime; has config => sub { my $file = File::Temp->new; @@ -38,4 +40,40 @@ has framework => sub { return $t; }; +sub dump_error { + return sub { + my $self = shift; + if ( my $error = $self->tx->res->dom->at('pre[id="error"]') ) { + diag $error->text; + } else { + diag $self->tx->res->to_string; + } + }; +} + +sub register_customer { + my $self = shift; + my $args = shift; + + my $json = { + usertype => 'customer', + %$args, + }; + + $self->framework->post_ok('/api/register' => json => $json) + ->status_is(200)->or($self->dump_error) + ->json_is('/success', Mojo::JSON->true)->or($self->dump_error); +} + +sub login { + my $self = shift; + my $args = shift; + + $self->framework->post_ok('/api/login' => json => $args) + ->status_is(200)->or($self->dump_error) + ->json_is('/success', Mojo::JSON->true)->or($self->dump_error); + + return $self->framework->tx->res->json->{session_key}; +} + 1; diff --git a/t/api/user.t b/t/api/user.t new file mode 100644 index 0000000..774ab6f --- /dev/null +++ b/t/api/user.t @@ -0,0 +1,56 @@ +use Mojo::Base -strict; + +use Test::More; +use Mojo::JSON; +use Test::Pear::LocalLoop; +use DateTime; + +my $framework = Test::Pear::LocalLoop->new; +my $t = $framework->framework; +my $schema = $t->app->schema; + +my $account_token = 'a'; +my $email = 'test@example.com'; +my $password = 'abc123'; + +$schema->resultset('AccountToken')->create({ + name => $account_token +}); + +$framework->register_customer({ + 'token' => $account_token, + 'name' => 'Test User', + 'email' => $email, + 'postcode' => 'LA1 1AA', + 'password' => $password, + 'age_range' => 1 +}); + +my $session_key = $framework->login({ + email => $email, + password => $password, +}); + +my $json_no_date = { session_key => $session_key }; +$t->post_ok('/api/user/day', json => $json_no_date) + ->status_is(200) + ->json_is('/success', Mojo::JSON->true); + +my $json_invalid_date = { + session_key => $session_key, + day => 'invalid', +}; +$t->post_ok('/api/user/day', json => $json_invalid_date) + ->status_is(400)->or($framework->dump_error) + ->json_is('/success', Mojo::JSON->false) + ->json_like('/message', qr/Invalid ISO8601 Datetime/); + +my $json_valid_date = { + session_key => $session_key, + day => $t->app->datetime_formatter->format_datetime(DateTime->now), +}; +$t->post_ok('/api/user/day', json => $json_valid_date) + ->status_is(200)->or($framework->dump_error) + ->json_is('/success', Mojo::JSON->true); + +done_testing;