/
86
/
exam
Обзор
Документация
Войти
/
86
/
exam
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
tests/Feature/AuthApiTest.php
50 строк
1 KB
Danil Tkach
init
05 июн 2026, 00:14
Не верифицирован
05 июн 2026, 00:14
f1b3a67
Код
Авторство
О чём код?
<?php namespace Tests\Feature; use App\Models\User; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Support\Facades\Hash; use Tests\TestCase; class AuthApiTest extends TestCase { use RefreshDatabase; public function test_user_can_register_and_receive_token(): void { $response = $this->postJson('/api/register', [ 'name' => 'Student', 'email' => 'student@example.com', 'password' => 'password', 'password_confirmation' => 'password', ]); $response->assertCreated() ->assertJsonStructure(['user' => ['id', 'name', 'email'], 'token', 'token_type']); } public function test_user_can_login_and_access_protected_endpoint(): void { User::create([ 'name' => 'Student', 'email' => 'student@example.com', 'password' => Hash::make('password'), ]); $token = $this->postJson('/api/login', [ 'email' => 'student@example.com', 'password' => 'password', ])->assertOk()->json('token'); $this->withHeader('Authorization', 'Bearer '.$token) ->getJson('/api/me') ->assertOk() ->assertJsonPath('user.email', 'student@example.com'); } public function test_protected_endpoint_requires_token(): void { $this->getJson('/api/secret')->assertUnauthorized(); } }