Skip to content

Commit ad18d3d

Browse files
Add integration test for Email authentication; fix case-sensitivity bug. (vufind-org#3778)
Co-authored-by: Ere Maijala <ere.maijala@helsinki.fi>
1 parent bd54fa8 commit ad18d3d

File tree

4 files changed

+134
-11
lines changed

4 files changed

+134
-11
lines changed

build.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,8 @@
467467

468468
<!-- set up appropriate read/write permissions for Apache -->
469469
<exec command="chmod -R a+w ${localdir}/cache" />
470+
<exec command="touch ${srcdir}/vufind-mail.log" />
471+
<exec command="chmod a+w ${srcdir}/vufind-mail.log" />
470472
<exec command="touch ${srcdir}/vufind-exception.log" />
471473
<exec command="chmod a+w ${srcdir}/vufind-exception.log" />
472474

@@ -824,6 +826,12 @@ ${git_status}
824826
<delete file="${srcdir}/vufind-exception.log" failonerror="true" />
825827
</then>
826828
</if>
829+
<if>
830+
<available file="${srcdir}/vufind-mail.log" />
831+
<then>
832+
<delete file="${srcdir}/vufind-mail.log" failonerror="true" />
833+
</then>
834+
</if>
827835
<delete dir="${srcdir}/vendor" includeemptydirs="true" failonerror="false" />
828836
<if>
829837
<available file="${localdir}" type="dir" />

module/VuFind/src/VuFind/Auth/Email.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function authenticate($request)
8888
$this->emailAuthenticator->sendAuthenticationLink(
8989
$user['email'],
9090
$loginData,
91-
['auth_method' => 'email']
91+
['auth_method' => 'Email']
9292
);
9393
}
9494
// Don't reveal the result
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
/**
4+
* Trait adding the ability to inspect sent emails.
5+
*
6+
* PHP version 8
7+
*
8+
* Copyright (C) Villanova University 2024.
9+
*
10+
* This program is free software; you can redistribute it and/or modify
11+
* it under the terms of the GNU General Public License version 2,
12+
* as published by the Free Software Foundation.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU General Public License
20+
* along with this program; if not, write to the Free Software
21+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22+
*
23+
* @category VuFind
24+
* @package Tests
25+
* @author Demian Katz <demian.katz@villanova.edu>
26+
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27+
* @link https://vufind.org/wiki/development:testing:unit_tests Wiki
28+
*/
29+
30+
namespace VuFindTest\Feature;
31+
32+
/**
33+
* Trait adding the ability to inspect sent emails.
34+
*
35+
* @category VuFind
36+
* @package Tests
37+
* @author Demian Katz <demian.katz@villanova.edu>
38+
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
39+
* @link https://vufind.org/wiki/development:testing:unit_tests Wiki
40+
*/
41+
trait EmailTrait
42+
{
43+
/**
44+
* Get the path to the email message log file.
45+
*
46+
* @return string
47+
*/
48+
protected function getEmailLogPath(): string
49+
{
50+
return APPLICATION_PATH . '/vufind-mail.log';
51+
}
52+
53+
/**
54+
* Clear out the email log to eliminate any past contents.
55+
*
56+
* @return void
57+
*/
58+
protected function resetEmailLog(): void
59+
{
60+
file_put_contents($this->getEmailLogPath(), '');
61+
}
62+
}

