From 018f6fa5c6925f713ed3a2ffb5effc7fbae6b200 Mon Sep 17 00:00:00 2001 From: Manager Bot Date: Thu, 24 Nov 2022 20:51:19 +0000 Subject: [PATCH] More tests --- Web/t/01_endpoints/02_auth/06_login.t | 12 ++++++ Web/t/01_endpoints/02_auth/07_do_login.t | 48 ++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 Web/t/01_endpoints/02_auth/06_login.t create mode 100644 Web/t/01_endpoints/02_auth/07_do_login.t diff --git a/Web/t/01_endpoints/02_auth/06_login.t b/Web/t/01_endpoints/02_auth/06_login.t new file mode 100644 index 0000000..7b24fb9 --- /dev/null +++ b/Web/t/01_endpoints/02_auth/06_login.t @@ -0,0 +1,12 @@ +#!/usr/bin/env perl +use MJB::Web::Test; + +my $t = Test::Mojo::MJB->new('MJB::Web'); + +## +# This test ensures that a user who hasn't logged in can access the login page. +### + +$t->get_ok( '/login' )->status_is( 200 ); + +done_testing(); diff --git a/Web/t/01_endpoints/02_auth/07_do_login.t b/Web/t/01_endpoints/02_auth/07_do_login.t new file mode 100644 index 0000000..2d5574d --- /dev/null +++ b/Web/t/01_endpoints/02_auth/07_do_login.t @@ -0,0 +1,48 @@ +#!/usr/bin/env perl +use MJB::Web::Test; + +#== +# This test ensures that login works. +# +# It will create an account through the normal registration, logout, +# confirm it is logged out, log in, and confirm it is logged in. +#== + +my $t = Test::Mojo::MJB->new('MJB::Web'); + +# Make sure that open registration method is enabled. +$t->app->config->{register}{enable_open} = 1; + +# 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...'); + }); + +# Remove session information so we are logged out of the fred account. +$t->reset_session; + +# Try to login to the newly created account. +$t->post_ok( '/login', form => { + email => 'fred@blog.com', + password => 'SuperSecure', + })->status_is( 302 + )->header_is( location => '/dashboard', 'Login redirected to dashboard' ) + ->code_block( sub { + is( scalar(@{shift->stash->{errors}}), 0, 'No errors' ); + })->get_ok( '/' + )->code_block( sub { + is(shift->stash->{person}->name, 'fred', 'Got the fred after login...'); + }); + +done_testing();