mirror of
https://github.com/Bubka/2FAuth.git
synced 2025-08-19 01:46:03 +02:00
176 lines
3.9 KiB
PHP
176 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Auth;
|
|
|
|
use App\User;
|
|
use Tests\TestCase;
|
|
use Illuminate\Auth\Authenticatable;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Illuminate\Auth\RequestGuard;
|
|
|
|
class LoginTest extends TestCase
|
|
{
|
|
/** @var \App\User */
|
|
protected $user;
|
|
|
|
|
|
/**
|
|
* @test
|
|
*/
|
|
public function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->user = factory(User::class)->create();
|
|
}
|
|
|
|
|
|
/**
|
|
* test User login via API
|
|
*
|
|
* @test
|
|
*/
|
|
public function testUserLogin()
|
|
{
|
|
|
|
$response = $this->json('POST', '/api/login', [
|
|
'email' => $this->user->email,
|
|
'password' => 'password'
|
|
]);
|
|
|
|
$response->assertStatus(200)
|
|
->assertJsonStructure([
|
|
'message' => ['token']
|
|
]);
|
|
}
|
|
|
|
|
|
/**
|
|
* test User login via API
|
|
*
|
|
* @test
|
|
*/
|
|
public function testUserLoginAlreadyAuthenticated()
|
|
{
|
|
|
|
$response = $this->json('POST', '/api/login', [
|
|
'email' => $this->user->email,
|
|
'password' => 'password'
|
|
]);
|
|
|
|
$response = $this->actingAs($this->user, 'api')
|
|
->json('POST', '/api/login', [
|
|
'email' => $this->user->email,
|
|
'password' => 'password'
|
|
]);
|
|
|
|
$response->assertStatus(400)
|
|
->assertJson([
|
|
'message' => __('auth.already_authenticated')
|
|
]);
|
|
}
|
|
|
|
|
|
/**
|
|
* test User login with missing values via API
|
|
*
|
|
* @test
|
|
*/
|
|
public function testUserLoginWithMissingValues()
|
|
{
|
|
$response = $this->json('POST', '/api/login', [
|
|
'email' => '',
|
|
'password' => ''
|
|
]);
|
|
|
|
$response->assertStatus(422)
|
|
->assertJsonValidationErrors([
|
|
'email',
|
|
'password'
|
|
]);
|
|
}
|
|
|
|
|
|
/**
|
|
* test User login with invalid credentials via API
|
|
*
|
|
* @test
|
|
*/
|
|
public function testUserLoginWithInvalidCredential()
|
|
{
|
|
$response = $this->json('POST', '/api/login', [
|
|
'email' => $this->user->email,
|
|
'password' => 'badPassword'
|
|
]);
|
|
|
|
$response->assertStatus(401)
|
|
->assertJson([
|
|
'message' => 'unauthorised'
|
|
]);
|
|
}
|
|
|
|
|
|
/**
|
|
* test User login with invalid credentials via API
|
|
*
|
|
* @test
|
|
*/
|
|
public function testTooManyAttempsWithInvalidCredential()
|
|
{
|
|
$response = $this->json('POST', '/api/login', [
|
|
'email' => $this->user->email,
|
|
'password' => 'badPassword'
|
|
]);
|
|
|
|
$response = $this->json('POST', '/api/login', [
|
|
'email' => $this->user->email,
|
|
'password' => 'badPassword'
|
|
]);
|
|
|
|
$response = $this->json('POST', '/api/login', [
|
|
'email' => $this->user->email,
|
|
'password' => 'badPassword'
|
|
]);
|
|
|
|
$response = $this->json('POST', '/api/login', [
|
|
'email' => $this->user->email,
|
|
'password' => 'badPassword'
|
|
]);
|
|
|
|
$response = $this->json('POST', '/api/login', [
|
|
'email' => $this->user->email,
|
|
'password' => 'badPassword'
|
|
]);
|
|
|
|
$response = $this->json('POST', '/api/login', [
|
|
'email' => $this->user->email,
|
|
'password' => 'badPassword'
|
|
]);
|
|
|
|
$response->assertStatus(429);
|
|
}
|
|
|
|
|
|
/**
|
|
* test User logout via API
|
|
*
|
|
* @test
|
|
*/
|
|
public function testUserLogout()
|
|
{
|
|
$response = $this->json('POST', '/api/login', [
|
|
'email' => $this->user->email,
|
|
'password' => 'password'
|
|
]);
|
|
|
|
$headers = ['Authorization' => "Bearer " . $response->original['message']['token']];
|
|
|
|
$response = $this->json('POST', '/api/logout', [], $headers)
|
|
->assertStatus(200)
|
|
->assertJson([
|
|
'message' => 'signed out',
|
|
]);
|
|
}
|
|
|
|
} |