83 lines
1.9 KiB
PHP
83 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Auth\Role;
|
|
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Foundation\Testing\WithFaker;
|
|
use Tests\TestCase;
|
|
|
|
use App\Models\Role;
|
|
use App\Models\User;
|
|
use App\Models\Permission;
|
|
|
|
use Laravel\Sanctum\Sanctum;
|
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
|
|
class StoreTest extends TestCase
|
|
{
|
|
use DatabaseTransactions;
|
|
|
|
/**
|
|
* A basic feature test example.
|
|
*/
|
|
public function test_success(): void
|
|
{
|
|
$permission = Permission::where("code","auth.role:create")->first();
|
|
$role = Role::factory()->create();
|
|
$role->permissions()->attach($permission->id);
|
|
|
|
$user = User::factory()->create();
|
|
$user->roles()->attach($role->id);
|
|
|
|
Sanctum::actingAs($user);
|
|
|
|
$response = $this->post('/auth/role/',[
|
|
"name" => "new role"
|
|
]);
|
|
|
|
$response->assertStatus(201);
|
|
$response->assertJson([
|
|
"data" => [
|
|
"name" => "new role"
|
|
]
|
|
]);
|
|
}
|
|
|
|
|
|
/**
|
|
* A basic feature test example.
|
|
*/
|
|
public function test_with_roles_success(): void
|
|
{
|
|
$permission = Permission::where("code","auth.role:create")->first();
|
|
$role = Role::factory()->create();
|
|
$role->permissions()->attach($permission->id);
|
|
|
|
$user = User::factory()->create();
|
|
$user->roles()->attach($role->id);
|
|
|
|
$role2 = Role::factory()->create();
|
|
|
|
|
|
Sanctum::actingAs($user);
|
|
|
|
$response = $this->post('/auth/role/',[
|
|
"name" => "new role",
|
|
"permissions" => [$permission->id]
|
|
]);
|
|
|
|
$response->assertStatus(201);
|
|
$response->assertJson([
|
|
"data" => [
|
|
"name" => "new role",
|
|
"permissions" => [
|
|
[
|
|
"id" => $permission->id
|
|
],
|
|
]
|
|
]
|
|
]);
|
|
}
|
|
}
|