]>
git.agnieray.net Git - galette.git/blob - tests/Galette/Core/tests/units/Password.php
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2013-2014 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-2014 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-2014 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
62 * @param string $testMethod Method name
66 public function beforeTestMethod($testMethod)
68 $this->zdb
= new \Galette\Core\
Db();
69 $this->pass
= new \Galette\Core\
Password($this->zdb
, false);
73 * Test unique password generator
77 public function testRandom()
81 for ($i = 0; $i < 200; $i++
) {
82 $random = $this->pass
->makeRandomPassword(15);
83 $this->string($random)->hasLength(15);
85 $exists = in_array($random, $results);
86 $this->boolean($exists)->isFalse();
89 $this->array($results)->hasSize($i +
1);
92 $random = $this->pass
->makeRandomPassword();
93 $this->string($random)->hasLength(\Galette\Core\Password
::DEFAULT_SIZE
);
97 * Create member and get its id
101 private function createMember()
104 $this->deleteMember();
105 } catch (\Exception
$e) {
109 $status = new \Galette\Entity\
Status($this->zdb
);
110 if (count($status->getList()) === 0) {
111 $res = $status->installInit();
112 $this->boolean($res)->isTrue();
114 $insert = $this->zdb
->insert(\Galette\Entity\Adherent
::TABLE
);
117 'nom_adh' => 'Test password user',
118 'login_adh' => 'test_password_user'
121 $this->zdb
->execute($insert);
123 if ($this->zdb
->isPostgres()) {
124 return $this->zdb
->driver
->getLastGeneratedValue(
125 PREFIX_DB
. 'adherents_id_seq'
128 return $this->zdb
->driver
->getLastGeneratedValue();
137 private function deleteMember()
139 $delete = $this->zdb
->delete(\Galette\Entity\Adherent
::TABLE
);
140 $delete->where(['login_adh' => 'test_password_user']);
141 $this->zdb
->execute($delete);
145 * Test new PasswordImage generation
149 public function testGenerateNewPassword()
151 $id_adh = $this->createMember();
153 $res = $pass->generateNewPassword($id_adh);
154 $this->boolean($res)->isTrue();
155 $new_pass = $pass->getNewPassword();
156 $this->string($new_pass)
157 ->hasLength($pass::DEFAULT_SIZE
);
158 $hash = $pass->getHash();
159 $this->string($hash)->hasLength(60);
161 $is_valid = $pass->isHashValid($hash);
162 $this->variable($is_valid)->isNotNull();
164 $select = $this->zdb
->select(\Galette\Core\Password
::TABLE
);
165 $results = $this->zdb
->execute($select);
166 $this->integer($results->count())->isIdenticalTo(1);
168 $removed = $pass->removeHash($hash);
169 $this->boolean($removed)->isTrue();
171 $results = $this->zdb
->execute($select);
172 $this->integer($results->count())->isIdenticalTo(0);
174 $this->deleteMember();
182 public function testCleanExpired()
184 $id_adh = $this->createMember();
186 $date = new \
DateTime();
187 $date->sub(new \
DateInterval('PT48H'));
189 $insert = $this->zdb
->insert(\Galette\Core\Password
::TABLE
);
192 \Galette\Core\Password
::PK
=> $id_adh,
193 'date_crea_tmp_passwd' => $date->format('Y-m-d'),
194 'tmp_passwd' => 'azerty'
197 $this->zdb
->execute($insert);
199 $select = $this->zdb
->select(\Galette\Core\Password
::TABLE
);
200 $results = $this->zdb
->execute($select);
201 $this->integer($results->count())->isIdenticalTo(1);
203 $pass = new \Galette\Core\
Password($this->zdb
, true);
205 $results = $this->zdb
->execute($select);
206 $this->integer($results->count())->isIdenticalTo(0);
208 $this->deleteMember();
212 * Generate new password that throws an exception
216 public function testGenerateNewPasswordWException()
218 $this->zdb
= new \mock\Galette\Core\
Db();
219 $this->calling($this->zdb
)->execute
= function ($o) {
220 throw new \
LogicException('Error executing query!', 123);
223 $pass = new \Galette\Core\
Password($this->zdb
, false);
224 $res = $pass->generateNewPassword(12);
225 $this->boolean($res)->isFalse();
229 * Generate new password when insert returns false
233 public function testGenerateNewPasswordWFalseInsert()
235 $this->zdb
= new \mock\Galette\Core\
Db();
236 $this->calling($this->zdb
)->execute
= function ($o) {
240 $pass = new \Galette\Core\
Password($this->zdb
, false);
241 $res = $pass->generateNewPassword(12);
242 $this->boolean($res)->isFalse();
246 * Test cleanExpired that throws an exception
250 public function testCleanExpiredWException()
252 $this->zdb
= new \mock\Galette\Core\
Db();
253 $this->calling($this->zdb
)->execute
= function ($o) {
254 throw new \
LogicException('Error executing query!', 123);
257 $pass = new \Galette\Core\
Password($this->zdb
);
261 * Test hash validity that throws an exception
265 public function testIsHashValidWException()
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 $res = $pass->isHashValid('thehash');
274 $this->boolean($res)->isFalse();
278 * Test hash removal that throws an exception
282 public function testRemoveHashWException()
284 $this->zdb
= new \mock\Galette\Core\
Db();
285 $this->calling($this->zdb
)->execute
= function ($o) {
286 throw new \
LogicException('Error executing query!', 123);
289 $pass = new \Galette\Core\
Password($this->zdb
, false);
290 $res = $pass->removeHash('thehash');
291 $this->boolean($res)->isFalse();