]>
git.agnieray.net Git - galette.git/blob - tests/Galette/Core/tests/units/Password.php
7ef0655e654326668955f103021b06500842860b
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2013-2023 The Galette Team
12 * This file is part of Galette (http://galette.tuxfamily.org).
14 * Galette is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * Galette is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
28 * @package GaletteTests
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2013-2023 The Galette Team
32 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
34 * @link http://galette.tuxfamily.org
38 namespace Galette\Core\test\units
;
43 * Password tests class
47 * @package GaletteTests
48 * @author Johan Cwiklinski <johan@x-tnd.be>
49 * @copyright 2013-2023 The Galette Team
50 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
51 * @link http://galette.tuxfamily.org
54 class Password
extends atoum
56 private ?\Galette\Core\Password
$pass = null;
57 private \Galette\Core\Db
$zdb;
62 * @param string $method Method name
66 public function beforeTestMethod($method)
68 $this->zdb
= new \Galette\Core\
Db();
69 $this->pass
= new \Galette\Core\
Password($this->zdb
, false);
75 * @param string $method Calling method
79 public function afterTestMethod($method)
81 if (TYPE_DB
=== 'mysql') {
82 $this->array($this->zdb
->getWarnings())->isIdenticalTo([]);
87 * Test unique password generator
91 public function testRandom()
95 for ($i = 0; $i < 200; $i++
) {
96 $random = $this->pass
->makeRandomPassword(15);
97 $this->string($random)->hasLength(15);
99 $exists = in_array($random, $results);
100 $this->boolean($exists)->isFalse();
102 $results[] = $random;
103 $this->array($results)->hasSize($i +
1);
106 $random = $this->pass
->makeRandomPassword();
107 $this->string($random)->hasLength(\Galette\Core\Password
::DEFAULT_SIZE
);
111 * Create member and get its id
115 private function createMember()
118 $this->deleteMember();
119 } catch (\Exception
$e) {
123 $status = new \Galette\Entity\
Status($this->zdb
);
124 if (count($status->getList()) === 0) {
125 $res = $status->installInit();
126 $this->boolean($res)->isTrue();
128 $insert = $this->zdb
->insert(\Galette\Entity\Adherent
::TABLE
);
131 'nom_adh' => 'Test password user',
132 'login_adh' => 'test_password_user',
133 'adresse_adh' => 'The address'
136 $this->zdb
->execute($insert);
138 if ($this->zdb
->isPostgres()) {
139 return $this->zdb
->driver
->getLastGeneratedValue(
140 PREFIX_DB
. 'adherents_id_seq'
143 return $this->zdb
->driver
->getLastGeneratedValue();
152 private function deleteMember()
154 $delete = $this->zdb
->delete(\Galette\Entity\Adherent
::TABLE
);
155 $delete->where(['login_adh' => 'test_password_user']);
156 $this->zdb
->execute($delete);
160 * Test new Password generation
164 public function testGenerateNewPassword()
166 $id_adh = $this->createMember();
168 $res = $pass->generateNewPassword($id_adh);
169 $this->boolean($res)->isTrue();
170 $new_pass = $pass->getNewPassword();
171 $this->string($new_pass)
172 ->hasLength($pass::DEFAULT_SIZE
);
173 $hash = $pass->getHash();
174 $this->string($hash)->hasLength(60);
176 $is_valid = $pass->isHashValid($hash);
177 $this->variable($is_valid)->isNotNull();
179 $select = $this->zdb
->select(\Galette\Core\Password
::TABLE
);
180 $results = $this->zdb
->execute($select);
181 $this->integer($results->count())->isIdenticalTo(1);
183 $removed = $pass->removeHash($hash);
184 $this->boolean($removed)->isTrue();
186 $results = $this->zdb
->execute($select);
187 $this->integer($results->count())->isIdenticalTo(0);
189 $this->deleteMember();
197 public function testCleanExpired()
199 $id_adh = $this->createMember();
201 $date = new \
DateTime();
202 $date->sub(new \
DateInterval('PT48H'));
204 $insert = $this->zdb
->insert(\Galette\Core\Password
::TABLE
);
207 \Galette\Core\Password
::PK
=> $id_adh,
208 'date_crea_tmp_passwd' => $date->format('Y-m-d'),
209 'tmp_passwd' => 'azerty'
212 $this->zdb
->execute($insert);
214 $select = $this->zdb
->select(\Galette\Core\Password
::TABLE
);
215 $results = $this->zdb
->execute($select);
216 $this->integer($results->count())->isIdenticalTo(1);
218 $pass = new \Galette\Core\
Password($this->zdb
, true);
220 $results = $this->zdb
->execute($select);
221 $this->integer($results->count())->isIdenticalTo(0);
223 $this->deleteMember();
227 * Generate new password that throws an exception
231 public function testGenerateNewPasswordWException()
233 $this->zdb
= new \mock\Galette\Core\
Db();
234 $this->calling($this->zdb
)->execute
= function ($o) {
235 throw new \
LogicException('Error executing query!', 123);
238 $pass = new \Galette\Core\
Password($this->zdb
, false);
239 $res = $pass->generateNewPassword(12);
240 $this->boolean($res)->isFalse();
244 * Generate new password when insert returns false
248 public function testGenerateNewPasswordWFalseInsert()
250 $this->zdb
= new \mock\Galette\Core\
Db();
251 $this->calling($this->zdb
)->execute
= function ($o) {
252 throw new \
Exception('Ba. Da. Boum.');
255 $pass = new \Galette\Core\
Password($this->zdb
, false);
256 $res = $pass->generateNewPassword(12);
257 $this->boolean($res)->isFalse();
261 * Test cleanExpired that throws an exception
265 public function testCleanExpiredWException()
267 $this->zdb
= new \mock\Galette\Core\
Db();
268 $this->calling($this->zdb
)->execute
= function ($o) {
269 throw new \
LogicException('Error executing query!', 123);
272 $pass = new \Galette\Core\
Password($this->zdb
, false);
273 $this->boolean($pass->cleanExpired())->isFalse();
277 * Test hash validity that throws an exception
281 public function testIsHashValidWException()
283 $this->zdb
= new \mock\Galette\Core\
Db();
284 $this->calling($this->zdb
)->execute
= function ($o) {
285 throw new \
LogicException('Error executing query!', 123);
288 $pass = new \Galette\Core\
Password($this->zdb
, false);
289 $res = $pass->isHashValid('thehash');
290 $this->boolean($res)->isFalse();
294 * Test hash removal that throws an exception
298 public function testRemoveHashWException()
300 $this->zdb
= new \mock\Galette\Core\
Db();
301 $this->calling($this->zdb
)->execute
= function ($o) {
302 throw new \
LogicException('Error executing query!', 123);
305 $pass = new \Galette\Core\
Password($this->zdb
, false);
306 $res = $pass->removeHash('thehash');
307 $this->boolean($res)->isFalse();