Skip to content

Commit 7a86dbf

Browse files
committed
wip
1 parent 628c972 commit 7a86dbf

10 files changed

Lines changed: 61 additions & 128 deletions

File tree

app/Console/Commands/InviteUserCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function handle()
3838
]);
3939

4040
$user->passwordResetToken()->create([
41-
'hashed_token' => $token = Str::uuid()->toString(),
41+
'hashed_token' => $token = str_uuid(),
4242
'expires_at' => now()->addDays(7)->endOfDay(),
4343
]);
4444

app/Http/Controllers/Guest/RequestPasswordResetController.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use App\Models\User;
88
use Illuminate\Http\Request;
99
use Illuminate\Support\Facades\Mail;
10-
use Illuminate\Support\Str;
1110

1211
class RequestPasswordResetController
1312
{
@@ -41,7 +40,7 @@ public function post(Request $request)
4140

4241
PasswordResetToken::create([
4342
'user_id' => $user->id,
44-
'hashed_token' => $token = Str::uuid()->toString(),
43+
'hashed_token' => $token = str_uuid(),
4544
'expires_at' => now()->addHours(4),
4645
]);
4746

app/Models/User.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ class User extends Authenticatable
1111
use HasFactory, Notifiable;
1212

1313
protected $casts = [
14-
'email_verified_at' => 'datetime',
1514
'password' => 'hashed',
1615
'last_seen_at' => 'datetime',
1716
];

database/factories/ApplicationFactory.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66
use App\Models\ApplicationAlertPreference;
77
use App\Models\Server;
88
use Illuminate\Database\Eloquent\Factories\Factory;
9-
use Illuminate\Support\Str;
109

1110
class ApplicationFactory extends Factory
1211
{
1312
public function definition(): array
1413
{
1514
return [
1615
'server_id' => Server::factory(),
17-
'uuid' => $uuid = Str::uuid()->toString(),
16+
'uuid' => $uuid = str_uuid(),
1817
'slug' => $uuid,
1918
'name' => 'The Application Name',
2019
'env' => 'production',

database/factories/ServerFactory.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55
use App\Models\Server;
66
use App\Models\ServerAlertPreference;
77
use Illuminate\Database\Eloquent\Factories\Factory;
8-
use Illuminate\Support\Str;
98

109
class ServerFactory extends Factory
1110
{
1211
public function definition(): array
1312
{
1413
return [
15-
'uuid' => $uuid = Str::uuid()->toString(),
14+
'uuid' => $uuid = str_uuid(),
1615
'slug' => $uuid,
1716
'name' => 'The Server Name',
1817
'os' => 'Ubuntu',

database/factories/UserFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function definition(): array
1515
{
1616
return [
1717
'first_name' => Arr::random(['Hank', 'Bill', 'Dale', 'Bobby', 'John']),
18-
'email' => Str::uuid()->toString().'@example.com',
18+
'email' => str_uuid().'@example.com',
1919
'password' => '$2y$04$HmysXLhjC2gcCrM0eNEyXeL1G3SDwdEGlFmB3RdZzWfdAWymQeN7m',
2020
'last_seen_at' => now(),
2121
'remember_token' => Str::random(10),

tests/Unit/Controllers/LoginControllerTest.php

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,82 +2,62 @@
22

33
namespace Tests\Unit\Controllers;
44

5-
use Illuminate\Testing\TestResponse;
5+
use App\Models\User;
66
use PHPUnit\Framework\Attributes\Test;
77
use Tests\TestCase;
88

99
class LoginControllerTest extends TestCase
1010
{
11-
public function settingUp(): void
12-
{
13-
parent::settingUp();
14-
15-
$this->markTestSkipped();
16-
}
17-
1811
#[Test]
19-
public function it_can_show_the_login_page(): void
12+
public function it_can_show_the_login_page()
2013
{
2114
$this->showLogin()->assertStatus(200);
2215
}
2316

2417
#[Test]
25-
public function it_redirects_guests(): void
18+
public function it_redirects_guests()
2619
{
27-
$this->userLogin()
20+
$user = User::factory()->create();
21+
22+
$this->actingAs($user)
2823
->showLogin()
29-
->assertRedirect(route('runs.index'));
24+
->assertRedirect(route('dashboard'));
3025
}
3126

3227
#[Test]
33-
public function it_can_login_a_user(): void
28+
public function it_can_login_a_user()
3429
{
35-
$user = factory()->user();
30+
$user = User::factory()->create();
3631

3732
$this->assertGuest();
3833

3934
$this->postLogin(['email' => $user->email, 'password' => 'password'])
4035
->assertSessionDoesntHaveErrors()
41-
->assertRedirect(route('runs.index'));
36+
->assertRedirect(route('dashboard'));
4237

4338
$this->assertAuthenticatedAs($user);
4439
}
4540

4641
#[Test]
47-
public function you_have_to_verify_your_email_before_you_can_login(): void
48-
{
49-
$user = factory()->user();
50-
51-
$user->update(['email_verified_at' => null]);
52-
53-
$this->postLogin(['email' => $user->email, 'password' => 'password'])
54-
->assertSessionHasErrors('email')
55-
->assertStatus(302);
56-
57-
$this->assertGuest();
58-
}
59-
60-
#[Test]
61-
public function it_can_fail_a_login(): void
42+
public function it_can_fail_a_login()
6243
{
63-
$user = factory()->user();
44+
$user = User::factory()->create();
6445

6546
$this->assertGuest();
6647

67-
$this->postLogin(['email' => $user->email, 'password' => 'wrong!!!'])
48+
$this->postLogin(['email' => $user->email, 'password' => 'wrong'])
6849
->assertStatus(302)
6950
->assertSessionHasErrors();
7051

7152
$this->assertGuest();
7253
}
7354

74-
private function showLogin(): TestResponse
55+
private function showLogin()
7556
{
7657
return $this->get(route('login'));
7758
}
7859

79-
// @phpstan-ignore-next-line
80-
private function postLogin(array $data): TestResponse
60+
private function postLogin(array $data)
8161
{
8262
return $this->post(route('login'), $data);
8363
}

tests/Unit/Controllers/ReceiveBundleControllerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ public function it_creates_a_pending_alert_if_supervisor_is_not_all_running()
174174
$postBundle = function () {
175175
Server::receiveBundle($this->fixtureBundle1()->put([
176176
'server/supervisor-status' => <<<'STRING'
177-
all-organizations-database-queue:all-organizations-database-queue_01 FATAL Exited too quickly (process log may have details)
178-
aaaaaaaaaaaaaa-aaaaa-aaaaaaa-aaaaa-horizon:aaaaaaaaaaaaaa-aaaaa-aaaaaaa-aaaaa-horizon_00 RUNNING pid 174708, uptime 2 days, 0:01:25
177+
all-organizations-database-queue:all-organizations-database-queue_01 FATAL Exited too quickly (process log may have details)
178+
aaaaaaaaaaaaaa-aaaaa-aaaaaaa-aaaaa-horizon:aaaaaaaaaaaaaa-aaaaa-aaaaaaa-aaaaa-horizon_00 RUNNING pid 174708, uptime 2 days, 0:01:25
179179
STRING,
180180
])->get());
181181
};

tests/Unit/Controllers/RequestPasswordResetControllerTest.php

Lines changed: 35 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -3,119 +3,108 @@
33
namespace Tests\Unit\Controllers;
44

55
use App\Mail\PasswordResetMail;
6+
use App\Models\PasswordResetToken;
67
use App\Models\User;
7-
use Carbon\CarbonInterface;
8-
use Illuminate\Support\Facades\DB;
98
use Illuminate\Support\Facades\Mail;
10-
use Illuminate\Support\Str;
119
use Illuminate\Testing\TestResponse;
1210
use PHPUnit\Framework\Attributes\Test;
1311
use Tests\TestCase;
1412

1513
class RequestPasswordResetControllerTest extends TestCase
1614
{
17-
public function settingUp(): void
18-
{
19-
parent::settingUp();
20-
21-
$this->markTestSkipped();
22-
}
23-
2415
#[Test]
25-
public function it_can_show_the_request_page(): void
16+
public function it_can_show_the_request_page()
2617
{
27-
$this->showRequestPage()->assertStatus(200);
18+
$this->get(route('login.requestPassword'))->assertStatus(200);
2819
}
2920

3021
#[Test]
31-
public function it_can_show_the_success_page(): void
22+
public function it_can_show_the_success_page()
3223
{
33-
$this->showSuccessPage()->assertStatus(200);
24+
$this->get(route('login.requestPassword.success'))->assertStatus(200);
3425
}
3526

3627
#[Test]
37-
public function it_can_request_a_reset(): void
28+
public function it_can_request_a_reset()
3829
{
3930
Mail::fake();
4031

41-
$user = factory()->user();
32+
$user = User::factory()->create();
4233

4334
$this->postRequestReset($user->email)
4435
->assertSessionHasNoErrors()
4536
->assertRedirect(route('login.requestPassword.success'));
4637

47-
$record = DB::table('password_resets')->where('email', $user->email)->first();
38+
$this->assertSame(1, PasswordResetToken::count());
39+
$token = PasswordResetToken::firstWhere('user_id', $user->id);
4840

49-
throw_if(! isset($record->token), '(message for Larastan)');
50-
51-
$this->assertNotNull($record->token);
41+
$this->assertTrue($token->expires_at->isFuture());
5242

5343
Mail::assertQueued(PasswordResetMail::class, 1);
5444
Mail::assertQueued(fn (PasswordResetMail $email) => $email->user->id === $user->id);
5545
}
5646

5747
#[Test]
58-
public function you_cant_request_a_reset_if_you_have_not_verified_your_email(): void
59-
{
60-
$user = factory()->user();
61-
62-
$user->update(['email_verified_at' => null]);
63-
64-
$this->postRequestReset($user->email)
65-
->assertSessionHasErrors('email')
66-
->assertStatus(302);
67-
}
68-
69-
#[Test]
70-
public function it_throttles_reset_requests(): void
48+
public function it_throttles_reset_requests()
7149
{
7250
Mail::fake();
7351

74-
$user = factory()->user();
52+
$user = User::factory()->create();
7553

76-
$this->createToken($user, now()->subMinutes(5));
54+
$token = PasswordResetToken::create([
55+
'user_id' => $user->id,
56+
'hashed_token' => str_uuid(),
57+
'expires_at' => now()->addHours(4),
58+
'created_at' => now()->subMinutes(15),
59+
]);
7760

7861
$this->postRequestReset($user->email)
7962
->assertSessionHasErrors(['email' => 'You already requested a password reset recently'])
8063
->assertStatus(302);
8164

65+
$this->assertModelExists($token);
66+
8267
Mail::assertNothingOutgoing();
8368
}
8469

8570
#[Test]
86-
public function it_updates_existing_tokens(): void
71+
public function it_updates_existing_tokens()
8772
{
8873
Mail::fake();
8974

90-
$user = factory()->user();
75+
$user = User::factory()->create();
9176

92-
[$email, $token] = $this->createToken($user, now()->subHours(2));
77+
$token = PasswordResetToken::create([
78+
'user_id' => $user->id,
79+
'hashed_token' => str_uuid(),
80+
'expires_at' => now()->subHours(4),
81+
'created_at' => now()->subMinutes(120),
82+
]);
9383

9484
$this->postRequestReset($user->email)
9585
->assertSessionHasNoErrors()
9686
->assertRedirect(route('login.requestPassword.success'));
9787

98-
$record = DB::table('password_resets')->where('email', $user->email)->first();
88+
$this->assertModelMissing($token);
9989

100-
throw_if(! isset($record->token), '(message for Larastan)');
101-
102-
$this->assertNotSame($record->token, $token);
90+
$this->assertNotNull($user->passwordResetToken);
91+
$this->assertTrue($user->passwordResetToken->expires_at->isFuture());
10392
}
10493

10594
#[Test]
106-
public function it_validates_the_email(): void
95+
public function it_validates_the_email()
10796
{
108-
$user = factory()->user();
97+
$user = User::factory()->create();
10998

11099
$this->postRequestReset('wrong@example.com')
111100
->assertSessionHasErrors('email')
112101
->assertStatus(302);
113102
}
114103

115104
#[Test]
116-
public function it_can_render_the_password_reset_email(): void
105+
public function it_can_render_the_password_reset_email()
117106
{
118-
$user = factory()->user();
107+
$user = User::factory()->create();
119108

120109
$email = new PasswordResetMail($user, 'token');
121110

@@ -126,34 +115,8 @@ public function it_can_render_the_password_reset_email(): void
126115
$email->render();
127116
}
128117

129-
/** @return array{string, string} */
130-
private function createToken(string|User $email, string|CarbonInterface|null $createdAt = null): array
131-
{
132-
if ($email instanceof User) {
133-
$email = $email->email;
134-
}
135-
136-
DB::table('password_resets')->insert([
137-
'email' => $email,
138-
'token' => $token = sha1(Str::random()),
139-
'created_at' => $createdAt ?? now(),
140-
]);
141-
142-
return [$email, $token];
143-
}
144-
145-
private function showRequestPage(): TestResponse
146-
{
147-
return $this->get(route('login.requestPassword'));
148-
}
149-
150118
private function postRequestReset(string $email): TestResponse
151119
{
152120
return $this->post(route('login.requestPassword'), ['email' => $email]);
153121
}
154-
155-
private function showSuccessPage(): TestResponse
156-
{
157-
return $this->get(route('login.requestPassword.success'));
158-
}
159122
}

0 commit comments

Comments
 (0)