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

93 lines
2.6 KiB
PHP
Raw Normal View History

2020-03-03 17:06:40 +01:00
<?php
2022-03-31 12:09:25 +02:00
namespace Tests\Feature\Http\Auth;
2020-03-03 17:06:40 +01:00
2021-12-02 13:15:53 +01:00
use App\Models\User;
2020-03-03 17:06:40 +01:00
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Notification;
2022-11-22 15:15:52 +01:00
use Illuminate\Support\Facades\Password;
2021-11-22 01:09:54 +01:00
use Tests\FeatureTestCase;
2020-03-03 17:06:40 +01:00
2022-12-09 10:52:17 +01:00
/**
* @covers \App\Http\Controllers\Auth\ResetPasswordController
* @covers \App\Models\User
*/
2021-11-22 01:09:54 +01:00
class ResetPasswordControllerTest extends FeatureTestCase
2020-03-03 17:06:40 +01:00
{
2021-11-22 01:09:54 +01:00
/**
2021-12-02 13:15:53 +01:00
* @var \App\Models\User
2021-11-22 01:09:54 +01:00
*/
2020-03-03 17:06:40 +01:00
protected $user;
/**
2021-11-22 01:09:54 +01:00
* @test
2020-03-03 17:06:40 +01:00
*/
2021-11-22 01:09:54 +01:00
public function test_submit_reset_password_without_input_returns_validation_error()
2020-03-03 17:06:40 +01:00
{
2022-03-31 08:38:35 +02:00
$response = $this->json('POST', '/user/password/reset', [
2022-11-22 15:15:52 +01:00
'email' => '',
'password' => '',
2020-03-03 17:06:40 +01:00
'password_confirmation' => '',
2022-11-22 15:15:52 +01:00
'token' => '',
2020-03-03 17:06:40 +01:00
]);
$response->assertStatus(422)
2022-12-09 10:52:17 +01:00
->assertJsonValidationErrors(['email', 'password', 'token']);
2020-03-03 17:06:40 +01:00
}
/**
2021-11-22 01:09:54 +01:00
* @test
2020-03-03 17:06:40 +01:00
*/
2021-11-22 01:09:54 +01:00
public function test_submit_reset_password_with_invalid_data_returns_validation_error()
2020-03-03 17:06:40 +01:00
{
2022-03-31 08:38:35 +02:00
$response = $this->json('POST', '/user/password/reset', [
2022-11-22 15:15:52 +01:00
'email' => 'qsdqsdqsd',
'password' => 'foofoofoo',
2020-03-03 17:06:40 +01:00
'password_confirmation' => 'barbarbar',
2022-11-22 15:15:52 +01:00
'token' => 'token',
2020-03-03 17:06:40 +01:00
]);
$response->assertStatus(422)
2022-12-09 10:52:17 +01:00
->assertJsonValidationErrors(['email', 'password']);
2020-03-03 17:06:40 +01:00
}
/**
2021-11-22 01:09:54 +01:00
* @test
2020-03-03 17:06:40 +01:00
*/
2021-11-22 01:09:54 +01:00
public function test_submit_reset_password_with_too_short_pwd_returns_validation_error()
2020-03-03 17:06:40 +01:00
{
2022-03-31 08:38:35 +02:00
$response = $this->json('POST', '/user/password/reset', [
2022-11-22 15:15:52 +01:00
'email' => 'foo@bar.com',
'password' => 'foo',
2020-03-03 17:06:40 +01:00
'password_confirmation' => 'foo',
2022-11-22 15:15:52 +01:00
'token' => 'token',
2020-03-03 17:06:40 +01:00
]);
$response->assertStatus(422)
2022-12-09 10:52:17 +01:00
->assertJsonValidationErrors(['password']);
2020-03-03 17:06:40 +01:00
}
/**
2021-11-22 01:09:54 +01:00
* @test
2020-03-03 17:06:40 +01:00
*/
2021-11-22 01:09:54 +01:00
public function test_submit_reset_password_returns_success()
2020-03-03 17:06:40 +01:00
{
Notification::fake();
2021-12-02 13:15:53 +01:00
$this->user = User::factory()->create();
2022-11-22 15:15:52 +01:00
$token = Password::broker()->createToken($this->user);
2020-03-03 17:06:40 +01:00
2022-03-31 08:38:35 +02:00
$response = $this->json('POST', '/user/password/reset', [
2022-11-22 15:15:52 +01:00
'email' => $this->user->email,
'password' => 'newpassword',
2020-03-03 17:06:40 +01:00
'password_confirmation' => 'newpassword',
2022-11-22 15:15:52 +01:00
'token' => $token,
2020-03-03 17:06:40 +01:00
]);
$this->user->refresh();
2021-11-22 01:09:54 +01:00
$response->assertOk();
2020-03-03 17:06:40 +01:00
$this->assertTrue(Hash::check('newpassword', $this->user->password));
}
}