@@ -12,42 +12,43 @@ class AuthTest extends TestCase
12
12
public function testLoginLogout ()
13
13
{
14
14
$ user = factory (User::class)->create ();
15
- // $user->attachRole('root');
15
+ $ user ->attachRole ('root ' );
16
16
$ user ->save ();
17
-
18
- $ response = $ this ->post ('/api/auth/login ' ,[
19
- 'email ' => $ user ->email ,
20
- 'password ' => 'secret '
17
+ // Login
18
+ $ response = $ this ->post ('/api/auth/login ' , [
19
+ 'email ' => $ user ->email ,
20
+ 'password ' => 'secret '
21
21
]);
22
- $ response ->assertStatus (200 );
23
22
23
+ $ response ->assertStatus (200 );
24
24
$ token = json_decode ($ response ->getContent (), true )['data ' ]['token ' ];
25
-
25
+ // Get the token. Query self.
26
26
$ this ->refreshApplication ();
27
-
28
- $ selfQueryResponse = $ this ->get ('/api/auth/user ' ,[
29
- 'Authorization ' => 'Bearer ' .$ token ,
27
+ $ selfQueryResponse = $ this ->get ('/api/auth/user ' , [
28
+ 'Authorization ' => 'Bearer ' . $ token ,
30
29
]);
31
30
$ selfQueryResponse ->assertStatus (200 );
32
31
33
- $ tokenRefreshResponse = $ this ->patch ('/api/auth/refresh ' ,[
32
+ // Refresh token
33
+ $ this ->refreshApplication ();
34
+ $ tokenRefreshResponse = $ this ->patch ('/api/auth/refresh ' , [
34
35
//
35
- ],[
36
- 'Authorization ' => 'Bearer ' . $ token ,
36
+ ], [
37
+ 'Authorization ' => 'Bearer ' . $ token ,
37
38
]);
38
39
39
40
$ tokenRefreshResponse ->assertStatus (200 );
40
41
$ this ->refreshApplication ();
41
42
42
- $ logout = $ this ->delete ('/api/auth/invalidate ' ,[
43
- 'Authorization ' => 'Bearer ' .$ token ,
43
+ // Logout
44
+ $ logoutResponse = $ this ->delete ('/api/auth/invalidate ' , [], [
45
+ 'Authorization ' => 'Bearer ' . $ token ,
44
46
]);
45
- $ logout ->assertStatus (200 );
46
- $ this ->refreshApplication ();
47
+ $ logoutResponse ->assertStatus (200 );
47
48
48
- $ loggedoutTestQuery = $ this -> get ( ' /api/auth/users ' , [
49
- ' Authorization ' => ' Bearer ' . $ token ,
50
- ] );
51
- $ loggedoutTestQuery ->assertStatus (401 );
49
+ // Now you cannot query yourself
50
+ $ this -> refreshApplication ();
51
+ $ loggedOutTestQuery = $ this -> get ( ' /api/auth/user ' );
52
+ $ loggedOutTestQuery ->assertStatus (401 );
52
53
}
53
54
}
0 commit comments