Merge branch 'TBSiver/Year-Of-Birth' into development

This commit is contained in:
Tom Bloor 2017-06-13 21:48:46 +01:00
commit ac9041f02a
22 changed files with 84 additions and 324 deletions

View file

@ -125,7 +125,6 @@ sub startup {
my $c = shift;
$c->respond_to(any => { data => '', status => 200 });
});
$api_public_get->get('/info/ages')->to('api-info#get_ages');
# Always available api routes
my $api_public = $api_public_get->under('/')->to('api-auth#check_json');

View file

@ -25,13 +25,6 @@ sub run {
my $schema = $self->app->schema;
$schema->resultset('AgeRange')->populate([
[ qw/ string / ],
[ '20-35' ],
[ '35-50' ],
[ '50+' ],
]);
$schema->resultset('Leaderboard')->populate([
[ qw/ name type / ],
[ 'Daily Total', 'daily_total' ],
@ -50,7 +43,7 @@ sub run {
customer => {
full_name => 'Test User',
display_name => 'Test User',
age_range_id => 1,
year_of_birth => 2006,
postcode => 'LA1 1AA',
},
administrator => {},
@ -62,7 +55,7 @@ sub run {
customer => {
full_name => 'Test User 2',
display_name => 'Test User 2',
age_range_id => 1,
year_of_birth => 2006,
postcode => 'LA1 1AA',
},
});
@ -73,7 +66,7 @@ sub run {
customer => {
full_name => 'Test User 3',
display_name => 'Test User 3',
age_range_id => 1,
year_of_birth => 2006,
postcode => 'LA1 1AA',
},
});

View file

@ -1,12 +0,0 @@
package Pear::LocalLoop::Controller::Api::Info;
use Mojo::Base 'Mojolicious::Controller';
sub get_ages {
my $c = shift;
my $ages = $c->schema->resultset('AgeRange');
$c->render( json => { ages => [ $ages->all ] } );
}
1;

View file

@ -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'),
});
});
}

View file

@ -1,84 +0,0 @@
package Pear::LocalLoop::Controller::Register;
use Mojo::Base 'Mojolicious::Controller';
use DateTime;
has error_messages => sub {
return {
name => 'Full Name is required',
email => 'Email Address is required, and must be a valid address that is not already registered',
password => 'Password is required, and must match the Confirmation field',
postcode => 'Postcode is required, and must be a valid UK Postcode',
token => 'Token is required, and must be a valid, unused token',
agerange => 'Age Range is required, and must be a selection from the drop-down',
unknown => 'Sorry, there was a problem registering! Have you already registered?',
};
};
sub index {
my $c = shift;
my $agerange_rs = $c->schema->resultset('AgeRange');
$agerange_rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
$c->stash( ageranges => [ $agerange_rs->all ], form_data => {}, error => undef );
}
sub register {
my $c = shift;
my $validation = $c->validation;
$validation->required('name');
$validation->required('password')->equal_to('password2');
$validation->required('postcode')->postcode;
my $user_rs = $c->schema->resultset('User');
$validation->required('email')->email->not_in_resultset('email', $user_rs);
my $token_rs = $c->schema->resultset('AccountToken')->search_rs({used => 0});
$validation->required('token')->in_resultset('name', $token_rs);
my $age_rs = $c->schema->resultset('AgeRange');
$validation->required('agerange')->in_resultset('id', $age_rs);
my @error_messages;
if ( $validation->has_error ) {
my $failed_vals = $validation->failed;
@error_messages = map {$c->error_messages->{ $_ } } @$failed_vals;
} else {
my $new_user = $c->schema->resultset('User')->find_or_new({
email => $validation->param('email'),
password => $validation->param('password'),
customer => {
username => $validation->param('name'),
postcode => $validation->param('postcode'),
age_range_id => $validation->param('agerange'),
},
});
if ( $new_user->in_storage ) {
@error_messages = ( $c->error_messages->{unknown} );
} else {
$new_user->insert;
}
}
if ( scalar @error_messages ) {
$age_rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
$c->stash(
error => \@error_messages,
ageranges => [ $age_rs->all ],
form_data => {
name => $validation->param('name'),
email => $validation->param('email'),
postcode => $validation->param('postcode'),
token => $validation->param('token'),
agerange => $validation->param('agerange'),
}
);
$c->render( template => 'register/index' );
} else {
$c->flash( success => 'Registered Successfully, please log in' );
$c->redirect_to('/');
}
}
1;

View file

@ -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/\.[^.]*/;

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,
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",

