From 1d1b4aa1cf1cd76e368d3521071a09a57f29512f Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Sat, 22 Apr 2017 19:35:19 +0100 Subject: [PATCH] Change registration to be the same as the api docs --- lib/Pear/LocalLoop/Controller/Api/Register.pm | 63 +++------- lib/Pear/LocalLoop/Schema/Result/Customer.pm | 4 +- t/admin-approve.t | 16 +-- t/admin-merge.t | 16 +-- t/api/login.t | 4 +- t/register.t | 115 +++++++----------- t/search.t | 11 +- t/upload.t | 16 +-- 8 files changed, 91 insertions(+), 154 deletions(-) diff --git a/lib/Pear/LocalLoop/Controller/Api/Register.pm b/lib/Pear/LocalLoop/Controller/Api/Register.pm index ae8a129..db12e9d 100644 --- a/lib/Pear/LocalLoop/Controller/Api/Register.pm +++ b/lib/Pear/LocalLoop/Controller/Api/Register.pm @@ -8,10 +8,8 @@ has error_messages => sub { required => { message => 'No token sent.', status => 400 }, in_resultset => { message => 'Token invalid or has been used.', status => 401 }, }, - username => { - required => { message => 'No username sent or was blank.', status => 400 }, - like => { message => 'Username can only be A-Z, a-z and 0-9 characters.', status => 400 }, - not_in_resultset => { message => 'Username exists.', status => 403 }, + name => { + required => { message => 'No name sent or was blank.', status => 400 }, }, email => { required => { message => 'No email sent.', status => 400 }, @@ -20,6 +18,7 @@ has error_messages => sub { }, postcode => { required => { message => 'No postcode sent.', status => 400 }, + postcode => { message => 'Postcode is invalid', status => 400 }, }, password => { required => { message => 'No password sent.', status => 400 }, @@ -28,10 +27,10 @@ has error_messages => sub { required => { message => 'No usertype sent.', status => 400 }, in => { message => '"usertype" is invalid.', status => 400 }, }, - age => { - required => { message => 'No age sent.', status => 400 }, - number => { message => 'Age range is invalid', status => 400 }, - in_resultset => { message => 'Age range is invalid.', status => 400 }, + age_range => { + required => { message => 'No age_range sent.', status => 400 }, + number => { message => 'age_range is invalid', status => 400 }, + in_resultset => { message => 'age_range is invalid.', status => 400 }, }, street_name => { required => { message => 'No street_name sent.', status => 400 }, @@ -46,62 +45,30 @@ sub post_register{ my $c = shift; my $validation = $c->validation; - - my $json = $c->req->json; - - if ( ! defined $json ){ - return $c->render( json => { - success => Mojo::JSON->false, - message => 'No json sent.', - }, - status => 400,); #Malformed request - } - $validation->input( $json ); + $validation->input( $c->stash->{api_json} ); my $token_rs = $c->schema->resultset('AccountToken')->search_rs({used => 0}); $validation->required('token')->in_resultset('name', $token_rs); - my $customer_rs = $c->schema->resultset('Customer'); - $validation->required('username')->like(qr/^[A-Za-z0-9]+$/)->not_in_resultset('username', $customer_rs); - my $user_rs = $c->schema->resultset('User'); $validation->required('email')->email->not_in_resultset('email', $user_rs); - - #TODO test to see if post code is valid. - $validation->required('postcode'); - - #TODO should we enforce password requirements. $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' ) { - my $age_rs = $c->schema->resultset('AgeRange'); - $validation->required('age')->number->in_resultset('id', $age_rs); - + $validation->required('age_range')->number->in_resultset('id', $age_rs); } elsif ( $usertype eq 'organisation' ) { - $validation->required('street_name'); $validation->required('town'); - } - if ( $validation->has_error ) { - my $failed_vals = $validation->failed; - for my $val ( @$failed_vals ) { - my $check = shift @{ $validation->error($val) }; - return $c->render( - json => { - success => Mojo::JSON->false, - message => $c->error_messages->{$val}->{$check}->{message}, - }, - status => $c->error_messages->{$val}->{$check}->{status}, - ); - } - } + return $c->api_validation_error if $validation->has_error; if ($usertype eq 'customer'){ @@ -112,8 +79,8 @@ sub post_register{ })->update({ used => 1 }); $c->schema->resultset('User')->create({ customer => { - username => $validation->param('username'), - age_range_id => $validation->param('age'), + name => $validation->param('name'), + age_range_id => $validation->param('age_range'), postcode => $validation->param('postcode'), }, email => $validation->param('email'), @@ -132,7 +99,7 @@ sub post_register{ })->update({ used => 1 }); $c->schema->resultset('User')->create({ organisation => { - name => $validation->param('username'), + name => $validation->param('name'), street_name => $validation->param('street_name'), town => $validation->param('town'), postcode => $validation->param('postcode'), diff --git a/lib/Pear/LocalLoop/Schema/Result/Customer.pm b/lib/Pear/LocalLoop/Schema/Result/Customer.pm index a1deee4..100be3a 100644 --- a/lib/Pear/LocalLoop/Schema/Result/Customer.pm +++ b/lib/Pear/LocalLoop/Schema/Result/Customer.pm @@ -13,7 +13,7 @@ __PACKAGE__->add_columns( is_auto_increment => 1, is_nullable => 0, }, - "username" => { + "name" => { data_type => "varchar", size => 255, is_nullable => 0, @@ -32,8 +32,6 @@ __PACKAGE__->add_columns( __PACKAGE__->set_primary_key("id"); -__PACKAGE__->add_unique_constraint(["username"]); - __PACKAGE__->belongs_to( "age_range", "Pear::LocalLoop::Schema::Result::AgeRange", diff --git a/t/admin-approve.t b/t/admin-approve.t index 3bac901..0f108f0 100644 --- a/t/admin-approve.t +++ b/t/admin-approve.t @@ -25,11 +25,11 @@ my $passwordReno = 'turks'; my $testJson = { 'usertype' => 'customer', 'token' => shift(@account_tokens), - 'username' => 'Reno', + 'name' => 'Reno', 'email' => $emailReno, - 'postcode' => 'E1 MP01', + 'postcode' => 'SA4 3FA', 'password' => $passwordReno, - 'age' => 1 + 'age_range' => 1 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(200)->or($dump_error) @@ -41,9 +41,9 @@ my $passwordBilly = 'Choco'; $testJson = { 'usertype' => 'organisation', 'token' => shift(@account_tokens), - 'username' => 'ChocoBillysGreens', + 'name' => 'ChocoBillysGreens', 'email' => $emailBilly, - 'postcode' => 'E4 C12', + 'postcode' => 'ST20 0LG', 'password' => $passwordBilly, 'street_name' => 'Chocobo Farm, Eastern Continent', 'town' => 'Gaia', @@ -58,11 +58,11 @@ my $passwordAdmin = 'ethics'; $testJson = { 'usertype' => 'customer', 'token' => shift(@account_tokens), - 'username' => 'admin', + 'name' => 'admin', 'email' => $emailAdmin, - 'postcode' => 'NW1 W01', + 'postcode' => 'HD5 9XU', 'password' => $passwordAdmin, - 'age' => 2 + 'age_range' => 2 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(200) diff --git a/t/admin-merge.t b/t/admin-merge.t index dac5ce4..83f86f4 100644 --- a/t/admin-merge.t +++ b/t/admin-merge.t @@ -25,11 +25,11 @@ my $passwordReno = 'turks'; my $testJson = { 'usertype' => 'customer', 'token' => shift(@account_tokens), - 'username' => 'Reno', + 'name' => 'Reno', 'email' => $emailReno, - 'postcode' => 'E1 MP01', + 'postcode' => 'SA4 3FA', 'password' => $passwordReno, - 'age' => 1 + 'age_range' => 1 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(200) @@ -41,9 +41,9 @@ my $passwordBilly = 'Choco'; $testJson = { 'usertype' => 'organisation', 'token' => shift(@account_tokens), - 'username' => 'ChocoBillysGreens', + 'name' => 'ChocoBillysGreens', 'email' => $emailBilly, - 'postcode' => 'E4 C12', + 'postcode' => 'ST20 0LG', 'password' => $passwordBilly, 'street_name' => 'Chocobo Farm, Eastern Continent', town => 'Gaia', @@ -59,11 +59,11 @@ my $passwordAdmin = 'ethics'; $testJson = { 'usertype' => 'customer', 'token' => shift(@account_tokens), - 'username' => 'admin', + 'name' => 'admin', 'email' => $emailAdmin, - 'postcode' => 'NW1 W01', + 'postcode' => 'HD5 9XU', 'password' => $passwordAdmin, - 'age' => 2 + 'age_range' => 2 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(200) diff --git a/t/api/login.t b/t/api/login.t index 93a0fb8..74dda80 100644 --- a/t/api/login.t +++ b/t/api/login.t @@ -19,11 +19,11 @@ $schema->resultset('AccountToken')->create({ my $test_json = { 'usertype' => 'customer', 'token' => $account_token, - 'username' => 'RufusShinra', + 'name' => 'RufusShinra', 'email' => $email, 'postcode' => 'LA1 1AA', 'password' => $password, - 'age' => 1 + 'age_range' => 1 }; $t->post_ok('/api/register' => json => $test_json) ->status_is(200) diff --git a/t/register.t b/t/register.t index 6ea19eb..a462f7c 100644 --- a/t/register.t +++ b/t/register.t @@ -7,6 +7,7 @@ use Test::Pear::LocalLoop; my $framework = Test::Pear::LocalLoop->new; my $t = $framework->framework; 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'); @@ -20,7 +21,7 @@ $schema->resultset('AccountToken')->populate([ #No JSON sent $t->post_ok('/api/register') - ->status_is(400) + ->status_is(400)->or($dump_error) ->json_is('/success', Mojo::JSON->false) ->json_like('/message', qr/JSON is missing/i); @@ -32,11 +33,11 @@ $t->post_ok('/api/register' => json => $testJson) #token missing JSON $testJson = { 'usertype' => 'customer', - 'username' => shift(@names), + 'name' => shift(@names), 'email' => shift(@emails), 'postcode' => 'LA1 1AA', 'password' => 'Meh', - 'age' => 3 + 'age_range' => 3 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(400) @@ -48,11 +49,11 @@ $t->post_ok('/api/register' => json => $testJson) $testJson = { 'usertype' => 'customer', 'token' => ' ', - 'username' => shift(@names), + 'name' => shift(@names), 'email' => shift(@emails), 'postcode' => 'LA1 1AA', 'password' => 'Meh', - 'age' => 3 + 'age_range' => 3 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(401) @@ -60,64 +61,49 @@ $t->post_ok('/api/register' => json => $testJson) ->content_like(qr/token/i) ->content_like(qr/invalid/i); -#username missing JSON +#name missing JSON $testJson = { 'usertype' => 'customer', 'token' => shift(@tokens), 'email' => shift(@emails), 'postcode' => 'LA1 1AA', 'password' => 'Meh', - 'age' => 3 + 'age_range' => 3 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(400) ->json_is('/success', Mojo::JSON->false) - ->content_like(qr/no username sent/i); + ->content_like(qr/no name sent/i); -#Blank username +#Blank name $testJson = { 'usertype' => 'customer', 'token' => shift(@tokens), - 'username' => '', + 'name' => '', 'email' => shift(@emails), 'postcode' => 'LA1 1AA', 'password' => 'Meh', - 'age' => 3 + '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/username/i); + ->content_like(qr/name/i); -#Not alpha numeric chars e.g. ! -$testJson = { - 'usertype' => 'customer', - 'token' => shift(@tokens), - 'username' => 'asa!', - 'email' => shift(@emails), - 'postcode' => 'LA1 1AA', - 'password' => 'Meh', - 'age' => 3 -}; -$t->post_ok('/api/register' => json => $testJson) - ->status_is(400) - ->json_is('/success', Mojo::JSON->false) - ->content_like(qr/username/i); - -my $usernameToReuse = shift(@names); +my $nameToReuse = shift(@names); my $emailToReuse = shift(@emails); #Valid customer $testJson = { 'usertype' => 'customer', 'token' => shift(@tokens), - 'username' => $usernameToReuse, + 'name' => $nameToReuse, 'email' => $emailToReuse, 'postcode' => 'LA1 1AA', 'password' => 'Meh', - 'age' => 3 + 'age_range' => 3 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(200) @@ -127,11 +113,11 @@ $t->post_ok('/api/register' => json => $testJson) $testJson = { 'usertype' => 'customer', 'token' => shift(@tokens), - 'username' => shift(@names), + 'name' => shift(@names), 'email' => shift(@emails), 'postcode' => 'LA1 1AA', 'password' => 'Meh', - 'age' => 2 + 'age_range' => 2 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(200) @@ -141,39 +127,24 @@ $t->post_ok('/api/register' => json => $testJson) $testJson = { 'usertype' => 'customer', 'token' => shift(@tokens), - 'username' => shift(@names), + 'name' => shift(@names), 'email' => shift(@emails), 'postcode' => 'LA1 1AA', 'password' => 'Meh', - 'age' => 1 + 'age_range' => 1 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(200) ->json_is('/success', Mojo::JSON->true); -#Username exists -$testJson = { - 'usertype' => 'customer', - 'token' => shift(@tokens), - 'username' => $usernameToReuse, - 'email' => shift(@emails), - 'postcode' => 'LA1 1AA', - 'password' => 'Meh', - 'age' => 3 -}; -$t->post_ok('/api/register' => json => $testJson) - ->status_is(403) - ->json_is('/success', Mojo::JSON->false) - ->content_like(qr/exists/i); - #email missing JSON $testJson = { 'usertype' => 'customer', 'token' => shift(@tokens), - 'username' => shift(@names), + 'name' => shift(@names), 'postcode' => 'LA1 1AA', 'password' => 'Meh', - 'age' => 3 + 'age_range' => 3 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(400) @@ -184,11 +155,11 @@ $t->post_ok('/api/register' => json => $testJson) $testJson = { 'usertype' => 'customer', 'token' => shift(@tokens), - 'username' => shift(@names), + 'name' => shift(@names), 'email' => 'dfsd@.com', 'postcode' => 'LA1 1AA', 'password' => 'Meh', - 'age' => 2 + 'age_range' => 2 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(400) @@ -200,11 +171,11 @@ $t->post_ok('/api/register' => json => $testJson) $testJson = { 'usertype' => 'customer', 'token' => shift(@tokens), - 'username' => shift(@names), + 'name' => shift(@names), 'email' => 'dfsd@com', 'postcode' => 'LA1 1AA', 'password' => 'Meh', - 'age' => 2 + 'age_range' => 2 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(400) @@ -216,11 +187,11 @@ $t->post_ok('/api/register' => json => $testJson) $testJson = { 'usertype' => 'customer', 'token' => shift(@tokens), - 'username' => shift(@names), + 'name' => shift(@names), 'email' => $emailToReuse, 'postcode' => 'LA1 1AA', 'password' => 'Meh', - 'age' => 2 + 'age_range' => 2 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(403) @@ -232,10 +203,10 @@ $t->post_ok('/api/register' => json => $testJson) $testJson = { 'usertype' => 'customer', 'token' => shift(@tokens), - 'username' => shift(@names), + 'name' => shift(@names), 'email' => shift(@emails), 'password' => 'Meh', - 'age' => 3 + 'age_range' => 3 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(400) @@ -248,10 +219,10 @@ $t->post_ok('/api/register' => json => $testJson) $testJson = { 'usertype' => 'customer', 'token' => shift(@tokens), - 'username' => shift(@names), + 'name' => shift(@names), 'email' => shift(@emails), 'postcode' => 'LA1 1AA', - 'age' => 3 + 'age_range' => 3 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(400) @@ -263,11 +234,11 @@ $t->post_ok('/api/register' => json => $testJson) #usertype missing JSON $testJson = { 'token' => shift(@tokens), - 'username' => shift(@names), + 'name' => shift(@names), 'email' => shift(@emails), 'postcode' => 'LA1 1AA', 'password' => 'Meh', - 'age' => 3 + 'age_range' => 3 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(400) @@ -278,7 +249,7 @@ $t->post_ok('/api/register' => json => $testJson) $testJson = { 'usertype' => 'organisation1', 'token' => shift(@tokens), - 'username' => shift(@names), + 'name' => shift(@names), 'email' => shift(@emails), 'postcode' => 'LA1 1AA', 'password' => 'Meh', @@ -291,11 +262,11 @@ $t->post_ok('/api/register' => json => $testJson) ->content_like(qr/invalid/i); -#age missing JSON +#age_range missing JSON $testJson = { 'usertype' => 'customer', 'token' => shift(@tokens), - 'username' => shift(@names), + 'name' => shift(@names), 'email' => shift(@emails), 'postcode' => 'LA1 1AA', 'password' => 'Meh', @@ -303,29 +274,29 @@ $testJson = { $t->post_ok('/api/register' => json => $testJson) ->status_is(400) ->json_is('/success', Mojo::JSON->false) - ->content_like(qr/no age sent/i); + ->content_like(qr/no age_range sent/i); #Age is invalid $testJson = { 'usertype' => 'customer', 'token' => shift(@tokens), - 'username' => shift(@names), + 'name' => shift(@names), 'email' => shift(@emails), 'postcode' => 'LA1 1AA', 'password' => 'Meh', - 'age' => 'invalid' + 'age_range' => 'invalid' }; $t->post_ok('/api/register' => json => $testJson) ->status_is(400) ->json_is('/success', Mojo::JSON->false) - ->content_like(qr/age/i) + ->content_like(qr/age_range/i) ->content_like(qr/invalid/i); #full address missing JSON $testJson = { 'usertype' => 'organisation', 'token' => shift(@tokens), - 'username' => shift(@names), + 'name' => shift(@names), 'email' => shift(@emails), 'postcode' => 'LA1 1AA', 'password' => 'Meh', @@ -341,7 +312,7 @@ $t->post_ok('/api/register' => json => $testJson) $testJson = { 'usertype' => 'organisation', 'token' => shift(@tokens), - 'username' => shift(@names), + 'name' => shift(@names), 'email' => shift(@emails), 'postcode' => 'LA1 1AA', 'password' => 'Meh', diff --git a/t/search.t b/t/search.t index 7df9213..b0aa1a8 100644 --- a/t/search.t +++ b/t/search.t @@ -7,6 +7,7 @@ use Test::Pear::LocalLoop; my $framework = Test::Pear::LocalLoop->new; my $t = $framework->framework; my $schema = $t->app->schema; +my $dump_error = sub { diag $t->tx->res->to_string }; my @account_tokens = ('a', 'b'); $schema->resultset('AccountToken')->populate([ @@ -30,14 +31,14 @@ my $passwordRufus = 'MakoGold'; my $testJson = { 'usertype' => 'customer', 'token' => shift(@account_tokens), - 'username' => 'RufusShinra', + 'name' => 'RufusShinra', 'email' => $emailRufus, - 'postcode' => 'LA1 1CF', + 'postcode' => 'RG26 5NU', 'password' => $passwordRufus, - 'age' => 1 + 'age_range' => 1 }; $t->post_ok('/api/register' => json => $testJson) - ->status_is(200) + ->status_is(200)->or($dump_error) ->json_is('/success', Mojo::JSON->true); #test with an organisation. @@ -47,7 +48,7 @@ my $passwordBilly = 'Choco'; $testJson = { 'usertype' => 'organisation', 'token' => shift(@account_tokens), - 'username' => 'ChocoBillysGreens', + 'name' => 'ChocoBillysGreens', 'email' => $emailBilly, 'postcode' => 'LA1 1HT', 'password' => $passwordBilly, diff --git a/t/upload.t b/t/upload.t index 7778b5f..8973c8e 100644 --- a/t/upload.t +++ b/t/upload.t @@ -38,11 +38,11 @@ my $passwordRufus = 'MakoGold'; my $testJson = { 'usertype' => 'customer', 'token' => shift(@account_tokens), - 'username' => 'RufusShinra', + 'name' => 'RufusShinra', 'email' => $emailRufus, - 'postcode' => 'E1 MP01', + 'postcode' => 'GU10 5SA', 'password' => $passwordRufus, - 'age' => 1 + 'age_range' => 1 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(200) @@ -54,11 +54,11 @@ my $passwordHojo = 'Mako'; $testJson = { 'usertype' => 'customer', 'token' => shift(@account_tokens), - 'username' => 'ProfessorHojo', + 'name' => 'ProfessorHojo', 'email' => $emailHojo, - 'postcode' => 'E1 MP01', + 'postcode' => 'DE15 9LT', 'password' => $passwordHojo, - 'age' => 1 + 'age_range' => 1 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(200) @@ -70,9 +70,9 @@ my $passwordBilly = 'Choco'; $testJson = { 'usertype' => 'organisation', 'token' => shift(@account_tokens), - 'username' => 'ChocoBillysGreens', + 'name' => 'ChocoBillysGreens', 'email' => $emailBilly, - 'postcode' => 'E4 C12', + 'postcode' => 'SO50 7NJ', 'password' => $passwordBilly, 'street_name' => 'Chocobo Farm, Eastern Continent', 'town' => 'Gaia',