Skip to content

Commit

Permalink
Updates ACL Tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-cintra committed Oct 22, 2024
1 parent 492cfab commit e64c1fc
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 36 deletions.
26 changes: 15 additions & 11 deletions stubs/modules/Acl/Tests/Permission/PermissionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@
use Inertia\Testing\AssertableInertia as Assert;
use Modules\User\Models\User;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
use Tests\TestCase;

uses(TestCase::class, RefreshDatabase::class);

beforeEach(function () {
$role = Role::create(['name' => 'root']);
$this->permission = Permission::create(['name' => 'first']);

$this->user = User::factory()->create();
$this->loggedRequest = $this->actingAs($this->user);
$this->user->assignRole($role);

$this->permission = Permission::create(['name' => 'first']);
$this->loggedRequest = $this->actingAs($this->user);
});

test('permission list can be rendered', function () {
Expand All @@ -21,12 +25,12 @@
$response->assertStatus(200);

$response->assertInertia(
fn (Assert $page) => $page
fn(Assert $page) => $page
->component('AclPermission/PermissionIndex')
->has(
'permissions.data',
1,
fn (Assert $page) => $page
fn(Assert $page) => $page
->where('id', $this->permission->id)
->where('name', $this->permission->name)
->where('guard', null)
Expand All @@ -47,16 +51,16 @@
});

test('permission edit can be rendered', function () {
$response = $this->loggedRequest->get('/admin/acl-permission/'.$this->permission->id.'/edit');
$response = $this->loggedRequest->get('/admin/acl-permission/' . $this->permission->id . '/edit');

$response->assertStatus(200);

$response->assertInertia(
fn (Assert $page) => $page
fn(Assert $page) => $page
->component('AclPermission/PermissionForm')
->has(
'permission',
fn (Assert $page) => $page
fn(Assert $page) => $page
->where('id', $this->permission->id)
->where('name', $this->permission->name)
->where('guard_name', $this->permission->guard_name)
Expand All @@ -67,20 +71,20 @@
});

test('permission can be updated', function () {
$response = $this->loggedRequest->put('/admin/acl-permission/'.$this->permission->id, [
$response = $this->loggedRequest->put('/admin/acl-permission/' . $this->permission->id, [
'name' => 'z Permission Name',
]);

$response->assertRedirect('/admin/acl-permission');

$redirectResponse = $this->loggedRequest->get('/admin/acl-permission');
$redirectResponse->assertInertia(
fn (Assert $page) => $page
fn(Assert $page) => $page
->component('AclPermission/PermissionIndex')
->has(
'permissions.data',
1,
fn (Assert $page) => $page
fn(Assert $page) => $page
->where('id', $this->permission->id)
->where('name', 'z Permission Name')
->where('guard', null)
Expand All @@ -89,7 +93,7 @@
});

test('permission can be deleted', function () {
$response = $this->loggedRequest->delete('/admin/acl-permission/'.$this->permission->id);
$response = $this->loggedRequest->delete('/admin/acl-permission/' . $this->permission->id);

$response->assertRedirect('/admin/acl-permission');

Expand Down
14 changes: 9 additions & 5 deletions stubs/modules/Acl/Tests/Permission/UserPermissionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
use Modules\Acl\Services\GetUserPermissions;
use Modules\User\Models\User;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
use Tests\TestCase;

uses(TestCase::class, RefreshDatabase::class);

beforeEach(function () {
$role = Role::create(['name' => 'root']);
$this->user = User::factory()->create();
$this->user->assignRole($role);

$this->loggedRequest = $this->actingAs($this->user);

$this->permission = Permission::create(['name' => 'first', 'guard_name' => 'user']);
Expand All @@ -20,23 +24,23 @@
});

test('user permissions can be rendered', function () {
$response = $this->loggedRequest->get('/admin/acl-user-permission/'.$this->user->id.'/edit');
$response = $this->loggedRequest->get('/admin/acl-user-permission/' . $this->user->id . '/edit');

$response->assertStatus(200);

$response->assertInertia(
fn (Assert $page) => $page
fn(Assert $page) => $page
->component('AclUserPermission/UserPermissionForm')
->has(
'user',
fn (Assert $page) => $page
fn(Assert $page) => $page
->where('id', $this->user->id)
->etc()
)
->has(
'userPermissions',
1,
fn (Assert $page) => $page
fn(Assert $page) => $page
->where('id', $this->permission->id)
->where('name', $this->permission->name)
)
Expand All @@ -48,7 +52,7 @@
});

test('user permissions can be updated', function () {
$response = $this->loggedRequest->put('/admin/acl-user-permission/'.$this->user->id, [
$response = $this->loggedRequest->put('/admin/acl-user-permission/' . $this->user->id, [
'userPermissions' => [$this->permission2->id],
]);

Expand Down
20 changes: 11 additions & 9 deletions stubs/modules/Acl/Tests/Role/RolePermissionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,37 @@
uses(TestCase::class, RefreshDatabase::class);

beforeEach(function () {
$this->role = Role::create(['name' => 'root']);
$this->user = User::factory()->create();
$this->loggedRequest = $this->actingAs($this->user);

$this->role = Role::create(['name' => 'root', 'guard_name' => 'user']);

$this->permission = Permission::create(['name' => 'first', 'guard_name' => 'user']);
$this->permission2 = Permission::create(['name' => 'second', 'guard_name' => 'user']);
$this->permission = Permission::create(['name' => 'first']);
$this->permission2 = Permission::create(['name' => 'second']);

$this->role->syncPermissions([$this->permission->id]);
$this->user->assignRole($this->role);

$this->loggedRequest = $this->actingAs($this->user);
});

test('role permissions can be rendered', function () {
$response = $this->loggedRequest->get('/admin/acl-role-permission/'.$this->role->id.'/edit');
$response = $this->loggedRequest->get('/admin/acl-role-permission/' . $this->role->id . '/edit');

$response->assertStatus(200);

$response->assertInertia(
fn (Assert $page) => $page
fn(Assert $page) => $page
->component('AclRolePermission/RolePermissionForm')
->has(
'role',
fn (Assert $page) => $page
fn(Assert $page) => $page
->where('id', $this->role->id)
->etc()
)
->has(
'role.permissions',
1,
fn (Assert $page) => $page
fn(Assert $page) => $page
->where('id', $this->permission->id)
->where('name', $this->permission->name)
)
Expand All @@ -50,7 +52,7 @@
});

test('role permissions can be updated', function () {
$response = $this->loggedRequest->put('/admin/acl-role-permission/'.$this->role->id, [
$response = $this->loggedRequest->put('/admin/acl-role-permission/' . $this->role->id, [
'rolePermissions' => [$this->permission2->id],
]);

Expand Down
23 changes: 12 additions & 11 deletions stubs/modules/Acl/Tests/Role/RoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
uses(TestCase::class, RefreshDatabase::class);

beforeEach(function () {
$this->role = Role::create(['name' => 'root']);
$this->user = User::factory()->create();
$this->loggedRequest = $this->actingAs($this->user);

$this->role = Role::create(['name' => 'root', 'guard_name' => 'user']);
$this->user->assignRole($this->role);
$this->loggedRequest = $this->actingAs($this->user);
});

test('role list can be rendered', function () {
Expand All @@ -21,12 +22,12 @@
$response->assertStatus(200);

$response->assertInertia(
fn (Assert $page) => $page
fn(Assert $page) => $page
->component('AclRole/RoleIndex')
->has(
'roles.data',
1,
fn (Assert $page) => $page
fn(Assert $page) => $page
->where('id', $this->role->id)
->where('name', $this->role->name)
->where('guard_name', $this->role->guard_name)
Expand All @@ -48,16 +49,16 @@
});

test('role edit can be rendered', function () {
$response = $this->loggedRequest->get('/admin/acl-role/'.$this->role->id.'/edit');
$response = $this->loggedRequest->get('/admin/acl-role/' . $this->role->id . '/edit');

$response->assertStatus(200);

$response->assertInertia(
fn (Assert $page) => $page
fn(Assert $page) => $page
->component('AclRole/RoleForm')
->has(
'role',
fn (Assert $page) => $page
fn(Assert $page) => $page
->where('id', $this->role->id)
->where('name', $this->role->name)
->where('guard_name', $this->role->guard_name)
Expand All @@ -67,20 +68,20 @@
});

test('role can be updated', function () {
$response = $this->loggedRequest->put('/admin/acl-role/'.$this->role->id, [
$response = $this->loggedRequest->put('/admin/acl-role/' . $this->role->id, [
'name' => 'z Role Name',
]);

$response->assertRedirect('/admin/acl-role');

$redirectResponse = $this->loggedRequest->get('/admin/acl-role');
$redirectResponse->assertInertia(
fn (Assert $page) => $page
fn(Assert $page) => $page
->component('AclRole/RoleIndex')
->has(
'roles.data',
1,
fn (Assert $page) => $page
fn(Assert $page) => $page
->where('id', $this->role->id)
->where('name', 'z Role Name')
->where('guard_name', $this->role->guard_name)
Expand All @@ -89,7 +90,7 @@
});

test('role can be deleted', function () {
$response = $this->loggedRequest->delete('/admin/acl-role/'.$this->role->id);
$response = $this->loggedRequest->delete('/admin/acl-role/' . $this->role->id);

$response->assertRedirect('/admin/acl-role');

Expand Down
1 change: 1 addition & 0 deletions stubs/site/modules/Index/Tests/IndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
uses(TestCase::class);

test("the site's index page returns a successful response", function () {
$this->withoutVite();
$response = $this->get('/');
$response->assertStatus(200);
});

0 comments on commit e64c1fc

Please sign in to comment.