View file

@ -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' ],

View file

@ -30,7 +30,7 @@ my $testJson = {
'email' => $emailReno,
'postcode' => 'SA4 3FA',
'password' => $passwordReno,
'age_range' => 1
year_of_birth => 2006
};
$t->post_ok('/api/register' => json => $testJson)
->status_is(200)->or($dump_error)
@ -64,7 +64,7 @@ $testJson = {
'email' => $emailAdmin,
'postcode' => 'HD5 9XU',
'password' => $passwordAdmin,
'age_range' => 2
year_of_birth => 2006
};
$t->post_ok('/api/register' => json => $testJson)
->status_is(200)

View file

@ -30,7 +30,7 @@ my $testJson = {
'email' => $emailReno,
'postcode' => 'SA4 3FA',
'password' => $passwordReno,
'age_range' => 1
year_of_birth => 2006
};
$t->post_ok('/api/register' => json => $testJson)
->status_is(200)
@ -65,7 +65,7 @@ $testJson = {
'email' => $emailAdmin,
'postcode' => 'HD5 9XU',
'password' => $passwordAdmin,
'age_range' => 2
year_of_birth => 2006
};
$t->post_ok('/api/register' => json => $testJson)
->status_is(200)

View file

@ -24,7 +24,7 @@ my $test_json = {
'email' => $email,
'postcode' => 'LA1 1AA',
'password' => $password,
'age_range' => 1
year_of_birth => 2006
};
$t->post_ok('/api/register' => json => $test_json)
->status_is(200)

View file

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

View file

@ -36,7 +36,7 @@ my $testJson = {
'email' => $emailRufus,
'postcode' => 'RG26 5NU',
'password' => $passwordRufus,
'age_range' => 1
'year_of_birth' => 2006
};
$t->post_ok('/api/register' => json => $testJson)
->status_is(200)->or($framework->dump_error)

View file

