Skip to content

Commit e8bd267

Browse files
committed
Add authentication middleware tests
1 parent 58c84eb commit e8bd267

7 files changed

+245
-42
lines changed

src/Authentication.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
use ActiveCollab\Authentication\Authorizer\AuthorizerInterface;
1919
use ActiveCollab\Authentication\Exception\InvalidAuthenticationRequestException;
2020
use Exception;
21+
use LogicException;
2122
use Psr\Http\Message\ResponseInterface;
2223
use Psr\Http\Message\ServerRequestInterface;
23-
use RuntimeException;
2424

2525
/**
2626
* @package ActiveCollab\Authentication
@@ -56,7 +56,7 @@ public function __construct(array $adapters)
5656
{
5757
foreach ($adapters as $adapter) {
5858
if (!($adapter instanceof AdapterInterface)) {
59-
throw new RuntimeException('Invalid object type provided');
59+
throw new LogicException('Invalid authentication adapter provided');
6060
}
6161
}
6262

test/src/ApplyAuthenticationMiddlewareTest.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
namespace ActiveCollab\Authentication\Test;
1010

1111
use ActiveCollab\Authentication\Adapter\BrowserSessionAdapter;
12-
use ActiveCollab\Authentication\AuthenticationResult\Transport\Authentication\AuthenticationTransport;
1312
use ActiveCollab\Authentication\AuthenticationResult\Transport\Authorization\AuthorizationTransport;
1413
use ActiveCollab\Authentication\Middleware\ApplyAuthenticationMiddleware;
1514
use ActiveCollab\Authentication\Test\AuthenticatedUser\AuthenticatedUser;
@@ -44,7 +43,7 @@ public function setUp()
4443
}
4544

4645
/**
47-
* Test that user is authenticated.
46+
* Test if authentication is applied based on request attribute.
4847
*/
4948
public function testUserIsAuthenticated()
5049
{

test/src/ApplyTransportTest.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@
88

99
namespace ActiveCollab\Authentication\Test;
1010

11+
use ActiveCollab\Authentication\Adapter\TokenBearerAdapter;
1112
use ActiveCollab\Authentication\AuthenticatedUser\AuthenticatedUserInterface;
1213
use ActiveCollab\Authentication\AuthenticationResult\Transport\Authentication\AuthenticationTransport;
1314
use ActiveCollab\Authentication\AuthenticationResult\Transport\Authorization\AuthorizationTransport;
1415
use ActiveCollab\Authentication\AuthenticationResult\Transport\CleanUp\CleanUpTransport;
1516
use ActiveCollab\Authentication\AuthenticationResult\Transport\Deauthentication\DeauthenticationTransport;
16-
use ActiveCollab\Authentication\Test\TestCase\RequestResponseTestCase;
1717
use ActiveCollab\Authentication\Test\AuthenticatedUser\AuthenticatedUser;
1818
use ActiveCollab\Authentication\Test\AuthenticatedUser\Repository as UserRepository;
19+
use ActiveCollab\Authentication\Test\TestCase\RequestResponseTestCase;
1920
use ActiveCollab\Authentication\Test\Token\Repository as TokenRepository;
2021
use ActiveCollab\Authentication\Test\Token\Token;
21-
use ActiveCollab\Authentication\Adapter\TokenBearerAdapter;
2222
use Psr\Http\Message\ResponseInterface;
2323
use Psr\Http\Message\ServerRequestInterface;
2424

@@ -79,7 +79,7 @@ public function testAuthenticationTransportSetsAttributes()
7979

8080
/** @var ServerRequestInterface $request */
8181
/** @var ResponseInterface $response */
82-
list ($request, $response) = $transport->applyTo($this->request, $this->response);
82+
list($request, $response) = $transport->applyTo($this->request, $this->response);
8383
$this->assertInstanceOf(ServerRequestInterface::class, $request);
8484
$this->assertInstanceOf(ResponseInterface::class, $response);
8585

@@ -98,7 +98,7 @@ public function testAuthorizationTransportSetsAttributes()
9898

9999
/** @var ServerRequestInterface $request */
100100
/** @var ResponseInterface $response */
101-
list ($request, $response) = $transport->applyTo($this->request, $this->response);
101+
list($request, $response) = $transport->applyTo($this->request, $this->response);
102102
$this->assertInstanceOf(ServerRequestInterface::class, $request);
103103
$this->assertInstanceOf(ResponseInterface::class, $response);
104104

@@ -119,7 +119,7 @@ public function testDeauthenticationTransportDoesNotSetAnyNewAttributes()
119119

120120
/** @var ServerRequestInterface $request */
121121
/** @var ResponseInterface $response */
122-
list ($request, $response) = $transport->applyTo($this->request, $this->response);
122+
list($request, $response) = $transport->applyTo($this->request, $this->response);
123123
$this->assertInstanceOf(ServerRequestInterface::class, $request);
124124
$this->assertInstanceOf(ResponseInterface::class, $response);
125125

@@ -138,7 +138,7 @@ public function testCleanupTransportDoesNotSetAnyNewAttributes()
138138

139139
/** @var ServerRequestInterface $request */
140140
/** @var ResponseInterface $response */
141-
list ($request, $response) = $transport->applyTo($this->request, $this->response);
141+
list($request, $response) = $transport->applyTo($this->request, $this->response);
142142
$this->assertInstanceOf(ServerRequestInterface::class, $request);
143143
$this->assertInstanceOf(ResponseInterface::class, $response);
144144

+226
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Active Collab Authentication project.
5+
*
6+
* (c) A51 doo <[email protected]>. All rights reserved.
7+
*/
8+
9+
namespace ActiveCollab\Authentication\Test;
10+
11+
use ActiveCollab\Authentication\Adapter\BrowserSessionAdapter;
12+
use ActiveCollab\Authentication\Adapter\TokenBearerAdapter;
13+
use ActiveCollab\Authentication\AuthenticatedUser\AuthenticatedUserInterface;
14+
use ActiveCollab\Authentication\AuthenticatedUser\RepositoryInterface;
15+
use ActiveCollab\Authentication\Authentication;
16+
use ActiveCollab\Authentication\Session\SessionInterface;
17+
use ActiveCollab\Authentication\Test\AuthenticatedUser\AuthenticatedUser;
18+
use ActiveCollab\Authentication\Test\AuthenticatedUser\Repository as UserRepository;
19+
use ActiveCollab\Authentication\Test\Session\Repository as SessionRepository;
20+
use ActiveCollab\Authentication\Test\Session\Session;
21+
use ActiveCollab\Authentication\Test\TestCase\RequestResponseTestCase;
22+
use ActiveCollab\Authentication\Test\Token\Repository as TokenRepository;
23+
use ActiveCollab\Authentication\Test\Token\Token;
24+
use ActiveCollab\Authentication\Token\TokenInterface;
25+
use ActiveCollab\Cookies\Adapter\Adapter;
26+
use ActiveCollab\Cookies\Cookies;
27+
use ActiveCollab\Cookies\CookiesInterface;
28+
use Psr\Http\Message\ResponseInterface;
29+
use Psr\Http\Message\ServerRequestInterface;
30+
31+
/**
32+
* @package ActiveCollab\Authentication\Test
33+
*/
34+
class AuthenticationMiddlewareTest extends RequestResponseTestCase
35+
{
36+
/**
37+
* @var CookiesInterface
38+
*/
39+
private $cookies;
40+
41+
/**
42+
* @var AuthenticatedUserInterface
43+
*/
44+
private $user;
45+
46+
/**
47+
* @var RepositoryInterface
48+
*/
49+
private $user_repository;
50+
51+
/**
52+
* @var \ActiveCollab\Authentication\Session\RepositoryInterface
53+
*/
54+
private $session_repository;
55+
56+
/**
57+
* @var string
58+
*/
59+
private $browser_session_cookie_name = 'test-session-cookie';
60+
61+
/**
62+
* @var BrowserSessionAdapter
63+
*/
64+
private $browser_session_adapter;
65+
66+
/**
67+
* @var TokenRepository
68+
*/
69+
private $token_repository;
70+
71+
/**
72+
* @var TokenBearerAdapter
73+
*/
74+
private $token_bearer_adapter;
75+
76+
/**
77+
* {@inheritdoc}
78+
*/
79+
public function setUp()
80+
{
81+
parent::setUp();
82+
83+
$this->cookies = new Cookies(new Adapter());
84+
85+
$this->user = new AuthenticatedUser(1, '[email protected]', 'Ilija Studen', '123');
86+
$this->user_repository = new UserRepository([
87+
'[email protected]' => new AuthenticatedUser(1, '[email protected]', 'Ilija Studen', '123'),
88+
]);
89+
90+
$this->session_repository = new SessionRepository([new Session('my-session-id', '[email protected]')]);
91+
$this->browser_session_adapter = new BrowserSessionAdapter($this->user_repository, $this->session_repository, $this->cookies, $this->browser_session_cookie_name);
92+
93+
$this->token_repository = new TokenRepository(['awesome-token' => new Token('awesome-token', '[email protected]')]);
94+
$this->token_bearer_adapter = new TokenBearerAdapter($this->user_repository, $this->token_repository);
95+
}
96+
97+
/**
98+
* @expectedException \LogicException
99+
* @expectedExceptionMessage Invalid authentication adapter provided
100+
*/
101+
public function testExceptionIfInvalidAdaptersAreSet()
102+
{
103+
new Authentication([new \stdClass()]);
104+
}
105+
106+
public function testMiddlewareAcceptsMultipleAdapters()
107+
{
108+
$middleware = new Authentication([$this->browser_session_adapter, $this->token_bearer_adapter]);
109+
110+
$this->assertInternalType('array', $middleware->getAdapters());
111+
$this->assertCount(2, $middleware->getAdapters());
112+
}
113+
114+
/**
115+
* Test that user is authenticated.
116+
*/
117+
public function testBrowserSessionAuthentication()
118+
{
119+
/** @var ServerRequestInterface $request */
120+
/** @var ResponseInterface $response */
121+
list($request, $response) = $this->cookies->set($this->request, $this->response, $this->browser_session_cookie_name, 'my-session-id');
122+
123+
$middleware = new Authentication([$this->browser_session_adapter]);
124+
125+
/** @var ServerRequestInterface $modified_request */
126+
$modified_request = null;
127+
128+
/** @var ResponseInterface $response */
129+
$response = call_user_func($middleware, $request, $response, function (ServerRequestInterface $request, ResponseInterface $response, callable $next = null) use (&$modified_request) {
130+
$modified_request = $request;
131+
132+
if ($next) {
133+
$response = $next($request, $response);
134+
}
135+
136+
return $response;
137+
});
138+
139+
$this->assertInstanceOf(ServerRequestInterface::class, $modified_request);
140+
$this->assertInstanceOf(ResponseInterface::class, $response);
141+
142+
// Test if authentication attributes are set
143+
$this->assertArrayHasKey('authentication_adapter', $modified_request->getAttributes());
144+
$this->assertArrayHasKey('authenticated_user', $modified_request->getAttributes());
145+
$this->assertArrayHasKey('authenticated_with', $modified_request->getAttributes());
146+
147+
// Test if session cookie is set
148+
$set_cookie_header = $response->getHeaderLine('Set-Cookie');
149+
150+
$this->assertNotEmpty($set_cookie_header);
151+
$this->assertContains($this->browser_session_cookie_name, $set_cookie_header);
152+
$this->assertContains('my-session-id', $set_cookie_header);
153+
154+
$this->assertInstanceOf(AuthenticatedUserInterface::class, $middleware->getAuthenticatedUser());
155+
$this->assertInstanceOf(SessionInterface::class, $middleware->getAuthenticatedWith());
156+
}
157+
158+
public function testTokenBearerAuthentication()
159+
{
160+
/** @var ServerRequestInterface $request */
161+
$request = $this->request->withHeader('Authorization', 'Bearer awesome-token');
162+
163+
$middleware = new Authentication([$this->token_bearer_adapter]);
164+
165+
/** @var ServerRequestInterface $modified_request */
166+
$modified_request = null;
167+
168+
$response = call_user_func($middleware, $request, $this->response, function (ServerRequestInterface $request, ResponseInterface $response, callable $next = null) use (&$modified_request) {
169+
$modified_request = $request;
170+
171+
if ($next) {
172+
$response = $next($request, $response);
173+
}
174+
175+
return $response;
176+
});
177+
178+
$this->assertInstanceOf(ServerRequestInterface::class, $modified_request);
179+
$this->assertInstanceOf(ResponseInterface::class, $response);
180+
181+
// Test if authentication attributes are set
182+
$this->assertArrayHasKey('authentication_adapter', $modified_request->getAttributes());
183+
$this->assertArrayHasKey('authenticated_user', $modified_request->getAttributes());
184+
$this->assertArrayHasKey('authenticated_with', $modified_request->getAttributes());
185+
186+
// Test if session cookie is set
187+
$set_cookie_header = $response->getHeaderLine('Set-Cookie');
188+
$this->assertEmpty($set_cookie_header);
189+
190+
$this->assertInstanceOf(AuthenticatedUserInterface::class, $middleware->getAuthenticatedUser());
191+
$this->assertInstanceOf(TokenInterface::class, $middleware->getAuthenticatedWith());
192+
}
193+
194+
/**
195+
* @expectedException \ActiveCollab\Authentication\Exception\InvalidAuthenticationRequestException
196+
* @expectedExceptionMessage You can not be authenticated with more than one authentication method
197+
*/
198+
public function testExceptionOnMultipleIds()
199+
{
200+
/** @var ServerRequestInterface $request */
201+
/** @var ResponseInterface $response */
202+
list($request, $response) = $this->cookies->set($this->request, $this->response, $this->browser_session_cookie_name, 'my-session-id');
203+
204+
/** @var ServerRequestInterface $request */
205+
$request = $request->withHeader('Authorization', 'Bearer awesome-token');
206+
207+
call_user_func(new Authentication([$this->browser_session_adapter, $this->token_bearer_adapter]), $request, $response);
208+
}
209+
210+
public function testOnAuthenticatedUserCallback()
211+
{
212+
/** @var ServerRequestInterface $request */
213+
$request = $this->request->withHeader('Authorization', 'Bearer awesome-token');
214+
215+
$middleware = new Authentication([$this->token_bearer_adapter]);
216+
217+
$callback_is_called = false;
218+
$middleware->setOnAuthenciatedUserChanged(function () use (&$callback_is_called) {
219+
$callback_is_called = true;
220+
});
221+
222+
call_user_func($middleware, $request, $this->response);
223+
224+
$this->assertTrue($callback_is_called);
225+
}
226+
}

test/src/AuthenticationTest.php

-25
This file was deleted.

test/src/RequestProcessorTest.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
<?php
22

3+
/*
4+
* This file is part of the Active Collab Authentication project.
5+
*
6+
* (c) A51 doo <[email protected]>. All rights reserved.
7+
*/
8+
39
namespace ActiveCollab\Authentication\Test;
410

511
use ActiveCollab\Authentication\Authorizer\RequestProcessor\RequestProcessingResult\RequestProcessingResult;
@@ -29,4 +35,4 @@ public function testConstructRequestProcessingResult()
2935
$this->assertSame($credentials, $result->getCredentials());
3036
$this->assertSame($payload, $result->getDefaultPayload());
3137
}
32-
}
38+
}

