4
4
5
5
use App \Models \User ;
6
6
use Illuminate \Foundation \Testing \RefreshDatabase ;
7
- use Illuminate \Support \Str ;
8
7
use Illuminate \Testing \Fluent \AssertableJson ;
9
8
use Tests \TestCase ;
10
9
@@ -14,17 +13,32 @@ class AuthenticationTest extends TestCase
14
13
15
14
public function test_users_can_authenticate_using_the_login_screen (): void
16
15
{
16
+ $ guard = config ('auth.defaults.guard ' );
17
+
17
18
$ user = User::factory ()->create ();
18
19
19
20
$ response = $ this ->postJson ('/api/v1/login ' , [
20
21
'email ' => $ user ->email ,
21
22
'password ' => 'password ' ,
23
+ ], [
24
+ 'Origin ' => isset (config ('sanctum.stateful ' )[0 ]) ? config ('sanctum.stateful ' )[0 ] : 'localhost ' ,
22
25
]);
23
26
24
27
$ response ->assertStatus (200 );
25
- $ response ->assertJson (fn (AssertableJson $ json ) => $ json
26
- ->hasAll (['ok ' , 'token ' ])
27
- );
28
+
29
+ if ($ guard === 'api ' ) {
30
+ $ response ->assertJson (
31
+ fn (AssertableJson $ json ) => $ json
32
+ ->hasAll (['ok ' , 'token ' ])
33
+ );
34
+ } else {
35
+ $ response ->assertJson (
36
+ fn (AssertableJson $ json ) => $ json
37
+ ->has ('ok ' )
38
+ ->where ('ok ' , true )
39
+ ->missing ('token ' )
40
+ );
41
+ }
28
42
}
29
43
30
44
public function test_users_can_not_authenticate_with_invalid_password (): void
@@ -36,23 +50,30 @@ public function test_users_can_not_authenticate_with_invalid_password(): void
36
50
'password ' => 'wrong-password ' ,
37
51
]);
38
52
39
- $ response ->assertJson (fn (AssertableJson $ json ) => $ json
40
- ->hasAll (['ok ' , 'message ' , 'errors ' ])
41
- ->missing ('token ' )
53
+ $ response ->assertJson (
54
+ fn (AssertableJson $ json ) => $ json
55
+ ->hasAll (['ok ' , 'message ' , 'errors ' ])
56
+ ->where ('ok ' , false )
57
+ ->missing ('token ' )
42
58
);
43
59
}
44
60
45
61
public function test_users_can_logout (): void
46
62
{
47
- $ user = User::factory ()->create ([
48
- 'ulid ' => Str::ulid ()->toBase32 (),
49
- ]);
63
+ $ guard = config ('auth.defaults.guard ' );
50
64
51
- $ token = $ user ->createToken ('test-token ' )->plainTextToken ;
65
+ /** @var User $user */
66
+ $ user = User::factory ()->create ();
52
67
53
- $ response = $ this ->post ('/api/v1/logout ' , [], [
54
- 'Authorization ' => 'Bearer ' .$ token ,
55
- ]);
68
+ if ($ guard === 'api ' ) {
69
+ $ token = $ user ->createDeviceToken ('test-device ' , '127.0.0.1 ' );
70
+ $ response = $ this ->post ('/api/v1/logout ' , [], [
71
+ 'Authorization ' => 'Bearer ' . $ token ,
72
+ ]);
73
+ } else {
74
+ $ this ->actingAs ($ user , $ guard );
75
+ $ response = $ this ->post ('/api/v1/logout ' );
76
+ }
56
77
57
78
$ response ->assertJson (['ok ' => true ], true );
58
79
}
0 commit comments