Skip to content

Commit

Permalink
Added some unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbyoung committed Aug 10, 2023
1 parent c6fb36b commit 13b7948
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/Auth/AuthModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Aphiria\Framework\Application\AphiriaModule;
use Aphiria\Net\Http\Headers\SameSiteMode;
use Aphiria\Net\Http\HttpStatusCode;
use App\Auth\Binders\AuthServiceBinder;
use App\Auth\Binders\AuthBinder;
use App\Auth\Policies\OwnerOrAdminRequirement;
use App\Auth\Policies\OwnerOrAdminRequirementHandler;
use App\Auth\Schemes\BasicAuthenticationHandler;
Expand All @@ -30,7 +30,7 @@ final class AuthModule extends AphiriaModule
*/
public function configure(IApplicationBuilder $appBuilder): void
{
$this->withBinders($appBuilder, new AuthServiceBinder())
$this->withBinders($appBuilder, new AuthBinder())
// Add our default authentication scheme
->withAuthenticationScheme(
$appBuilder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@
use App\Auth\SqlTokenService;

/**
* Defines the auth service binder
* Defines the auth binder
*/
final class AuthServiceBinder extends Binder
final class AuthBinder extends Binder
{
/**
* @inheritdoc
*/
public function bind(IContainer $container): void
{
// Configure the token service
$container->bindClass(ITokenService::class, SqlTokenService::class);
}
}
Empty file removed tests/Unit/.gitkeep
Empty file.
80 changes: 80 additions & 0 deletions tests/Unit/Auth/Policies/OwnerOrAdminRequirementHandlerTest.php
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);
}
}

0 comments on commit 13b7948

Please sign in to comment.