2017-04-18 12:05:04 +00:00
|
|
|
use Mojo::Base -strict;
|
|
|
|
|
2017-01-31 15:15:46 +00:00
|
|
|
use Test::More;
|
|
|
|
use Mojo::JSON;
|
2017-04-18 21:31:08 +00:00
|
|
|
use Test::Pear::LocalLoop;
|
2017-01-31 15:15:46 +00:00
|
|
|
|
2017-04-18 21:31:08 +00:00
|
|
|
my $framework = Test::Pear::LocalLoop->new;
|
|
|
|
my $t = $framework->framework;
|
2017-04-18 12:14:35 +00:00
|
|
|
my $schema = $t->app->schema;
|
2017-01-31 15:15:46 +00:00
|
|
|
|
|
|
|
#Variables to be used for uniqueness when testing.
|
2017-02-01 12:07:51 +00:00
|
|
|
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');
|
2017-04-18 12:14:35 +00:00
|
|
|
|
|
|
|
$schema->resultset('AccountToken')->populate([
|
2017-04-21 20:28:50 +00:00
|
|
|
[ qw/ name / ],
|
2017-04-18 12:14:35 +00:00
|
|
|
map { [ $_ ] } @tokens,
|
|
|
|
]);
|
2017-01-31 15:15:46 +00:00
|
|
|
|
2017-02-01 12:07:51 +00:00
|
|
|
#No JSON sent
|
2017-04-06 21:43:27 +00:00
|
|
|
$t->post_ok('/api/register')
|
2017-02-01 12:07:51 +00:00
|
|
|
->status_is(400)
|
|
|
|
->json_is('/success', Mojo::JSON->false)
|
2017-04-20 00:27:18 +00:00
|
|
|
->json_like('/message', qr/JSON is missing/i);
|
2017-02-01 12:07:51 +00:00
|
|
|
|
|
|
|
#Empty JSON
|
|
|
|
my $testJson = {};
|
2017-04-06 21:43:27 +00:00
|
|
|
$t->post_ok('/api/register' => json => $testJson)
|
2017-02-01 12:07:51 +00:00
|
|
|
->json_is('/success', Mojo::JSON->false);
|
|
|
|
|
|
|
|
#token missing JSON
|
2017-04-18 12:05:04 +00:00
|
|
|
$testJson = {
|
2017-02-01 12:07:51 +00:00
|
|
|
'usertype' => 'customer',
|
|
|
|
'username' => shift(@names),
|
|
|
|
'email' => shift(@emails),
|
|
|
|
'postcode' => 'LA1 1AA',
|
|
|
|
'password' => 'Meh',
|
2017-04-21 20:20:47 +00:00
|
|
|
'age' => 3
|
2017-02-01 12:07:51 +00:00
|
|
|
};
|
2017-04-06 21:43:27 +00:00
|
|
|
$t->post_ok('/api/register' => json => $testJson)
|
2017-02-01 12:07:51 +00:00
|
|
|
->status_is(400)
|
|
|
|
->json_is('/success', Mojo::JSON->false)
|
|
|
|
->content_like(qr/no token sent/i);
|
|
|
|
|
2017-01-31 15:15:46 +00:00
|
|
|
|
|
|
|
#Not valid token.
|
2017-04-18 12:05:04 +00:00
|
|
|
$testJson = {
|
2017-01-31 15:15:46 +00:00
|
|
|
'usertype' => 'customer',
|
|
|
|
'token' => ' ',
|
|
|
|
'username' => shift(@names),
|
|
|
|
'email' => shift(@emails),
|
|
|
|
'postcode' => 'LA1 1AA',
|
|
|
|
'password' => 'Meh',
|
2017-04-21 20:20:47 +00:00
|
|
|
'age' => 3
|
2017-01-31 15:15:46 +00:00
|
|
|
};
|
2017-04-06 21:43:27 +00:00
|
|
|
$t->post_ok('/api/register' => json => $testJson)
|
2017-02-01 12:07:51 +00:00
|
|
|
->status_is(401)
|
2017-01-31 15:15:46 +00:00
|
|
|
->json_is('/success', Mojo::JSON->false)
|
2017-02-01 12:07:51 +00:00
|
|
|
->content_like(qr/token/i)
|
|
|
|
->content_like(qr/invalid/i);
|
|
|
|
|
|
|
|
#username missing JSON
|
2017-04-18 12:05:04 +00:00
|
|
|
$testJson = {
|
2017-02-01 12:07:51 +00:00
|
|
|
'usertype' => 'customer',
|
|
|
|
'token' => shift(@tokens),
|
|
|
|
'email' => shift(@emails),
|
|
|
|
'postcode' => 'LA1 1AA',
|
|
|
|
'password' => 'Meh',
|
2017-04-21 20:20:47 +00:00
|
|
|
'age' => 3
|
2017-02-01 12:07:51 +00:00
|
|
|
};
|
2017-04-06 21:43:27 +00:00
|
|
|
$t->post_ok('/api/register' => json => $testJson)
|
2017-02-01 12:07:51 +00:00
|
|
|
->status_is(400)
|
|
|
|
->json_is('/success', Mojo::JSON->false)
|
|
|
|
->content_like(qr/no username sent/i);
|
|
|
|
|
2017-01-31 15:15:46 +00:00
|
|
|
|
|
|
|
#Blank username
|
2017-04-18 12:05:04 +00:00
|
|
|
$testJson = {
|
2017-01-31 15:15:46 +00:00
|
|
|
'usertype' => 'customer',
|
|
|
|
'token' => shift(@tokens),
|
|
|
|
'username' => '',
|
|
|
|
'email' => shift(@emails),
|
|
|
|
'postcode' => 'LA1 1AA',
|
|
|
|
'password' => 'Meh',
|
2017-04-21 20:20:47 +00:00
|
|
|
'age' => 3
|
2017-01-31 15:15:46 +00:00
|
|
|
};
|
2017-04-06 21:43:27 +00:00
|
|
|
$t->post_ok('/api/register' => json => $testJson)
|
2017-01-31 15:15:46 +00:00
|
|
|
->status_is(400)
|
|
|
|
->json_is('/success', Mojo::JSON->false)
|
|
|
|
->content_like(qr/blank/i)
|
|
|
|
->content_like(qr/username/i);
|
|
|
|
|
|
|
|
#Not alpha numeric chars e.g. !
|
2017-04-18 12:05:04 +00:00
|
|
|
$testJson = {
|
2017-01-31 15:15:46 +00:00
|
|
|
'usertype' => 'customer',
|
|
|
|
'token' => shift(@tokens),
|
|
|
|
'username' => 'asa!',
|
|
|
|
'email' => shift(@emails),
|
|
|
|
'postcode' => 'LA1 1AA',
|
|
|
|
'password' => 'Meh',
|
2017-04-21 20:20:47 +00:00
|
|
|
'age' => 3
|
2017-01-31 15:15:46 +00:00
|
|
|
};
|
2017-04-06 21:43:27 +00:00
|
|
|
$t->post_ok('/api/register' => json => $testJson)
|
2017-01-31 15:15:46 +00:00
|
|
|
->status_is(400)
|
|
|
|
->json_is('/success', Mojo::JSON->false)
|
|
|
|
->content_like(qr/username/i);
|
|
|
|
|
|
|
|
my $usernameToReuse = shift(@names);
|
|
|
|
my $emailToReuse = shift(@emails);
|
|
|
|
|
|
|
|
#Valid customer
|
2017-04-18 12:05:04 +00:00
|
|
|
$testJson = {
|
2017-01-31 15:15:46 +00:00
|
|
|
'usertype' => 'customer',
|
|
|
|
'token' => shift(@tokens),
|
|
|
|
'username' => $usernameToReuse,
|
|
|
|
'email' => $emailToReuse,
|
|
|
|
'postcode' => 'LA1 1AA',
|
|
|
|
'password' => 'Meh',
|
2017-04-21 20:20:47 +00:00
|
|
|
'age' => 3
|
2017-01-31 15:15:46 +00:00
|
|
|
};
|
2017-04-06 21:43:27 +00:00
|
|
|
$t->post_ok('/api/register' => json => $testJson)
|
2017-01-31 15:15:46 +00:00
|
|
|
->status_is(200)
|
|
|
|
->json_is('/success', Mojo::JSON->true);
|
|
|
|
|
|
|
|
#Valid customer2
|
2017-04-18 12:05:04 +00:00
|
|
|
$testJson = {
|
2017-01-31 15:15:46 +00:00
|
|
|
'usertype' => 'customer',
|
|
|
|
'token' => shift(@tokens),
|
|
|
|
'username' => shift(@names),
|
|
|
|
'email' => shift(@emails),
|
|
|
|
'postcode' => 'LA1 1AA',
|
|
|
|
'password' => 'Meh',
|
2017-04-21 20:20:47 +00:00
|
|
|
'age' => 2
|
2017-01-31 15:15:46 +00:00
|
|
|
};
|
2017-04-06 21:43:27 +00:00
|
|
|
$t->post_ok('/api/register' => json => $testJson)
|
2017-01-31 15:15:46 +00:00
|
|
|
->status_is(200)
|
|
|
|
->json_is('/success', Mojo::JSON->true);
|
|
|
|
|
|
|
|
#Valid customer3
|
2017-04-18 12:05:04 +00:00
|
|
|
$testJson = {
|
2017-01-31 15:15:46 +00:00
|
|
|
'usertype' => 'customer',
|
|
|
|
'token' => shift(@tokens),
|
|
|
|
'username' => shift(@names),
|
|
|
|
'email' => shift(@emails),
|
|
|
|
'postcode' => 'LA1 1AA',
|
|
|
|
'password' => 'Meh',
|
2017-04-21 20:20:47 +00:00
|
|
|
'age' => 1
|
2017-01-31 15:15:46 +00:00
|
|
|
};
|
2017-04-06 21:43:27 +00:00
|
|
|
$t->post_ok('/api/register' => json => $testJson)
|
2017-01-31 15:15:46 +00:00
|
|
|
->status_is(200)
|
|
|
|
->json_is('/success', Mojo::JSON->true);
|
|
|
|
|
|
|
|
#Username exists
|
2017-04-18 12:05:04 +00:00
|
|
|
$testJson = {
|
2017-01-31 15:15:46 +00:00
|
|
|
'usertype' => 'customer',
|
|
|
|
'token' => shift(@tokens),
|
|
|
|
'username' => $usernameToReuse,
|
|
|
|
'email' => shift(@emails),
|
|
|
|
'postcode' => 'LA1 1AA',
|
|
|
|
'password' => 'Meh',
|
2017-04-21 20:20:47 +00:00
|
|
|
'age' => 3
|
2017-01-31 15:15:46 +00:00
|
|
|
};
|
2017-04-06 21:43:27 +00:00
|
|
|
$t->post_ok('/api/register' => json => $testJson)
|
2017-01-31 15:15:46 +00:00
|
|
|
->status_is(403)
|
|
|
|
->json_is('/success', Mojo::JSON->false)
|
|
|
|
->content_like(qr/exists/i);
|
|
|
|
|
2017-02-01 12:07:51 +00:00
|
|
|
#email missing JSON
|
2017-04-18 12:05:04 +00:00
|
|
|
$testJson = {
|
2017-02-01 12:07:51 +00:00
|
|
|
'usertype' => 'customer',
|
|
|
|
'token' => shift(@tokens),
|
|
|
|
'username' => shift(@names),
|
|
|
|
'postcode' => 'LA1 1AA',
|
|
|
|
'password' => 'Meh',
|
2017-04-21 20:20:47 +00:00
|
|
|
'age' => 3
|
2017-02-01 12:07:51 +00:00
|
|
|
};
|
2017-04-06 21:43:27 +00:00
|
|
|
$t->post_ok('/api/register' => json => $testJson)
|
2017-02-01 12:07:51 +00:00
|
|
|
->status_is(400)
|
|
|
|
->json_is('/success', Mojo::JSON->false)
|
|
|
|
->content_like(qr/no email sent/i);
|
|
|
|
|
2017-01-31 15:15:46 +00:00
|
|
|
#invalid email 1
|
2017-04-18 12:05:04 +00:00
|
|
|
$testJson = {
|
2017-01-31 15:15:46 +00:00
|
|
|
'usertype' => 'customer',
|
|
|
|
'token' => shift(@tokens),
|
|
|
|
'username' => shift(@names),
|
|
|
|
'email' => 'dfsd@.com',
|
|
|
|
'postcode' => 'LA1 1AA',
|
|
|
|
'password' => 'Meh',
|
2017-04-21 20:20:47 +00:00
|
|
|
'age' => 2
|
2017-01-31 15:15:46 +00:00
|
|
|
};
|
2017-04-06 21:43:27 +00:00
|
|
|
$t->post_ok('/api/register' => json => $testJson)
|
2017-01-31 15:15:46 +00:00
|
|
|
->status_is(400)
|
|
|
|
->json_is('/success', Mojo::JSON->false)
|
|
|
|
->content_like(qr/email/i)
|
|
|
|
->content_like(qr/invalid/i);
|
|
|
|
|
|
|
|
#invalid email 2
|
2017-04-18 12:05:04 +00:00
|
|
|
$testJson = {
|
2017-01-31 15:15:46 +00:00
|
|
|
'usertype' => 'customer',
|
|
|
|
'token' => shift(@tokens),
|
|
|
|
'username' => shift(@names),
|
|
|
|
'email' => 'dfsd@com',
|
|
|
|
'postcode' => 'LA1 1AA',
|
|
|
|
'password' => 'Meh',
|
2017-04-21 20:20:47 +00:00
|
|
|
'age' => 2
|
2017-01-31 15:15:46 +00:00
|
|
|
};
|
2017-04-06 21:43:27 +00:00
|
|
|
$t->post_ok('/api/register' => json => $testJson)
|
2017-01-31 15:15:46 +00:00
|
|
|
->status_is(400)
|
|
|
|
->json_is('/success', Mojo::JSON->false)
|
|
|
|
->content_like(qr/email/i)
|
|
|
|
->content_like(qr/invalid/i);
|
|
|
|
|
|
|
|
#Email exists
|
2017-04-18 12:05:04 +00:00
|
|
|
$testJson = {
|
2017-01-31 15:15:46 +00:00
|
|
|
'usertype' => 'customer',
|
|
|
|
'token' => shift(@tokens),
|
|
|
|
'username' => shift(@names),
|
|
|
|
'email' => $emailToReuse,
|
|
|
|
'postcode' => 'LA1 1AA',
|
|
|
|
'password' => 'Meh',
|
2017-04-21 20:20:47 +00:00
|
|
|
'age' => 2
|
2017-01-31 15:15:46 +00:00
|
|
|
};
|
2017-04-06 21:43:27 +00:00
|
|
|
$t->post_ok('/api/register' => json => $testJson)
|
2017-01-31 15:15:46 +00:00
|
|
|
->status_is(403)
|
|
|
|
->json_is('/success', Mojo::JSON->false)
|
|
|
|
->content_like(qr/email/i)
|
|
|
|
->content_like(qr/exists/i);
|
|
|
|
|
2017-02-01 12:07:51 +00:00
|
|
|
#postcode missing JSON
|
2017-04-18 12:05:04 +00:00
|
|
|
$testJson = {
|
2017-02-01 12:07:51 +00:00
|
|
|
'usertype' => 'customer',
|
|
|
|
'token' => shift(@tokens),
|
|
|
|
'username' => shift(@names),
|
|
|
|
'email' => shift(@emails),
|
|
|
|
'password' => 'Meh',
|
2017-04-21 20:20:47 +00:00
|
|
|
'age' => 3
|
2017-02-01 12:07:51 +00:00
|
|
|
};
|
2017-04-06 21:43:27 +00:00
|
|
|
$t->post_ok('/api/register' => json => $testJson)
|
2017-02-01 12:07:51 +00:00
|
|
|
->status_is(400)
|
|
|
|
->json_is('/success', Mojo::JSON->false)
|
|
|
|
->content_like(qr/no postcode sent/i);
|
|
|
|
|
|
|
|
#TODO validate postcode
|
|
|
|
|
|
|
|
#password missing JSON
|
2017-04-18 12:05:04 +00:00
|
|
|
$testJson = {
|
2017-02-01 12:07:51 +00:00
|
|
|
'usertype' => 'customer',
|
|
|
|
'token' => shift(@tokens),
|
|
|
|
'username' => shift(@names),
|
|
|
|
'email' => shift(@emails),
|
|
|
|
'postcode' => 'LA1 1AA',
|
2017-04-21 20:20:47 +00:00
|
|
|
'age' => 3
|
2017-02-01 12:07:51 +00:00
|
|
|
};
|
2017-04-06 21:43:27 +00:00
|
|
|
$t->post_ok('/api/register' => json => $testJson)
|
2017-02-01 12:07:51 +00:00
|
|
|
->status_is(400)
|
|
|
|
->json_is('/success', Mojo::JSON->false)
|
|
|
|
->content_like(qr/no password sent/i);
|
|
|
|
|
|
|
|
#TODO enforce password complexity requirements.
|
|
|
|
|
|
|
|
#usertype missing JSON
|
2017-04-18 12:05:04 +00:00
|
|
|
$testJson = {
|
2017-02-01 12:07:51 +00:00
|
|
|
'token' => shift(@tokens),
|
|
|
|
'username' => shift(@names),
|
|
|
|
'email' => shift(@emails),
|
|
|
|
'postcode' => 'LA1 1AA',
|
|
|
|
'password' => 'Meh',
|
2017-04-21 20:20:47 +00:00
|
|
|
'age' => 3
|
2017-02-01 12:07:51 +00:00
|
|
|
};
|
2017-04-06 21:43:27 +00:00
|
|
|
$t->post_ok('/api/register' => json => $testJson)
|
2017-02-01 12:07:51 +00:00
|
|
|
->status_is(400)
|
|
|
|
->json_is('/success', Mojo::JSON->false)
|
|
|
|
->content_like(qr/no usertype sent/i);
|
|
|
|
|
|
|
|
#Invalid user type
|
2017-04-18 12:05:04 +00:00
|
|
|
$testJson = {
|
2017-02-01 12:07:51 +00:00
|
|
|
'usertype' => 'organisation1',
|
|
|
|
'token' => shift(@tokens),
|
|
|
|
'username' => shift(@names),
|
|
|
|
'email' => shift(@emails),
|
|
|
|
'postcode' => 'LA1 1AA',
|
|
|
|
'password' => 'Meh',
|
|
|
|
'fulladdress' => 'mary lane testing....'
|
|
|
|
};
|
2017-04-06 21:43:27 +00:00
|
|
|
$t->post_ok('/api/register' => json => $testJson)
|
2017-02-01 12:07:51 +00:00
|
|
|
->status_is(400)
|
|
|
|
->json_is('/success', Mojo::JSON->false)
|
|
|
|
->content_like(qr/usertype/i)
|
|
|
|
->content_like(qr/invalid/i);
|
|
|
|
|
|
|
|
|
|
|
|
#age missing JSON
|
2017-04-18 12:05:04 +00:00
|
|
|
$testJson = {
|
2017-02-01 12:07:51 +00:00
|
|
|
'usertype' => 'customer',
|
|
|
|
'token' => shift(@tokens),
|
|
|
|
'username' => shift(@names),
|
|
|
|
'email' => shift(@emails),
|
|
|
|
'postcode' => 'LA1 1AA',
|
|
|
|
'password' => 'Meh',
|
|
|
|
};
|
2017-04-06 21:43:27 +00:00
|
|
|
$t->post_ok('/api/register' => json => $testJson)
|
2017-02-01 12:07:51 +00:00
|
|
|
->status_is(400)
|
|
|
|
->json_is('/success', Mojo::JSON->false)
|
|
|
|
->content_like(qr/no age sent/i);
|
2017-01-31 15:15:46 +00:00
|
|
|
|
|
|
|
#Age is invalid
|
2017-04-18 12:05:04 +00:00
|
|
|
$testJson = {
|
2017-01-31 15:15:46 +00:00
|
|
|
'usertype' => 'customer',
|
|
|
|
'token' => shift(@tokens),
|
|
|
|
'username' => shift(@names),
|
|
|
|
'email' => shift(@emails),
|
|
|
|
'postcode' => 'LA1 1AA',
|
|
|
|
'password' => 'Meh',
|
|
|
|
'age' => 'invalid'
|
|
|
|
};
|
2017-04-06 21:43:27 +00:00
|
|
|
$t->post_ok('/api/register' => json => $testJson)
|
2017-01-31 15:15:46 +00:00
|
|
|
->status_is(400)
|
|
|
|
->json_is('/success', Mojo::JSON->false)
|
|
|
|
->content_like(qr/age/i)
|
|
|
|
->content_like(qr/invalid/i);
|
|
|
|
|
2017-02-01 12:07:51 +00:00
|
|
|
#full address missing JSON
|
2017-04-18 12:05:04 +00:00
|
|
|
$testJson = {
|
2017-01-31 15:15:46 +00:00
|
|
|
'usertype' => 'organisation',
|
|
|
|
'token' => shift(@tokens),
|
|
|
|
'username' => shift(@names),
|
|
|
|
'email' => shift(@emails),
|
|
|
|
'postcode' => 'LA1 1AA',
|
|
|
|
'password' => 'Meh',
|
|
|
|
};
|
2017-04-06 21:43:27 +00:00
|
|
|
$t->post_ok('/api/register' => json => $testJson)
|
2017-02-01 12:07:51 +00:00
|
|
|
->status_is(400)
|
|
|
|
->json_is('/success', Mojo::JSON->false)
|
2017-04-21 13:57:04 +00:00
|
|
|
->content_like(qr/no street_name sent/i);
|
2017-01-31 15:15:46 +00:00
|
|
|
|
2017-02-01 12:07:51 +00:00
|
|
|
#TODO Validation of full address
|
|
|
|
|
|
|
|
#Organisation valid
|
2017-04-18 12:05:04 +00:00
|
|
|
$testJson = {
|
2017-02-01 12:07:51 +00:00
|
|
|
'usertype' => 'organisation',
|
2017-01-31 15:15:46 +00:00
|
|
|
'token' => shift(@tokens),
|
|
|
|
'username' => shift(@names),
|
|
|
|
'email' => shift(@emails),
|
|
|
|
'postcode' => 'LA1 1AA',
|
|
|
|
'password' => 'Meh',
|
2017-04-21 13:57:04 +00:00
|
|
|
'street_name' => 'mary lane testing....',
|
|
|
|
'town' => 'Lancaster',
|
2017-01-31 15:15:46 +00:00
|
|
|
};
|
2017-04-06 21:43:27 +00:00
|
|
|
$t->post_ok('/api/register' => json => $testJson)
|
2017-02-01 12:07:51 +00:00
|
|
|
->status_is(200)
|
|
|
|
->json_is('/success', Mojo::JSON->true);
|
|
|
|
|
2017-04-18 12:05:04 +00:00
|
|
|
is $t->app->schema->resultset('User')->count, 4, 'Correct user count';
|
|
|
|
is $t->app->schema->resultset('Customer')->count, 3, 'Correct customer count';
|
|
|
|
is $t->app->schema->resultset('Organisation')->count, 1, 'Correct organisation count';
|
2017-01-31 15:15:46 +00:00
|
|
|
|
|
|
|
done_testing();
|