Started on user day entry

This commit is contained in:
Tom Bloor 2017-04-23 16:59:35 +01:00
parent b03b7ccc5f
commit 1598909958
6 changed files with 142 additions and 1 deletions

View file

@ -14,5 +14,5 @@ requires 'DBIx::Class::TimeStamp';
requires 'DBIx::Class::Schema::Loader'; requires 'DBIx::Class::Schema::Loader';
requires 'SQL::Translator'; requires 'SQL::Translator';
requires 'DateTime'; requires 'DateTime';
requires 'DateTime::Format::Strptime';
requires 'DateTime::Format::SQLite'; requires 'DateTime::Format::SQLite';
requires 'TryCatch';

View file

@ -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 { $self->helper( get_path_from_uuid => sub {
my $c = shift; my $c = shift;
my $uuid = shift; my $uuid = shift;
@ -120,6 +130,7 @@ sub startup {
}); });
$api->post('/upload')->to('api-upload#post_upload'); $api->post('/upload')->to('api-upload#post_upload');
$api->post('/search')->to('api-upload#post_search'); $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('/edit')->to('api-api#post_edit');
$api->post('/fetchuser')->to('api-api#post_fetchuser'); $api->post('/fetchuser')->to('api-api#post_fetchuser');
$api->post('/user-history')->to('api-user#post_user_history'); $api->post('/user-history')->to('api-user#post_user_history');

View file

@ -1,5 +1,34 @@
package Pear::LocalLoop::Controller::Api::User; package Pear::LocalLoop::Controller::Api::User;
use Mojo::Base 'Mojolicious::Controller'; 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 Data::Dumper;
use Mojo::JSON; use Mojo::JSON;
use DateTime; use DateTime;

View file

@ -5,6 +5,7 @@ use Email::Valid;
use Geo::UK::Postcode; use Geo::UK::Postcode;
use Scalar::Util qw/ looks_like_number /; use Scalar::Util qw/ looks_like_number /;
use File::Basename; use File::Basename;
use DateTime::Format::Strptime;
sub register { sub register {
my ( $plugin, $app, $conf ) = @_; my ( $plugin, $app, $conf ) = @_;
@ -45,6 +46,12 @@ sub register {
$extension =~ s/^\.//; $extension =~ s/^\.//;
return $app->types->type($extension) eq $filetype ? undef : 1; 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; 1;

View file

@ -1,8 +1,10 @@
package Test::Pear::LocalLoop; package Test::Pear::LocalLoop;
use Mojo::Base -base; use Mojo::Base -base;
use Test::More;
use File::Temp; use File::Temp;
use Test::Mojo; use Test::Mojo;
use DateTime::Format::Strptime;
has config => sub { has config => sub {
my $file = File::Temp->new; my $file = File::Temp->new;
@ -38,4 +40,40 @@ has framework => sub {
return $t; 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; 1;

56
t/api/user.t Normal file
View file

@ -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;