test/src/TokenBearerAdapterApplyTest.php

+3-6
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,14 @@
88

99
namespace ActiveCollab\Authentication\Test;
1010

11+
use ActiveCollab\Authentication\Adapter\TokenBearerAdapter;
1112
use ActiveCollab\Authentication\AuthenticatedUser\AuthenticatedUserInterface;
1213
use ActiveCollab\Authentication\AuthenticationResult\Transport\Authentication\AuthenticationTransport;
13-
use ActiveCollab\Authentication\AuthenticationResult\Transport\Authorization\AuthorizationTransport;
14-
use ActiveCollab\Authentication\AuthenticationResult\Transport\CleanUp\CleanUpTransport;
15-
use ActiveCollab\Authentication\AuthenticationResult\Transport\Deauthentication\DeauthenticationTransport;
16-
use ActiveCollab\Authentication\Test\TestCase\RequestResponseTestCase;
1714
use ActiveCollab\Authentication\Test\AuthenticatedUser\AuthenticatedUser;
1815
use ActiveCollab\Authentication\Test\AuthenticatedUser\Repository as UserRepository;
16+
use ActiveCollab\Authentication\Test\TestCase\RequestResponseTestCase;
1917
use ActiveCollab\Authentication\Test\Token\Repository as TokenRepository;
2018
use ActiveCollab\Authentication\Test\Token\Token;
21-
use ActiveCollab\Authentication\Adapter\TokenBearerAdapter;
2219
use Psr\Http\Message\ResponseInterface;
2320
use Psr\Http\Message\ServerRequestInterface;
2421

@@ -75,7 +72,7 @@ public function testTokenBearerAdapterReturnsRequestAndResponse()
7572
{
7673
$transport = new AuthenticationTransport($this->token_bearer_adapter, $this->user, $this->token);
7774

78-
list ($request, $response) = $this->token_bearer_adapter->applyTo($this->request, $this->response, $transport);
75+
list($request, $response) = $this->token_bearer_adapter->applyTo($this->request, $this->response, $transport);
7976
$this->assertInstanceOf(ServerRequestInterface::class, $request);
8077
$this->assertInstanceOf(ResponseInterface::class, $response);
8178
}

0 commit comments

Comments
 (0)