write test for another items and change some part of the code

This commit is contained in:
2025-05-14 15:38:33 +03:30
parent db8cda55df
commit 16fe0be555
20 changed files with 1258 additions and 45 deletions

View File

@@ -0,0 +1,76 @@
<?php
namespace Tests\Feature\User;
use App\Models\Permission;
use App\Models\Role;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class ShowTest extends TestCase
{
use RefreshDatabase, WithFaker;
public function test_unauthenticated_user_cannot_access(): void
{
$user = User::factory()->create();
$response = $this->getJson(route('users.show',[$user->id]));
$response->assertUnauthorized();
}
public function test_user_without_permission_cannot_show_users(): void
{
$user = User::factory()
->create();
$response = $this->actingAs($user)->getJson(route('users.show',[$user->id]));
$response->assertForbidden();
}
public function test_superAdmin_cant_updated()
{
$user = User::factory()->create([
'username' => "witel",
]);
$response = $this->actingAs($user)->getJson(route('users.show',[$user->id]));
$response->assertForbidden();
}
public function test_user_with_permission_can_show_users(): void
{
$user = User::factory()
->has(Permission::factory([
'name' => 'manage_users',
'name_fa' => 'مدیریت کاربران'
]))
->create();
$role = Role::factory()
->has(Permission::factory([
'name' => 'test'
]))->create();
$user->assignRole($role->id);
$response = $this->actingAs($user)->getJson(route('users.show',[$user->id]));
$response->assertJsonStructure([
'data' => [
'username',
'full_name',
'id',
'gender',
'email',
'phone_number',
'avatar',
'national_id',
'position',
'province_id',
'province_name',
'role',
'telephone_id'
]
]);
}
}