Strict test for register and check database for entries after testing

This commit is contained in:
Tom Bloor 2017-04-18 13:05:04 +01:00
parent 98d4309b67
commit e932c60af3

View file

@ -1,3 +1,5 @@
use Mojo::Base -strict;
use Test::More; use Test::More;
use Test::Mojo; use Test::Mojo;
use Mojo::JSON; use Mojo::JSON;
@ -19,7 +21,7 @@ for (split ';', $sqlDeployment){
$dbh->do($_) or die $dbh->errstr; $dbh->do($_) or die $dbh->errstr;
} }
my $sqlDeployment = Mojo::File->new("$FindBin::Bin/../schema.sql")->slurp; $sqlDeployment = Mojo::File->new("$FindBin::Bin/../schema.sql")->slurp;
for (split ';', $sqlDeployment){ for (split ';', $sqlDeployment){
$dbh->do($_) or die $dbh->errstr; $dbh->do($_) or die $dbh->errstr;
} }
@ -34,21 +36,18 @@ foreach (@tokens){
} }
#No JSON sent #No JSON sent
print "test1\n\n";
$t->post_ok('/api/register') $t->post_ok('/api/register')
->status_is(400) ->status_is(400)
->json_is('/success', Mojo::JSON->false) ->json_is('/success', Mojo::JSON->false)
->content_like(qr/no json sent/i); ->content_like(qr/no json sent/i);
#Empty JSON #Empty JSON
print "test2\n\n";
my $testJson = {}; my $testJson = {};
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->json_is('/success', Mojo::JSON->false); ->json_is('/success', Mojo::JSON->false);
#token missing JSON #token missing JSON
print "test3\n\n"; $testJson = {
my $testJson = {
'usertype' => 'customer', 'usertype' => 'customer',
'username' => shift(@names), 'username' => shift(@names),
'email' => shift(@emails), 'email' => shift(@emails),
@ -63,8 +62,7 @@ $t->post_ok('/api/register' => json => $testJson)
#Not valid token. #Not valid token.
print "test4\n\n"; $testJson = {
my $testJson = {
'usertype' => 'customer', 'usertype' => 'customer',
'token' => ' ', 'token' => ' ',
'username' => shift(@names), 'username' => shift(@names),
@ -80,8 +78,7 @@ $t->post_ok('/api/register' => json => $testJson)
->content_like(qr/invalid/i); ->content_like(qr/invalid/i);
#username missing JSON #username missing JSON
print "test5\n\n"; $testJson = {
my $testJson = {
'usertype' => 'customer', 'usertype' => 'customer',
'token' => shift(@tokens), 'token' => shift(@tokens),
'email' => shift(@emails), 'email' => shift(@emails),
@ -96,8 +93,7 @@ $t->post_ok('/api/register' => json => $testJson)
#Blank username #Blank username
print "test6\n\n"; $testJson = {
my $testJson = {
'usertype' => 'customer', 'usertype' => 'customer',
'token' => shift(@tokens), 'token' => shift(@tokens),
'username' => '', 'username' => '',
@ -113,8 +109,7 @@ $t->post_ok('/api/register' => json => $testJson)
->content_like(qr/username/i); ->content_like(qr/username/i);
#Not alpha numeric chars e.g. ! #Not alpha numeric chars e.g. !
print "test7\n\n"; $testJson = {
my $testJson = {
'usertype' => 'customer', 'usertype' => 'customer',
'token' => shift(@tokens), 'token' => shift(@tokens),
'username' => 'asa!', 'username' => 'asa!',
@ -132,8 +127,7 @@ my $usernameToReuse = shift(@names);
my $emailToReuse = shift(@emails); my $emailToReuse = shift(@emails);
#Valid customer #Valid customer
print "test8\n\n"; $testJson = {
my $testJson = {
'usertype' => 'customer', 'usertype' => 'customer',
'token' => shift(@tokens), 'token' => shift(@tokens),
'username' => $usernameToReuse, 'username' => $usernameToReuse,
@ -147,8 +141,7 @@ $t->post_ok('/api/register' => json => $testJson)
->json_is('/success', Mojo::JSON->true); ->json_is('/success', Mojo::JSON->true);
#Valid customer2 #Valid customer2
print "test9\n\n"; $testJson = {
my $testJson = {
'usertype' => 'customer', 'usertype' => 'customer',
'token' => shift(@tokens), 'token' => shift(@tokens),
'username' => shift(@names), 'username' => shift(@names),
@ -162,8 +155,7 @@ $t->post_ok('/api/register' => json => $testJson)
->json_is('/success', Mojo::JSON->true); ->json_is('/success', Mojo::JSON->true);
#Valid customer3 #Valid customer3
print "test10\n\n"; $testJson = {
my $testJson = {
'usertype' => 'customer', 'usertype' => 'customer',
'token' => shift(@tokens), 'token' => shift(@tokens),
'username' => shift(@names), 'username' => shift(@names),
@ -177,8 +169,7 @@ $t->post_ok('/api/register' => json => $testJson)
->json_is('/success', Mojo::JSON->true); ->json_is('/success', Mojo::JSON->true);
#Username exists #Username exists
print "test11\n\n"; $testJson = {
my $testJson = {
'usertype' => 'customer', 'usertype' => 'customer',
'token' => shift(@tokens), 'token' => shift(@tokens),
'username' => $usernameToReuse, 'username' => $usernameToReuse,
@ -193,8 +184,7 @@ $t->post_ok('/api/register' => json => $testJson)
->content_like(qr/exists/i); ->content_like(qr/exists/i);
#email missing JSON #email missing JSON
print "test12\n\n"; $testJson = {
my $testJson = {
'usertype' => 'customer', 'usertype' => 'customer',
'token' => shift(@tokens), 'token' => shift(@tokens),
'username' => shift(@names), 'username' => shift(@names),
@ -208,8 +198,7 @@ $t->post_ok('/api/register' => json => $testJson)
->content_like(qr/no email sent/i); ->content_like(qr/no email sent/i);
#invalid email 1 #invalid email 1
print "test13\n\n"; $testJson = {
my $testJson = {
'usertype' => 'customer', 'usertype' => 'customer',
'token' => shift(@tokens), 'token' => shift(@tokens),
'username' => shift(@names), 'username' => shift(@names),
@ -225,8 +214,7 @@ $t->post_ok('/api/register' => json => $testJson)
->content_like(qr/invalid/i); ->content_like(qr/invalid/i);
#invalid email 2 #invalid email 2
print "test14\n\n"; $testJson = {
my $testJson = {
'usertype' => 'customer', 'usertype' => 'customer',
'token' => shift(@tokens), 'token' => shift(@tokens),
'username' => shift(@names), 'username' => shift(@names),
@ -242,8 +230,7 @@ $t->post_ok('/api/register' => json => $testJson)
->content_like(qr/invalid/i); ->content_like(qr/invalid/i);
#Email exists #Email exists
print "test15\n\n"; $testJson = {
my $testJson = {
'usertype' => 'customer', 'usertype' => 'customer',
'token' => shift(@tokens), 'token' => shift(@tokens),
'username' => shift(@names), 'username' => shift(@names),
@ -259,8 +246,7 @@ $t->post_ok('/api/register' => json => $testJson)
->content_like(qr/exists/i); ->content_like(qr/exists/i);
#postcode missing JSON #postcode missing JSON
print "test16\n\n"; $testJson = {
my $testJson = {
'usertype' => 'customer', 'usertype' => 'customer',
'token' => shift(@tokens), 'token' => shift(@tokens),
'username' => shift(@names), 'username' => shift(@names),
@ -276,8 +262,7 @@ $t->post_ok('/api/register' => json => $testJson)
#TODO validate postcode #TODO validate postcode
#password missing JSON #password missing JSON
print "test17\n\n"; $testJson = {
my $testJson = {
'usertype' => 'customer', 'usertype' => 'customer',
'token' => shift(@tokens), 'token' => shift(@tokens),
'username' => shift(@names), 'username' => shift(@names),
@ -293,8 +278,7 @@ $t->post_ok('/api/register' => json => $testJson)
#TODO enforce password complexity requirements. #TODO enforce password complexity requirements.
#usertype missing JSON #usertype missing JSON
print "test18\n\n"; $testJson = {
my $testJson = {
'token' => shift(@tokens), 'token' => shift(@tokens),
'username' => shift(@names), 'username' => shift(@names),
'email' => shift(@emails), 'email' => shift(@emails),
@ -308,8 +292,7 @@ $t->post_ok('/api/register' => json => $testJson)
->content_like(qr/no usertype sent/i); ->content_like(qr/no usertype sent/i);
#Invalid user type #Invalid user type
print "test19\n\n"; $testJson = {
my $testJson = {
'usertype' => 'organisation1', 'usertype' => 'organisation1',
'token' => shift(@tokens), 'token' => shift(@tokens),
'username' => shift(@names), 'username' => shift(@names),
@ -326,8 +309,7 @@ $t->post_ok('/api/register' => json => $testJson)
#age missing JSON #age missing JSON
print "test20\n\n"; $testJson = {
my $testJson = {
'usertype' => 'customer', 'usertype' => 'customer',
'token' => shift(@tokens), 'token' => shift(@tokens),
'username' => shift(@names), 'username' => shift(@names),
@ -341,8 +323,7 @@ $t->post_ok('/api/register' => json => $testJson)
->content_like(qr/no age sent/i); ->content_like(qr/no age sent/i);
#Age is invalid #Age is invalid
print "test21\n\n"; $testJson = {
my $testJson = {
'usertype' => 'customer', 'usertype' => 'customer',
'token' => shift(@tokens), 'token' => shift(@tokens),
'username' => shift(@names), 'username' => shift(@names),
@ -358,8 +339,7 @@ $t->post_ok('/api/register' => json => $testJson)
->content_like(qr/invalid/i); ->content_like(qr/invalid/i);
#full address missing JSON #full address missing JSON
print "test22\n\n"; $testJson = {
my $testJson = {
'usertype' => 'organisation', 'usertype' => 'organisation',
'token' => shift(@tokens), 'token' => shift(@tokens),
'username' => shift(@names), 'username' => shift(@names),
@ -375,8 +355,7 @@ $t->post_ok('/api/register' => json => $testJson)
#TODO Validation of full address #TODO Validation of full address
#Organisation valid #Organisation valid
print "test23\n\n"; $testJson = {
my $testJson = {
'usertype' => 'organisation', 'usertype' => 'organisation',
'token' => shift(@tokens), 'token' => shift(@tokens),
'username' => shift(@names), 'username' => shift(@names),
@ -389,22 +368,8 @@ $t->post_ok('/api/register' => json => $testJson)
->status_is(200) ->status_is(200)
->json_is('/success', Mojo::JSON->true); ->json_is('/success', Mojo::JSON->true);
is $t->app->schema->resultset('User')->count, 4, 'Correct user count';
#No JSON sent is $t->app->schema->resultset('Customer')->count, 3, 'Correct customer count';
#print "test14\n\n"; is $t->app->schema->resultset('Organisation')->count, 1, 'Correct organisation count';
#my $testJson = {
# 'usertype' => 'customer',
# 'token' => shift(@tokens),
# 'username' => shift(@names),
# 'email' => shift(@emails),
# 'postcode' => 'LA1 1AA',
# 'password' => 'Meh',
# 'age' => '50+'
#};
#$t->post_ok('/api/register' => json => $testJson)
# ->status_is(401)->or(sub{ diag $t->tx->res->body})
# ->json_is('/success', Mojo::JSON->false)
# ->content_like(qr/token/i);
done_testing(); done_testing();