Skip to content

Commit bab9112

Browse files
committed
fix: adapt tests to support web and api guards
1 parent 7be20cd commit bab9112

File tree

3 files changed

+39
-24
lines changed

3 files changed

+39
-24
lines changed

database/factories/UserFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class UserFactory extends Factory
2424
public function definition(): array
2525
{
2626
return [
27+
'ulid' => Str::ulid()->toBase32(),
2728
'name' => fake()->name(),
2829
'email' => fake()->unique()->safeEmail(),
2930
'email_verified_at' => now(),
@@ -37,7 +38,7 @@ public function definition(): array
3738
*/
3839
public function unverified(): static
3940
{
40-
return $this->state(fn (array $attributes) => [
41+
return $this->state(fn(array $attributes) => [
4142
'email_verified_at' => null,
4243
]);
4344
}

tests/Feature/Auth/AuthenticationTest.php

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use App\Models\User;
66
use Illuminate\Foundation\Testing\RefreshDatabase;
7-
use Illuminate\Support\Str;
87
use Illuminate\Testing\Fluent\AssertableJson;
98
use Tests\TestCase;
109

@@ -14,17 +13,32 @@ class AuthenticationTest extends TestCase
1413

1514
public function test_users_can_authenticate_using_the_login_screen(): void
1615
{
16+
$guard = config('auth.defaults.guard');
17+
1718
$user = User::factory()->create();
1819

1920
$response = $this->postJson('/api/v1/login', [
2021
'email' => $user->email,
2122
'password' => 'password',
23+
], [
24+
'Origin' => isset(config('sanctum.stateful')[0]) ? config('sanctum.stateful')[0] : 'localhost',
2225
]);
2326

2427
$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+
}
2842
}
2943

3044
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
3650
'password' => 'wrong-password',
3751
]);
3852

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')
4258
);
4359
}
4460

4561
public function test_users_can_logout(): void
4662
{
47-
$user = User::factory()->create([
48-
'ulid' => Str::ulid()->toBase32(),
49-
]);
63+
$guard = config('auth.defaults.guard');
5064

51-
$token = $user->createToken('test-token')->plainTextToken;
65+
/** @var User $user */
66+
$user = User::factory()->create();
5267

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+
}
5677

5778
$response->assertJson(['ok' => true], true);
5879
}

tests/Feature/Auth/EmailVerificationTest.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Illuminate\Foundation\Testing\RefreshDatabase;
88
use Illuminate\Support\Facades\Event;
99
use Illuminate\Support\Facades\URL;
10-
use Str;
1110
use Tests\TestCase;
1211

1312
class EmailVerificationTest extends TestCase
@@ -18,10 +17,7 @@ public function test_email_can_be_verified(): void
1817
{
1918
$this->withoutMiddleware();
2019

21-
$user = User::factory()->create([
22-
'email_verified_at' => null,
23-
'ulid' => Str::ulid()->toBase32(),
24-
]);
20+
$user = User::factory()->unverified()->create();
2521

2622
Event::fake();
2723

@@ -42,10 +38,7 @@ public function test_email_is_not_verified_with_invalid_hash(): void
4238
{
4339
$this->withoutMiddleware();
4440

45-
$user = User::factory()->create([
46-
'email_verified_at' => null,
47-
'ulid' => Str::ulid()->toBase32(),
48-
]);
41+
$user = User::factory()->unverified()->create();
4942

5043
$verificationUrl = URL::temporarySignedRoute(
5144
'verification.verify',

0 commit comments

Comments
 (0)