From ffb59ad6528579ce8752e2694d96628da1e3faeb Mon Sep 17 00:00:00 2001 From: Manager Bot Date: Wed, 30 Nov 2022 06:41:12 +0000 Subject: [PATCH] More tests! --- .../06_user_settings/01_profile.t | 29 ++++++++++ .../06_user_settings/02_do_profile.t | 53 +++++++++++++++++++ .../06_user_settings/03_change_password.t | 17 ++++++ .../06_user_settings/04_do_change_password.t | 50 +++++++++++++++++ 4 files changed, 149 insertions(+) create mode 100644 Web/t/01_endpoints/06_user_settings/01_profile.t create mode 100644 Web/t/01_endpoints/06_user_settings/02_do_profile.t create mode 100644 Web/t/01_endpoints/06_user_settings/03_change_password.t create mode 100644 Web/t/01_endpoints/06_user_settings/04_do_change_password.t diff --git a/Web/t/01_endpoints/06_user_settings/01_profile.t b/Web/t/01_endpoints/06_user_settings/01_profile.t new file mode 100644 index 0000000..6d9f202 --- /dev/null +++ b/Web/t/01_endpoints/06_user_settings/01_profile.t @@ -0,0 +1,29 @@ +#!/usr/bin/env perl +use MJB::Web::Test; + +#== +# This test ensures that the profile page displays the user's name and email +# address. +# +# 1. Create user and login. +# 2. Go to the profile page +# 3. Confirm the form fields contain the correct information +#== + +my $t = Test::Mojo::MJB->new('MJB::Web'); + +my $blog_id = $t->create_user + ->get_ok( '/profile' ) + ->status_is( 200 ) + ->code_block(sub { + my $self = shift; + + + ok exists $self->stash->{form}->{name}, 'Name exists in profile form.'; + ok exists $self->stash->{form}->{email}, 'Email address exists in profile form.'; + + is $self->stash->{form}->{name}, $self->stash->{person}->name, "Name is set correctly."; + is $self->stash->{form}->{email}, $self->stash->{person}->email, "Email is set correctly."; + }); + +done_testing; diff --git a/Web/t/01_endpoints/06_user_settings/02_do_profile.t b/Web/t/01_endpoints/06_user_settings/02_do_profile.t new file mode 100644 index 0000000..b150b7e --- /dev/null +++ b/Web/t/01_endpoints/06_user_settings/02_do_profile.t @@ -0,0 +1,53 @@ +#!/usr/bin/env perl +use MJB::Web::Test; + +#== +# This test ensures that the profile page displays the user's name and email +# address. +# +# 1. Create user and login. +# 2. Go to the profile page +# 3. Confirm the form fields contain the correct information +# 4. Change the username and email +# 5. Get the profile again and confirm the changes. +#== + +my $t = Test::Mojo::MJB->new('MJB::Web'); + +# Create user and confirm profile +$t->create_user + ->get_ok( '/profile' ) + ->status_is( 200 ) + ->code_block(sub { + my $self = shift; + + + ok exists $self->stash->{form}->{name}, 'Name exists in profile form.'; + ok exists $self->stash->{form}->{email}, 'Email address exists in profile form.'; + + is $self->stash->{form}->{name}, $self->stash->{person}->name, "Name is set correctly."; + is $self->stash->{form}->{email}, $self->stash->{person}->email, "Email is set correctly."; + }); + +# Update the name/email and confirm. +$t->post_ok( '/profile', form => { + name => 'Test Name', + email => 'test@test.com', + password => $t->stash->{person}->name, + }) + ->status_is( 302 ) + ->header_is( location => '/profile' ) + ->get_ok( '/profile' ) + ->status_is( 200 ) + ->code_block(sub { + my $self = shift; + + + ok exists $self->stash->{form}->{name}, 'Name exists in profile form.'; + ok exists $self->stash->{form}->{email}, 'Email address exists in profile form.'; + + is $self->stash->{form}->{name}, 'Test Name', "Name was changed"; + is $self->stash->{form}->{email}, 'test@test.com', "Email was changed"; + }); + +done_testing; diff --git a/Web/t/01_endpoints/06_user_settings/03_change_password.t b/Web/t/01_endpoints/06_user_settings/03_change_password.t new file mode 100644 index 0000000..386dc98 --- /dev/null +++ b/Web/t/01_endpoints/06_user_settings/03_change_password.t @@ -0,0 +1,17 @@ +#!/usr/bin/env perl +use MJB::Web::Test; + +#== +# This test ensures that the password change page exists. +# +# 1. Create user and login. +# 2. Go to the password page and confirm it exists. +#== + +my $t = Test::Mojo::MJB->new('MJB::Web'); + +$t->create_user + ->get_ok( '/password' ) + ->status_is( 200 ); + +done_testing; diff --git a/Web/t/01_endpoints/06_user_settings/04_do_change_password.t b/Web/t/01_endpoints/06_user_settings/04_do_change_password.t new file mode 100644 index 0000000..5902f24 --- /dev/null +++ b/Web/t/01_endpoints/06_user_settings/04_do_change_password.t @@ -0,0 +1,50 @@ +#!/usr/bin/env perl +use MJB::Web::Test; + +#== +# This test ensures that the password change page works as expected. +# +# 1. Create user and login. +# 2. Go to the password page and confirm it exists. +# 3. Change the password for the user account +# 4. Logout and confirm being logged out. +# 5. Login with the new credentials and confirm being logged in. +#== + +my $t = Test::Mojo::MJB->new('MJB::Web'); + +# Create a user and confirm the password page exists. +$t->create_user + ->get_ok( '/password' ) + ->status_is( 200 ); + +# Change the password. +$t->post_ok( '/password', form => { + password => $t->stash->{person}->name, + new_password => 'Test-Password', + password_confirm => 'Test-Password', + }) + ->status_is( 302 ) + ->header_is( location => '/password' ); + +# Grab the email we'll use for logging back in. +my $email = $t->stash->{person}->email; + +# Logout +$t->get_ok( '/logout' ); + +$t->get_ok( '/dashboard' ) + ->status_is( 302 ) + ->header_is( location => '/login', "User is logged out." ); + +# Try to login with the new password. +$t->post_ok( '/login', form => { + email => $email, + password => 'Test-Password', + })->status_is( 302 + )->header_is( location => '/dashboard', 'Login redirected to dashboard' ) + ->code_block( sub { + is( scalar(@{shift->stash->{errors}}), 0, 'No errors' ); + }); + +done_testing;