тесты аутентификации
This commit is contained in:
parent
c45c53d4fb
commit
ae6f767c86
4
Makefile
4
Makefile
@ -1,6 +1,7 @@
|
||||
up: docker-up
|
||||
down: docker-down
|
||||
reload: docker-down docker-up
|
||||
restart: docker-restart
|
||||
init: docker-down-clear docker-pull docker-build docker-up app-init app-db-seed
|
||||
|
||||
docker-up:
|
||||
@ -12,6 +13,9 @@ docker-down:
|
||||
docker-down-clear:
|
||||
docker compose down -v --remove-orphans
|
||||
|
||||
docker-restart:
|
||||
docker compose up -d --force-recreate
|
||||
|
||||
docker-pull:
|
||||
docker compose pull
|
||||
|
||||
|
||||
@ -17,7 +17,9 @@ class RoleFactory extends Factory
|
||||
public function definition(): array
|
||||
{
|
||||
return [
|
||||
//
|
||||
'name' => fake()->word(),
|
||||
'code' => fake()->unique()->word(),
|
||||
'description' => fake()->sentence(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Models\Role;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Str;
|
||||
@ -29,6 +30,7 @@ class UserFactory extends Factory
|
||||
'email_verified_at' => now(),
|
||||
'password' => static::$password ??= Hash::make('password'),
|
||||
'remember_token' => Str::random(10),
|
||||
'role_uuid' => Role::factory()->create()->uuid,
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
58
src/tests/Feature/AuthTest.php
Normal file
58
src/tests/Feature/AuthTest.php
Normal file
@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
use function Pest\Laravel\get;
|
||||
use function Pest\Laravel\post;
|
||||
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
it('can display the login page', function () {
|
||||
$response = get('/login');
|
||||
|
||||
$response->assertStatus(200);
|
||||
$response->assertSee('Email:');
|
||||
$response->assertSee('Password:');
|
||||
$response->assertSee('Войти');
|
||||
});
|
||||
|
||||
it('can authenticate a user', function () {
|
||||
$user = User::factory()->create([
|
||||
'password' => bcrypt('password123'),
|
||||
]);
|
||||
|
||||
$response = post('/login', [
|
||||
'email' => $user->email,
|
||||
'password' => 'password123',
|
||||
]);
|
||||
|
||||
$response->assertRedirect('/dashboard');
|
||||
expect(auth()->check())->toBeTrue();
|
||||
});
|
||||
|
||||
it('cannot authenticate a user with invalid credentials', function () {
|
||||
$user = User::factory()->create([
|
||||
'password' => bcrypt('password123'),
|
||||
]);
|
||||
|
||||
$response = post('/login', [
|
||||
'email' => $user->email,
|
||||
'password' => 'wrongpassword',
|
||||
]);
|
||||
|
||||
$response->assertSessionHasErrors();
|
||||
expect(auth()->check())->toBeFalse();
|
||||
});
|
||||
|
||||
it('can logout a user', function () {
|
||||
$user = User::factory()->create();
|
||||
|
||||
$response = $this->actingAs($user)->post('/logout');
|
||||
|
||||
$response->assertRedirect('/');
|
||||
expect(auth()->check())->toBeFalse();
|
||||
});
|
||||
@ -8,5 +8,5 @@ use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
|
||||
|
||||
abstract class TestCase extends BaseTestCase
|
||||
{
|
||||
//
|
||||
use CreateApplication;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user