2017-04-18 13:05:04 +01:00
use Mojo::Base - strict ;
2017-01-31 15:15:46 +00:00
use Test::More ;
use Mojo::JSON ;
2017-04-18 22:31:08 +01:00
use Test::Pear::LocalLoop ;
2017-01-31 15:15:46 +00:00
2017-04-18 22:31:08 +01:00
my $ framework = Test::Pear::LocalLoop - > new ;
my $ t = $ framework - > framework ;
2017-04-18 13:14:35 +01:00
my $ schema = $ t - > app - > schema ;
2017-06-13 21:02:24 +01:00
my $ dump_error = $ framework - > dump_error ;
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 @ 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 13:14:35 +01:00
$ schema - > resultset ( 'AccountToken' ) - > populate ( [
2017-04-21 21:28:50 +01:00
[ qw/ name / ] ,
2017-04-18 13:14:35 +01: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 22:43:27 +01:00
$ t - > post_ok ( '/api/register' )
2017-04-22 19:35:19 +01:00
- > status_is ( 400 ) - > or ( $ dump_error )
2017-02-01 12:07:51 +00:00
- > json_is ( '/success' , Mojo::JSON - > false )
2017-04-20 01:27:18 +01:00
- > json_like ( '/message' , qr/JSON is missing/ i ) ;
2017-02-01 12:07:51 +00:00
#Empty JSON
my $ testJson = { } ;
2017-04-06 22:43:27 +01: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 13:05:04 +01:00
$ testJson = {
2017-02-01 12:07:51 +00:00
'usertype' = > 'customer' ,
2017-04-25 21:44:13 +01:00
'full_name' = > 'test name' ,
'display_name' = > 'test name' ,
'email' = > 'a@b.com' ,
2017-02-01 12:07:51 +00:00
'postcode' = > 'LA1 1AA' ,
'password' = > 'Meh' ,
2017-06-13 21:02:24 +01:00
'year_of_birth' = > 2005
2017-02-01 12:07:51 +00:00
} ;
2017-04-06 22:43:27 +01: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 13:05:04 +01:00
$ testJson = {
2017-01-31 15:15:46 +00:00
'usertype' = > 'customer' ,
2017-04-25 21:44:13 +01:00
'token' = > 'testing' ,
'display_name' = > 'test name' ,
'full_name' = > 'test name' ,
'email' = > 'a@b.com' ,
2017-01-31 15:15:46 +00:00
'postcode' = > 'LA1 1AA' ,
'password' = > 'Meh' ,
2017-06-13 21:02:24 +01:00
'year_of_birth' = > 2005
2017-01-31 15:15:46 +00:00
} ;
2017-04-06 22:43:27 +01: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 ) ;
2017-04-22 19:35:19 +01:00
#name missing JSON
2017-04-18 13:05:04 +01:00
$ testJson = {
2017-02-01 12:07:51 +00:00
'usertype' = > 'customer' ,
2017-04-25 21:44:13 +01:00
'token' = > 'a' ,
'full_name' = > 'test name' ,
'email' = > 'a@b.com' ,
'postcode' = > 'LA1 1AA' ,
'password' = > 'Meh' ,
2017-06-13 21:02:24 +01:00
'year_of_birth' = > 2005
2017-04-25 21:44:13 +01:00
} ;
$ t - > post_ok ( '/api/register' = > json = > $ testJson )
- > status_is ( 400 )
- > json_is ( '/success' , Mojo::JSON - > false )
- > content_like ( qr/no name sent/ i ) ;
#name missing JSON
$ testJson = {
'usertype' = > 'customer' ,
'token' = > 'a' ,
'display_name' = > 'test name' ,
'email' = > 'a@b.com' ,
2017-02-01 12:07:51 +00:00
'postcode' = > 'LA1 1AA' ,
'password' = > 'Meh' ,
2017-06-13 21:02:24 +01:00
'year_of_birth' = > 2005
2017-02-01 12:07:51 +00:00
} ;
2017-04-06 22:43:27 +01: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-22 19:35:19 +01:00
- > content_like ( qr/no name sent/ i ) ;
2017-02-01 12:07:51 +00:00
2017-04-22 19:35:19 +01:00
#Blank name
2017-04-18 13:05:04 +01:00
$ testJson = {
2017-01-31 15:15:46 +00:00
'usertype' = > 'customer' ,
2017-04-25 21:44:13 +01:00
'token' = > 'a' ,
'display_name' = > 'test name' ,
'full_name' = > '' ,
'email' = > 'a@b.com' ,
'postcode' = > 'LA1 1AA' ,
'password' = > 'Meh' ,
2017-06-13 21:02:24 +01:00
'year_of_birth' = > 2005
2017-04-25 21:44:13 +01:00
} ;
$ t - > post_ok ( '/api/register' = > json = > $ testJson )
- > status_is ( 400 )
- > json_is ( '/success' , Mojo::JSON - > false )
- > content_like ( qr/blank/ i )
- > content_like ( qr/name/ i ) ;
#Blank name
$ testJson = {
'usertype' = > 'customer' ,
'token' = > 'a' ,
'display_name' = > '' ,
'full_name' = > 'test name' ,
'email' = > 'a@b.com' ,
2017-01-31 15:15:46 +00:00
'postcode' = > 'LA1 1AA' ,
'password' = > 'Meh' ,
2017-06-13 21:02:24 +01:00
'year_of_birth' = > 2005
2017-01-31 15:15:46 +00:00
} ;
2017-04-06 22:43:27 +01: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 )
2017-04-22 19:35:19 +01:00
- > content_like ( qr/name/ i ) ;
2017-01-31 15:15:46 +00:00
#Valid customer
2017-04-18 13:05:04 +01:00
$ testJson = {
2017-01-31 15:15:46 +00:00
'usertype' = > 'customer' ,
2017-04-25 21:44:13 +01:00
'token' = > 'a' ,
'full_name' = > 'test name' ,
'display_name' = > 'test name' ,
'email' = > 'a@b.com' ,
2017-01-31 15:15:46 +00:00
'postcode' = > 'LA1 1AA' ,
'password' = > 'Meh' ,
2017-06-13 21:02:24 +01:00
'year_of_birth' = > 2005
2017-01-31 15:15:46 +00:00
} ;
2017-04-06 22:43:27 +01: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 13:05:04 +01:00
$ testJson = {
2017-01-31 15:15:46 +00:00
'usertype' = > 'customer' ,
2017-04-25 21:44:13 +01:00
'token' = > 'b' ,
'full_name' = > 'test name' ,
'display_name' = > 'test name' ,
'email' = > 'b@c.com' ,
2017-01-31 15:15:46 +00:00
'postcode' = > 'LA1 1AA' ,
'password' = > 'Meh' ,
2017-06-13 21:02:24 +01:00
'year_of_birth' = > 2005
2017-01-31 15:15:46 +00:00
} ;
2017-04-06 22:43:27 +01:00
$ t - > post_ok ( '/api/register' = > json = > $ testJson )
2017-06-13 21:02:24 +01:00
- > or ( $ dump_error )
2017-01-31 15:15:46 +00:00
- > status_is ( 200 )
2017-06-13 21:02:24 +01:00
- > or ( $ dump_error )
- > json_is ( '/success' , Mojo::JSON - > true )
- > or ( $ dump_error ) ;
2017-01-31 15:15:46 +00:00
#Valid customer3
2017-04-18 13:05:04 +01:00
$ testJson = {
2017-01-31 15:15:46 +00:00
'usertype' = > 'customer' ,
2017-04-25 21:44:13 +01:00
'token' = > 'c' ,
'full_name' = > 'test name' ,
'display_name' = > 'test name' ,
'email' = > 'c@d.com' ,
2017-01-31 15:15:46 +00:00
'postcode' = > 'LA1 1AA' ,
'password' = > 'Meh' ,
2017-06-13 21:02:24 +01:00
'year_of_birth' = > 2005
2017-01-31 15:15:46 +00:00
} ;
2017-04-06 22:43:27 +01: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 ) ;
2017-02-01 12:07:51 +00:00
#email missing JSON
2017-04-18 13:05:04 +01:00
$ testJson = {
2017-02-01 12:07:51 +00:00
'usertype' = > 'customer' ,
2017-04-25 21:44:13 +01:00
'token' = > 'd' ,
'full_name' = > 'test name' ,
'display_name' = > 'test name' ,
2017-02-01 12:07:51 +00:00
'postcode' = > 'LA1 1AA' ,
'password' = > 'Meh' ,
2017-06-13 21:02:24 +01:00
'year_of_birth' = > 2005
2017-02-01 12:07:51 +00:00
} ;
2017-04-06 22:43:27 +01: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 13:05:04 +01:00
$ testJson = {
2017-01-31 15:15:46 +00:00
'usertype' = > 'customer' ,
2017-04-25 21:44:13 +01:00
'token' = > 'd' ,
'full_name' = > 'test name' ,
'display_name' = > 'test name' ,
2017-01-31 15:15:46 +00:00
'email' = > 'dfsd@.com' ,
'postcode' = > 'LA1 1AA' ,
'password' = > 'Meh' ,
2017-06-13 21:02:24 +01:00
'year_of_birth' = > 2006
2017-01-31 15:15:46 +00:00
} ;
2017-04-06 22:43:27 +01: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 13:05:04 +01:00
$ testJson = {
2017-01-31 15:15:46 +00:00
'usertype' = > 'customer' ,
2017-04-25 21:44:13 +01:00
'token' = > 'd' ,
'full_name' = > 'test name' ,
'display_name' = > 'test name' ,
2017-01-31 15:15:46 +00:00
'email' = > 'dfsd@com' ,
'postcode' = > 'LA1 1AA' ,
'password' = > 'Meh' ,
2017-06-13 21:02:24 +01:00
'year_of_birth' = > 2006
2017-01-31 15:15:46 +00:00
} ;
2017-04-06 22:43:27 +01: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 13:05:04 +01:00
$ testJson = {
2017-01-31 15:15:46 +00:00
'usertype' = > 'customer' ,
2017-04-25 21:44:13 +01:00
'token' = > 'd' ,
'full_name' = > 'test name' ,
'display_name' = > 'test name' ,
'email' = > 'a@b.com' ,
2017-01-31 15:15:46 +00:00
'postcode' = > 'LA1 1AA' ,
'password' = > 'Meh' ,
2017-06-13 21:02:24 +01:00
'year_of_birth' = > 2006
2017-01-31 15:15:46 +00:00
} ;
2017-04-06 22:43:27 +01: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 13:05:04 +01:00
$ testJson = {
2017-02-01 12:07:51 +00:00
'usertype' = > 'customer' ,
2017-04-25 21:44:13 +01:00
'token' = > 'd' ,
'full_name' = > 'test name' ,
'display_name' = > 'test name' ,
'email' = > 'd@e.com' ,
2017-02-01 12:07:51 +00:00
'password' = > 'Meh' ,
2017-06-13 21:02:24 +01:00
'year_of_birth' = > 2006
2017-02-01 12:07:51 +00:00
} ;
2017-04-06 22:43:27 +01: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 13:05:04 +01:00
$ testJson = {
2017-02-01 12:07:51 +00:00
'usertype' = > 'customer' ,
2017-04-25 21:44:13 +01:00
'token' = > 'd' ,
'full_name' = > 'test name' ,
'display_name' = > 'test name' ,
'email' = > 'd@e.com' ,
2017-02-01 12:07:51 +00:00
'postcode' = > 'LA1 1AA' ,
2017-06-13 21:02:24 +01:00
'year_of_birth' = > 2006
2017-02-01 12:07:51 +00:00
} ;
2017-04-06 22:43:27 +01: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 13:05:04 +01:00
$ testJson = {
2017-04-25 21:44:13 +01:00
'token' = > 'f' ,
'full_name' = > 'test name' ,
'display_name' = > 'test name' ,
'email' = > 'd@e.com' ,
2017-02-01 12:07:51 +00:00
'postcode' = > 'LA1 1AA' ,
'password' = > 'Meh' ,
2017-06-13 21:02:24 +01:00
'year_of_birth' = > 2006
2017-02-01 12:07:51 +00:00
} ;
2017-04-06 22:43:27 +01: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 13:05:04 +01:00
$ testJson = {
2017-02-01 12:07:51 +00:00
'usertype' = > 'organisation1' ,
2017-04-25 21:44:13 +01:00
'token' = > 'f' ,
'name' = > 'test name' ,
'email' = > 'org@org.com' ,
2017-02-01 12:07:51 +00:00
'postcode' = > 'LA1 1AA' ,
'password' = > 'Meh' ,
'fulladdress' = > 'mary lane testing....'
} ;
2017-04-06 22:43:27 +01: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 ) ;
2017-06-13 21:02:24 +01:00
#year_of_birth missing JSON
2017-04-18 13:05:04 +01:00
$ testJson = {
2017-02-01 12:07:51 +00:00
'usertype' = > 'customer' ,
2017-04-25 21:44:13 +01:00
'token' = > 'f' ,
'display_name' = > 'test name' ,
'full_name' = > 'test name' ,
'email' = > 'broke@example.com' ,
2017-02-01 12:07:51 +00:00
'postcode' = > 'LA1 1AA' ,
'password' = > 'Meh' ,
} ;
2017-04-06 22:43:27 +01: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-06-13 21:02:24 +01:00
- > content_like ( qr/no year_of_birth sent/ i ) ;
2017-01-31 15:15:46 +00:00
#Age is invalid
2017-04-18 13:05:04 +01:00
$ testJson = {
2017-01-31 15:15:46 +00:00
'usertype' = > 'customer' ,
2017-04-25 21:44:13 +01:00
'token' = > 'f' ,
'full_name' = > 'test name' ,
'display_name' = > 'test name' ,
'email' = > 'test@example.com' ,
2017-01-31 15:15:46 +00:00
'postcode' = > 'LA1 1AA' ,
'password' = > 'Meh' ,
2017-06-13 21:02:24 +01:00
'year_of_birth' = > 'invalid'
2017-01-31 15:15:46 +00:00
} ;
2017-04-06 22:43:27 +01: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 )
2017-06-13 21:02:24 +01:00
- > content_like ( qr/year_of_birth/ i )
2017-01-31 15:15:46 +00:00
- > content_like ( qr/invalid/ i ) ;
2017-02-01 12:07:51 +00:00
#full address missing JSON
2017-04-18 13:05:04 +01:00
$ testJson = {
2017-01-31 15:15:46 +00:00
'usertype' = > 'organisation' ,
2017-04-25 21:44:13 +01:00
'token' = > 'f' ,
'name' = > 'test org' ,
'email' = > 'org@org.com' ,
2017-01-31 15:15:46 +00:00
'postcode' = > 'LA1 1AA' ,
'password' = > 'Meh' ,
} ;
2017-04-06 22:43:27 +01: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 14:57:04 +01: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 13:05:04 +01:00
$ testJson = {
2017-02-01 12:07:51 +00:00
'usertype' = > 'organisation' ,
2017-04-25 21:44:13 +01:00
'token' = > 'f' ,
'name' = > 'org name' ,
'email' = > 'org@org.com' ,
2017-01-31 15:15:46 +00:00
'postcode' = > 'LA1 1AA' ,
'password' = > 'Meh' ,
2017-04-21 14:57:04 +01:00
'street_name' = > 'mary lane testing....' ,
'town' = > 'Lancaster' ,
2017-01-31 15:15:46 +00:00
} ;
2017-04-06 22:43:27 +01: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 13:05:04 +01: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 ( ) ;