-
Notifications
You must be signed in to change notification settings - Fork 1
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
c6fb36b
commit 13b7948
Showing
4 changed files
with
84 additions
and
5 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
Empty file.
80 changes: 80 additions & 0 deletions
80
tests/Unit/Auth/Policies/OwnerOrAdminRequirementHandlerTest.php
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,80 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Tests\Unit\Auth\Policies; | ||
|
||
use Aphiria\Authorization\AuthorizationContext; | ||
use Aphiria\Security\PrincipalBuilder; | ||
use App\Auth\Policies\OwnerOrAdminRequirement; | ||
use App\Auth\Policies\OwnerOrAdminRequirementHandler; | ||
use App\Users\User; | ||
use InvalidArgumentException; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class OwnerOrAdminRequirementHandlerTest extends TestCase | ||
{ | ||
private OwnerOrAdminRequirementHandler $requirementHandler; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->requirementHandler = new OwnerOrAdminRequirementHandler(); | ||
} | ||
|
||
public function testBeingNeitherAdminNorOwnerFails(): void | ||
{ | ||
$userToAccess = new User(1, 'foo@example.com', []); | ||
$userAccessing = (new PrincipalBuilder('example.com'))->withNameIdentifier(2) | ||
->build(); | ||
$requirement = new OwnerOrAdminRequirement('admin'); | ||
$context = new AuthorizationContext($userAccessing, [$requirement], $userToAccess); | ||
$this->requirementHandler->handle($userAccessing, $requirement, $context); | ||
$this->assertFalse($context->allRequirementsPassed()); | ||
} | ||
|
||
public function testBeingOwnerPasses(): void | ||
{ | ||
$userToAccess = new User(1, 'foo@example.com', []); | ||
$userAccessing = (new PrincipalBuilder('example.com'))->withNameIdentifier(1) | ||
->build(); | ||
$requirement = new OwnerOrAdminRequirement('admin'); | ||
$context = new AuthorizationContext($userAccessing, [$requirement], $userToAccess); | ||
$this->requirementHandler->handle($userAccessing, $requirement, $context); | ||
$this->assertTrue($context->allRequirementsPassed()); | ||
} | ||
|
||
public function testHavingAnAdminRolePasses(): void | ||
{ | ||
$userToAccess = new User(1, 'foo@example.com', []); | ||
$userAccessing = (new PrincipalBuilder('example.com'))->withRoles('admin') | ||
->build(); | ||
$requirement = new OwnerOrAdminRequirement('admin'); | ||
$context = new AuthorizationContext($userAccessing, [$requirement], $userToAccess); | ||
$this->requirementHandler->handle($userAccessing, $requirement, $context); | ||
$this->assertTrue($context->allRequirementsPassed()); | ||
} | ||
|
||
public function testRequirementOfIncorrectTypeThrowsException(): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage('Requirement must be of type ' . OwnerOrAdminRequirement::class); | ||
$userToAccess = new User(1, 'foo@example.com', []); | ||
$userAccessing = (new PrincipalBuilder('example.com'))->withRoles('admin') | ||
->build(); | ||
$requirement = $this; | ||
$context = new AuthorizationContext($userAccessing, [$requirement], $userToAccess); | ||
/** @psalm-suppress InvalidArgument Purposely testing this */ | ||
$this->requirementHandler->handle($userAccessing, $requirement, $context); | ||
} | ||
|
||
public function testResourceOfIncorrectTypeThrowsException(): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage('Resource must be of type ' . User::class); | ||
$userAccessing = (new PrincipalBuilder('example.com'))->build(); | ||
$requirement = new OwnerOrAdminRequirement('admin'); | ||
$context = new AuthorizationContext($userAccessing, [$requirement], $this); | ||
/** @psalm-suppress InvalidArgument Purposely testing this */ | ||
$this->requirementHandler->handle($userAccessing, $requirement, $context); | ||
} | ||
} |