]> git.agnieray.net Git - galette.git/blob - tests/Galette/Core/tests/units/Password.php
7ef0655e654326668955f103021b06500842860b
[galette.git] / tests / Galette / Core / tests / units / Password.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Password tests
7 *
8 * PHP version 5
9 *
10 * Copyright © 2013-2023 The Galette Team
11 *
12 * This file is part of Galette (http://galette.tuxfamily.org).
13 *
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.
18 *
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.
23 *
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/>.
26 *
27 * @category Core
28 * @package GaletteTests
29 *
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
33 * @version SVN: $Id$
34 * @link http://galette.tuxfamily.org
35 * @since 2013-10-22
36 */
37
38 namespace Galette\Core\test\units;
39
40 use atoum;
41
42 /**
43 * Password tests class
44 *
45 * @category Core
46 * @name Password
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
52 * @since 2013-01-13
53 */
54 class Password extends atoum
55 {
56 private ?\Galette\Core\Password $pass = null;
57 private \Galette\Core\Db $zdb;
58
59 /**
60 * Set up tests
61 *
62 * @param string $method Method name
63 *
64 * @return void
65 */
66 public function beforeTestMethod($method)
67 {
68 $this->zdb = new \Galette\Core\Db();
69 $this->pass = new \Galette\Core\Password($this->zdb, false);
70 }
71
72 /**
73 * Tear down tests
74 *
75 * @param string $method Calling method
76 *
77 * @return void
78 */
79 public function afterTestMethod($method)
80 {
81 if (TYPE_DB === 'mysql') {
82 $this->array($this->zdb->getWarnings())->isIdenticalTo([]);
83 }
84 }
85
86 /**
87 * Test unique password generator
88 *
89 * @return void
90 */
91 public function testRandom()
92 {
93 $results = array();
94
95 for ($i = 0; $i < 200; $i++) {
96 $random = $this->pass->makeRandomPassword(15);
97 $this->string($random)->hasLength(15);
98
99 $exists = in_array($random, $results);
100 $this->boolean($exists)->isFalse();
101
102 $results[] = $random;
103 $this->array($results)->hasSize($i + 1);
104 }
105
106 $random = $this->pass->makeRandomPassword();
107 $this->string($random)->hasLength(\Galette\Core\Password::DEFAULT_SIZE);
108 }
109
110 /**
111 * Create member and get its id
112 *
113 * @return int
114 */
115 private function createMember()
116 {
117 try {
118 $this->deleteMember();
119 } catch (\Exception $e) {
120 //empty catch
121 }
122
123 $status = new \Galette\Entity\Status($this->zdb);
124 if (count($status->getList()) === 0) {
125 $res = $status->installInit();
126 $this->boolean($res)->isTrue();
127 }
128 $insert = $this->zdb->insert(\Galette\Entity\Adherent::TABLE);
129 $insert->values(
130 [
131 'nom_adh' => 'Test password user',
132 'login_adh' => 'test_password_user',
133 'adresse_adh' => 'The address'
134 ]
135 );
136 $this->zdb->execute($insert);
137
138 if ($this->zdb->isPostgres()) {
139 return $this->zdb->driver->getLastGeneratedValue(
140 PREFIX_DB . 'adherents_id_seq'
141 );
142 } else {
143 return $this->zdb->driver->getLastGeneratedValue();
144 }
145 }
146
147 /**
148 * Delete member
149 *
150 * @return void
151 */
152 private function deleteMember()
153 {
154 $delete = $this->zdb->delete(\Galette\Entity\Adherent::TABLE);
155 $delete->where(['login_adh' => 'test_password_user']);
156 $this->zdb->execute($delete);
157 }
158
159 /**
160 * Test new Password generation
161 *
162 * @return void
163 */
164 public function testGenerateNewPassword()
165 {
166 $id_adh = $this->createMember();
167 $pass = $this->pass;
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);
175
176 $is_valid = $pass->isHashValid($hash);
177 $this->variable($is_valid)->isNotNull();
178
179 $select = $this->zdb->select(\Galette\Core\Password::TABLE);
180 $results = $this->zdb->execute($select);
181 $this->integer($results->count())->isIdenticalTo(1);
182
183 $removed = $pass->removeHash($hash);
184 $this->boolean($removed)->isTrue();
185
186 $results = $this->zdb->execute($select);
187 $this->integer($results->count())->isIdenticalTo(0);
188
189 $this->deleteMember();
190 }
191
192 /**
193 * Test cleanExpired
194 *
195 * @return void
196 */
197 public function testCleanExpired()
198 {
199 $id_adh = $this->createMember();
200
201 $date = new \DateTime();
202 $date->sub(new \DateInterval('PT48H'));
203
204 $insert = $this->zdb->insert(\Galette\Core\Password::TABLE);
205 $insert->values(
206 [
207 \Galette\Core\Password::PK => $id_adh,
208 'date_crea_tmp_passwd' => $date->format('Y-m-d'),
209 'tmp_passwd' => 'azerty'
210 ]
211 );
212 $this->zdb->execute($insert);
213
214 $select = $this->zdb->select(\Galette\Core\Password::TABLE);
215 $results = $this->zdb->execute($select);
216 $this->integer($results->count())->isIdenticalTo(1);
217
218 $pass = new \Galette\Core\Password($this->zdb, true);
219
220 $results = $this->zdb->execute($select);
221 $this->integer($results->count())->isIdenticalTo(0);
222
223 $this->deleteMember();
224 }
225
226 /**
227 * Generate new password that throws an exception
228 *
229 * @return void
230 */
231 public function testGenerateNewPasswordWException()
232 {
233 $this->zdb = new \mock\Galette\Core\Db();
234 $this->calling($this->zdb)->execute = function ($o) {
235 throw new \LogicException('Error executing query!', 123);
236 };
237
238 $pass = new \Galette\Core\Password($this->zdb, false);
239 $res = $pass->generateNewPassword(12);
240 $this->boolean($res)->isFalse();
241 }
242
243 /**
244 * Generate new password when insert returns false
245 *
246 * @return void
247 */
248 public function testGenerateNewPasswordWFalseInsert()
249 {
250 $this->zdb = new \mock\Galette\Core\Db();
251 $this->calling($this->zdb)->execute = function ($o) {
252 throw new \Exception('Ba. Da. Boum.');
253 };
254
255 $pass = new \Galette\Core\Password($this->zdb, false);
256 $res = $pass->generateNewPassword(12);
257 $this->boolean($res)->isFalse();
258 }
259
260 /**
261 * Test cleanExpired that throws an exception
262 *
263 * @return void
264 */
265 public function testCleanExpiredWException()
266 {
267 $this->zdb = new \mock\Galette\Core\Db();
268 $this->calling($this->zdb)->execute = function ($o) {
269 throw new \LogicException('Error executing query!', 123);
270 };
271
272 $pass = new \Galette\Core\Password($this->zdb, false);
273 $this->boolean($pass->cleanExpired())->isFalse();
274 }
275
276 /**
277 * Test hash validity that throws an exception
278 *
279 * @return void
280 */
281 public function testIsHashValidWException()
282 {
283 $this->zdb = new \mock\Galette\Core\Db();
284 $this->calling($this->zdb)->execute = function ($o) {
285 throw new \LogicException('Error executing query!', 123);
286 };
287
288 $pass = new \Galette\Core\Password($this->zdb, false);
289 $res = $pass->isHashValid('thehash');
290 $this->boolean($res)->isFalse();
291 }
292
293 /**
294 * Test hash removal that throws an exception
295 *
296 * @return void
297 */
298 public function testRemoveHashWException()
299 {
300 $this->zdb = new \mock\Galette\Core\Db();
301 $this->calling($this->zdb)->execute = function ($o) {
302 throw new \LogicException('Error executing query!', 123);
303 };
304
305 $pass = new \Galette\Core\Password($this->zdb, false);
306 $res = $pass->removeHash('thehash');
307 $this->boolean($res)->isFalse();
308 }
309 }