Skip to content

Commit 2b148ab

Browse files
authored
Testing improvements (make more robust) (#71)
* Update AccountControllerTest.php * Update LoginControllerTest.php * Update EmailVerificationControllerTest.php * Update LogoutControllerTest.php * Update OrganisationControllerTest.php * Update RegisterControllerTest.php * Update ResetPasswordControllerTest.php
1 parent 0de4de6 commit 2b148ab

10 files changed

+124
-72
lines changed

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ BCRYPT_ROUNDS=12
99

1010
SEED_SUPER_ADMIN_EMAIL=super@laravel-inertia-template.test
1111
SEED_ADMIN_EMAIL=admin@laravel-inertia-template.test
12+
SEED_USER_EMAIL=user@laravel-inertia-template.test
1213

1314
LOG_CHANNEL=stack
1415
LOG_STACK=single

.env.testing

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ BCRYPT_ROUNDS=4
99

1010
SEED_SUPER_ADMIN_EMAIL=super@laravel-inertia-template.test
1111
SEED_ADMIN_EMAIL=admin@laravel-inertia-template.test
12+
SEED_USER_EMAIL=user@laravel-inertia-template.test
1213

1314
LOG_CHANNEL=stack
1415
LOG_STACK=single

database/factories/UserFactory.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,15 @@ public function admin(?string $email = null)
5252
$user->assignRole(Role::ADMIN->value);
5353
});
5454
}
55+
56+
public function user(?string $email = null)
57+
{
58+
return $this
59+
->state(fn (array $attributes) => [
60+
'email' => $email ?: \env('SEED_USER_EMAIL'),
61+
])
62+
->afterCreating(function (User $user) {
63+
$user->assignRole(Role::USER->value);
64+
});
65+
}
5566
}

tests/Feature/Controllers/AccountControllerTest.php

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
use Illuminate\Support\Facades\Hash;
55
use Inertia\Testing\AssertableInertia as Assert;
66

7+
use function Pest\Faker\fake;
78
use function Pest\Laravel\actingAs;
8-
use function Pest\Laravel\from;
99
use function Pest\Laravel\get;
1010
use function Pest\Laravel\patch;
1111

@@ -28,23 +28,23 @@
2828

2929
test('Can update their details', function () {
3030
$user = User::factory()->create([
31-
'first_name' => 'Jim',
32-
'last_name' => 'Gordon',
33-
'email' => 'jim@test.com',
34-
'password' => 'oldPassword#123',
31+
'first_name' => fake()->firstName(),
32+
'last_name' => fake()->lastName(),
33+
'email' => fake()->safeEmail(),
34+
'password' => fake()->password(),
3535
]);
3636

37-
from(route('account.edit'))
38-
->actingAs($user)
37+
actingAs($user)
38+
->fromRoute('account.edit')
3939
->patch(route('account.update'), $newData = [
40-
'first_name' => 'Tim',
41-
'last_name' => 'Drake',
42-
'email' => 'tim@test.com',
43-
'password' => 'newPassword#123',
40+
'first_name' => fake()->firstName(),
41+
'last_name' => fake()->lastName(),
42+
'email' => fake()->safeEmail(),
43+
'password' => 'newPassword123#',
4444
])
45-
->assertRedirectToRoute('account.edit')
4645
->assertSessionDoesntHaveErrors()
47-
->assertSessionHas('success', __('account.updated'));
46+
->assertSessionHas('success', __('account.updated'))
47+
->assertRedirectToRoute('account.edit');
4848

4949
expect($user->refresh())
5050
->first_name->toBe($newData['first_name'])
@@ -55,23 +55,27 @@
5555
});
5656

