]> git.agnieray.net Git - galette.git/blob - tests/Galette/Util/tests/units/Password.php
0253d9c17ec6e37fa82c7cbf28746a55e75009e2
[galette.git] / tests / Galette / Util / tests / units / Password.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Telemetry tests
7 *
8 * PHP version 5
9 *
10 * Copyright © 2020 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 Util
28 * @package GaletteTests
29 *
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
34 * @since 2020-04-25
35 */
36
37 namespace Galette\Util\test\units;
38
39 use \atoum;
40 use Galette\Core\Preferences;
41
42 /**
43 * Password tests class
44 *
45 * @category Util
46 * @name Telemetry
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
52 * @since 2020-04-25
53 */
54 class Password extends atoum
55 {
56 private $zdb;
57 private $preferences;
58
59 /**
60 * Tear down tests
61 *
62 * @param string $method Method tested
63 *
64 * @return void
65 */
66 public function afterTestMethod($method)
67 {
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);
73 }
74
75 /**
76 * Set up tests
77 *
78 * @param string $testMethod Method tested
79 *
80 * @return void
81 */
82 public function beforeTestMethod($testMethod)
83 {
84 $this->zdb = new \Galette\Core\Db();
85 $this->preferences = new \Galette\Core\Preferences($this->zdb);
86 }
87
88 /**
89 * Passwords data provider
90 *
91 * @return array
92 */
93 protected function passProvider()
94 {
95 return [
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', '@']],
108
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', '@']],
115
116 [Preferences::PWD_STRONG, 'Foobar﴾', ['1']],
117 [Preferences::PWD_STRONG, 'foo-b0r!', ['A']],
118
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!', []],
125 ];
126 }
127
128 /**
129 * Test password validation
130 *
131 * @dataProvider passProvider
132 *
133 * @param integer $level Password level
134 * @param string $pass Password
135 * @param array $errors Errors
136 *
137 * @return void
138 */
139 public function testValidatePassword($level, $pass, $errors)
140 {
141 //errror messages mapping
142 foreach ($errors as &$err) {
143 switch ($err) {
144 case 'nl':
145 $err = 'Does not contains letters';
146 break;
147 case 'a':
148 $err = 'Does not contains lowercase letters';
149 break;
150 case 'A':
151 $err = 'Does not contains uppercase letters';
152 break;
153 case 1:
154 $err = 'Does not contains numbers';
155 break;
156 case '@':
157 $err = 'Does not contains special characters';
158 break;
159 }
160 }
161
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);
167 }
168
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);
174 }
175
176 /**
177 * Blacklist password provider
178 *
179 * @return array
180 */
181 protected function blacklistProvider()
182 {
183 return [
184 ['galette', true],
185 ['toto', false],
186 ['mypassisgreat', false],
187 ['starwars', true],
188 ['123456', true],
189 ['588795', false]
190 ];
191 }
192
193 /**
194 * Test password blacklist
195 *
196 * @dataProvider blacklistProvider
197 *
198 * @param string $pass Password to test
199 * @param boolean $expected Excpected return
200 *
201 * @return void
202 */
203 public function testBlacklist($pass, $expected)
204 {
205 $this->preferences->pref_password_blacklist = true;
206 $password = new \Galette\Util\Password($this->preferences);
207 $this->boolean($password->isBlacklisted($pass))->isIdenticalTo($expected, $pass);
208
209 $this->preferences->pref_password_blacklist = false;
210 $password = new \Galette\Util\Password($this->preferences);
211 $this->boolean($password->isBlacklisted($pass))->isFalse();
212 }
213
214 /**
215 * Test with personal information
216 *
217 * @return void
218 */
219 public function testPersonalInformation()
220 {
221 $infos = [
222 'login' => 'mylogin',
223 'name' => 'myname',
224 'surname' => 'mysurname',
225 'nickname' => 'mynickname'
226 ];
227
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([]);
234 }
235
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!']);
243 }
244
245 $this->boolean($password->isValid('MyLoGiN'))->isFalse();
246 $this->boolean($password->isValid('iMyLoGiN'))->isTrue();
247
248 //create member
249 global $zdb, $login, $i18n; // globals :(
250 $zdb = $this->zdb;
251 $session = new \RKA\Session();
252 $i18n = new \Galette\Core\I18n(
253 \Galette\Core\I18n::DEFAULT_LANG
254 );
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;
259
260 $adh = new \Galette\Entity\Adherent($this->zdb);
261 $adh->setDependencies(
262 $this->preferences,
263 $members_fields,
264 $history
265 );
266
267 $adh_data = [
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
278 ];
279 $check = $adh->check($adh_data, [], []);
280 if (is_array($check)) {
281 var_dump($check);
282 }
283 $this->boolean($check)->isTrue();
284
285 $password = new \Galette\Util\Password($this->preferences);
286 $password->setAdherent($adh);
287
288 unset($adh_data['date_crea_adh']);
289 unset($adh_data['sexe_adh']);
290 //add compounds
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';
297
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';
304
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';
311
312 foreach ($adh_data as $key => $data) {
313 $this->boolean($password->isValid($data))->isFalse($key);
314 }
315
316 $this->boolean($password->isValid('19800501'))->isFalse();
317 $this->boolean($password->isValid('01051980'))->isFalse();
318 }
319 }