Remove AgeRange table and first pass of fixing tests

This commit is contained in:
Tom Bloor 2017-06-13 21:02:24 +01:00
parent a1a608ea44
commit d73ddb6ba2
6 changed files with 55 additions and 95 deletions

View file

@ -33,10 +33,11 @@ has error_messages => sub {
required => { message => 'No usertype sent.', status => 400 }, required => { message => 'No usertype sent.', status => 400 },
in => { message => '"usertype" is invalid.', status => 400 }, in => { message => '"usertype" is invalid.', status => 400 },
}, },
age_range => { year_of_birth => {
required => { message => 'No age_range sent.', status => 400 }, required => { message => 'No year_of_birth sent.', status => 400 },
number => { message => 'age_range is invalid', status => 400 }, number => { message => 'year_of_birth is invalid', status => 400 },
in_resultset => { message => 'age_range 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 => { street_name => {
required => { message => 'No street_name sent.', status => 400 }, required => { message => 'No street_name sent.', status => 400 },
@ -68,8 +69,8 @@ sub post_register{
if ( $usertype eq 'customer' ) { if ( $usertype eq 'customer' ) {
$validation->required('display_name'); $validation->required('display_name');
$validation->required('full_name'); $validation->required('full_name');
my $age_rs = $c->schema->resultset('AgeRange'); my $year = DateTime->now->year;
$validation->required('age_range')->number->in_resultset('id', $age_rs); $validation->required('year_of_birth')->number->gt_num($year - 150)->lt_num($year - 10);
} elsif ( $usertype eq 'organisation' ) { } elsif ( $usertype eq 'organisation' ) {
$validation->required('name'); $validation->required('name');
$validation->required('street_name'); $validation->required('street_name');
@ -85,13 +86,15 @@ sub post_register{
name => $validation->param('token'), name => $validation->param('token'),
used => 0, used => 0,
})->update({ used => 1 }); })->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({ $c->schema->resultset('User')->create({
customer => { customer => $customer,
full_name => $validation->param('full_name'),
display_name => $validation->param('display_name'),
age_range_id => $validation->param('age_range'),
postcode => $validation->param('postcode'),
},
email => $validation->param('email'), email => $validation->param('email'),
password => $validation->param('password'), password => $validation->param('password'),
}); });
@ -106,15 +109,16 @@ sub post_register{
name => $validation->param('token'), name => $validation->param('token'),
used => 0, used => 0,
})->update({ used => 1 }); })->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({ $c->schema->resultset('User')->create({
organisation => { organisation => $organisation,
name => $validation->param('name'), email => $validation->param('email'),
street_name => $validation->param('street_name'), password => $validation->param('password'),
town => $validation->param('town'),
postcode => $validation->param('postcode'),
},
email => $validation->param('email'),
password => $validation->param('password'),
}); });
}); });
} }

View file

