]>
git.agnieray.net Git - galette.git/blob - tests/Galette/Util/tests/units/Password.php
0253d9c17ec6e37fa82c7cbf28746a55e75009e2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2020 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 2020 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 * @link http://galette.tuxfamily.org
37 namespace Galette\Util\test\units
;
40 use Galette\Core\Preferences
;
43 * Password tests class
47 * @package GaletteTests
48 * @author Johan Cwiklinski <johan@x-tnd.be>
49 * @copyright 2020 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 $method Method tested
66 public function afterTestMethod($method)
68 $this->preferences
->pref_password_strength
= Preferences
::PWD_NONE
;
69 $this->preferences
->pref_password_length
= 6;
70 $this->preferences
->pref_password_blacklist
= false;
71 $this->preferences
->store();
72 return parent
::afterTestMethod($method);
78 * @param string $testMethod Method tested
82 public function beforeTestMethod($testMethod)
84 $this->zdb
= new \Galette\Core\
Db();
85 $this->preferences
= new \Galette\Core\
Preferences($this->zdb
);
89 * Passwords data provider
93 protected function passProvider()
96 // [strength, password, errors]
97 [Preferences
::PWD_WEAK
, 'weaker', ['A', '1', '@']],
98 [Preferences
::PWD_WEAK
, '123456', ['nl', '@']],
99 [Preferences
::PWD_WEAK
, '²²²²²²', ['nl', '@']],
100 [Preferences
::PWD_WEAK
, 'foobar', ['A', '1', '@']],
101 [Preferences
::PWD_WEAK
, 'ömgwat', ['A', '1', '@']],
102 [Preferences
::PWD_WEAK
, '!.!.!.', ['nl', '1']],
103 [Preferences
::PWD_WEAK
, '!.!.!﴾', ['nl', '1']],
104 [Preferences
::PWD_WEAK
, '7857375923752947', ['nl', '@']],
105 [Preferences
::PWD_WEAK
, 'FSDFJSLKFFSDFDSF', ['a', '1', '@']],
106 [Preferences
::PWD_WEAK
, 'FÜKFJSLKFFSDFDSF', ['a', '1', '@']],
107 [Preferences
::PWD_WEAK
, 'fjsfjdljfsjsjjlsj', ['A', '1', '@']],
109 [Preferences
::PWD_MEDIUM
, 'wee6eak',['A', '@']],
110 [Preferences
::PWD_MEDIUM
, 'foobar!', ['A', '1']],
111 [Preferences
::PWD_MEDIUM
, 'Foobar', ['1', '@']],
112 [Preferences
::PWD_MEDIUM
, '123456!', ['nl']],
113 [Preferences
::PWD_MEDIUM
, 'fjsfjdljfsjsjjls1', ['A', '@']],
114 [Preferences
::PWD_MEDIUM
, '785737592375294b', ['A', '@']],
116 [Preferences
::PWD_STRONG
, 'Foobar﴾', ['1']],
117 [Preferences
::PWD_STRONG
, 'foo-b0r!', ['A']],
119 [Preferences
::PWD_VERY_STRONG
, 'Foobar!55!', []],
120 [Preferences
::PWD_VERY_STRONG
, 'Foobar$55', []],
121 [Preferences
::PWD_VERY_STRONG
, 'Foobar€55', []],
122 [Preferences
::PWD_VERY_STRONG
, 'Foobar€55', []],
123 [Preferences
::PWD_VERY_STRONG
, 'Foobar$55_4&F', []],
124 [Preferences
::PWD_VERY_STRONG
, 'L33RoyJ3Jenkins!', []],
129 * Test password validation
131 * @dataProvider passProvider
133 * @param integer $level Password level
134 * @param string $pass Password
135 * @param array $errors Errors
139 public function testValidatePassword($level, $pass, $errors)
141 //errror messages mapping
142 foreach ($errors as &$err) {
145 $err = 'Does not contains letters';
148 $err = 'Does not contains lowercase letters';
151 $err = 'Does not contains uppercase letters';
154 $err = 'Does not contains numbers';
157 $err = 'Does not contains special characters';
162 if ($level < Preferences
::PWD_VERY_STRONG
) {
163 $this->preferences
->pref_password_strength
= $level +
1;
164 $password = new \Galette\Util\
Password($this->preferences
);
165 $this->boolean($password->isValid($pass))->isFalse();
166 $this->array($password->getErrors())->isEqualTo($errors);
169 $this->preferences
->pref_password_strength
= $level;
170 $password = new \Galette\Util\
Password($this->preferences
);
171 $this->boolean($password->isValid($pass))->isTrue(implode(', ', $password->getErrors()));
172 $this->array($password->getErrors())->isEqualTo([]);
173 $this->array($password->getStrenghtErrors())->isEqualTo($errors);
177 * Blacklist password provider
181 protected function blacklistProvider()
186 ['mypassisgreat', false],
194 * Test password blacklist
196 * @dataProvider blacklistProvider
198 * @param string $pass Password to test
199 * @param boolean $expected Excpected return
203 public function testBlacklist($pass, $expected)
205 $this->preferences
->pref_password_blacklist
= true;
206 $password = new \Galette\Util\
Password($this->preferences
);
207 $this->boolean($password->isBlacklisted($pass))->isIdenticalTo($expected, $pass);
209 $this->preferences
->pref_password_blacklist
= false;
210 $password = new \Galette\Util\
Password($this->preferences
);
211 $this->boolean($password->isBlacklisted($pass))->isFalse();
215 * Test with personal information
219 public function testPersonalInformation()
222 'login' => 'mylogin',
224 'surname' => 'mysurname',
225 'nickname' => 'mynickname'
228 $this->preferences
->pref_password_strength
= Preferences
::PWD_NONE
;
229 $password = new \Galette\Util\
Password($this->preferences
);
230 $password->addPersonalInformation($infos);
231 foreach ($infos as $info) {
232 $this->boolean($password->isValid($info))->isTrue(implode(', ', $password->getErrors()));
233 $this->array($password->getErrors())->isEqualTo([]);
236 $this->preferences
->pref_password_strength
= Preferences
::PWD_WEAK
;
237 $password = new \Galette\Util\
Password($this->preferences
);
238 $password->addPersonalInformation($infos);
239 foreach ($infos as $info) {
240 $this->boolean($password->isValid($info))->isFalse();
241 $this->array($password->getErrors())
242 ->isEqualTo(['Do not use any of your personal information as password!']);
245 $this->boolean($password->isValid('MyLoGiN'))->isFalse();
246 $this->boolean($password->isValid('iMyLoGiN'))->isTrue();
249 global $zdb, $login, $i18n; // globals :(
251 $session = new \RKA\
Session();
252 $i18n = new \Galette\Core\
I18n(
253 \Galette\Core\I18n
::DEFAULT_LANG
255 $login = new \Galette\Core\
Login($this->zdb
, $i18n, $session);
256 $history = new \Galette\Core\
History($this->zdb
, $login);
257 include_once GALETTE_ROOT
. 'includes/fields_defs/members_fields.php';
258 $members_fields = $members_fields;
260 $adh = new \Galette\Entity\
Adherent($this->zdb
);
261 $adh->setDependencies(
268 'nom_adh' => 'Pignon',
269 'prenom_adh' => 'Jean-Cloud Juste',
270 'ddn_adh' => '1980-05-01',
271 'ville_adh' => 'Paris',
272 'pseudo_adh' => 'petit-cheval-de-manège',
273 'login_adh' => 'log_In',
274 'email_adh' => 'mail@galette.eu',
275 //required for check to work
276 'date_crea_adh' => date('Y-m-d'),
277 'sexe_adh' => \Galette\Entity\Adherent
::NC
279 $check = $adh->check($adh_data, [], []);
280 if (is_array($check)) {
283 $this->boolean($check)->isTrue();
285 $password = new \Galette\Util\
Password($this->preferences
);
286 $password->setAdherent($adh);
288 unset($adh_data['date_crea_adh']);
289 unset($adh_data['sexe_adh']);
291 $adh_data['c00'] = 'jean-cloud justepignon';
292 $adh_data['c000'] = 'pignonjean-cloud juste';
293 $adh_data['c01'] = 'jcjpignon';
294 $adh_data['c02'] = 'pignonjcj';
295 $adh_data['c03'] = 'pignonj';
296 $adh_data['c04'] = 'jpignon';
298 $adh_data['c10'] = 'log_inpignon';
299 $adh_data['c100'] = 'pignonlog_in';
300 $adh_data['c11'] = 'pignonli';
301 $adh_data['c12'] = 'lipignon';
302 $adh_data['c13'] = 'lpignon';
303 $adh_data['c14'] = 'pignonl';
305 $adh_data['c20'] = 'petit-cheval-de-manègepignon';
306 $adh_data['c200'] = 'pignonpetit-cheval-de-manège';
307 $adh_data['c21'] = 'pignonpcdm';
308 $adh_data['c22'] = 'pcdmpignon';
309 $adh_data['c23'] = 'ppignon';
310 $adh_data['c24'] = 'pignonp';
312 foreach ($adh_data as $key => $data) {
313 $this->boolean($password->isValid($data))->isFalse($key);
316 $this->boolean($password->isValid('19800501'))->isFalse();
317 $this->boolean($password->isValid('01051980'))->isFalse();