-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7a3ef91
commit 1b941c0
Showing
4 changed files
with
374 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,371 @@ | ||
<?php namespace GeneaLabs\LaravelGovernor\Tests\Unit\Policies; | ||
|
||
use GeneaLabs\LaravelGovernor\Tests\UnitTestCase; | ||
use GeneaLabs\LaravelGovernor\Tests\Fixtures\User; | ||
use GeneaLabs\LaravelGovernor\Tests\Fixtures\Author; | ||
use GeneaLabs\LaravelGovernor\Team; | ||
use GeneaLabs\LaravelGovernor\Permission; | ||
|
||
class BasePolicyTest extends UnitTestCase | ||
{ | ||
protected $author; | ||
protected $user; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->otherUser = factory(User::class)->create(); | ||
$this->otherUser->roles()->attach("SuperAdmin"); | ||
$this->actingAs($this->otherUser); | ||
$this->otherTeam = (new Team)->create([ | ||
"name" => "Test Team", | ||
"description" => "bla bla bla", | ||
]); | ||
$this->otherAuthor = factory(Author::class)->create(); | ||
$this->otherAuthor->teams()->attach($this->otherTeam); | ||
|
||
$this->user = factory(User::class)->create(); | ||
$this->actingAs($this->user); | ||
$this->team = (new Team)->create([ | ||
"name" => "Test Team", | ||
"description" => "bla bla bla", | ||
]); | ||
$this->author = factory(Author::class)->create(); | ||
$this->author->teams()->attach($this->team); | ||
} | ||
|
||
protected function updatePermission(string $role, string $action, string $ownership) | ||
{ | ||
$permission = (new Permission)->firstOrNew([ | ||
"role_name" => $role, | ||
"entity_name" => "author", | ||
"action_name" => $action, | ||
]); | ||
$permission->ownership_name = $ownership; | ||
$permission->save(); | ||
} | ||
|
||
protected function deletePermission(string $action) | ||
{ | ||
(new Permission) | ||
->where("entity_name", "author") | ||
->where("action_name", $action) | ||
->delete(); | ||
} | ||
|
||
public function testCannotCreateWithoutPolicy() | ||
{ | ||
$this->deletePermission("create"); | ||
|
||
$this->assertFalse($this->user->can("create", Author::class)); | ||
} | ||
|
||
public function testCanCreateWithoutPolicyAsSuperAdmin() | ||
{ | ||
$this->deletePermission("create"); | ||
|
||
$this->assertTrue($this->otherUser->can("create", Author::class)); | ||
} | ||
|
||
public function testCannotCreateWithPolicy() | ||
{ | ||
$this->updatePermission("Member", "create", "no"); | ||
|
||
$this->assertFalse($this->user->can("create", Author::class)); | ||
} | ||
|
||
public function testCanCreateWithNegativePolicyAsSuperAdmin() | ||
{ | ||
$this->updatePermission("SuperAdmin", "create", "no"); | ||
|
||
$this->assertTrue($this->otherUser->can("create", Author::class)); | ||
} | ||
|
||
public function testCanCreateWithPolicy() | ||
{ | ||
$this->updatePermission("Member", "create", "any"); | ||
|
||
$this->assertTrue($this->user->can("create", Author::class)); | ||
} | ||
|
||
public function testCannotViewAnyWithoutPolicy() | ||
{ | ||
$this->deletePermission("viewAny"); | ||
|
||
$this->assertFalse($this->user->can("viewAny", Author::class)); | ||
} | ||
|
||
public function testCanViewAnyWithoutPolicyAsSuperAdmin() | ||
{ | ||
$this->deletePermission("viewAny"); | ||
|
||
$this->assertTrue($this->otherUser->can("viewAny", Author::class)); | ||
} | ||
|
||
public function testCannotViewAnyWithPolicy() | ||
{ | ||
$this->updatePermission("Member", "viewAny", "no"); | ||
|
||
$this->assertFalse($this->user->can("viewAny", Author::class)); | ||
} | ||
|
||
public function testCanViewAnyWithNegativePolicyAsSuperAdmin() | ||
{ | ||
$this->updatePermission("SuperAdmin", "viewAny", "no"); | ||
|
||
$this->assertTrue($this->otherUser->can("viewAny", Author::class)); | ||
} | ||
|
||
public function testCanViewAnyWithPolicy() | ||
{ | ||
$this->updatePermission("Member", "viewAny", "any"); | ||
|
||
$this->assertTrue($this->user->can("viewAny", Author::class)); | ||
} | ||
|
||
public function testCannotUpdateWithoutPolicy() | ||
{ | ||
$this->deletePermission("update"); | ||
|
||
$this->assertFalse($this->user->can("update", $this->author)); | ||
} | ||
|
||
public function testCanUpdateWithoutPolicyAsSuperAdmin() | ||
{ | ||
$this->deletePermission("update"); | ||
|
||
$this->assertTrue($this->otherUser->can("update", $this->author)); | ||
} | ||
|
||
public function testCannotUpdateWithPolicy() | ||
{ | ||
$this->updatePermission("Member", "update", "no"); | ||
|
||
$this->assertFalse($this->user->can("update", $this->author)); | ||
} | ||
|
||
public function testCanUpdateWithNegativePolicyAsSuperAdmin() | ||
{ | ||
$this->updatePermission("SuperAdmin", "update", "no"); | ||
|
||
$this->assertTrue($this->otherUser->can("update", $this->author)); | ||
} | ||
|
||
public function testCanUpdateAnyWithPolicy() | ||
{ | ||
$this->updatePermission("Member", "update", "any"); | ||
|
||
$this->assertTrue($this->user->can("update", $this->author)); | ||
} | ||
|
||
public function testCannotUpdateOwnWithPolicy() | ||
{ | ||
$this->updatePermission("Member", "viewAny", "own"); | ||
|
||
$this->assertFalse($this->user->can("update", $this->otherAuthor)); | ||
} | ||
|
||
public function testCanUpdateOwnWithPolicy() | ||
{ | ||
$this->updatePermission("Member", "update", "own"); | ||
|
||
$this->assertTrue($this->user->can("update", $this->author)); | ||
} | ||
|
||
public function testCannotViewWithoutPolicy() | ||
{ | ||
$this->deletePermission("view"); | ||
|
||
$this->assertFalse($this->user->can("view", $this->author)); | ||
} | ||
|
||
public function testCanViewWithoutPolicyAsSuperAdmin() | ||
{ | ||
$this->deletePermission("view"); | ||
|
||
$this->assertTrue($this->otherUser->can("view", $this->author)); | ||
} | ||
|
||
public function testCannotViewWithPolicy() | ||
{ | ||
$this->updatePermission("Member", "view", "no"); | ||
|
||
$this->assertFalse($this->user->can("view", $this->author)); | ||
} | ||
|
||
public function testCanViewWithNegativePolicyAsSuperAdmin() | ||
{ | ||
$this->updatePermission("SuperAdmin", "view", "no"); | ||
|
||
$this->assertTrue($this->otherUser->can("view", $this->author)); | ||
} | ||
|
||
public function testCanViewWithPolicy() | ||
{ | ||
$this->updatePermission("Member", "view", "any"); | ||
|
||
$this->assertTrue($this->user->can("view", $this->author)); | ||
} | ||
|
||
public function testCannotViewOwnWithPolicy() | ||
{ | ||
$this->updatePermission("Member", "view", "own"); | ||
|
||
$this->assertFalse($this->user->can("view", $this->otherAuthor)); | ||
} | ||
|
||
public function testCanViewOwnWithPolicy() | ||
{ | ||
$this->updatePermission("Member", "view", "own"); | ||
|
||
$this->assertTrue($this->user->can("view", $this->author)); | ||
} | ||
|
||
public function testCannotDeleteWithoutPolicy() | ||
{ | ||
$this->deletePermission("delete"); | ||
|
||
$this->assertFalse($this->user->can("delete", $this->author)); | ||
} | ||
|
||
public function testCanDeleteWithoutPolicyAsSuperAdmin() | ||
{ | ||
$this->deletePermission("delete"); | ||
|
||
$this->assertTrue($this->otherUser->can("delete", $this->author)); | ||
} | ||
|
||
public function testCannotDeleteWithPolicy() | ||
{ | ||
$this->updatePermission("Member", "delete", "no"); | ||
|
||
$this->assertFalse($this->user->can("delete", $this->author)); | ||
} | ||
|
||
public function testCanDeleteWithNegativePolicyAsSuperAdmin() | ||
{ | ||
$this->updatePermission("SuperAdmin", "delete", "no"); | ||
|
||
$this->assertTrue($this->otherUser->can("delete", $this->author)); | ||
} | ||
|
||
public function testCanDeleteWithPolicy() | ||
{ | ||
$this->updatePermission("Member", "delete", "any"); | ||
|
||
$this->assertTrue($this->user->can("delete", $this->author)); | ||
} | ||
|
||
public function testCannotDeleteOwnWithPolicy() | ||
{ | ||
$this->updatePermission("Member", "delete", "own"); | ||
|
||
$this->assertFalse($this->user->can("delete", $this->otherAuthor)); | ||
} | ||
|
||
public function testCanDeleteOwnWithPolicy() | ||
{ | ||
$this->updatePermission("Member", "delete", "own"); | ||
|
||
$this->assertTrue($this->user->can("delete", $this->author)); | ||
} | ||
|
||
public function testCannotRestoreWithoutPolicy() | ||
{ | ||
$this->deletePermission("restore"); | ||
|
||
$this->assertFalse($this->user->can("restore", $this->author)); | ||
} | ||
|
||
public function testCanRestoreWithoutPolicyAsSuperAdmin() | ||
{ | ||
$this->deletePermission("restore"); | ||
|
||
$this->assertTrue($this->otherUser->can("restore", $this->author)); | ||
} | ||
|
||
public function testCannotRestoreWithPolicy() | ||
{ | ||
$this->updatePermission("Member", "restore", "no"); | ||
|
||
$this->assertFalse($this->user->can("restore", $this->author)); | ||
} | ||
|
||
public function testCanRestoreWithNegativePolicyAsSuperAdmin() | ||
{ | ||
$this->updatePermission("SuperAdmin", "restore", "no"); | ||
|
||
$this->assertTrue($this->otherUser->can("restore", $this->author)); | ||
} | ||
|
||
public function testCanRestoreWithPolicy() | ||
{ | ||
$this->updatePermission("Member", "restore", "any"); | ||
|
||
$this->assertTrue($this->user->can("restore", $this->author)); | ||
} | ||
|
||
public function testCannotRestoreOwnWithPolicy() | ||
{ | ||
$this->updatePermission("Member", "restore", "own"); | ||
|
||
$this->assertFalse($this->user->can("restore", $this->otherAuthor)); | ||
} | ||
|
||
public function testCanRestoreOwnWithPolicy() | ||
{ | ||
$this->updatePermission("Member", "restore", "own"); | ||
|
||
$this->assertTrue($this->user->can("restore", $this->author)); | ||
} | ||
|
||
public function testCannotForceDeleteWithoutPolicy() | ||
{ | ||
$this->deletePermission("forceDelete"); | ||
|
||
$this->assertFalse($this->user->can("forceDelete", $this->author)); | ||
} | ||
|
||
public function testCanForceDeleteWithoutPolicyAsSuperAdmin() | ||
{ | ||
$this->deletePermission("forceDelete"); | ||
|
||
$this->assertTrue($this->otherUser->can("forceDelete", $this->author)); | ||
} | ||
|
||
public function testCannotForceDeleteWithPolicy() | ||
{ | ||
$this->updatePermission("Member", "forceDelete", "no"); | ||
|
||
$this->assertFalse($this->user->can("forceDelete", $this->author)); | ||
} | ||
|
||
public function testCanForceDeleteWithNegativePolicyAsSuperAdmin() | ||
{ | ||
$this->updatePermission("SuperAdmin", "forceDelete", "no"); | ||
|
||
$this->assertTrue($this->otherUser->can("forceDelete", $this->author)); | ||
} | ||
|
||
public function testCanForceDeleteWithPolicy() | ||
{ | ||
$this->updatePermission("Member", "forceDelete", "any"); | ||
|
||
$this->assertTrue($this->user->can("forceDelete", $this->author)); | ||
} | ||
|
||
public function testCannotForceDeleteOwnWithPolicy() | ||
{ | ||
$this->updatePermission("Member", "forceDelete", "own"); | ||
|
||
$this->assertFalse($this->user->can("forceDelete", $this->otherAuthor)); | ||
} | ||
|
||
public function testCanForceDeleteOwnWithPolicy() | ||
{ | ||
$this->updatePermission("Member", "forceDelete", "own"); | ||
|
||
$this->assertTrue($this->user->can("forceDelete", $this->author)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.