Skip to content
This repository was archived by the owner on Dec 2, 2021. It is now read-only.

Commit b47287c

Browse files
committed
Add test cases for PUBLIC_ACCESS
(cherry picked from commit 9961ed41fec20cf58e941230aca7612e14a55266)
1 parent 6136298 commit b47287c

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

Tests/Security/Authorization/TwoFactorAccessDeciderTest.php

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1313
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
1414
use Symfony\Component\Security\Http\AccessMapInterface;
15+
use Symfony\Component\Security\Http\Firewall\AccessListener;
1516
use Symfony\Component\Security\Http\HttpUtils;
1617
use Symfony\Component\Security\Http\Logout\LogoutUrlGenerator;
1718

@@ -20,6 +21,7 @@ class TwoFactorAccessDeciderTest extends TestCase
2021
private const BASE_URL = '/app_dev.php';
2122
private const LOGOUT_PATH = '/logout';
2223
private const LOGOUT_PATH_WITH_BASE_URL = self::BASE_URL.self::LOGOUT_PATH;
24+
private const ACCESS_MAP_ATTRIBUTES = [TwoFactorInProgressVoter::IS_AUTHENTICATED_2FA_IN_PROGRESS];
2325

2426
/**
2527
* @var MockObject|Request
@@ -36,6 +38,11 @@ class TwoFactorAccessDeciderTest extends TestCase
3638
*/
3739
private $accessMap;
3840

41+
/**
42+
* @var string[]
43+
*/
44+
private $attributes;
45+
3946
/**
4047
* @var MockObject|AccessDecisionManagerInterface
4148
*/
@@ -64,15 +71,17 @@ protected function setUp(): void
6471
$this->accessDecisionManager = $this->createMock(AccessDecisionManagerInterface::class);
6572
$this->httpUtils = $this->createMock(HttpUtils::class);
6673
$this->logoutUrlGenerator = $this->createMock(LogoutUrlGenerator::class);
74+
$this->accessDecider = new TwoFactorAccessDecider($this->accessMap, $this->accessDecisionManager, $this->httpUtils, $this->logoutUrlGenerator);
75+
}
6776

68-
// Stub an access rule
77+
private function stubAccessMapReturnsAttributes(array $attributes): void
78+
{
79+
$this->attributes = $attributes;
6980
$this->accessMap
7081
->expects($this->any())
7182
->method('getPatterns')
7283
->with($this->request)
73-
->willReturn([[TwoFactorInProgressVoter::IS_AUTHENTICATED_2FA_IN_PROGRESS], 'https']);
74-
75-
$this->accessDecider = new TwoFactorAccessDecider($this->accessMap, $this->accessDecisionManager, $this->httpUtils, $this->logoutUrlGenerator);
84+
->willReturn([$attributes, 'https']);
7685
}
7786

7887
private function whenGeneratedLogoutPath(string $generatedLogoutPath): void
@@ -96,7 +105,7 @@ private function whenPathAccess(bool $accessGranted): void
96105
$this->accessDecisionManager
97106
->expects($this->any())
98107
->method('decide')
99-
->with($this->isInstanceOf(TokenInterface::class), [TwoFactorInProgressVoter::IS_AUTHENTICATED_2FA_IN_PROGRESS], $this->request)
108+
->with($this->isInstanceOf(TokenInterface::class), $this->attributes, $this->request)
100109
->willReturn($accessGranted);
101110
}
102111

@@ -114,18 +123,37 @@ private function whenIsLogoutPath(bool $accessGranted): void
114123
*/
115124
public function isAccessible_pathAccessGranted_returnTrue(): void
116125
{
126+
$this->stubAccessMapReturnsAttributes(self::ACCESS_MAP_ATTRIBUTES);
117127
$this->whenPathAccess(true);
118128
$this->whenIsLogoutPath(false);
119129

120130
$returnValue = $this->accessDecider->isAccessible($this->request, $this->token);
121131
$this->assertTrue($returnValue);
122132
}
123133

134+
/**
135+
* @test
136+
*/
137+
public function isAccessible_isPublic_returnTrue(): void
138+
{
139+
$this->requireSymfony5_1();
140+
141+
$this->stubAccessMapReturnsAttributes([AccessListener::PUBLIC_ACCESS]);
142+
$this->whenRequestBaseUrl('');
143+
$this->whenGeneratedLogoutPath(self::LOGOUT_PATH);
144+
$this->whenPathAccess(false);
145+
$this->whenIsLogoutPath(false);
146+
147+
$returnValue = $this->accessDecider->isAccessible($this->request, $this->token);
148+
$this->assertTrue($returnValue);
149+
}
150+
124151
/**
125152
* @test
126153
*/
127154
public function isAccessible_isLogoutPathNoBasePath_returnTrue(): void
128155
{
156+
$this->stubAccessMapReturnsAttributes(self::ACCESS_MAP_ATTRIBUTES);
129157
$this->whenRequestBaseUrl('');
130158
$this->whenGeneratedLogoutPath(self::LOGOUT_PATH);
131159
$this->whenPathAccess(false);
@@ -140,6 +168,7 @@ public function isAccessible_isLogoutPathNoBasePath_returnTrue(): void
140168
*/
141169
public function isAccessible_isLogoutPathWithBasePath_returnTrue(): void
142170
{
171+
$this->stubAccessMapReturnsAttributes(self::ACCESS_MAP_ATTRIBUTES);
143172
$this->whenRequestBaseUrl(self::BASE_URL);
144173
$this->whenGeneratedLogoutPath(self::LOGOUT_PATH_WITH_BASE_URL);
145174
$this->whenPathAccess(false);
@@ -154,6 +183,7 @@ public function isAccessible_isLogoutPathWithBasePath_returnTrue(): void
154183
*/
155184
public function isAccessible_isNotAccessible_returnFalse(): void
156185
{
186+
$this->stubAccessMapReturnsAttributes(self::ACCESS_MAP_ATTRIBUTES);
157187
$this->whenRequestBaseUrl('');
158188
$this->whenGeneratedLogoutPath(self::LOGOUT_PATH);
159189
$this->whenPathAccess(false);

Tests/TestCase.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,22 @@
55
namespace Scheb\TwoFactorBundle\Tests;
66

77
use PHPUnit\Framework\TestCase as PHPUnitTestCase;
8+
use Symfony\Component\HttpKernel\Kernel;
89

10+
// phpcs:ignore Symfony.NamingConventions.ValidClassName
911
abstract class TestCase extends PHPUnitTestCase
1012
{
13+
private const SYMFONY_5_1 = 50100;
14+
15+
protected function requireSymfony5_1()
16+
{
17+
$this->requireSymfonyVersion(self::SYMFONY_5_1);
18+
}
19+
20+
private function requireSymfonyVersion(int $version)
21+
{
22+
if (Kernel::VERSION_ID < $version) {
23+
$this->markTestSkipped("This Symfony version doesn't support authenticators.");
24+
}
25+
}
1126
}

0 commit comments

Comments
 (0)