From 1d5dd1e603dd143e9f76cf5b4a4732bb4461a447 Mon Sep 17 00:00:00 2001 From: Manager Bot Date: Fri, 25 Nov 2022 06:38:02 +0000 Subject: [PATCH] More tests. --- Web/t/01_endpoints/01_admin/10_servers.t | 51 +++++++++++++ Web/t/01_endpoints/01_admin/11_do_server.t | 59 +++++++++++++++ .../01_admin/12_do_server_remove.t | 73 +++++++++++++++++++ 3 files changed, 183 insertions(+) create mode 100644 Web/t/01_endpoints/01_admin/10_servers.t create mode 100644 Web/t/01_endpoints/01_admin/11_do_server.t create mode 100644 Web/t/01_endpoints/01_admin/12_do_server_remove.t diff --git a/Web/t/01_endpoints/01_admin/10_servers.t b/Web/t/01_endpoints/01_admin/10_servers.t new file mode 100644 index 0000000..d32f44e --- /dev/null +++ b/Web/t/01_endpoints/01_admin/10_servers.t @@ -0,0 +1,51 @@ +#!/usr/bin/env perl +use MJB::Web::Test; + +#== +# This test file ensures that the /admin/servers page has the correct permissions +# and has a server array in the stash. +#== + +my $t = Test::Mojo::MJB->new('MJB::Web'); + +# Make sure an unauthed user cannot access this. +$t->get_ok( '/admin/servers' ) + ->status_is( 302 ) + ->header_is( location => '/login', 'Anonymouse users may not access the admin server panel.' ); + +# Register a user account and log into it. +# +# A normal user should still not be allowed to view this page. +# +# Promote the user to an admin +$t->app->config->{register}{enable_open} = 1; +$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' ); + }) + ->get_ok( '/admin/servers' ) + ->status_is( 302 ) + ->header_is( location => '/dashboard', 'Normal users may not access the admin server panel.' ) + ->get_ok( '/' ) + ->code_block( sub { + my $self = shift; + $self->stash->{person}->is_admin( 1 ); + ok( $self->stash->{person}->update, 'Promoted fred to an admin' ); + }); + +$t->get_ok( '/admin/servers' ) + ->status_is( 200 ) + ->code_block( sub { + my $self = shift; + + is ref($self->stash->{servers}), 'ARRAY', 'Have an array ref for servers'; + is scalar(@{$self->stash->{servers}}), 0, 'Have an empty array ref for servers'; + }); + +done_testing; diff --git a/Web/t/01_endpoints/01_admin/11_do_server.t b/Web/t/01_endpoints/01_admin/11_do_server.t new file mode 100644 index 0000000..d4e6a28 --- /dev/null +++ b/Web/t/01_endpoints/01_admin/11_do_server.t @@ -0,0 +1,59 @@ +#!/usr/bin/env perl +use MJB::Web::Test; + +#== +# This test file ensures that servers can be added through the admin panel. +# +# It creates an admin user, who then creates a servers name, and it confirms the +# server exists in the stash for the /admin/servers page. +#== + +my $t = Test::Mojo::MJB->new('MJB::Web'); + +# Make sure an unauthed user cannot access this. +$t->get_ok( '/admin/servers' ) + ->status_is( 302 ) + ->header_is( location => '/login', 'Anonymouse users may not access the admin server panel.' ); + +# Register a user account and log into it. +# +# A normal user should still not be allowed to view this page. +# +# Promote the user to an admin +$t->app->config->{register}{enable_open} = 1; +$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' ); + }) + ->get_ok( '/admin/servers' ) + ->status_is( 302 ) + ->header_is( location => '/dashboard', 'Normal users may not access the admin server panel.' ) + ->get_ok( '/' ) + ->code_block( sub { + my $self = shift; + $self->stash->{person}->is_admin( 1 ); + ok( $self->stash->{person}->update, 'Promoted fred to an admin' ); + }); + +# Add a server. +$t->post_ok( '/admin/server', form => { + server_fqdn => 'example-deploy-server.com', + }) + ->header_is( location => '/admin/servers' ); + +# Check to ensure that the server exists now. +$t->get_ok( '/admin/servers' ) + ->code_block( sub { + my $self = shift; + + is ref($self->stash->{servers}), 'ARRAY', 'Have an array ref for servers'; + is $self->stash->{servers}->[0]->hostname, 'example-deploy-server.com', 'Have an entry for the server.'; + }); + +done_testing; diff --git a/Web/t/01_endpoints/01_admin/12_do_server_remove.t b/Web/t/01_endpoints/01_admin/12_do_server_remove.t new file mode 100644 index 0000000..8da2acf --- /dev/null +++ b/Web/t/01_endpoints/01_admin/12_do_server_remove.t @@ -0,0 +1,73 @@ +#!/usr/bin/env perl +use MJB::Web::Test; + +#== +# This test file ensures that servers can be removed through the admin panel. +# +# It creates an admin user, who then creates a servers name, and it confirms the +# server exists in the stash for the /admin/servers page, then it deletes the +# server and confirms it is no longer available. +#== + +my $t = Test::Mojo::MJB->new('MJB::Web'); + +# Make sure an unauthed user cannot access this. +$t->get_ok( '/admin/servers' ) + ->status_is( 302 ) + ->header_is( location => '/login', 'Anonymouse users may not access the admin server panel.' ); + +# Register a user account and log into it. +# +# A normal user should still not be allowed to view this page. +# +# Promote the user to an admin +$t->app->config->{register}{enable_open} = 1; +$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' ); + }) + ->get_ok( '/admin/servers' ) + ->status_is( 302 ) + ->header_is( location => '/dashboard', 'Normal users may not access the admin server panel.' ) + ->get_ok( '/' ) + ->code_block( sub { + my $self = shift; + $self->stash->{person}->is_admin( 1 ); + ok( $self->stash->{person}->update, 'Promoted fred to an admin' ); + }); + +# Add a server. +$t->post_ok( '/admin/server', form => { + server_fqdn => 'example-deploy-server.com', + }) + ->header_is( location => '/admin/servers' ); + +# Check to ensure that the server exists now, and remove it. +$t->get_ok( '/admin/servers' ) + ->code_block( sub { + my $self = shift; + + is ref($self->stash->{servers}), 'ARRAY', 'Have an array ref for servers'; + is $self->stash->{servers}->[0]->hostname, 'example-deploy-server.com', 'Have an entry for the server.'; + + # Now we will remove the server. + $t->post_ok( '/admin/server/remove', form => { sid => $self->stash->{servers}->[0]->id }) + ->header_is( location => '/admin/servers' ); + }); + +# Confirm the server has been removed. +$t->get_ok( '/admin/servers' ) + ->code_block( sub { + my $self = shift; + + is ref($self->stash->{servers}), 'ARRAY', 'Have an array ref for servers'; + is scalar(@{$self->stash->{servers}}), 0, 'Server was removed.'; + }); + +done_testing;