Browse Source

tests

develop
Robin Thoni 8 years ago
parent
commit
31bf034093

+ 32
- 21
src/Auth/Business/LuUsersBusiness.php View File

@@ -8,7 +8,7 @@
8 8
 
9 9
 namespace Luticate\Auth\Business;
10 10
 
11
-use Luticate\Auth\DataAccess\LuUserDataAccess;
11
+use Luticate\Auth\DataAccess\LuUsersDataAccess;
12 12
 use Luticate\Auth\Dbo\LuBuiltInPermissions;
13 13
 use Luticate\Auth\Dbo\Users\LuUsersAddDbo;
14 14
 use Luticate\Auth\Dbo\Users\LuUsersDbo;
@@ -27,36 +27,36 @@ class LuUsersBusiness extends LuBusiness
27 27
     const KEY_DATA =  "data";
28 28
 
29 29
     /**
30
-     * @return LuUserDataAccess
30
+     * @return LuUsersDataAccess
31 31
      */
32 32
     protected static function getDataAccess()
33 33
     {
34
-        return new LuUserDataAccess();
34
+        return new LuUsersDataAccess();
35 35
     }
36 36
 
37
-    protected function badPassword()
37
+    protected static function badPassword()
38 38
     {
39 39
         static::unauthorized("Bad username/password");
40 40
     }
41 41
 
42
-    public function hashPassword(string $password)
42
+    public static function hashPassword(string $password)
43 43
     {
44 44
         return password_hash($password, PASSWORD_BCRYPT);
45 45
     }
46 46
 
47
-    public function verifyPassword(string $password, string $hash)
47
+    public static function verifyPassword(string $password, string $hash)
48 48
     {
49 49
         return password_verify($password, $hash);
50 50
     }
51 51
 
52
-    public function checkPasswordRequirements(string $password)
52
+    public static function checkPasswordRequirements(string $password)
53 53
     {
54 54
         if (strlen($password) < 5) { //TODO: add a setting
55 55
             self::badInput("Password must have at least 5 characters");
56 56
         }
57 57
     }
58 58
 
59
-    public function getSalt($length = 16)
59
+    public static function getSalt($length = 16)
60 60
     {
61 61
         $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
62 62
         $charactersLength = strlen($characters);
@@ -67,7 +67,7 @@ class LuUsersBusiness extends LuBusiness
67 67
         return $randomString;
68 68
     }
69 69
 
