Skip to content

Commit de84a60

Browse files
committed
Merge pull request #96 from FriendsOfSymfony/add-missing-tests
add some missing tests for the security context
2 parents 7a2a540 + c6ccc06 commit de84a60

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

Tests/Unit/EventListener/UserContextSubscriberTest.php

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@
2222

2323
class UserContextSubscriberTest extends \PHPUnit_Framework_TestCase
2424
{
25+
/**
26+
* @expectedException \InvalidArgumentException
27+
*/
28+
public function testMisconfiguration()
29+
{
30+
new UserContextSubscriber(
31+
\Mockery::mock('\Symfony\Component\HttpFoundation\RequestMatcherInterface'),
32+
\Mockery::mock('\FOS\HttpCache\UserContext\HashGenerator'),
33+
array()
34+
);
35+
}
36+
2537
public function testOnKernelRequest()
2638
{
2739
$request = new Request();
@@ -45,6 +57,23 @@ public function testOnKernelRequest()
4557
$this->assertEquals('max-age=0, no-cache, private', $response->headers->get('Cache-Control'));
4658
}
4759

60+
public function testOnKernelRequestNonMaster()
61+
{
62+
$request = new Request();
63+
$request->setMethod('HEAD');
64+
65+
$requestMatcher = $this->getRequestMatcher($request, true);
66+
$hashGenerator = \Mockery::mock('\FOS\HttpCache\UserContext\HashGenerator');
67+
$hashGenerator->shouldReceive('generateHash')->never();
68+
69+
$userContextSubscriber = new UserContextSubscriber($requestMatcher, $hashGenerator, array('X-SessionId'), 'X-Hash');
70+
$event = $this->getKernelRequestEvent($request, HttpKernelInterface::SUB_REQUEST);
71+
72+
$userContextSubscriber->onKernelRequest($event);
73+
74+
$this->assertNull($event->getResponse());
75+
}
76+
4877
public function testOnKernelRequestCached()
4978
{
5079
$request = new Request();
@@ -103,6 +132,23 @@ public function testOnKernelResponse()
103132
$this->assertContains('X-Hash', $event->getResponse()->headers->get('Vary'));
104133
}
105134

135+
public function testOnKernelResponseNotMaster()
136+
{
137+
$request = new Request();
138+
$request->setMethod('HEAD');
139+
$request->headers->set('X-Hash', 'hash');
140+
141+
$requestMatcher = $this->getRequestMatcher($request, false);
142+
$hashGenerator = \Mockery::mock('\FOS\HttpCache\UserContext\HashGenerator');
143+
144+
$userContextSubscriber = new UserContextSubscriber($requestMatcher, $hashGenerator, array('X-SessionId'), 'X-Hash');
145+
$event = $this->getKernelResponseEvent($request, null, HttpKernelInterface::SUB_REQUEST);
146+
147+
$userContextSubscriber->onKernelResponse($event);
148+
149+
$this->assertFalse($event->getResponse()->headers->has('Vary'));
150+
}
151+
106152
/**
107153
* If there is no hash in the request, vary on the user identifier.
108154
*/
@@ -122,21 +168,21 @@ public function testOnKernelResponseNotCached()
122168
$this->assertEquals('X-SessionId', $event->getResponse()->headers->get('Vary'));
123169
}
124170

125-
protected function getKernelRequestEvent(Request $request)
171+
protected function getKernelRequestEvent(Request $request, $type = HttpKernelInterface::MASTER_REQUEST)
126172
{
127173
return new GetResponseEvent(
128174
\Mockery::mock('\Symfony\Component\HttpKernel\HttpKernelInterface'),
129175
$request,
130-
HttpKernelInterface::MASTER_REQUEST
176+
$type
131177
);
132178
}
133179

134-
protected function getKernelResponseEvent(Request $request, Response $response = null)
180+
protected function getKernelResponseEvent(Request $request, Response $response = null, $type = HttpKernelInterface::MASTER_REQUEST)
135181
{
136182
return new FilterResponseEvent(
137183
\Mockery::mock('\Symfony\Component\HttpKernel\HttpKernelInterface'),
138184
$request,
139-
HttpKernelInterface::MASTER_REQUEST,
185+
$type,
140186
$response ?: new Response()
141187
);
142188
}

Tests/Unit/UserContext/RoleProviderTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,13 @@ public function testProviderWithoutToken()
5050

5151
$this->assertEmpty($userContext->getParameters());
5252
}
53+
54+
/**
55+
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
56+
*/
57+
public function testNotUnderFirewall()
58+
{
59+
$roleProvider = new RoleProvider();
60+
$roleProvider->updateUserContext(new UserContext());
61+
}
5362
}

0 commit comments

Comments
 (0)