2022-11-16 17:57:38 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Feature;
|
|
|
|
|
2024-04-19 13:25:08 +00:00
|
|
|
use App\Services\AuthenticationService;
|
2022-11-16 17:57:38 +00:00
|
|
|
use Illuminate\Support\Facades\Hash;
|
2024-10-24 10:45:45 +00:00
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2024-01-09 18:34:40 +00:00
|
|
|
use Tests\TestCase;
|
2022-11-16 17:57:38 +00:00
|
|
|
|
2024-01-11 12:41:33 +00:00
|
|
|
use function Tests\create_user;
|
|
|
|
|
2022-11-16 17:57:38 +00:00
|
|
|
class AuthTest extends TestCase
|
|
|
|
{
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function logIn(): void
|
2022-11-16 17:57:38 +00:00
|
|
|
{
|
2024-01-11 12:41:33 +00:00
|
|
|
create_user([
|
2022-11-16 17:57:38 +00:00
|
|
|
'email' => 'koel@koel.dev',
|
|
|
|
'password' => Hash::make('secret'),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->post('api/me', [
|
|
|
|
'email' => 'koel@koel.dev',
|
|
|
|
'password' => 'secret',
|
|
|
|
])
|
|
|
|
->assertOk()
|
|
|
|
->assertJsonStructure([
|
|
|
|
'token',
|
|
|
|
'audio-token',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->post('api/me', [
|
|
|
|
'email' => 'koel@koel.dev',
|
|
|
|
'password' => 'wrong-secret',
|
|
|
|
])
|
|
|
|
->assertUnauthorized();
|
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function loginViaOneTimeToken(): void
|
2024-04-19 13:25:08 +00:00
|
|
|
{
|
|
|
|
$user = create_user();
|
|
|
|
$authService = app(AuthenticationService::class);
|
|
|
|
$token = $authService->generateOneTimeToken($user);
|
|
|
|
|
|
|
|
$this->post('api/me/otp', ['token' => $token])
|
|
|
|
->assertOk()
|
|
|
|
->assertJsonStructure([
|
|
|
|
'token',
|
|
|
|
'audio-token',
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function logOut(): void
|
2022-11-16 17:57:38 +00:00
|
|
|
{
|
2024-01-11 12:41:33 +00:00
|
|
|
$user = create_user([
|
2022-11-16 17:57:38 +00:00
|
|
|
'email' => 'koel@koel.dev',
|
|
|
|
'password' => Hash::make('secret'),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$response = $this->post('api/me', [
|
|
|
|
'email' => 'koel@koel.dev',
|
|
|
|
'password' => 'secret',
|
|
|
|
]);
|
|
|
|
|
|
|
|
self::assertSame(2, $user->tokens()->count()); // 1 for API, 1 for audio token
|
|
|
|
|
|
|
|
$this->withToken($response->json('token'))
|
|
|
|
->delete('api/me')
|
|
|
|
->assertNoContent();
|
|
|
|
|
|
|
|
self::assertSame(0, $user->tokens()->count());
|
|
|
|
}
|
|
|
|
}
|