From 95088b4ffd270f55bc766e120757ab8ac56b0643 Mon Sep 17 00:00:00 2001 From: Julian Maurice Date: Tue, 17 Apr 2018 18:28:38 +0200 Subject: [PATCH] Bug 20635: Write authentication tests with Test::Mojo The authentication code needs to be rewritten. There were several attempts (bugs 7174, 18315) and other bugs without patch that talk about it (12547, 12548, 15204). Bug 13503 says that before rewriting one of the most sensible area of Koha, we need to write tests. Bug 19181 contain some tests, but they require Selenium, firefox, xfvb, java, and a running web server. With Mojolicious we can write equivalent tests without all those dependencies. This patch add basic tests for password and CAS authentication. Test plan: 1. Apply bug 20582 2. Apply this patch 3. Run `prove t/integration/auth` --- t/integration/auth/cas.t | 102 ++++++++++++++++++++++++++++++++++++++++++ t/integration/auth/password.t | 90 +++++++++++++++++++++++++++++++++++++ 2 files changed, 192 insertions(+) create mode 100644 t/integration/auth/cas.t create mode 100755 t/integration/auth/password.t diff --git a/t/integration/auth/cas.t b/t/integration/auth/cas.t new file mode 100644 index 0000000000..aa4e9fceba --- /dev/null +++ b/t/integration/auth/cas.t @@ -0,0 +1,102 @@ +#!/usr/bin/env perl + +use Modern::Perl; + +use Test::More tests => 2; +use Test::Mojo; +use Test::MockModule; + +use Authen::CAS::Client; +use Authen::CAS::Client::Response; + +use t::lib::TestBuilder; +use t::lib::Mocks; + +use Koha::AuthUtils qw( hash_password ); +use Koha::Database; + +my $authen_cas_client = Test::MockModule->new('Authen::CAS::Client'); +$authen_cas_client->mock(service_validate => sub { + my ($self, $service, $ticket) = @_; + if ($ticket eq 'goodticket') { + return Authen::CAS::Client::Response::AuthSuccess->new( + user => 'test', + ); + } + + return Authen::CAS::Client::Response::AuthFailure->new( + message => 'bad ticket', + ); +}); + +my $schema = Koha::Database->new->schema; +$schema->storage->txn_begin; + +t::lib::Mocks::mock_preference('SessionStorage', 'tmp'); + +my $builder = t::lib::TestBuilder->new; + +my $borrower = $builder->build({ + source => 'Borrower', + value => { + userid => 'test', + password => hash_password('test'), + firstname => 'John', + surname => 'Doe', + flags => 1, + }, +}); + +subtest 'intranet' => sub { + plan tests => 10; + + my $t = Test::Mojo->new('Koha::App::Koha'); + + $t->get_ok('/cgi-bin/koha/mainpage.pl') + ->element_exists('form#loginform'); + + my $formdata = { + ticket => 'goodticket', + }; + $t->post_ok('/cgi-bin/koha/mainpage.pl', form => $formdata) + ->element_exists_not('form#loginform') + ->text_is('#logged-in-info-full .loggedinusername', 'test'); + + $t->get_ok('/cgi-bin/koha/mainpage.pl?logout.x=1') + ->element_exists('form#loginform'); + + $formdata = { + ticket => 'badticket', + }; + $t->post_ok('/cgi-bin/koha/mainpage.pl', form => $formdata) + ->element_exists('form#loginform') + ->content_like(qr/Sorry, the CAS login failed/); +}; + +subtest 'opac' => sub { + plan tests => 10; + + my $t = Test::Mojo->new('Koha::App::Koha'); + + $t->get_ok('/cgi-bin/koha/opac-user.pl') + ->element_exists('form#auth'); + + my $formdata = { + ticket => 'goodticket', + }; + $t->post_ok('/cgi-bin/koha/opac-user.pl', form => $formdata) + ->element_exists_not('form#auth') + ->text_like('.loggedinusername', qr/John Doe$/); + + $t->get_ok('/cgi-bin/koha/opac-user.pl?logout.x=1') + ->element_exists('form#auth'); + + $formdata = { + ticket => 'badticket', + }; + $t->post_ok('/cgi-bin/koha/opac-user.pl', form => $formdata) + ->element_exists('form#auth') + ->content_like(qr/Sorry, the CAS login failed/); +}; + +$schema->storage->txn_rollback; diff --git a/t/integration/auth/password.t b/t/integration/auth/password.t new file mode 100755 index 0000000000..aa98b6e9bb --- /dev/null +++ b/t/integration/auth/password.t @@ -0,0 +1,90 @@ +#!/usr/bin/env perl + +use Modern::Perl; + +use Test::More tests => 2; +use Test::Mojo; + +use t::lib::TestBuilder; +use t::lib::Mocks; + +use Koha::AuthUtils qw( hash_password ); +use Koha::Database; + +my $schema = Koha::Database->new->schema; +$schema->storage->txn_begin; + +t::lib::Mocks::mock_preference('SessionStorage', 'tmp'); + +my $builder = t::lib::TestBuilder->new; + +my $borrower = $builder->build({ + source => 'Borrower', + value => { + userid => 'test', + password => hash_password('test'), + firstname => 'John', + surname => 'Doe', + flags => 1, + }, +}); + +subtest 'intranet' => sub { + plan tests => 10; + + my $t = Test::Mojo->new('Koha::App::Koha'); + + $t->get_ok('/cgi-bin/koha/mainpage.pl') + ->element_exists('form#loginform'); + + my $formdata = { + userid => 'test', + password => 'test', + }; + $t->post_ok('/cgi-bin/koha/mainpage.pl', form => $formdata) + ->element_exists_not('form#loginform') + ->text_is('#logged-in-info-full .loggedinusername', 'test'); + + $t->get_ok('/cgi-bin/koha/mainpage.pl?logout.x=1') + ->element_exists('form#loginform'); + + $formdata = { + userid => 'test', + password => 'test2', + }; + $t->post_ok('/cgi-bin/koha/mainpage.pl', form => $formdata) + ->element_exists('form#loginform') + ->element_exists('#login_error'); +}; + +subtest 'opac' => sub { + plan tests => 10; + + my $t = Test::Mojo->new('Koha::App::Koha'); + + $t->get_ok('/cgi-bin/koha/opac-user.pl') + ->element_exists('form#auth'); + + my $formdata = { + userid => 'test', + password => 'test', + koha_login_context => 'opac', + }; + $t->post_ok('/cgi-bin/koha/opac-user.pl', form => $formdata) + ->element_exists_not('form#auth') + ->text_like('.loggedinusername', qr/John Doe$/); + + $t->get_ok('/cgi-bin/koha/opac-user.pl?logout.x=1') + ->element_exists('form#auth'); + + $formdata = { + userid => 'test', + password => 'badpassword', + koha_login_context => 'opac', + }; + $t->post_ok('/cgi-bin/koha/opac-user.pl', form => $formdata) + ->element_exists('form#auth') + ->element_exists('div.alert'); +}; + +$schema->storage->txn_rollback; -- 2.14.2