5757
test("Can't update their email to one that already exists", function () {
58-
User::factory()->create([
59-
'email' => 'jim@test.com',
58+
$user1 = User::factory()->create([
59+
'email' => fake()->email(),
6060
]);
6161

62-
$user = User::factory()->create([
63-
'email' => 'jeff@test.com',
62+
$oldEmail = fake()->email();
63+
64+
$user2 = User::factory()->create([
65+
'email' => $oldEmail,
6466
]);
6567

66-
actingAs($user)
67-
->from(route('account.edit'))
68-
->patch(route('account.update'), $newData = [
69-
'email' => 'jim@test.com',
68+
actingAs($user2)
69+
->fromRoute('account.edit')
70+
->patch(route('account.update'), [
71+
'email' => $user1->email,
72+
])
73+
->assertSessionHasErrors([
74+
'email' => __('validation.unique', ['attribute' => 'email']),
7075
])
71-
->assertSessionHasErrors('email')
7276
->assertRedirectToRoute('account.edit');
7377

74-
expect($user->refresh()->email)->not()->toBe($newData['email']);
78+
expect($user2->refresh()->email)->toBe($oldEmail);
7579
});
7680
});
7781

@@ -83,6 +87,7 @@
8387

8488
test("Can't update details", function () {
8589
patch(route('account.update'))
90+
->assertSessionDoesntHaveErrors()
8691
->assertRedirectToRoute('login');
8792
});
8893
});

tests/Feature/Controllers/EmailVerificationControllerTest.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
use Inertia\Testing\AssertableInertia as Assert;
66

77
use function Pest\Laravel\actingAs;
8-
use function Pest\Laravel\from;
98
use function Pest\Laravel\get;
10-
use function Pest\Laravel\withoutExceptionHandling;
119

1210
describe('Users', function () {
1311
test('Can access the verification page', function () {
@@ -21,8 +19,6 @@
2119
});
2220

2321
test('Can verify their email address', function () {
24-
withoutExceptionHandling();
25-
2622
$user = User::factory()->unverified()->create();
2723

2824
expect($user->verified_at)->toBeNull();
@@ -44,8 +40,8 @@
4440

4541
$user = User::factory()->unverified()->create();
4642

47-
from(route('verification.notice'))
48-
->actingAs($user)
43+
actingAs($user)
44+
->fromRoute('verification.notice')
4945
->post(route('verification.send'))
5046
->assertSessionDoesntHaveErrors()
5147
->assertRedirectToRoute('verification.notice');

tests/Feature/Controllers/LoginControllerTest.php

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use App\Models\User;
44
use Inertia\Testing\AssertableInertia as Assert;
55

6+
use function Pest\Faker\fake;
67
use function Pest\Laravel\actingAs;
78
use function Pest\Laravel\assertAuthenticated;
89
use function Pest\Laravel\assertGuest;
@@ -29,13 +30,16 @@
2930
});
3031

3132
test('Can login', function () {
33+
$password = fake()->password();
3234
$user = User::factory()->create([
33-
'password' => '12345',
35+
'password' => $password,
3436
]);
3537

38+
assertGuest();
39+
3640
post(route('login'), [
3741
'email' => $user->email,
38-
'password' => '12345',
42+
'password' => $password,
3943
])
4044
->assertSessionDoesntHaveErrors()
4145
->assertRedirectToRoute('home');
@@ -44,34 +48,43 @@
4448
});
4549

4650
test('Can be redirected after login', function () {
51+
$password = fake()->password();
4752
$user = User::factory()->create([
48-
'password' => '12345',
53+
'password' => $password,
4954
]);
5055

51-
$redirect = 'https://google.com';
56+
$redirectUrl = 'https://www.google.com/';
57+
58+
assertGuest();
5259

5360
from(route('login'))
5461
->post(route('login'), [
5562
'email' => $user->email,
56-
'password' => '12345',
57-
'redirect' => $redirect,
63+
'password' => $password,
64+
'redirect' => $redirectUrl,
5865
])
5966
->assertSessionDoesntHaveErrors()
60-
->assertRedirect($redirect);
67+
->assertRedirect($redirectUrl);
6168

6269
assertAuthenticated();
6370
});
6471

6572
test("Can't login with invalid credentials", function () {
6673
$user = User::factory()->create([
67-
'password' => '12345',
74+
'password' => fake()->password(),
6875
]);
6976

70-
post(route('login'), [
71-
'email' => $user->email,
72-
'password' => 'test',
73-
])
74-
->assertSessionHasErrors();
77+
assertGuest();
78+
79+
from(route('login'))
80+
->post(route('login'), [
81+
'email' => $user->email,
82+
'password' => 'test',
83+
])
84+
->assertSessionHasErrors([
85+
'email' => __('auth.failed'),
86+
])
87+
->assertRedirectToRoute('login');
7588

7689
assertGuest();
7790
});

tests/Feature/Controllers/LogoutControllerTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33
use App\Models\User;
44

55
use function Pest\Laravel\actingAs;
6+
use function Pest\Laravel\assertAuthenticated;
67
use function Pest\Laravel\assertGuest;
78
use function Pest\Laravel\post;
89

910
describe('Users', function () {
1011
test('Can logout', function () {
11-
actingAs(User::factory()->create())
12-
->post(route('logout'))
12+
actingAs(User::factory()->create());
13+
14+
assertAuthenticated();
15+
16+
post(route('logout'))
1317
->assertSessionDoesntHaveErrors()
1418
->assertRedirectToRoute('login');
1519

tests/Feature/Controllers/OrganisationControllerTest.php

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@
33
use App\Models\User;
44
use Inertia\Testing\AssertableInertia as Assert;
55

6+
use function Pest\Faker\fake;
67
use function Pest\Laravel\actingAs;
7-
use function Pest\Laravel\from;
88
use function Pest\Laravel\get;
99
use function Pest\Laravel\patch;
1010

1111
beforeEach(function () {
1212
$this->adminUser = User::factory()
13-
->admin('admin@test.com')
13+
->admin(fake()->email())
1414
->create();
1515

16+
$this->orgName = fake()->company();
17+
1618
$this->adminUser->organisations()->create([
17-
'name' => 'GCPD',
19+
'name' => $this->orgName,
1820
]);
1921

2022
$this->adminUser->organisation()->associate($this->adminUser->organisations->first())->save();
@@ -32,23 +34,27 @@
3234
});
3335

3436
test("Can update their organisation's name", function () {
35-
expect($this->adminUser->organisation->name)->toBe('GCPD');
37+
expect($this->adminUser->organisation->name)->toBe($this->orgName);
3638

37-
from(route('organisation.edit'))
38-
->actingAs($this->adminUser)
39-
->patch(route('organisation.update'), [
40-
'name' => 'New Name',
41-
])
39+
$data = [
40+
'name' => fake()->company(),
41+
];
42+
43+
actingAs($this->adminUser)
44+
->fromRoute('organisation.edit')
45+
->patch(route('organisation.update'), $data)
4246
->assertSessionDoesntHaveErrors()
4347
->assertRedirectToRoute('organisation.edit');
4448

45-
expect($this->adminUser->organisation->refresh()->name)->toBe('New Name');
49+
expect($this->adminUser->organisation->refresh()->name)->toBe($data['name']);
4650
});
4751
});
4852

4953
describe('Non-Admins', function () {
5054
test("Can't see the edit page for their organisation", function () {
51-
$user = User::factory()->create();
55+
$user = User::factory()
56+
->user(fake()->email())
57+
->create();
5258

5359
$user->organisations()->save($this->adminUser->organisation);
5460
$user->organisation()->associate($user->organisations->first())->save();
@@ -59,20 +65,22 @@
5965
});
6066

6167
test("Can't update their organisation's name", function () {
62-
$user = User::factory()->create();
68+
$user = User::factory()
69+
->user(fake()->email())
70+
->create();
6371

6472
$user->organisations()->save($this->adminUser->organisation);
6573
$user->organisation()->associate($user->organisations->first())->save();
6674

67-
expect($user->organisation->name)->toBe('GCPD');
75+
expect($user->organisation->name)->toBe($this->orgName);
6876

6977
actingAs($user)
7078
->patch(route('organisation.update'), [
7179
'name' => 'New Name',
7280
])
7381
->assertForbidden();
7482

75-
expect($user->organisation->refresh()->name)->toBe('GCPD');
83+
expect($user->organisation->refresh()->name)->toBe($this->orgName);
7684
});
7785
});
7886

tests/Feature/Controllers/RegisterControllerTest.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use function Pest\Laravel\actingAs;
88
use function Pest\Laravel\assertAuthenticated;
99
use function Pest\Laravel\assertDatabaseHas;
10+
use function Pest\Laravel\assertGuest;
11+
use function Pest\Laravel\from;
1012
use function Pest\Laravel\get;
1113
use function Pest\Laravel\post;
1214

@@ -32,12 +34,14 @@
3234
$orgName = fake()->company();
3335
$email = fake()->email();
3436

37+
assertGuest();
38+
3539
post(route('register.store'), [
3640
'organisation_name' => $orgName,
3741
'first_name' => fake()->firstName(),
3842
'last_name' => fake()->lastName(),
3943
'email' => $email,
40-
'password' => 'P$ssword12345#',
44+
'password' => 'Pa$$word12345#',
4145
])
4246
->assertSessionDoesntHaveErrors()
4347
->assertRedirectToRoute('home');
@@ -62,13 +66,17 @@
6266
'email' => $email,
6367
]);
6468

65-
post(route('register.store'), [
66-
'organisation_name' => fake()->company(),
67-
'first_name' => fake()->firstName(),
68-
'last_name' => fake()->lastName(),
69-
'email' => $email,
70-
'password' => 'P$ssword12345#',
71-
])
72-
->assertSessionHasErrors('email');
69+
from(route('register'))
70+
->post(route('register.store'), [
71+
'organisation_name' => fake()->company(),
72+
'first_name' => fake()->firstName(),
73+
'last_name' => fake()->lastName(),
74+
'email' => $email,
75+
'password' => 'P$ssword12345#',
76+
])
77+
->assertSessionHasErrors([
78+
'email' => __('validation.unique', ['attribute' => 'email']),
79+
])
80+
->assertRedirectToRoute('register');
7381
});
7482
});

0 commit comments

Comments
 (0)