@ -17,7 +17,7 @@ my $user = {
email => 'test@example.com',
postcode => 'LA1 1AA',
password => 'abc123',
age_range => 1,
year_of_birth => 2006,
};
my $org = {

View file

@ -17,7 +17,7 @@ my $user1 = {
email => 'test1@example.com',
postcode => 'LA1 1AA',
password => 'abc123',
age_range => 1,
year_of_birth => 2006,
};
my $user2 = {
@ -27,7 +27,7 @@ my $user2 = {
email => 'test2@example.com',
postcode => 'LA1 1AA',
password => 'abc123',
age_range => 1,
year_of_birth => 2006,
};
my $user3 = {
@ -37,7 +37,7 @@ my $user3 = {
email => 'test3@example.com',
postcode => 'LA1 1AA',
password => 'abc123',
age_range => 1,
year_of_birth => 2006,
};
my $user4 = {
@ -47,7 +47,7 @@ my $user4 = {
email => 'test4@example.com',
postcode => 'LA1 1AA',
password => 'abc123',
age_range => 1,
year_of_birth => 2006,
};
my $org = {

View file

@ -43,7 +43,7 @@ my $testJson = {
'email' => $emailRufus,
'postcode' => 'GU10 5SA',
'password' => $passwordRufus,
'age_range' => 1
'year_of_birth' => 2006
};
$t->post_ok('/api/register' => json => $testJson)
->status_is(200)
@ -60,7 +60,7 @@ $testJson = {
'email' => $emailHojo,
'postcode' => 'DE15 9LT',
'password' => $passwordHojo,
'age_range' => 1
'year_of_birth' => 2006
};
$t->post_ok('/api/register' => json => $testJson)
->status_is(200)

View file

@ -24,7 +24,7 @@ $framework->register_customer({
'email' => $email,
'postcode' => 'LA1 1AA',
'password' => $password,
'age_range' => 1
year_of_birth => 2006
});
my $session_key = $framework->login({

View file

@ -17,7 +17,7 @@ my $user1 = {
email => 'test1@example.com',
postcode => 'LA1 1AA',
password => 'abc123',
age_range => 1,
year_of_birth => 2006,
};
my $user2 = {
@ -27,7 +27,7 @@ my $user2 = {
email => 'test2@example.com',
postcode => 'LA1 1AA',
password => 'abc123',
age_range => 1,
year_of_birth => 2006,
};
my $user3 = {
@ -37,7 +37,7 @@ my $user3 = {
email => 'test3@example.com',
postcode => 'LA1 1AA',
password => 'abc123',
age_range => 1,
year_of_birth => 2006,
};
my $user4 = {
@ -47,7 +47,7 @@ my $user4 = {
email => 'test4@example.com',
postcode => 'LA1 1AA',
password => 'abc123',
age_range => 1,
year_of_birth => 2006,
};
my $org = {

View file

@ -17,7 +17,7 @@ my $user1 = {
email => 'test1@example.com',
postcode => 'LA1 1AA',
password => 'abc123',
age_range => 1,
year_of_birth => 2006,
};
my $user2 = {
@ -27,7 +27,7 @@ my $user2 = {
email => 'test2@example.com',
postcode => 'LA1 1AA',
password => 'abc123',
age_range => 1,
year_of_birth => 2006,
};
my $user3 = {
@ -37,7 +37,7 @@ my $user3 = {
email => 'test3@example.com',
postcode => 'LA1 1AA',
password => 'abc123',
age_range => 1,
year_of_birth => 2006,
};
my $user4 = {
@ -47,7 +47,7 @@ my $user4 = {
email => 'test4@example.com',
postcode => 'LA1 1AA',
password => 'abc123',
age_range => 1,
year_of_birth => 2006,
};
my $org = {

View file

@ -17,7 +17,7 @@ my $user1 = {
email => 'test1@example.com',
postcode => 'LA1 1AA',
password => 'abc123',
age_range => 1,
year_of_birth => 2006,
};
my $user2 = {
@ -27,7 +27,7 @@ my $user2 = {
email => 'test2@example.com',
postcode => 'LA1 1AA',
password => 'abc123',
age_range => 1,
year_of_birth => 2006,
};
my $user3 = {
@ -37,7 +37,7 @@ my $user3 = {
email => 'test3@example.com',
postcode => 'LA1 1AA',
password => 'abc123',
age_range => 1,
year_of_birth => 2006,
};
my $user4 = {
@ -47,7 +47,7 @@ my $user4 = {
email => 'test4@example.com',
postcode => 'LA1 1AA',
password => 'abc123',
age_range => 1,
year_of_birth => 2006,
};
my $org = {

View file

@ -1,96 +0,0 @@
% layout 'default';
% title 'Register';
% content_for css => begin
<style>
body {
background: whitesmoke;
padding-top: 70px;
}
.panel {
background: white;
padding: 16px;
-webkit-box-shadow: 0px 2px 4px 0px rgba(0,0,0,0.2);
-moz-box-shadow: 0px 2px 4px 0px rgba(0,0,0,0.2);
box-shadow: 0px 2px 4px 0px rgba(0,0,0,0.2);
}
</style>
% end
% content_for javascript => begin
% end
<div class="container">
% if ( defined $error ) {
<div class="alert alert-danger" role="alert">
<strong>Error!</strong><br>
% for my $error_single ( @$error ) {
<%= $error_single %><br>
% }
</div>
% }
<div class="row justify-content-center">
<div class="col-8">
<div class="register-form panel">
<form action="<%= url_for %>" method="post">
<div class="form-group">
<label for="register-token">Invitation Token</label>
<input type="text"
id="register-token"
class="form-control"
name="token"
value="<%= $form_data->{token} %>"
required>
</div>
<div class="form-group">
<label for="register-name">Full Name</label>
<input type="text"
id="register-name"
class="form-control"
name="name"
value="<%= $form_data->{name} %>"
required>
</div>
<div class="form-group">
<label for="register-email">Email Address</label>
<input type="email"
id="register-email"
class="form-control"
name="email"
value="<%= $form_data->{email} %>"
required>
</div>
<div class="form-group">
<label for="register-postcode">Postcode</label>
<input type="text"
id="register-postcode"
class="form-control"
name="postcode"
value="<%= $form_data->{postcode} %>"
required>
</div>
<div class="form-group">
<label for="register-age">Age Range</label>
<select id="register-age" class="form-control" name="agerange" required>
% for my $agerange ( @$ageranges ) {
<option value="<%= $agerange->{id} %>"<%= defined $form_data->{agerange} && $form_data->{agerange} eq $agerange->{id} ? ' selected' : '' %>>
<%= $agerange->{string} %>
</option>
% }
</select>
</div>
<div class="form-group">
<label for="login-password">Password</label>
<input type="password" id="login-password" class="form-control" name="password" required>
</div>
<div class="form-group">
<label for="login-password">Confirm Password</label>
<input type="password" id="login-password" class="form-control" name="password2" required>
</div>
<input type="submit" class="btn btn-block btn-primary" value="Register">
</form>
</div>
</div>
</div>
</div><!-- Container fluid-->