2FAuth/tests/Api/v1/Controllers/Auth/ForgotPasswordControllerTest.php

95 lines
2.4 KiB
PHP
Raw Normal View History

2021-11-22 01:09:54 +01:00
<?php
2021-12-02 13:15:53 +01:00
namespace Tests\Api\v1\Controllers\Auth;
2021-11-22 01:09:54 +01:00
2021-12-02 13:15:53 +01:00
use App\Models\User;
2021-11-22 01:09:54 +01:00
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Config;
use Illuminate\Auth\Notifications\ResetPassword;
use Illuminate\Support\Facades\Notification;
use Tests\FeatureTestCase;
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
*/
public function test_submit_email_password_request_without_email_returns_validation_error()
{
$response = $this->json('POST', '/api/v1/user/password/lost', [
'email' => ''
]);
$response->assertStatus(422)
->assertJsonValidationErrors(['email']);
}
/**
* @test
*/
public function test_submit_email_password_request_with_invalid_email_returns_validation_error()
{
$response = $this->json('POST', '/api/v1/user/password/lost', [
'email' => 'nametest.com'
]);
$response->assertStatus(422)
->assertJsonValidationErrors(['email']);
}
/**
* @test
*/
public function test_submit_email_password_request_with_unknown_email_returns_validation_error()
{
$response = $this->json('POST', '/api/v1/user/password/lost', [
'email' => 'name@test.com'
]);
$response->assertStatus(422)
->assertJsonValidationErrors(['email']);
}
/**
* @test
*/
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
$response = $this->json('POST', '/api/v1/user/password/lost', [
'email' => $this->user->email
]);
$response->assertStatus(200);
$token = \Illuminate\Support\Facades\DB::table('password_resets')->first();
$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);
$response = $this->json('POST', '/api/v1/user/password/lost', [
'email' => ''
]);
$response->assertStatus(401);
}
}