module/VuFind/tests/integration-tests/src/VuFindTest/Mink/ChoiceAuthTest.php

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
*/
4343
final class ChoiceAuthTest extends \VuFindTest\Integration\MinkTestCase
4444
{
45+
use \VuFindTest\Feature\EmailTrait;
4546
use \VuFindTest\Feature\LiveDatabaseTrait;
4647
use \VuFindTest\Feature\UserCreationTrait;
4748

@@ -60,7 +61,7 @@ public static function setUpBeforeClass(): void
6061
*
6162
* @return array
6263
*/
63-
public function getConfigIniOverrides()
64+
protected function getConfigIniOverrides(): array
6465
{
6566
return [
6667
'Catalog' => [
@@ -80,7 +81,7 @@ public function getConfigIniOverrides()
8081
*
8182
* @return array
8283
*/
83-
public function getConfigIniSSOOverrides()
84+
protected function getConfigIniSSOOverrides(): array
8485
{
8586
return [
8687
'ChoiceAuth' => [
@@ -90,13 +91,29 @@ public function getConfigIniSSOOverrides()
9091
}
9192

9293
/**
93-
* Get Demo.ini override settings for testing ILS functions.
94+
* Get config.ini override settings for testing ChoiceAuth with Email authentication.
9495
*
95-
* @param string $bibId Bibliographic record ID to create fake item info for.
96+
* @return array
97+
*/
98+
protected function getConfigIniEmailOverrides(): array
99+
{
100+
return [
101+
'ChoiceAuth' => [
102+
'choice_order' => 'Database, Email',
103+
],
104+
'Mail' => [
105+
'testOnly' => true,
106+
'message_log' => $this->getEmailLogPath(),
107+
],
108+
];
109+
}
110+
111+
/**
112+
* Get Demo.ini override settings for testing ILS functions.
96113
*
97114
* @return array
98115
*/
99-
public function getDemoIniOverrides($bibId = 'testsample1')
116+
protected function getDemoIniOverrides(): array
100117
{
101118
return [
102119
'Users' => ['catuser' => 'catpass'],
@@ -108,7 +125,7 @@ public function getDemoIniOverrides($bibId = 'testsample1')
108125
*
109126
* @return array
110127
*/
111-
public function getSimulatedSSOIniOverrides()
128+
protected function getSimulatedSSOIniOverrides(): array
112129
{
113130
return [
114131
'General' => [
@@ -122,7 +139,7 @@ public function getSimulatedSSOIniOverrides()
122139
*
123140
* @return void
124141
*/
125-
public function testCreateDatabaseUser()
142+
public function testCreateDatabaseUser(): void
126143
{
127144
$this->changeConfigs(
128145
[
@@ -161,7 +178,7 @@ public function testCreateDatabaseUser()
161178
*
162179
* @return void
163180
*/
164-
public function testProfile()
181+
public function testProfile(): void
165182
{
166183
$this->changeConfigs(
167184
[
@@ -193,7 +210,7 @@ public function testProfile()
193210
*
194211
* @return void
195212
*/
196-
public function testRecordPageWithILSAndSSO()
213+
public function testRecordPageWithILSAndSSO(): void
197214
{
198215
// Set up configs and session
199216
$this->changeConfigs(
@@ -212,7 +229,6 @@ public function testRecordPageWithILSAndSSO()
212229
$this->clickCss($page, '#loginOptions a');
213230

214231
// login with ILS
215-
$this->clickCss($page, '#loginOptions a');
216232
$this->fillInLoginForm($page, 'catuser', 'catpass', false, '.authmethod0 ');
217233
$this->submitLoginForm($page, false, '.authmethod0 ');
218234

@@ -241,6 +257,43 @@ public function testRecordPageWithILSAndSSO()
241257
$this->clickCss($page, '.logoutOptions a.logout');
242258
}
243259

260+
/**
261+
* Test login with Email authentication.
262+
*
263+
* @return void
264+
*/
265+
public function testEmailAuthentication(): void
266+
{
267+
// Set up configs, session and message logging:
268+
$this->changeConfigs(
269+
[
270+
'config' => $this->getConfigIniEmailOverrides() + $this->getConfigIniOverrides(),
271+
]
272+
);
273+
$session = $this->getMinkSession();
274+
$session->visit($this->getVuFindUrl());
275+
$page = $session->getPage();
276+
$this->resetEmailLog();
277+
278+
// Click login and request email:
279+
$this->clickCss($page, '#loginOptions a');
280+
$this->findCssAndSetValue($page, '#login_Email_username', 'username1@ignore.com');
281+
$this->clickCss($page, '.authmethod1 input[type="submit"]');
282+
$this->assertEquals(
283+
'We have sent a login link to your email address. It may take a few moments for the link to arrive. '
284+
. "If you don't receive the link shortly, please check also your spam filter.",
285+
$this->findCssAndGetText($page, '.modal .alert-success')
286+
);
287+
288+
// Extract the link from the provided message:
289+
$email = file_get_contents($this->getEmailLogPath());
290+
preg_match('/Link to login: <(http.*)>/', $email, $matches);
291+
$session->visit($matches[1]);
292+
293+
// Log out
294+
$this->clickCss($page, '.logoutOptions a.logout');
295+
}
296+
244297
/**
245298
* Standard teardown method.
246299
*

0 commit comments

Comments
 (0)