Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Valentin Sickert <[email protected]>
  • Loading branch information
Lapotor committed Nov 26, 2023
1 parent 8891ca4 commit c2a2fcc
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions tests/Feature/AuthControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Tests\Feature;

use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Http\Response;
use Tests\TestCase;

class AuthControllerTest extends TestCase
{
use RefreshDatabase, WithFaker;

/**
* Test login with valid credentials.
*
* @return void
*/
public function test_login_with_valid_credentials(): void
{

$user = User::factory()->create([
'email' => '[email protected]',
'password' => bcrypt('ValidPassword'),
]);

$response = $this->postJson('/api/v1/login', [
'email' => '[email protected]',
'password' => 'ValidPassword',
]);

$response->assertStatus(Response::HTTP_OK)
->assertJsonStructure([
'user' => [
'id',
'name',
'email',
'email_verified_at',
'created_at',
'updated_at',
],
'access_token',
]);

$this->assertAuthenticatedAs($user);
}

/**
* Test login with invalid credentials.
*
* @return void
*/
public function test_login_with_invalid_credentials(): void
{
$response = $this->postJson('/api/v1/login', [
'email' => '[email protected]',
'password' => 'invalidpassword',
]);

$response->assertStatus(Response::HTTP_UNAUTHORIZED)
->assertJson([
'message' => 'Invalid login credentials',
]);

$this->assertGuest();
}
}

0 comments on commit c2a2fcc

Please sign in to comment.