added test for regster and login controller api

This commit is contained in:
2024-01-27 14:40:54 +03:00
parent 3b1cc48444
commit e4142eb477
2 changed files with 70 additions and 7 deletions

View File

@@ -0,0 +1,67 @@
<?php
namespace Tests\Feature\Api\Auth;
use App\Models\User;
use App\Models\UserDetailz;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Auth;
use Illuminate\Testing\Fluent\AssertableJson;
use Tests\TestCase;
class LoginTest extends TestCase
{
use RefreshDatabase;
protected User $user;
public function setUp(): void
{
$this->refreshApplicationWithLocale('en');
$this->user = User::factory()->create();
}
public function test_login_success()
{
$response = $this
->actingAs($this->user)
->postJson('/api/login', [
'email' => $this->user->email,
'password' => 'password',
]);
// Assert a token was returned
$response->assertStatus(200);
$response->assertJsonStructure(['token']);
}
public function test_login_failed_no_register_user()
{
$user = User::factory()->make();
$response = $this
->postJson('/api/login', [
'email' => $user->email,
'password' => 'password',
]);
// Assert a token was returned
$response->assertStatus(401);
}
public function test_login_failed_error_password()
{
$response = $this
->actingAs($this->user)
->postJson('/api/login', [
'email' => $this->user->email,
'password' => '1234567890',
]);
// Assert a token was returned
$response->assertStatus(401);
}
}

View File

@@ -5,6 +5,7 @@ namespace Tests\Feature\Api\Auth;
use App\Models\User;
use App\Models\UserDetailz;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Auth;
use Illuminate\Testing\Fluent\AssertableJson;
use Tests\TestCase;
@@ -42,14 +43,12 @@ class RegisterTest extends TestCase
'waist_circumference' => $this->userDetails->waist_circumference,
'hip_circumference' => $this->userDetails->hip_circumference,
'has_hypo' => $this->userDetails->has_hypo,
'has_hyper' => $this->userDetails->has_hypo,
'has_hyper' => $this->userDetails->has_hyper,
]);
$response
->assertStatus(200)
->assertJson(fn(AssertableJson $json) =>
$json->has('token')
);
->assertJsonStructure(['token']);
}
public function test_register_failed_this_email_already_taken()
@@ -77,10 +76,8 @@ class RegisterTest extends TestCase
$response
->assertStatus(422);
}
public function test_register_failed_empty_data()
{
$user = User::factory()->create(['is_consultant' => true]);
@@ -106,6 +103,5 @@ class RegisterTest extends TestCase
$response
->assertStatus(422);
}
}