99 lines
2.6 KiB
PHP
99 lines
2.6 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace Tests\Feature;
|
||
|
|
|
||
|
|
use App\Models\Planner;
|
||
|
|
use App\Models\Schedule;
|
||
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||
|
|
use Illuminate\Support\Facades\Hash;
|
||
|
|
use Tests\TestCase;
|
||
|
|
|
||
|
|
class PlannerLoginTest extends TestCase
|
||
|
|
{
|
||
|
|
use RefreshDatabase;
|
||
|
|
|
||
|
|
public function test_a_planner_can_log_in_with_correct_credentials(): void
|
||
|
|
{
|
||
|
|
$planner = Planner::factory()->create([
|
||
|
|
'email' => 'planner@example.com',
|
||
|
|
'password' => Hash::make('secret123'),
|
||
|
|
]);
|
||
|
|
|
||
|
|
$response = $this
|
||
|
|
->actingAs($planner)
|
||
|
|
->post(route('api.auth.login'), [
|
||
|
|
'email' => 'planner@example.com',
|
||
|
|
'password' => 'secret123',
|
||
|
|
]);
|
||
|
|
|
||
|
|
$response->assertOk();
|
||
|
|
$this->assertAuthenticatedAs($planner);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_login_fails_with_invalid_credentials(): void
|
||
|
|
{
|
||
|
|
Planner::factory()->create([
|
||
|
|
'email' => 'planner@example.com',
|
||
|
|
'password' => Hash::make('secret123'),
|
||
|
|
]);
|
||
|
|
|
||
|
|
$response = $this->postJson(route('api.auth.login'), [
|
||
|
|
'email' => 'planner@example.com',
|
||
|
|
'password' => 'wrongpassword',
|
||
|
|
]);
|
||
|
|
|
||
|
|
$response->assertUnauthorized();
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_a_logged_in_planner_can_log_out(): void
|
||
|
|
{
|
||
|
|
$planner = Planner::factory()->create([
|
||
|
|
'password' => Hash::make('secret123'),
|
||
|
|
]);
|
||
|
|
|
||
|
|
$this->post(route('api.auth.login'), [
|
||
|
|
'email' => $planner->email,
|
||
|
|
'password' => 'secret123',
|
||
|
|
]);
|
||
|
|
|
||
|
|
$response = $this->post(route('api.auth.logout'));
|
||
|
|
|
||
|
|
$response->assertOk();
|
||
|
|
$this->assertGuest(); // nobody should be logged in after logout
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_planner_can_register(): void
|
||
|
|
{
|
||
|
|
$schedulesCount = Schedule::all()->count();
|
||
|
|
|
||
|
|
$response = $this->post(route('api.auth.register'), [
|
||
|
|
'name' => 'High Functioning Planner',
|
||
|
|
'email' => 'planner@example.com',
|
||
|
|
'password' => 'secret123',
|
||
|
|
'password_confirmation' => 'secret123',
|
||
|
|
]);
|
||
|
|
|
||
|
|
$response->assertCreated();
|
||
|
|
|
||
|
|
$this->assertDatabaseHas('planners', [
|
||
|
|
'email' => 'planner@example.com',
|
||
|
|
]);
|
||
|
|
|
||
|
|
$this->assertGreaterThan($schedulesCount, Schedule::all()->count());
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_it_returns_the_authenticated_planner(): void
|
||
|
|
{
|
||
|
|
$planner = Planner::factory()->create();
|
||
|
|
|
||
|
|
$this
|
||
|
|
->actingAs($planner)
|
||
|
|
->get(route('api.auth.me'))
|
||
|
|
->assertOk()
|
||
|
|
->assertJsonFragment([
|
||
|
|
'email' => $planner->email,
|
||
|
|
'name' => $planner->name,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
}
|