From a885897fbe7e6714ecb02f4f2efb4cb5722501a6 Mon Sep 17 00:00:00 2001 From: Manager Bot Date: Thu, 24 Nov 2022 08:56:41 +0000 Subject: [PATCH] More tests. --- Web/t/01_endpoints/02_auth/01_register.t | 34 +++++++++ Web/t/01_endpoints/02_auth/02_register_open.t | 29 ++++++++ .../02_auth/03_do_register_open.t | 69 +++++++++++++++++++ .../01_endpoints/02_auth/04_register_invite.t | 29 ++++++++ .../02_auth/05_do_register_invite.t | 66 ++++++++++++++++++ 5 files changed, 227 insertions(+) create mode 100644 Web/t/01_endpoints/02_auth/01_register.t create mode 100644 Web/t/01_endpoints/02_auth/02_register_open.t create mode 100644 Web/t/01_endpoints/02_auth/03_do_register_open.t create mode 100644 Web/t/01_endpoints/02_auth/04_register_invite.t create mode 100644 Web/t/01_endpoints/02_auth/05_do_register_invite.t diff --git a/Web/t/01_endpoints/02_auth/01_register.t b/Web/t/01_endpoints/02_auth/01_register.t new file mode 100644 index 0000000..62e8331 --- /dev/null +++ b/Web/t/01_endpoints/02_auth/01_register.t @@ -0,0 +1,34 @@ +#!/usr/bin/env perl +use MJB::Web::Test; + +#== +# This test file ensures that the open registration redirect system works as expected. +# +# The value of config->{register}{default} controls the redirect: +# +# invite -> /registration/invite +# open -> /registration/open +#== + +my $t = Test::Mojo::MJB->new('MJB::Web'); + +# Ensure both registration systems are enabled. +$t->app->config->{register}{enable_open} = 1; +$t->app->config->{register}{enable_invite} = 1; + +# Check about redirects to the invite system working correctly. +$t->app->config->{register}{default} = 'invite'; + +$t->get_ok( '/register' ) + ->status_is( 302 ) + ->header_is( location => '/register/invite' ); + +# Check about redirects to the open system working correctly. +$t->app->config->{register}{default} = 'open'; + +# Ensure that a user who has come to this page is redirected to the default registration system +$t->get_ok( '/register' ) + ->status_is( 302 ) + ->header_is( location => '/register/open' ); + +done_testing(); diff --git a/Web/t/01_endpoints/02_auth/02_register_open.t b/Web/t/01_endpoints/02_auth/02_register_open.t new file mode 100644 index 0000000..89b42a2 --- /dev/null +++ b/Web/t/01_endpoints/02_auth/02_register_open.t @@ -0,0 +1,29 @@ +#!/usr/bin/env perl +use MJB::Web::Test; + +#== +# This test file ensures that the open registration system works as expected. +# +# 1. When the open registration system is disabled, attemps to use it will result +# in the user being redirected to /register. +# 2. When the open registration system is enabled, the page displayes. +#== + +my $t = Test::Mojo::MJB->new('MJB::Web'); + +# Make sure that this registration method is disabled. +$t->app->config->{register}{enable_open} = 0; + +# Ensure that a user who has come to this page is redirected to the default registration system +$t->get_ok( '/register/open' ) + ->status_is( 302 ) + ->header_is( location => '/register' ); + +# Make sure that this registration method is enabled. +$t->app->config->{register}{enable_open} = 1; + +# Ensure that the open registration page displays when it is enabled. +$t->get_ok( '/register/open' ) + ->status_is( 200 ); + +done_testing(); diff --git a/Web/t/01_endpoints/02_auth/03_do_register_open.t b/Web/t/01_endpoints/02_auth/03_do_register_open.t new file mode 100644 index 0000000..c24833d --- /dev/null +++ b/Web/t/01_endpoints/02_auth/03_do_register_open.t @@ -0,0 +1,69 @@ +#!/usr/bin/env perl +use MJB::Web::Test; + +#== +# This test file ensures that the open registration system works as expected. +# +# 1. When the open registration system is disabled, attemps to use it will result +# in the user being redirected to /register. +# 2. When the form is submitted with errors, the account is not created and those +# errors are reported to the user. +# 3. When there are no errors, the user account is created and the user is logged in. +#== + +my $t = Test::Mojo::MJB->new('MJB::Web'); + +# Make sure that this registration method is disabled. +$t->app->config->{register}{enable_open} = 0; + +# Ensure that a user who has come to this page is redirected to the default registration system +$t->get_ok( '/register/open' ) + ->status_is( 302 ) + ->header_is( location => '/register' ); + +# Ensure that a user who submits this form in a naughty way is redirected to the default registration system +$t->post_ok( '/register/open', form => { + name => 'fred', + email => 'fred@blog.com', + password => 'SuperSecure', + password_confirm => 'SuperSecure', + }) + ->status_is( 302 ) + ->header_is( location => '/register' ) + ->code_block( sub { + is( scalar(@{shift->stash->{errors}}), 0, 'No errors' ); + }); + +# Make sure that this registration method is enabled. +$t->app->config->{register}{enable_open} = 1; + +# Try creating an account with an error, ensure we get the error. +$t->post_ok( '/register/open', form => { + name => 'fred', + email => 'fred@blog.com', + password => 'SuperSecure', + password_confirm => 'SuperFail', + })->status_is( 302 + )->code_block( sub { + is( shift->stash->{errors}->[0], 'Password and confirm password must match', 'Expected error thrown' ); + })->code_block( sub { + is( shift->app->db->resultset('Person')->search( { name => 'fred'})->count, 0, 'No user created.'); + }); + +## Try creating a valid account, ensure it exists in the DB. +$t->post_ok( '/register/open', form => { + name => 'fred', + email => 'fred@blog.com', + password => 'SuperSecure', + password_confirm => 'SuperSecure', + })->status_is( 302 + )->code_block( sub { + is( scalar(@{shift->stash->{errors}}), 0, 'No errors' ); + })->code_block( sub { + is( shift->app->db->resultset('Person')->search( { name => 'fred'})->count, 1, 'User created.'); + })->get_ok( '/' + )->code_block( sub { + is(shift->stash->{person}->name, 'fred', 'Got the fred after login...'); + }); + +done_testing(); diff --git a/Web/t/01_endpoints/02_auth/04_register_invite.t b/Web/t/01_endpoints/02_auth/04_register_invite.t new file mode 100644 index 0000000..7227a10 --- /dev/null +++ b/Web/t/01_endpoints/02_auth/04_register_invite.t @@ -0,0 +1,29 @@ +#!/usr/bin/env perl +use MJB::Web::Test; + +#== +# This test file ensures that the invite registration system works as expected. +# +# 1. When the invite registration system is disabled, attemps to use it will result +# in the user being redirected to /register. +# 2. When the invite registration system is enabled, the page displayes. +#== + +my $t = Test::Mojo::MJB->new('MJB::Web'); + +# Make sure that this registration method is disabled. +$t->app->config->{register}{enable_invite} = 0; + +# Ensure that a user who has come to this page is redirected to the default registration system +$t->get_ok( '/register/invite' ) + ->status_is( 302 ) + ->header_is( location => '/register' ); + +# Make sure that this registration method is enabled. +$t->app->config->{register}{enable_invite} = 1; + +# Ensure that the invite registration page displays when it is enabled. +$t->get_ok( '/register/invite' ) + ->status_is( 200 ); + +done_testing(); diff --git a/Web/t/01_endpoints/02_auth/05_do_register_invite.t b/Web/t/01_endpoints/02_auth/05_do_register_invite.t new file mode 100644 index 0000000..8f7f5e0 --- /dev/null +++ b/Web/t/01_endpoints/02_auth/05_do_register_invite.t @@ -0,0 +1,66 @@ +#!/usr/bin/env perl +use MJB::Web::Test; + +#== +# This test file ensures that the invite registration system works as expected. +# +# 1. When the invite registration system is disabled, attemps to use it will result +# in the user being redirected to /register. +# 2. When the form is submitted without an invite code, the account is not created and +# an error is reported to the user. +# 3. When there is a valid invite code, the user account is created and the user is logged in. +#== + +my $t = Test::Mojo::MJB->new('MJB::Web'); + +# Make sure that this registration method is disabled. +$t->app->config->{register}{enable_invite} = 0; + +# Ensure you cannot register an account with the invite system when it is disabled. +$t->post_ok( '/register/invite', form => { + name => 'fred', + email => 'fred@blog.com', + password => 'SuperSecure', + password_confirm => 'SuperSecure', + invite_code => 'invite-code', + }) + ->status_is( 302 ) + ->header_is( location => '/register' ); + +# Make sure that this registration method is disabled. +$t->app->config->{register}{enable_invite} = 1; + +# Trying to register an account without a valid invite code will fail. +$t->post_ok( '/register/invite', form => { + name => 'fred', + email => 'fred@blog.com', + password => 'SuperSecure', + password_confirm => 'SuperSecure', + invite_code => 'invite-code', + }) + ->status_is( 302 ) + ->header_is( location => '/register/invite' ) + ->code_block( sub { + my $self = shift; + is( scalar(@{$self->stash->{errors}}), 1, 'Got an expected error.' ); + is( $self->stash->{errors}->[0], 'That invite code is not valid.', 'Expected error.' ); + }); + +# Create an invite code. +$t->app->db->invites->create({ code => 'invite-code' }); + +# Register an account with a valid invite code. +$t->post_ok( '/register/invite', form => { + name => 'fred', + email => 'fred@blog.com', + password => 'SuperSecure', + password_confirm => 'SuperSecure', + invite_code => 'invite-code', + }) + ->status_is( 302 ) + ->header_is( location => '/dashboard' ) + ->code_block( sub { + is( scalar(@{shift->stash->{errors}}), 0, 'No errors' ); + }); + +done_testing();