Skip to content

Commit

Permalink
Updated to use simpler way of setting auth schemes during mock authen…
Browse files Browse the repository at this point in the history
…tication
  • Loading branch information
davidbyoung committed Jul 18, 2023
1 parent e925324 commit 3d2fa85
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 14 deletions.
2 changes: 0 additions & 2 deletions tests/Integration/Demo/Auth/IMockedAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace App\Tests\Integration\Demo\Auth;

use Aphiria\Authentication\AuthenticationSchemeNotFoundException;
use Aphiria\Authentication\IAuthenticator;
use Aphiria\Security\IPrincipal;

Expand All @@ -19,7 +18,6 @@ interface IMockedAuthenticator extends IAuthenticator
* Mocks the next authentication call to act as the input principal
*
* @param IPrincipal $user The principal to act as for authentication calls
* @throws AuthenticationSchemeNotFoundException Thrown if any of the scheme names could not be found
*/
public function actingAs(IPrincipal $user): void;
}
8 changes: 8 additions & 0 deletions tests/Integration/Demo/Auth/MockAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ protected function authenticateWithScheme(
IAuthenticationSchemeHandler $schemeHandler
): AuthenticationResult {
if ($this->actor !== null) {
// Since we aren't actually calling the scheme handler, be sure to set the scheme name for any identities without one
foreach ($this->actor->getIdentities() as $identity) {
if ($identity->getAuthenticationSchemeName() === null) {
/** @psalm-suppress InternalMethod TODO: Remove this suppression once this logic lives inside Aphiria proper */
$identity->setAuthenticationSchemeName($scheme->name);
}
}

return AuthenticationResult::pass($this->actor, $scheme->name);
}

Expand Down
7 changes: 2 additions & 5 deletions tests/Integration/Demo/Authenticates.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace App\Tests\Integration\Demo;

use Aphiria\Authentication\AuthenticationSchemeNotFoundException;
use Aphiria\Authentication\IAuthenticator;
use Aphiria\DependencyInjection\IContainer;
use Aphiria\DependencyInjection\ResolutionException;
Expand All @@ -24,13 +23,11 @@ trait Authenticates
* Mocks the next authentication call to act as the input principal
*
* @param IPrincipal $user The principal to act as for authentication calls
* @param list<string>|string|null $schemeNames The scheme name or names to authenticate with, or null if using the default scheme
* @throws AuthenticationSchemeNotFoundException Thrown if any of the scheme names could not be found
* TODO: This should be moved into the integration test once the PoC is done
*/
protected function actingAs(IPrincipal $user, array|string $schemeNames = null): static
protected function actingAs(IPrincipal $user): static
{
$this->authenticator->actingAs($user, $schemeNames);
$this->authenticator->actingAs($user);

return $this;
}
Expand Down
9 changes: 2 additions & 7 deletions tests/Integration/Demo/Users/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

use Aphiria\DependencyInjection\Container;
use Aphiria\Net\Http\HttpStatusCode;
use Aphiria\Security\Claim;
use Aphiria\Security\ClaimType;
use Aphiria\Security\Identity;
use Aphiria\Security\IdentityBuilder;
use Aphiria\Security\PrincipalBuilder;
Expand Down Expand Up @@ -115,9 +113,7 @@ public function testGettingPagedUsersRedirectsToForbiddenPageForNonAdmins(): voi
{
$nonAdminUser = (new PrincipalBuilder('example.com'))
->withIdentity(function (IdentityBuilder $identity) {
$identity->withNameIdentifier(1)
// TODO: Need to determine whether I should really need to specify this (I'd like it to default to the default auth scheme, but that's tricky to do if we're passing in an already-created principal with auth schemes (not) set to actingAs())
/*->withAuthenticationSchemeName('cookie')*/;
$identity->withNameIdentifier(1);
})->build();
$response = $this->actingAs($nonAdminUser)->get('/demo/users');
$this->assertStatusCodeEquals(HttpStatusCode::Found, $response);
Expand All @@ -132,8 +128,7 @@ public function testGettingPagedUsersReturnsSuccessfullyForAdmins(): void
$adminUser = (new PrincipalBuilder('example.com'))
->withIdentity(function (IdentityBuilder $identity) {
$identity->withNameIdentifier(1)
->withRoles('admin')
->withAuthenticationSchemeName('cookie');
->withRoles('admin');
})->build();
$response = $this->actingAs($adminUser)->get('/demo/users');
$this->assertStatusCodeEquals(HttpStatusCode::Ok, $response);
Expand Down

0 comments on commit 3d2fa85

Please sign in to comment.