70
-    public function getToken(LuUsersDbo $user, $data = null)
70
+    public static function getToken(LuUsersDbo $user, $data = null)
71 71
     {
72 72
         $session_time = 30;//TODO add a setting
73 73
         return JwtHelper::encode(array(
@@ -121,13 +121,24 @@ class LuUsersBusiness extends LuBusiness
121 121
         }
122 122
     }
123 123
 
124
-    public function login(LuUsersLoginDbo $login)
124
+    public static function getByUsernameOrEmail(string $name)
125
+    {
126
+        $user = static::getDataAccess()->getByUsernameOrEmail($name);
127
+        if (is_null($user)) {
128
+            static::notFound(static::getResourceName() . " not found");
129
+        }
130
+        return $user;
131
+    }
132
+
133
+    public static function login(LuUsersLoginDbo $login)
125 134
     {
126 135
         $user = static::getDataAccess()->getByUsernameOrEmail($login->getUsername());
127
-        if (is_null($user))
136
+        if (is_null($user)) {
128 137
             self::badPassword();
129
-        if (!self::verifyPassword($login->getPassword(), $user->getPassword()))
138
+        }
139
+        if (!self::verifyPassword($login->getPassword(), $user->getPassword())) {
130 140
             self::badPassword();
141
+        }
131 142
 
132 143
         /**
133 144
          * @var $result LuUsersLoginResultDbo
@@ -137,7 +148,7 @@ class LuUsersBusiness extends LuBusiness
137 148
         return $result;
138 149
     }
139 150
 
140
-    public function logout(LuUsersDbo $user)
151
+    public static function logout(LuUsersDbo $user)
141 152
     {
142 153
         if ($user->getId() != 0) {
143 154
             $user->setSalt(self::getSalt());
@@ -146,7 +157,7 @@ class LuUsersBusiness extends LuBusiness
146 157
         return true;
147 158
     }
148 159
 
149
-    public function getSingleLiteById($userId)
160
+    public static function getSingleLiteById($userId)
150 161
     {
151 162
         /**
152 163
          * @var $user LuUsersDbo
@@ -159,7 +170,7 @@ class LuUsersBusiness extends LuBusiness
159 170
      * @param LuQueryDbo $query
160 171
      * @return LuPaginatedDbo
161 172
      */
162
-    public function getAllLite(LuQueryDbo $query)
173
+    public static function getAllLite(LuQueryDbo $query)
163 174
     {
164 175
         return static::getDataAccess()->getAll($query)->map(function($user)
165 176
         {
@@ -170,7 +181,7 @@ class LuUsersBusiness extends LuBusiness
170 181
         });
171 182
     }
172 183
 
173
-    public function add(LuUsersAddDbo $user)
184
+    public static function add(LuUsersAddDbo $user)
174 185
     {
175 186
         static::checkPasswordRequirements($user->getPassword());
176 187
 
@@ -205,7 +216,7 @@ class LuUsersBusiness extends LuBusiness
205 216
         return self::getById($id);
206 217
     }
207 218
 
208
-    public function del(int $userId)
219
+    public static function del(int $userId)
209 220
     {
210 221
         $user = static::getSingleLiteById($userId);
211 222
         if ($userId != 0) {
@@ -214,7 +225,7 @@ class LuUsersBusiness extends LuBusiness
214 225
         return $user;
215 226
     }
216 227
 
217
-    public function edit(int $userId, LuUsersEditDbo $user)
228
+    public static function edit(int $userId, LuUsersEditDbo $user)
218 229
     {
219 230
         $existingUser = static::getSingleLiteById($userId);
220 231
         if (!filter_var($user->getEmail(), FILTER_VALIDATE_EMAIL)) {
@@ -234,9 +245,9 @@ class LuUsersBusiness extends LuBusiness
234 245
         return static::getSingleLiteById($existingUser->getId());
235 246
     }
236 247
 
237
-    public function setPassword(int $userId, string $password)
248
+    public static function setPassword(int $userId, string $password)
238 249
     {
239
-        $this->checkPasswordRequirements($password);
250
+        static::checkPasswordRequirements($password);
240 251
 
241 252
         /**
242 253
          * @var $existingUser LuUsersDbo
@@ -250,7 +261,7 @@ class LuUsersBusiness extends LuBusiness
250 261
         return true;
251 262
     }
252 263
 
253
-    public function setPasswordMe(LuUsersDbo $_user, string $password, string $oldPassword)
264
+    public static function setPasswordMe(LuUsersDbo $_user, string $password, string $oldPassword)
254 265
     {
255 266
         $loginDbo = new LuUsersLoginDbo();
256 267
         $loginDbo->setUsername($_user->getUsername());

src/Auth/DataAccess/LuUserDataAccess.php → src/Auth/DataAccess/LuUsersDataAccess.php View File

@@ -14,7 +14,7 @@ use Luticate\Auth\Dbo\Users\LuUsersDbo;
14 14
 use Luticate\Utils\DataAccess\LuDataAccess;
15 15
 use Luticate\Utils\Dbo\LuQueryDbo;
16 16
 
17
-class LuUserDataAccess extends LuDataAccess
17
+class LuUsersDataAccess extends LuDataAccess
18 18
 {
19 19
     protected static $_connection = "luticatedb";
20 20
     protected static $_table = "lu_users";

+ 0
- 5
src/Auth/Middleware/LuAuthMiddleware.php View File

@@ -8,13 +8,8 @@
8 8
 
9 9
 namespace Luticate\Auth\Middleware;
10 10
 
11
-use Luticate\Auth\Business\JwtHelper;
12
-use Luticate\Auth\Business\LuPermissionsBusiness;
13 11
 use Luticate\Auth\Business\LuUsersBusiness;
14
-use Luticate\Auth\Dbo\LuBuiltInPermissions;
15
-use Luticate\Auth\Dbo\LuUsersDbo;
16 12
 use Luticate\Utils\Business\LuBusiness;
17
-use Luticate\Utils\Business\LuLog;
18 13
 use Luticate\Utils\Middleware\LuAbstractMiddleware;
19 14
 
20 15
 class LuAuthMiddleware implements LuAbstractMiddleware

+ 125
- 0
tests/LuUsersTest.php View File

@@ -2,6 +2,8 @@
2 2
 use Luticate\Auth\Business\LuUsersBusiness;
3 3
 use Luticate\Auth\Controller\LuUsersController;
4 4
 use Luticate\Auth\Dbo\Users\LuUsersAddDbo;
5
+use Luticate\Auth\Dbo\Users\LuUsersDbo;
6
+use Luticate\Auth\Dbo\Users\LuUsersEditDbo;
5 7
 use Luticate\Auth\Dbo\Users\LuUsersLoginDbo;
6 8
 use Luticate\Utils\Business\LuBusinessException;
7 9
 use Luticate\Utils\Controller\LuticateApplication;
@@ -75,6 +77,7 @@ class LuUsersTest extends \PHPUnit_Framework_TestCase
75 77
         $newUser->setProfileId(null);
76 78
 
77 79
         $this->expectException(LuBusinessException::class);
80
+        $this->expectExceptionCode(400);
78 81
         $ctrl->add($newUser);
79 82
     }
80 83
 
@@ -92,6 +95,7 @@ class LuUsersTest extends \PHPUnit_Framework_TestCase
92 95
         $newUser->setProfileId(null);
93 96
 
94 97
         $this->expectException(LuBusinessException::class);
98
+        $this->expectExceptionCode(400);
95 99
         $ctrl->add($newUser);
96 100
     }
97 101
 
@@ -153,4 +157,125 @@ class LuUsersTest extends \PHPUnit_Framework_TestCase
153 157
 
154 158
         $this->assertSame("test.user2@example.com", $loggedUser->getEmail());
155 159
     }
160
+
161
+    public function testUserLogin3()
162
+    {
163
+        $ctrl = static::getCtrl();
164
+
165
+        $loginDbo = new LuUsersLoginDbo();
166
+        $loginDbo->setUsername("azertyuiop");
167
+        $loginDbo->setPassword("azertyuiop");
168
+
169
+        $this->expectException(LuBusinessException::class);
170
+        $this->expectExceptionCode(401);
171
+        $ctrl->login($loginDbo);
172
+    }
173
+
174
+    public function testUserGetMe1()
175
+    {
176
+        $ctrl = static::getCtrl();
177
+
178
+        $loginDbo = new LuUsersLoginDbo();
179
+        $loginDbo->setUsername("_test_user1");
180
+        $loginDbo->setPassword("test42");
181
+        $loginResult = $ctrl->login($loginDbo);
182
+
183
+        $this->assertNotNull($loginResult);
184
+        $this->assertSame("test.user1@example.com", $loginResult->getEmail());
185
+
186
+        /**
187
+         * @var $user LuUsersDbo
188
+         */
189
+        $user = LuUsersBusiness::getById($loginResult->getId());
190
+
191
+        $me = $ctrl->getMe($user);
192
+
193
+        $this->assertSame("test.user1@example.com", $me->getEmail());
194
+        $this->assertSame("_test_user1", $user->getUsername());
195
+    }
196
+
197
+    public function testUserGetById1()
198
+    {
199
+        $ctrl = static::getCtrl();
200
+
201
+        $user = LuUsersBusiness::getByUsernameOrEmail("test.user1@example.com");
202
+
203
+        $user = $ctrl->getById($user->getId());
204
+
205
+        $this->assertSame("test.user1@example.com", $user->getEmail());
206
+        $this->assertSame("_test_user1", $user->getUsername());
207
+    }
208
+
209
+    public function testUserGetById2()
210
+    {
211
+        $ctrl = static::getCtrl();
212
+
213
+        $user = LuUsersBusiness::getByUsernameOrEmail("_test_user2");
214
+
215
+        $user = $ctrl->getById($user->getId());
216
+
217
+        $this->assertSame("test.user2@example.com", $user->getEmail());
218
+        $this->assertSame("_test_user2", $user->getUsername());
219
+    }
220
+
221
+    public function testUserEditMe1()
222
+    {
223
+        $ctrl = static::getCtrl();
224
+
225
+        $user = LuUsersBusiness::getByUsernameOrEmail("_test_user2");
226
+
227
+        $edited = new LuUsersEditDbo();
228
+        $edited->setEmail("test.user2+edited@example.com");
229
+        $edited->setFirstname("test-edited");
230
+        $edited->setLastname("user2-edited");
231
+
232
+        $ctrl->editMe($user, $edited);
233
+
234
+        $editedUser = $ctrl->getById($user->getId());
235
+
236
+        $this->assertSame("test.user2+edited@example.com", $editedUser->getEmail());
237
+        $this->assertSame("test-edited", $editedUser->getFirstname());
238
+        $this->assertSame("user2-edited", $editedUser->getLastname());
239
+    }
240
+
241
+    public function testUserEdit1()
242
+    {
243
+        $ctrl = static::getCtrl();
244
+
245
+        $user = LuUsersBusiness::getByUsernameOrEmail("_test_user2");
246
+
247
+        $edited = new LuUsersEditDbo();
248
+        $edited->setEmail("test.user2@example.com");
249
+        $edited->setFirstname("test");
250
+        $edited->setLastname("user2");
251
+
252
+        $ctrl->edit($user->getId(), $edited);
253
+
254
+        $editedUser = $ctrl->getById($user->getId());
255
+
256
+        $this->assertSame("test.user2@example.com", $editedUser->getEmail());
257
+        $this->assertSame("test", $editedUser->getFirstname());
258
+        $this->assertSame("user2", $editedUser->getLastname());
259
+    }
260
+
261
+    public function testUserDelete1()
262
+    {
263
+        //TODO
264
+//        $ctrl = static::getCtrl();
265
+//
266
+//        $loginDbo = new LuUsersLoginDbo();
267
+//        $loginDbo->setUsername("_test_user1");
268
+//        $loginDbo->setPassword("test42");
269
+//        $loginResult = $ctrl->login($loginDbo);
270
+//
271
+//        $this->assertNotNull($loginResult);
272
+//        $this->assertSame("test.user1@example.com", $loginResult->getEmail());
273
+//
274
+//        $user = LuUsersBusiness::getByUsernameOrEmail("test.user2@example.com");
275
+//
276
+//        $user = $ctrl->getById($user->getId());
277
+//
278
+//        $this->assertSame("test.user2@example.com", $user->getEmail());
279
+//        $this->assertSame("_test_user2", $user->getUsername());
280
+    }
156 281
 }

Loading…
Cancel
Save