From 1cd92d1e1210198deede4cd72f4b385da6668e3d Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Tue, 25 Apr 2017 19:47:50 +0100 Subject: [PATCH 1/9] Added a basic CORS options for api/* --- lib/Pear/LocalLoop.pm | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/Pear/LocalLoop.pm b/lib/Pear/LocalLoop.pm index b999724..d8231d3 100644 --- a/lib/Pear/LocalLoop.pm +++ b/lib/Pear/LocalLoop.pm @@ -112,6 +112,17 @@ sub startup { my $api_public_get = $r->under('/api'); $api_public_get->get('/info/ages')->to('api-info#get_ages'); + $api_public_get->options('*' => sub { + my $self = shift; + + $self->res->headers->header('Access-Control-Allow-Origin'=> 'http://localhost:7000'); + $self->res->headers->header('Access-Control-Allow-Credentials' => 'true'); + $self->res->headers->header('Access-Control-Allow-Methods' => 'GET, OPTIONS, POST, DELETE, PUT'); + $self->res->headers->header('Access-Control-Allow-Headers' => 'Content-Type, X-CSRF-Token'); + $self->res->headers->header('Access-Control-Max-Age' => '1728000'); + + $self->respond_to(any => { data => '', status => 200 }); + }); # Always available api routes my $api_public = $r->under('/api')->to('api-auth#check_json'); From fed62641ad500f6e0c3fc38eeaf8e737d5c7ab40 Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Tue, 25 Apr 2017 19:48:29 +0100 Subject: [PATCH 2/9] Change to * rule for CORS --- lib/Pear/LocalLoop.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Pear/LocalLoop.pm b/lib/Pear/LocalLoop.pm index d8231d3..2963ac6 100644 --- a/lib/Pear/LocalLoop.pm +++ b/lib/Pear/LocalLoop.pm @@ -115,7 +115,7 @@ sub startup { $api_public_get->options('*' => sub { my $self = shift; - $self->res->headers->header('Access-Control-Allow-Origin'=> 'http://localhost:7000'); + $self->res->headers->header('Access-Control-Allow-Origin'=> '*'); $self->res->headers->header('Access-Control-Allow-Credentials' => 'true'); $self->res->headers->header('Access-Control-Allow-Methods' => 'GET, OPTIONS, POST, DELETE, PUT'); $self->res->headers->header('Access-Control-Allow-Headers' => 'Content-Type, X-CSRF-Token'); From 0292ac7e2e3bd03a1a2bd51b3836436c79c726aa Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Tue, 25 Apr 2017 20:13:34 +0100 Subject: [PATCH 3/9] Improve CORS to cover all api routes, including get and post --- lib/Pear/LocalLoop.pm | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/Pear/LocalLoop.pm b/lib/Pear/LocalLoop.pm index 2963ac6..67b9976 100644 --- a/lib/Pear/LocalLoop.pm +++ b/lib/Pear/LocalLoop.pm @@ -110,22 +110,22 @@ sub startup { # $r->post('/register')->to('register#register'); $r->any('/admin/logout')->to('admin#auth_logout'); - my $api_public_get = $r->under('/api'); - $api_public_get->get('/info/ages')->to('api-info#get_ages'); - $api_public_get->options('*' => sub { + my $api_public_get = $r->under('/api' => sub { my $self = shift; - $self->res->headers->header('Access-Control-Allow-Origin'=> '*'); $self->res->headers->header('Access-Control-Allow-Credentials' => 'true'); $self->res->headers->header('Access-Control-Allow-Methods' => 'GET, OPTIONS, POST, DELETE, PUT'); $self->res->headers->header('Access-Control-Allow-Headers' => 'Content-Type, X-CSRF-Token'); $self->res->headers->header('Access-Control-Max-Age' => '1728000'); - - $self->respond_to(any => { data => '', status => 200 }); }); + $api_public_get->options('*' => sub { + $self->respond_to(any => { data => '', status => 200 }); + }); + $api_public_get->get('/info/ages')->to('api-info#get_ages'); + # Always available api routes - my $api_public = $r->under('/api')->to('api-auth#check_json'); + my $api_public = $api_public_get->under('/')->to('api-auth#check_json'); $api_public->post('/login')->to('api-auth#post_login'); $api_public->post('/register')->to('api-register#post_register'); From e31ec73934104678f5fc2250550b0054cf3e24b7 Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Tue, 25 Apr 2017 20:19:41 +0100 Subject: [PATCH 4/9] Forgot to define $c in CORS endpoints --- lib/Pear/LocalLoop.pm | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/Pear/LocalLoop.pm b/lib/Pear/LocalLoop.pm index 67b9976..aae42c2 100644 --- a/lib/Pear/LocalLoop.pm +++ b/lib/Pear/LocalLoop.pm @@ -111,16 +111,17 @@ sub startup { $r->any('/admin/logout')->to('admin#auth_logout'); my $api_public_get = $r->under('/api' => sub { - my $self = shift; - $self->res->headers->header('Access-Control-Allow-Origin'=> '*'); - $self->res->headers->header('Access-Control-Allow-Credentials' => 'true'); - $self->res->headers->header('Access-Control-Allow-Methods' => 'GET, OPTIONS, POST, DELETE, PUT'); - $self->res->headers->header('Access-Control-Allow-Headers' => 'Content-Type, X-CSRF-Token'); - $self->res->headers->header('Access-Control-Max-Age' => '1728000'); + my $c = shift; + $c->res->headers->header('Access-Control-Allow-Origin'=> '*'); + $c->res->headers->header('Access-Control-Allow-Credentials' => 'true'); + $c->res->headers->header('Access-Control-Allow-Methods' => 'GET, OPTIONS, POST, DELETE, PUT'); + $c->res->headers->header('Access-Control-Allow-Headers' => 'Content-Type, X-CSRF-Token'); + $c->res->headers->header('Access-Control-Max-Age' => '1728000'); }); $api_public_get->options('*' => sub { - $self->respond_to(any => { data => '', status => 200 }); + my $c = shift; + $c->respond_to(any => { data => '', status => 200 }); }); $api_public_get->get('/info/ages')->to('api-info#get_ages'); From 286ecf11e1d074b5526866394065567b5b583eba Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Tue, 25 Apr 2017 20:50:34 +0100 Subject: [PATCH 5/9] Fixed issue with validator for postcodes --- cpanfile | 1 + lib/Pear/LocalLoop/Plugin/Validators.pm | 8 +++++++- t/plugins/validators.t | 5 ++++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/cpanfile b/cpanfile index 4d40bc6..a363a30 100644 --- a/cpanfile +++ b/cpanfile @@ -16,3 +16,4 @@ requires 'SQL::Translator'; requires 'DateTime'; requires 'DateTime::Format::Strptime', "1.73"; requires 'DateTime::Format::SQLite'; +requires 'Try::Tiny'; diff --git a/lib/Pear/LocalLoop/Plugin/Validators.pm b/lib/Pear/LocalLoop/Plugin/Validators.pm index 8dfaf4e..eb2ebbb 100644 --- a/lib/Pear/LocalLoop/Plugin/Validators.pm +++ b/lib/Pear/LocalLoop/Plugin/Validators.pm @@ -6,6 +6,7 @@ use Geo::UK::Postcode; use Scalar::Util qw/ looks_like_number /; use File::Basename qw/ fileparse /; use DateTime::Format::Strptime; +use Try::Tiny; sub register { my ( $plugin, $app, $conf ) = @_; @@ -27,7 +28,12 @@ sub register { $app->validator->add_check( postcode => sub { my ( $validation, $name, $value ) = @_; - return Geo::UK::Postcode->new( $value )->valid ? undef : 1; + my $postcode; + try { + $postcode = Geo::UK::Postcode->new( $value ); + }; + return defined $postcode && $postcode->valid ? undef : 1; + }); $app->validator->add_check( number => sub { diff --git a/t/plugins/validators.t b/t/plugins/validators.t index 596cdad..60027b3 100644 --- a/t/plugins/validators.t +++ b/t/plugins/validators.t @@ -14,20 +14,23 @@ my $valid_email = 'test@example.com'; my $invalid_email = 'test.example.com'; my $valid_postcode = 'WC1H 9EB'; my $invalid_postcode = 'AB1 2CD'; +my $not_a_postcode = 'a'; $validation->input({ valid_email => $valid_email, invalid_email => $invalid_email, valid_postcode => $valid_postcode, invalid_postcode => $invalid_postcode, + not_a_postcode => $not_a_postcode, }); $validation->required('valid_email')->email; $validation->required('invalid_email')->email; $validation->required('valid_postcode')->postcode; $validation->required('invalid_postcode')->postcode; +$validation->required('not_a_postcode')->postcode; ok $validation->has_error, 'Have Errors'; -is_deeply $validation->failed, [ 'invalid_email', 'invalid_postcode' ], 'Correct Errors'; +is_deeply $validation->failed, [ qw/ invalid_email invalid_postcode not_a_postcode / ], 'Correct Errors'; done_testing; From aed22d1c9858b755e1e88d457aa6c9e5defb6079 Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Tue, 25 Apr 2017 21:44:13 +0100 Subject: [PATCH 6/9] Change to having display_name and full_name for customers --- lib/Pear/LocalLoop/Controller/Api/Register.pm | 13 +- lib/Pear/LocalLoop/Schema/Result/Customer.pm | 7 +- t/admin-approve.t | 6 +- t/admin-merge.t | 6 +- t/api/login.t | 3 +- t/api/user.t | 3 +- t/register.t | 156 +++++++++++------- t/search.t | 3 +- t/upload.t | 6 +- 9 files changed, 133 insertions(+), 70 deletions(-) diff --git a/lib/Pear/LocalLoop/Controller/Api/Register.pm b/lib/Pear/LocalLoop/Controller/Api/Register.pm index db12e9d..4b56b9a 100644 --- a/lib/Pear/LocalLoop/Controller/Api/Register.pm +++ b/lib/Pear/LocalLoop/Controller/Api/Register.pm @@ -11,6 +11,12 @@ has error_messages => sub { name => { required => { message => 'No name sent or was blank.', status => 400 }, }, + display_name => { + required => { message => 'No name sent or was blank.', status => 400 }, + }, + full_name => { + required => { message => 'No name sent or was blank.', status => 400 }, + }, email => { required => { message => 'No email sent.', status => 400 }, email => { message => 'Email is invalid.', status => 400 }, @@ -54,16 +60,18 @@ sub post_register{ $validation->required('email')->email->not_in_resultset('email', $user_rs); $validation->required('password'); - $validation->required('name'); $validation->required('postcode')->postcode; $validation->required('usertype')->in('customer', 'organisation'); my $usertype = $validation->param('usertype') || ''; if ( $usertype eq 'customer' ) { + $validation->required('display_name'); + $validation->required('full_name'); my $age_rs = $c->schema->resultset('AgeRange'); $validation->required('age_range')->number->in_resultset('id', $age_rs); } elsif ( $usertype eq 'organisation' ) { + $validation->required('name'); $validation->required('street_name'); $validation->required('town'); } @@ -79,7 +87,8 @@ sub post_register{ })->update({ used => 1 }); $c->schema->resultset('User')->create({ customer => { - name => $validation->param('name'), + full_name => $validation->param('full_name'), + display_name => $validation->param('display_name'), age_range_id => $validation->param('age_range'), postcode => $validation->param('postcode'), }, diff --git a/lib/Pear/LocalLoop/Schema/Result/Customer.pm b/lib/Pear/LocalLoop/Schema/Result/Customer.pm index 100be3a..5cd67a1 100644 --- a/lib/Pear/LocalLoop/Schema/Result/Customer.pm +++ b/lib/Pear/LocalLoop/Schema/Result/Customer.pm @@ -13,7 +13,12 @@ __PACKAGE__->add_columns( is_auto_increment => 1, is_nullable => 0, }, - "name" => { + "display_name" => { + data_type => "varchar", + size => 255, + is_nullable => 0, + }, + "full_name" => { data_type => "varchar", size => 255, is_nullable => 0, diff --git a/t/admin-approve.t b/t/admin-approve.t index 0f108f0..49ceaf5 100644 --- a/t/admin-approve.t +++ b/t/admin-approve.t @@ -25,7 +25,8 @@ my $passwordReno = 'turks'; my $testJson = { 'usertype' => 'customer', 'token' => shift(@account_tokens), - 'name' => 'Reno', + 'full_name' => 'Reno', + 'display_name' => 'Reno', 'email' => $emailReno, 'postcode' => 'SA4 3FA', 'password' => $passwordReno, @@ -58,7 +59,8 @@ my $passwordAdmin = 'ethics'; $testJson = { 'usertype' => 'customer', 'token' => shift(@account_tokens), - 'name' => 'admin', + 'display_name' => 'admin', + 'full_name' => 'admin', 'email' => $emailAdmin, 'postcode' => 'HD5 9XU', 'password' => $passwordAdmin, diff --git a/t/admin-merge.t b/t/admin-merge.t index 83f86f4..b23cfea 100644 --- a/t/admin-merge.t +++ b/t/admin-merge.t @@ -25,7 +25,8 @@ my $passwordReno = 'turks'; my $testJson = { 'usertype' => 'customer', 'token' => shift(@account_tokens), - 'name' => 'Reno', + 'full_name' => 'Reno', + 'display_name' => 'Reno', 'email' => $emailReno, 'postcode' => 'SA4 3FA', 'password' => $passwordReno, @@ -59,7 +60,8 @@ my $passwordAdmin = 'ethics'; $testJson = { 'usertype' => 'customer', 'token' => shift(@account_tokens), - 'name' => 'admin', + 'display_name' => 'admin', + 'full_name' => 'admin', 'email' => $emailAdmin, 'postcode' => 'HD5 9XU', 'password' => $passwordAdmin, diff --git a/t/api/login.t b/t/api/login.t index 74dda80..b1fef6d 100644 --- a/t/api/login.t +++ b/t/api/login.t @@ -19,7 +19,8 @@ $schema->resultset('AccountToken')->create({ my $test_json = { 'usertype' => 'customer', 'token' => $account_token, - 'name' => 'RufusShinra', + 'display_name' => 'RufusShinra', + 'full_name' => 'RufusShinra', 'email' => $email, 'postcode' => 'LA1 1AA', 'password' => $password, diff --git a/t/api/user.t b/t/api/user.t index 774ab6f..b65abdc 100644 --- a/t/api/user.t +++ b/t/api/user.t @@ -19,7 +19,8 @@ $schema->resultset('AccountToken')->create({ $framework->register_customer({ 'token' => $account_token, - 'name' => 'Test User', + 'full_name' => 'Test User', + 'display_name' => 'Test User', 'email' => $email, 'postcode' => 'LA1 1AA', 'password' => $password, diff --git a/t/register.t b/t/register.t index a462f7c..a654500 100644 --- a/t/register.t +++ b/t/register.t @@ -10,8 +10,6 @@ my $schema = $t->app->schema; my $dump_error = sub { diag $t->tx->res->dom->at('pre[id="error"]')->text }; #Variables to be used for uniqueness when testing. -my @names = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'); -my @emails = ('a@a.com', 'b@a.com', 'c@a.com', 'd@a.com', 'e@a.com', 'f@a.com', 'g@a.com', 'h@a.com', 'i@a.com', 'j@a.com', 'k@a.com', 'l@a.com', 'm@a.com', 'n@a.com', 'o@a.com', 'p@a.com', 'q@a.com', 'r@a.com', 's@a.com', 't@a.com', 'u@a.com', 'v@a.com', 'w@a.com', 'x@a.com', 'y@a.com', 'z@a.com'); my @tokens = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'); $schema->resultset('AccountToken')->populate([ @@ -33,8 +31,9 @@ $t->post_ok('/api/register' => json => $testJson) #token missing JSON $testJson = { 'usertype' => 'customer', - 'name' => shift(@names), - 'email' => shift(@emails), + 'full_name' => 'test name', + 'display_name' => 'test name', + 'email' => 'a@b.com', 'postcode' => 'LA1 1AA', 'password' => 'Meh', 'age_range' => 3 @@ -44,13 +43,13 @@ $t->post_ok('/api/register' => json => $testJson) ->json_is('/success', Mojo::JSON->false) ->content_like(qr/no token sent/i); - #Not valid token. $testJson = { 'usertype' => 'customer', - 'token' => ' ', - 'name' => shift(@names), - 'email' => shift(@emails), + 'token' => 'testing', + 'display_name' => 'test name', + 'full_name' => 'test name', + 'email' => 'a@b.com', 'postcode' => 'LA1 1AA', 'password' => 'Meh', 'age_range' => 3 @@ -64,8 +63,23 @@ $t->post_ok('/api/register' => json => $testJson) #name missing JSON $testJson = { 'usertype' => 'customer', - 'token' => shift(@tokens), - 'email' => shift(@emails), + 'token' => 'a', + 'full_name' => 'test name', + 'email' => 'a@b.com', + 'postcode' => 'LA1 1AA', + 'password' => 'Meh', + 'age_range' => 3 +}; +$t->post_ok('/api/register' => json => $testJson) + ->status_is(400) + ->json_is('/success', Mojo::JSON->false) + ->content_like(qr/no name sent/i); +#name missing JSON +$testJson = { + 'usertype' => 'customer', + 'token' => 'a', + 'display_name' => 'test name', + 'email' => 'a@b.com', 'postcode' => 'LA1 1AA', 'password' => 'Meh', 'age_range' => 3 @@ -75,13 +89,29 @@ $t->post_ok('/api/register' => json => $testJson) ->json_is('/success', Mojo::JSON->false) ->content_like(qr/no name sent/i); - #Blank name $testJson = { 'usertype' => 'customer', - 'token' => shift(@tokens), - 'name' => '', - 'email' => shift(@emails), + 'token' => 'a', + 'display_name' => 'test name', + 'full_name' => '', + 'email' => 'a@b.com', + 'postcode' => 'LA1 1AA', + 'password' => 'Meh', + 'age_range' => 3 +}; +$t->post_ok('/api/register' => json => $testJson) + ->status_is(400) + ->json_is('/success', Mojo::JSON->false) + ->content_like(qr/blank/i) + ->content_like(qr/name/i); +#Blank name +$testJson = { + 'usertype' => 'customer', + 'token' => 'a', + 'display_name' => '', + 'full_name' => 'test name', + 'email' => 'a@b.com', 'postcode' => 'LA1 1AA', 'password' => 'Meh', 'age_range' => 3 @@ -92,15 +122,14 @@ $t->post_ok('/api/register' => json => $testJson) ->content_like(qr/blank/i) ->content_like(qr/name/i); -my $nameToReuse = shift(@names); -my $emailToReuse = shift(@emails); #Valid customer $testJson = { 'usertype' => 'customer', - 'token' => shift(@tokens), - 'name' => $nameToReuse, - 'email' => $emailToReuse, + 'token' => 'a', + 'full_name' => 'test name', + 'display_name' => 'test name', + 'email' => 'a@b.com', 'postcode' => 'LA1 1AA', 'password' => 'Meh', 'age_range' => 3 @@ -112,9 +141,10 @@ $t->post_ok('/api/register' => json => $testJson) #Valid customer2 $testJson = { 'usertype' => 'customer', - 'token' => shift(@tokens), - 'name' => shift(@names), - 'email' => shift(@emails), + 'token' => 'b', + 'full_name' => 'test name', + 'display_name' => 'test name', + 'email' => 'b@c.com', 'postcode' => 'LA1 1AA', 'password' => 'Meh', 'age_range' => 2 @@ -126,9 +156,10 @@ $t->post_ok('/api/register' => json => $testJson) #Valid customer3 $testJson = { 'usertype' => 'customer', - 'token' => shift(@tokens), - 'name' => shift(@names), - 'email' => shift(@emails), + 'token' => 'c', + 'full_name' => 'test name', + 'display_name' => 'test name', + 'email' => 'c@d.com', 'postcode' => 'LA1 1AA', 'password' => 'Meh', 'age_range' => 1 @@ -140,8 +171,9 @@ $t->post_ok('/api/register' => json => $testJson) #email missing JSON $testJson = { 'usertype' => 'customer', - 'token' => shift(@tokens), - 'name' => shift(@names), + 'token' => 'd', + 'full_name' => 'test name', + 'display_name' => 'test name', 'postcode' => 'LA1 1AA', 'password' => 'Meh', 'age_range' => 3 @@ -154,8 +186,9 @@ $t->post_ok('/api/register' => json => $testJson) #invalid email 1 $testJson = { 'usertype' => 'customer', - 'token' => shift(@tokens), - 'name' => shift(@names), + 'token' => 'd', + 'full_name' => 'test name', + 'display_name' => 'test name', 'email' => 'dfsd@.com', 'postcode' => 'LA1 1AA', 'password' => 'Meh', @@ -170,8 +203,9 @@ $t->post_ok('/api/register' => json => $testJson) #invalid email 2 $testJson = { 'usertype' => 'customer', - 'token' => shift(@tokens), - 'name' => shift(@names), + 'token' => 'd', + 'full_name' => 'test name', + 'display_name' => 'test name', 'email' => 'dfsd@com', 'postcode' => 'LA1 1AA', 'password' => 'Meh', @@ -186,9 +220,10 @@ $t->post_ok('/api/register' => json => $testJson) #Email exists $testJson = { 'usertype' => 'customer', - 'token' => shift(@tokens), - 'name' => shift(@names), - 'email' => $emailToReuse, + 'token' => 'd', + 'full_name' => 'test name', + 'display_name' => 'test name', + 'email' => 'a@b.com', 'postcode' => 'LA1 1AA', 'password' => 'Meh', 'age_range' => 2 @@ -202,9 +237,10 @@ $t->post_ok('/api/register' => json => $testJson) #postcode missing JSON $testJson = { 'usertype' => 'customer', - 'token' => shift(@tokens), - 'name' => shift(@names), - 'email' => shift(@emails), + 'token' => 'd', + 'full_name' => 'test name', + 'display_name' => 'test name', + 'email' => 'd@e.com', 'password' => 'Meh', 'age_range' => 3 }; @@ -218,9 +254,10 @@ $t->post_ok('/api/register' => json => $testJson) #password missing JSON $testJson = { 'usertype' => 'customer', - 'token' => shift(@tokens), - 'name' => shift(@names), - 'email' => shift(@emails), + 'token' => 'd', + 'full_name' => 'test name', + 'display_name' => 'test name', + 'email' => 'd@e.com', 'postcode' => 'LA1 1AA', 'age_range' => 3 }; @@ -233,9 +270,10 @@ $t->post_ok('/api/register' => json => $testJson) #usertype missing JSON $testJson = { - 'token' => shift(@tokens), - 'name' => shift(@names), - 'email' => shift(@emails), + 'token' => 'f', + 'full_name' => 'test name', + 'display_name' => 'test name', + 'email' => 'd@e.com', 'postcode' => 'LA1 1AA', 'password' => 'Meh', 'age_range' => 3 @@ -248,9 +286,9 @@ $t->post_ok('/api/register' => json => $testJson) #Invalid user type $testJson = { 'usertype' => 'organisation1', - 'token' => shift(@tokens), - 'name' => shift(@names), - 'email' => shift(@emails), + 'token' => 'f', + 'name' => 'test name', + 'email' => 'org@org.com', 'postcode' => 'LA1 1AA', 'password' => 'Meh', 'fulladdress' => 'mary lane testing....' @@ -265,9 +303,10 @@ $t->post_ok('/api/register' => json => $testJson) #age_range missing JSON $testJson = { 'usertype' => 'customer', - 'token' => shift(@tokens), - 'name' => shift(@names), - 'email' => shift(@emails), + 'token' => 'f', + 'display_name' => 'test name', + 'full_name' => 'test name', + 'email' => 'broke@example.com', 'postcode' => 'LA1 1AA', 'password' => 'Meh', }; @@ -279,9 +318,10 @@ $t->post_ok('/api/register' => json => $testJson) #Age is invalid $testJson = { 'usertype' => 'customer', - 'token' => shift(@tokens), - 'name' => shift(@names), - 'email' => shift(@emails), + 'token' => 'f', + 'full_name' => 'test name', + 'display_name' => 'test name', + 'email' => 'test@example.com', 'postcode' => 'LA1 1AA', 'password' => 'Meh', 'age_range' => 'invalid' @@ -295,9 +335,9 @@ $t->post_ok('/api/register' => json => $testJson) #full address missing JSON $testJson = { 'usertype' => 'organisation', - 'token' => shift(@tokens), - 'name' => shift(@names), - 'email' => shift(@emails), + 'token' => 'f', + 'name' => 'test org', + 'email' => 'org@org.com', 'postcode' => 'LA1 1AA', 'password' => 'Meh', }; @@ -311,9 +351,9 @@ $t->post_ok('/api/register' => json => $testJson) #Organisation valid $testJson = { 'usertype' => 'organisation', - 'token' => shift(@tokens), - 'name' => shift(@names), - 'email' => shift(@emails), + 'token' => 'f', + 'name' => 'org name', + 'email' => 'org@org.com', 'postcode' => 'LA1 1AA', 'password' => 'Meh', 'street_name' => 'mary lane testing....', diff --git a/t/search.t b/t/search.t index d8f0f5c..c0ebb3a 100644 --- a/t/search.t +++ b/t/search.t @@ -31,7 +31,8 @@ my $passwordRufus = 'MakoGold'; my $testJson = { 'usertype' => 'customer', 'token' => shift(@account_tokens), - 'name' => 'RufusShinra', + 'full_name' => 'RufusShinra', + 'display_name' => 'RufusShinra', 'email' => $emailRufus, 'postcode' => 'RG26 5NU', 'password' => $passwordRufus, diff --git a/t/upload.t b/t/upload.t index 8973c8e..1cf8215 100644 --- a/t/upload.t +++ b/t/upload.t @@ -38,7 +38,8 @@ my $passwordRufus = 'MakoGold'; my $testJson = { 'usertype' => 'customer', 'token' => shift(@account_tokens), - 'name' => 'RufusShinra', + 'full_name' => 'RufusShinra', + 'display_name' => 'RufusShinra', 'email' => $emailRufus, 'postcode' => 'GU10 5SA', 'password' => $passwordRufus, @@ -54,7 +55,8 @@ my $passwordHojo = 'Mako'; $testJson = { 'usertype' => 'customer', 'token' => shift(@account_tokens), - 'name' => 'ProfessorHojo', + 'display_name' => 'ProfessorHojo', + 'full_name' => 'ProfessorHojo', 'email' => $emailHojo, 'postcode' => 'DE15 9LT', 'password' => $passwordHojo, From ae6884202a7bcb94f8347752031613ef0e712c31 Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Tue, 25 Apr 2017 23:33:35 +0100 Subject: [PATCH 7/9] Fixed issue with postcode validator --- lib/Pear/LocalLoop/Plugin/Validators.pm | 2 +- t/plugins/validators.t | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/Pear/LocalLoop/Plugin/Validators.pm b/lib/Pear/LocalLoop/Plugin/Validators.pm index eb2ebbb..199d878 100644 --- a/lib/Pear/LocalLoop/Plugin/Validators.pm +++ b/lib/Pear/LocalLoop/Plugin/Validators.pm @@ -32,7 +32,7 @@ sub register { try { $postcode = Geo::UK::Postcode->new( $value ); }; - return defined $postcode && $postcode->valid ? undef : 1; + return defined( $postcode ) && ( $postcode->valid ) && ! ( $postcode->partial ) ? undef : 1; }); diff --git a/t/plugins/validators.t b/t/plugins/validators.t index 60027b3..81c2f02 100644 --- a/t/plugins/validators.t +++ b/t/plugins/validators.t @@ -15,6 +15,7 @@ my $invalid_email = 'test.example.com'; my $valid_postcode = 'WC1H 9EB'; my $invalid_postcode = 'AB1 2CD'; my $not_a_postcode = 'a'; +my $not_a_whole_postcode = 'LA1'; $validation->input({ valid_email => $valid_email, @@ -22,6 +23,7 @@ $validation->input({ valid_postcode => $valid_postcode, invalid_postcode => $invalid_postcode, not_a_postcode => $not_a_postcode, + not_a_whole_postcode => $not_a_whole_postcode, }); $validation->required('valid_email')->email; @@ -29,8 +31,9 @@ $validation->required('invalid_email')->email; $validation->required('valid_postcode')->postcode; $validation->required('invalid_postcode')->postcode; $validation->required('not_a_postcode')->postcode; +$validation->required('not_a_whole_postcode')->postcode; ok $validation->has_error, 'Have Errors'; -is_deeply $validation->failed, [ qw/ invalid_email invalid_postcode not_a_postcode / ], 'Correct Errors'; +is_deeply $validation->failed, [ qw/ invalid_email invalid_postcode not_a_postcode not_a_whole_postcode / ], 'Correct Errors'; done_testing; From 7614b7bc2e22d1de5f8f4529bb9962930575a914 Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Wed, 26 Apr 2017 20:53:27 +0100 Subject: [PATCH 8/9] Possible fix for postcode validator not working all the time --- lib/Pear/LocalLoop/Plugin/Validators.pm | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/Pear/LocalLoop/Plugin/Validators.pm b/lib/Pear/LocalLoop/Plugin/Validators.pm index 199d878..ecdefb7 100644 --- a/lib/Pear/LocalLoop/Plugin/Validators.pm +++ b/lib/Pear/LocalLoop/Plugin/Validators.pm @@ -32,8 +32,9 @@ sub register { try { $postcode = Geo::UK::Postcode->new( $value ); }; - return defined( $postcode ) && ( $postcode->valid ) && ! ( $postcode->partial ) ? undef : 1; - + return 1 unless defined( $postcode ); + return 1 if $postcode->partial; + return undef if $postcode->valid; }); $app->validator->add_check( number => sub { From 56fc868786587f9e72a8f1199e796153d8bca98b Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Wed, 26 Apr 2017 20:58:24 +0100 Subject: [PATCH 9/9] missed failout return from email validator --- lib/Pear/LocalLoop/Plugin/Validators.pm | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/Pear/LocalLoop/Plugin/Validators.pm b/lib/Pear/LocalLoop/Plugin/Validators.pm index ecdefb7..6722424 100644 --- a/lib/Pear/LocalLoop/Plugin/Validators.pm +++ b/lib/Pear/LocalLoop/Plugin/Validators.pm @@ -35,6 +35,7 @@ sub register { return 1 unless defined( $postcode ); return 1 if $postcode->partial; return undef if $postcode->valid; + return 1; }); $app->validator->add_check( number => sub {