Skip to content

Commit 429864a

Browse files
committed
minor #73 Add authorization tests (X-Coder264)
This PR was merged into the 0.1-dev branch. Discussion ---------- Add authorization tests This PR adds a regression test which was previously missing (it checks that the bundle integrates properly with Symfony authorization). This test is failing on Symfony 5.4 without the changes that were made in #72 so it's needed to prove that the bundle works as intended after that change was made. Commits ------- fdff4d3 Add authorization test
2 parents a0b5585 + fdff4d3 commit 429864a

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

tests/Acceptance/SecurityLayerTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,40 @@ public function testAuthenticatedUserRolesRequest(): void
9595
$this->assertSame('These are the roles I have currently assigned: ROLE_OAUTH2_FANCY, ROLE_USER', $response->getContent());
9696
}
9797

98+
public function testSuccessfulAuthorizationForAuthenticatedUserRequest(): void
99+
{
100+
$accessToken = $this->client
101+
->getContainer()
102+
->get(AccessTokenManagerInterface::class)
103+
->find(FixtureFactory::FIXTURE_ACCESS_TOKEN_USER_BOUND_WITH_SCOPES);
104+
105+
$this->client->request('GET', '/security-test-authorization', [], [], [
106+
'HTTP_AUTHORIZATION' => sprintf('Bearer %s', TestHelper::generateJwtToken($accessToken)),
107+
]);
108+
109+
$response = $this->client->getResponse();
110+
111+
$this->assertSame(200, $response->getStatusCode());
112+
$this->assertSame('access granted', $response->getContent());
113+
}
114+
115+
public function testUnsuccessfulAuthorizationForAuthenticatedUserRequest(): void
116+
{
117+
$accessToken = $this->client
118+
->getContainer()
119+
->get(AccessTokenManagerInterface::class)
120+
->find(FixtureFactory::FIXTURE_ACCESS_TOKEN_USER_BOUND);
121+
122+
$this->client->request('GET', '/security-test-authorization', [], [], [
123+
'HTTP_AUTHORIZATION' => sprintf('Bearer %s', TestHelper::generateJwtToken($accessToken)),
124+
]);
125+
126+
$response = $this->client->getResponse();
127+
128+
$this->assertSame(403, $response->getStatusCode());
129+
$this->assertNotSame('access granted', $response->getContent());
130+
}
131+
98132
public function testExpiredRequest(): void
99133
{
100134
$accessToken = $this->client

tests/Fixtures/SecurityTestController.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,11 @@ public function rolesAction(): Response
4848
)
4949
);
5050
}
51+
52+
public function authorizationAction(): Response
53+
{
54+
$this->denyAccessUnlessGranted('ROLE_OAUTH2_FANCY');
55+
56+
return new Response('access granted');
57+
}
5158
}

tests/Fixtures/routes.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,8 @@
2323
->defaults([
2424
'oauth2_scopes' => ['fancy'],
2525
])
26+
27+
->add('security_test_authorization', '/security-test-authorization')
28+
->controller([SecurityTestController::class, 'authorizationAction'])
2629
;
2730
};

0 commit comments

Comments
 (0)