@ -48,6 +48,11 @@ sub register {
return $value > $check ? undef : 1; 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 { $app->validator->add_check( filetype => sub {
my ( $validation, $name, $value, $filetype ) = @_; my ( $validation, $name, $value, $filetype ) = @_;
my ( undef, undef, $extension ) = fileparse $value->filename, qr/\.[^.]*/; my ( undef, undef, $extension ) = fileparse $value->filename, qr/\.[^.]*/;

View file

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

View file

@ -23,9 +23,8 @@ __PACKAGE__->add_columns(
size => 255, size => 255,
is_nullable => 0, is_nullable => 0,
}, },
"age_range_id" => { "year_of_birth" => {
data_type => "integer", data_type => "integer",
is_foreign_key => 1,
is_nullable => 0, is_nullable => 0,
}, },
"postcode" => { "postcode" => {
@ -37,13 +36,6 @@ __PACKAGE__->add_columns(
__PACKAGE__->set_primary_key("id"); __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( __PACKAGE__->might_have(
"user", "user",
"Pear::LocalLoop::Schema::Result::User", "Pear::LocalLoop::Schema::Result::User",

View file

@ -30,13 +30,6 @@ has framework => sub {
my $schema = $t->app->schema; my $schema = $t->app->schema;
$schema->deploy; $schema->deploy;
$schema->resultset('AgeRange')->populate([
[ qw/ string / ],
[ '20-35' ],
[ '35-50' ],
[ '50+' ],
]);
$schema->resultset('Leaderboard')->populate([ $schema->resultset('Leaderboard')->populate([
[ qw/ name type / ], [ qw/ name type / ],
[ 'Daily Total', 'daily_total' ], [ 'Daily Total', 'daily_total' ],

View file

@ -7,7 +7,7 @@ use Test::Pear::LocalLoop;
my $framework = Test::Pear::LocalLoop->new; my $framework = Test::Pear::LocalLoop->new;
my $t = $framework->framework; my $t = $framework->framework;
my $schema = $t->app->schema; 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. #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'); 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', 'email' => 'a@b.com',
'postcode' => 'LA1 1AA', 'postcode' => 'LA1 1AA',
'password' => 'Meh', 'password' => 'Meh',
'age_range' => 3 'year_of_birth' => 2005
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(400) ->status_is(400)
@ -52,7 +52,7 @@ $testJson = {
'email' => 'a@b.com', 'email' => 'a@b.com',
'postcode' => 'LA1 1AA', 'postcode' => 'LA1 1AA',
'password' => 'Meh', 'password' => 'Meh',
'age_range' => 3 'year_of_birth' => 2005
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(401) ->status_is(401)
@ -68,7 +68,7 @@ $testJson = {
'email' => 'a@b.com', 'email' => 'a@b.com',
'postcode' => 'LA1 1AA', 'postcode' => 'LA1 1AA',
'password' => 'Meh', 'password' => 'Meh',
'age_range' => 3 'year_of_birth' => 2005
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(400) ->status_is(400)
@ -82,7 +82,7 @@ $testJson = {
'email' => 'a@b.com', 'email' => 'a@b.com',
'postcode' => 'LA1 1AA', 'postcode' => 'LA1 1AA',
'password' => 'Meh', 'password' => 'Meh',
'age_range' => 3 'year_of_birth' => 2005
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(400) ->status_is(400)
@ -98,7 +98,7 @@ $testJson = {
'email' => 'a@b.com', 'email' => 'a@b.com',
'postcode' => 'LA1 1AA', 'postcode' => 'LA1 1AA',
'password' => 'Meh', 'password' => 'Meh',
'age_range' => 3 'year_of_birth' => 2005
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(400) ->status_is(400)
@ -114,7 +114,7 @@ $testJson = {
'email' => 'a@b.com', 'email' => 'a@b.com',
'postcode' => 'LA1 1AA', 'postcode' => 'LA1 1AA',
'password' => 'Meh', 'password' => 'Meh',
'age_range' => 3 'year_of_birth' => 2005
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(400) ->status_is(400)
@ -132,7 +132,7 @@ $testJson = {
'email' => 'a@b.com', 'email' => 'a@b.com',
'postcode' => 'LA1 1AA', 'postcode' => 'LA1 1AA',
'password' => 'Meh', 'password' => 'Meh',
'age_range' => 3 'year_of_birth' => 2005
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(200) ->status_is(200)
@ -147,11 +147,14 @@ $testJson = {
'email' => 'b@c.com', 'email' => 'b@c.com',
'postcode' => 'LA1 1AA', 'postcode' => 'LA1 1AA',
'password' => 'Meh', 'password' => 'Meh',
'age_range' => 2 'year_of_birth' => 2005
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->or($dump_error)
->status_is(200) ->status_is(200)
->json_is('/success', Mojo::JSON->true); ->or($dump_error)
->json_is('/success', Mojo::JSON->true)
->or($dump_error);
#Valid customer3 #Valid customer3
$testJson = { $testJson = {
@ -162,7 +165,7 @@ $testJson = {
'email' => 'c@d.com', 'email' => 'c@d.com',
'postcode' => 'LA1 1AA', 'postcode' => 'LA1 1AA',
'password' => 'Meh', 'password' => 'Meh',
'age_range' => 1 'year_of_birth' => 2005
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(200) ->status_is(200)
@ -176,7 +179,7 @@ $testJson = {
'display_name' => 'test name', 'display_name' => 'test name',
'postcode' => 'LA1 1AA', 'postcode' => 'LA1 1AA',
'password' => 'Meh', 'password' => 'Meh',
'age_range' => 3 'year_of_birth' => 2005
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(400) ->status_is(400)
@ -192,7 +195,7 @@ $testJson = {
'email' => 'dfsd@.com', 'email' => 'dfsd@.com',
'postcode' => 'LA1 1AA', 'postcode' => 'LA1 1AA',
'password' => 'Meh', 'password' => 'Meh',
'age_range' => 2 'year_of_birth' => 2006
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(400) ->status_is(400)
@ -209,7 +212,7 @@ $testJson = {
'email' => 'dfsd@com', 'email' => 'dfsd@com',
'postcode' => 'LA1 1AA', 'postcode' => 'LA1 1AA',
'password' => 'Meh', 'password' => 'Meh',
'age_range' => 2 'year_of_birth' => 2006
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(400) ->status_is(400)
@ -226,7 +229,7 @@ $testJson = {
'email' => 'a@b.com', 'email' => 'a@b.com',
'postcode' => 'LA1 1AA', 'postcode' => 'LA1 1AA',
'password' => 'Meh', 'password' => 'Meh',
'age_range' => 2 'year_of_birth' => 2006
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(403) ->status_is(403)
@ -242,7 +245,7 @@ $testJson = {
'display_name' => 'test name', 'display_name' => 'test name',
'email' => 'd@e.com', 'email' => 'd@e.com',
'password' => 'Meh', 'password' => 'Meh',
'age_range' => 3 'year_of_birth' => 2006
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(400) ->status_is(400)
@ -259,7 +262,7 @@ $testJson = {
'display_name' => 'test name', 'display_name' => 'test name',
'email' => 'd@e.com', 'email' => 'd@e.com',
'postcode' => 'LA1 1AA', 'postcode' => 'LA1 1AA',
'age_range' => 3 'year_of_birth' => 2006
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(400) ->status_is(400)
@ -276,7 +279,7 @@ $testJson = {
'email' => 'd@e.com', 'email' => 'd@e.com',
'postcode' => 'LA1 1AA', 'postcode' => 'LA1 1AA',
'password' => 'Meh', 'password' => 'Meh',
'age_range' => 3 'year_of_birth' => 2006
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(400) ->status_is(400)
@ -300,7 +303,7 @@ $t->post_ok('/api/register' => json => $testJson)
->content_like(qr/invalid/i); ->content_like(qr/invalid/i);
#age_range missing JSON #year_of_birth missing JSON
$testJson = { $testJson = {
'usertype' => 'customer', 'usertype' => 'customer',
'token' => 'f', 'token' => 'f',
@ -313,7 +316,7 @@ $testJson = {
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(400) ->status_is(400)
->json_is('/success', Mojo::JSON->false) ->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 #Age is invalid
$testJson = { $testJson = {
@ -324,12 +327,12 @@ $testJson = {
'email' => 'test@example.com', 'email' => 'test@example.com',
'postcode' => 'LA1 1AA', 'postcode' => 'LA1 1AA',
'password' => 'Meh', 'password' => 'Meh',
'age_range' => 'invalid' 'year_of_birth' => 'invalid'
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(400) ->status_is(400)
->json_is('/success', Mojo::JSON->false) ->json_is('/success', Mojo::JSON->false)
->content_like(qr/age_range/i) ->content_like(qr/year_of_birth/i)
->content_like(qr/invalid/i); ->content_like(qr/invalid/i);
#full address missing JSON #full address missing JSON