From d73ddb6ba26d74dbde0fdf43a0eb1fd3d0fda467 Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Tue, 13 Jun 2017 21:02:24 +0100 Subject: [PATCH] Remove AgeRange table and first pass of fixing tests --- lib/Pear/LocalLoop/Controller/Api/Register.pm | 44 +++++++++-------- lib/Pear/LocalLoop/Plugin/Validators.pm | 5 ++ lib/Pear/LocalLoop/Schema/Result/AgeRange.pm | 37 --------------- lib/Pear/LocalLoop/Schema/Result/Customer.pm | 10 +--- lib/Test/Pear/LocalLoop.pm | 7 --- t/{ => api}/register.t | 47 ++++++++++--------- 6 files changed, 55 insertions(+), 95 deletions(-) delete mode 100644 lib/Pear/LocalLoop/Schema/Result/AgeRange.pm rename t/{ => api}/register.t (92%) diff --git a/lib/Pear/LocalLoop/Controller/Api/Register.pm b/lib/Pear/LocalLoop/Controller/Api/Register.pm index 4b56b9a..ee760ee 100644 --- a/lib/Pear/LocalLoop/Controller/Api/Register.pm +++ b/lib/Pear/LocalLoop/Controller/Api/Register.pm @@ -33,10 +33,11 @@ has error_messages => sub { required => { message => 'No usertype sent.', status => 400 }, in => { message => '"usertype" 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 }, + year_of_birth => { + required => { message => 'No year_of_birth sent.', status => 400 }, + number => { message => 'year_of_birth is invalid', status => 400 }, + gt_num => { message => 'year_of_birth must be within last 150 years', status => 400 }, + lt_num => { message => 'year_of_birth must be atleast 10 years ago', status => 400 }, }, street_name => { required => { message => 'No street_name sent.', status => 400 }, @@ -68,8 +69,8 @@ sub post_register{ 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); + my $year = DateTime->now->year; + $validation->required('year_of_birth')->number->gt_num($year - 150)->lt_num($year - 10); } elsif ( $usertype eq 'organisation' ) { $validation->required('name'); $validation->required('street_name'); @@ -85,13 +86,15 @@ sub post_register{ name => $validation->param('token'), used => 0, })->update({ used => 1 }); + # Create customer as a seperate step, so we dont leak data + my $customer = $c->schema->resultset('Customer')->create({ + full_name => $validation->param('full_name'), + display_name => $validation->param('display_name'), + year_of_birth => $validation->param('year_of_birth'), + postcode => $validation->param('postcode'), + }); $c->schema->resultset('User')->create({ - customer => { - full_name => $validation->param('full_name'), - display_name => $validation->param('display_name'), - age_range_id => $validation->param('age_range'), - postcode => $validation->param('postcode'), - }, + customer => $customer, email => $validation->param('email'), password => $validation->param('password'), }); @@ -106,15 +109,16 @@ sub post_register{ name => $validation->param('token'), used => 0, })->update({ used => 1 }); + my $organisation = $c->schema->resultset('Organisation')->create({ + name => $validation->param('name'), + street_name => $validation->param('street_name'), + town => $validation->param('town'), + postcode => $validation->param('postcode'), + }); $c->schema->resultset('User')->create({ - organisation => { - name => $validation->param('name'), - street_name => $validation->param('street_name'), - town => $validation->param('town'), - postcode => $validation->param('postcode'), - }, - email => $validation->param('email'), - password => $validation->param('password'), + organisation => $organisation, + email => $validation->param('email'), + password => $validation->param('password'), }); }); } diff --git a/lib/Pear/LocalLoop/Plugin/Validators.pm b/lib/Pear/LocalLoop/Plugin/Validators.pm index 6722424..677b4d0 100644 --- a/lib/Pear/LocalLoop/Plugin/Validators.pm +++ b/lib/Pear/LocalLoop/Plugin/Validators.pm @@ -48,6 +48,11 @@ sub register { return $value > $check ? undef : 1; }); + $app->validator->add_check( lt_num => sub { + my ( $validation, $name, $value, $check ) = @_; + return $value < $check ? undef : 1; + }); + $app->validator->add_check( filetype => sub { my ( $validation, $name, $value, $filetype ) = @_; my ( undef, undef, $extension ) = fileparse $value->filename, qr/\.[^.]*/; diff --git a/lib/Pear/LocalLoop/Schema/Result/AgeRange.pm b/lib/Pear/LocalLoop/Schema/Result/AgeRange.pm deleted file mode 100644 index 73c4ffd..0000000 --- a/lib/Pear/LocalLoop/Schema/Result/AgeRange.pm +++ /dev/null @@ -1,37 +0,0 @@ -package Pear::LocalLoop::Schema::Result::AgeRange; - -use strict; -use warnings; - -use base 'DBIx::Class::Core'; - -__PACKAGE__->table("age_ranges"); - -__PACKAGE__->add_columns( - "id", - { data_type => "integer", is_auto_increment => 1, is_nullable => 0 }, - "string", - { data_type => "text", is_nullable => 0 }, -); - -__PACKAGE__->set_primary_key("id"); - -__PACKAGE__->add_unique_constraint(["string"]); - -__PACKAGE__->has_many( - "customers", - "Pear::LocalLoop::Schema::Result::Customer", - { "foreign.age_range_id" => "self.id" }, - { cascade_copy => 0, cascade_delete => 0 }, -); - -sub TO_JSON { - my $self = shift; - - return { - id => $self->id, - string => $self->string, - }; -} - -1; diff --git a/lib/Pear/LocalLoop/Schema/Result/Customer.pm b/lib/Pear/LocalLoop/Schema/Result/Customer.pm index 5cd67a1..4ce92e2 100644 --- a/lib/Pear/LocalLoop/Schema/Result/Customer.pm +++ b/lib/Pear/LocalLoop/Schema/Result/Customer.pm @@ -23,9 +23,8 @@ __PACKAGE__->add_columns( size => 255, is_nullable => 0, }, - "age_range_id" => { + "year_of_birth" => { data_type => "integer", - is_foreign_key => 1, is_nullable => 0, }, "postcode" => { @@ -37,13 +36,6 @@ __PACKAGE__->add_columns( __PACKAGE__->set_primary_key("id"); -__PACKAGE__->belongs_to( - "age_range", - "Pear::LocalLoop::Schema::Result::AgeRange", - { id => "age_range_id" }, - { is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" }, -); - __PACKAGE__->might_have( "user", "Pear::LocalLoop::Schema::Result::User", diff --git a/lib/Test/Pear/LocalLoop.pm b/lib/Test/Pear/LocalLoop.pm index 9581b02..12758fe 100644 --- a/lib/Test/Pear/LocalLoop.pm +++ b/lib/Test/Pear/LocalLoop.pm @@ -30,13 +30,6 @@ has framework => sub { my $schema = $t->app->schema; $schema->deploy; - $schema->resultset('AgeRange')->populate([ - [ qw/ string / ], - [ '20-35' ], - [ '35-50' ], - [ '50+' ], - ]); - $schema->resultset('Leaderboard')->populate([ [ qw/ name type / ], [ 'Daily Total', 'daily_total' ], diff --git a/t/register.t b/t/api/register.t similarity index 92% rename from t/register.t rename to t/api/register.t index a654500..40020ac 100644 --- a/t/register.t +++ b/t/api/register.t @@ -7,7 +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 }; +my $dump_error = $framework->dump_error; #Variables to be used for uniqueness when testing. 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'); @@ -36,7 +36,7 @@ $testJson = { 'email' => 'a@b.com', 'postcode' => 'LA1 1AA', 'password' => 'Meh', - 'age_range' => 3 + 'year_of_birth' => 2005 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(400) @@ -52,7 +52,7 @@ $testJson = { 'email' => 'a@b.com', 'postcode' => 'LA1 1AA', 'password' => 'Meh', - 'age_range' => 3 + 'year_of_birth' => 2005 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(401) @@ -68,7 +68,7 @@ $testJson = { 'email' => 'a@b.com', 'postcode' => 'LA1 1AA', 'password' => 'Meh', - 'age_range' => 3 + 'year_of_birth' => 2005 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(400) @@ -82,7 +82,7 @@ $testJson = { 'email' => 'a@b.com', 'postcode' => 'LA1 1AA', 'password' => 'Meh', - 'age_range' => 3 + 'year_of_birth' => 2005 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(400) @@ -98,7 +98,7 @@ $testJson = { 'email' => 'a@b.com', 'postcode' => 'LA1 1AA', 'password' => 'Meh', - 'age_range' => 3 + 'year_of_birth' => 2005 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(400) @@ -114,7 +114,7 @@ $testJson = { 'email' => 'a@b.com', 'postcode' => 'LA1 1AA', 'password' => 'Meh', - 'age_range' => 3 + 'year_of_birth' => 2005 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(400) @@ -132,7 +132,7 @@ $testJson = { 'email' => 'a@b.com', 'postcode' => 'LA1 1AA', 'password' => 'Meh', - 'age_range' => 3 + 'year_of_birth' => 2005 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(200) @@ -147,11 +147,14 @@ $testJson = { 'email' => 'b@c.com', 'postcode' => 'LA1 1AA', 'password' => 'Meh', - 'age_range' => 2 + 'year_of_birth' => 2005 }; $t->post_ok('/api/register' => json => $testJson) + ->or($dump_error) ->status_is(200) - ->json_is('/success', Mojo::JSON->true); + ->or($dump_error) + ->json_is('/success', Mojo::JSON->true) + ->or($dump_error); #Valid customer3 $testJson = { @@ -162,7 +165,7 @@ $testJson = { 'email' => 'c@d.com', 'postcode' => 'LA1 1AA', 'password' => 'Meh', - 'age_range' => 1 + 'year_of_birth' => 2005 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(200) @@ -176,7 +179,7 @@ $testJson = { 'display_name' => 'test name', 'postcode' => 'LA1 1AA', 'password' => 'Meh', - 'age_range' => 3 + 'year_of_birth' => 2005 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(400) @@ -192,7 +195,7 @@ $testJson = { 'email' => 'dfsd@.com', 'postcode' => 'LA1 1AA', 'password' => 'Meh', - 'age_range' => 2 + 'year_of_birth' => 2006 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(400) @@ -209,7 +212,7 @@ $testJson = { 'email' => 'dfsd@com', 'postcode' => 'LA1 1AA', 'password' => 'Meh', - 'age_range' => 2 + 'year_of_birth' => 2006 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(400) @@ -226,7 +229,7 @@ $testJson = { 'email' => 'a@b.com', 'postcode' => 'LA1 1AA', 'password' => 'Meh', - 'age_range' => 2 + 'year_of_birth' => 2006 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(403) @@ -242,7 +245,7 @@ $testJson = { 'display_name' => 'test name', 'email' => 'd@e.com', 'password' => 'Meh', - 'age_range' => 3 + 'year_of_birth' => 2006 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(400) @@ -259,7 +262,7 @@ $testJson = { 'display_name' => 'test name', 'email' => 'd@e.com', 'postcode' => 'LA1 1AA', - 'age_range' => 3 + 'year_of_birth' => 2006 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(400) @@ -276,7 +279,7 @@ $testJson = { 'email' => 'd@e.com', 'postcode' => 'LA1 1AA', 'password' => 'Meh', - 'age_range' => 3 + 'year_of_birth' => 2006 }; $t->post_ok('/api/register' => json => $testJson) ->status_is(400) @@ -300,7 +303,7 @@ $t->post_ok('/api/register' => json => $testJson) ->content_like(qr/invalid/i); -#age_range missing JSON +#year_of_birth missing JSON $testJson = { 'usertype' => 'customer', 'token' => 'f', @@ -313,7 +316,7 @@ $testJson = { $t->post_ok('/api/register' => json => $testJson) ->status_is(400) ->json_is('/success', Mojo::JSON->false) - ->content_like(qr/no age_range sent/i); + ->content_like(qr/no year_of_birth sent/i); #Age is invalid $testJson = { @@ -324,12 +327,12 @@ $testJson = { 'email' => 'test@example.com', 'postcode' => 'LA1 1AA', 'password' => 'Meh', - 'age_range' => 'invalid' + 'year_of_birth' => 'invalid' }; $t->post_ok('/api/register' => json => $testJson) ->status_is(400) ->json_is('/success', Mojo::JSON->false) - ->content_like(qr/age_range/i) + ->content_like(qr/year_of_birth/i) ->content_like(qr/invalid/i); #full address missing JSON