2FAuth/tests/Feature/Http/Auth/ForgotPasswordControllerTest.php

115 lines
3.3 KiB
PHP
Raw Normal View History

2021-11-22 01:09:54 +01:00
<?php
2022-03-31 12:09:25 +02:00
namespace Tests\Feature\Http\Auth;
2021-11-22 01:09:54 +01:00
2023-08-01 11:28:27 +02:00
use App\Http\Controllers\Auth\ForgotPasswordController;
use App\Http\Middleware\RejectIfAuthenticated;
use App\Http\Middleware\RejectIfDemoMode;
2021-12-02 13:15:53 +01:00
use App\Models\User;
2021-11-22 01:09:54 +01:00
use Illuminate\Auth\Notifications\ResetPassword;
2022-11-22 15:15:52 +01:00
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Hash;
2021-11-22 01:09:54 +01:00
use Illuminate\Support\Facades\Notification;
2023-08-01 11:28:27 +02:00
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
2021-11-22 01:09:54 +01:00
use Tests\FeatureTestCase;
2022-12-09 10:52:17 +01:00
/**
2023-08-01 11:28:27 +02:00
* ForgotPasswordControllerTest test class
2022-12-09 10:52:17 +01:00
*/
2023-08-01 11:28:27 +02:00
#[CoversClass(ForgotPasswordController::class)]
#[CoversClass(User::class)]
#[CoversClass(RejectIfDemoMode::class)]
#[CoversClass(RejectIfAuthenticated::class)]
2021-11-22 01:09:54 +01:00
class ForgotPasswordControllerTest extends FeatureTestCase
{
/**
2021-12-02 13:15:53 +01:00
* @var \App\Models\User
2021-11-22 01:09:54 +01:00
*/
protected $user;
#[Test]
2021-11-22 01:09:54 +01:00
public function test_submit_email_password_request_without_email_returns_validation_error()
{
2022-03-31 08:38:35 +02:00
$response = $this->json('POST', '/user/password/lost', [
2022-11-22 15:15:52 +01:00
'email' => '',
2021-11-22 01:09:54 +01:00
]);
$response->assertStatus(422)
2022-12-09 10:52:17 +01:00
->assertJsonValidationErrors(['email']);
2021-11-22 01:09:54 +01:00
}
#[Test]
2021-11-22 01:09:54 +01:00
public function test_submit_email_password_request_with_invalid_email_returns_validation_error()
{
2022-03-31 08:38:35 +02:00
$response = $this->json('POST', '/user/password/lost', [
2022-11-22 15:15:52 +01:00
'email' => 'nametest.com',
2021-11-22 01:09:54 +01:00
]);
$response->assertStatus(422)
2022-12-09 10:52:17 +01:00
->assertJsonValidationErrors(['email']);
2021-11-22 01:09:54 +01:00
}
#[Test]
2021-11-22 01:09:54 +01:00
public function test_submit_email_password_request_with_unknown_email_returns_validation_error()
{
2022-03-31 08:38:35 +02:00
$response = $this->json('POST', '/user/password/lost', [
2022-11-22 15:15:52 +01:00
'email' => 'name@test.com',
2021-11-22 01:09:54 +01:00
]);
$response->assertStatus(422)
2022-12-09 10:52:17 +01:00
->assertJsonValidationErrors(['email']);
2021-11-22 01:09:54 +01:00
}
#[Test]
2021-11-22 01:09:54 +01:00
public function test_submit_email_password_request_returns_success()
{
Notification::fake();
2021-12-02 13:15:53 +01:00
$this->user = User::factory()->create();
2021-11-22 01:09:54 +01:00
2022-03-31 08:38:35 +02:00
$response = $this->json('POST', '/user/password/lost', [
2022-11-22 15:15:52 +01:00
'email' => $this->user->email,
2021-11-22 01:09:54 +01:00
]);
$response->assertStatus(200);
$token = \Illuminate\Support\Facades\DB::table(config('auth.passwords.users.table'))->first();
2021-11-22 01:09:54 +01:00
$this->assertNotNull($token);
Notification::assertSentTo($this->user, ResetPassword::class, function ($notification, $channels) use ($token) {
return Hash::check($notification->token, $token->token) === true;
});
}
#[Test]
2021-11-30 17:39:33 +01:00
public function test_submit_email_password_request_in_demo_mode_returns_unauthorized()
2021-11-22 01:09:54 +01:00
{
Config::set('2fauth.config.isDemoApp', true);
2022-03-31 08:38:35 +02:00
$response = $this->json('POST', '/user/password/lost', [
2022-11-22 15:15:52 +01:00
'email' => '',
2021-11-22 01:09:54 +01:00
]);
$response->assertStatus(401);
}
2022-12-09 10:52:17 +01:00
#[Test]
2022-12-09 10:52:17 +01:00
public function test_submit_email_password_request_when_authenticated_returns_bad_request()
{
2023-03-10 16:03:42 +01:00
/**
* @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
*/
2022-12-09 10:52:17 +01:00
$user = User::factory()->create();
$this->actingAs($user, 'web-guard')
->json('POST', '/user/password/lost', [
'email' => $user->email,
])
->assertStatus(400)
->assertJsonStructure([
'message',
]);
}
2022-11-22 15:15:52 +01:00
}