Skip to content

Commit 58c84eb

Browse files
committed
Improve test coverage
1 parent 44defe7 commit 58c84eb

File tree

3 files changed

+115
-6
lines changed

3 files changed

+115
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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\AuthenticationResult\Transport\Authentication\AuthenticationTransport;
13+
use ActiveCollab\Authentication\AuthenticationResult\Transport\Authorization\AuthorizationTransport;
14+
use ActiveCollab\Authentication\Middleware\ApplyAuthenticationMiddleware;
15+
use ActiveCollab\Authentication\Test\AuthenticatedUser\AuthenticatedUser;
16+
use ActiveCollab\Authentication\Test\AuthenticatedUser\Repository as UserRepository;
17+
use ActiveCollab\Authentication\Test\Session\Repository as SessionRepository;
18+
use ActiveCollab\Authentication\Test\Session\Session;
19+
use ActiveCollab\Authentication\Test\TestCase\RequestResponseTestCase;
20+
use ActiveCollab\Cookies\Adapter\Adapter;
21+
use ActiveCollab\Cookies\Cookies;
22+
use ActiveCollab\Cookies\CookiesInterface;
23+
use Psr\Http\Message\ResponseInterface;
24+
use Psr\Http\Message\ServerRequestInterface;
25+
26+
/**
27+
* @package ActiveCollab\Authentication\Test
28+
*/
29+
class ApplyAuthenticationMiddlewareTest extends RequestResponseTestCase
30+
{
31+
/**
32+
* @var CookiesInterface
33+
*/
34+
private $cookies;
35+
36+
/**
37+
* {@inheritdoc}
38+
*/
39+
public function setUp()
40+
{
41+
parent::setUp();
42+
43+
$this->cookies = new Cookies(new Adapter());
44+
}
45+
46+
/**
47+
* Test that user is authenticated.
48+
*/
49+
public function testUserIsAuthenticated()
50+
{
51+
$user = new AuthenticatedUser(1, '[email protected]', 'Ilija Studen', '123');
52+
$user_repository = new UserRepository([
53+
'[email protected]' => new AuthenticatedUser(1, '[email protected]', 'Ilija Studen', '123'),
54+
]);
55+
$session_repository = new SessionRepository([new Session('my-session-id', '[email protected]')]);
56+
57+
$session_cookie_name = 'test-session-cookie';
58+
59+
$session_adapter = new BrowserSessionAdapter($user_repository, $session_repository, $this->cookies, $session_cookie_name);
60+
$session = $session_adapter->authenticate($user, []);
61+
62+
/** @var ServerRequestInterface $request */
63+
$request = $this->request->withAttribute('test_transport', new AuthorizationTransport($session_adapter, $user, $session, [1, 2, 3]));
64+
65+
/** @var ResponseInterface $response */
66+
$response = call_user_func(new ApplyAuthenticationMiddleware('test_transport'), $request, $this->response);
67+
68+
$this->assertInstanceOf(ResponseInterface::class, $response);
69+
70+
$set_cookie_header = $response->getHeaderLine('Set-Cookie');
71+
72+
$this->assertNotEmpty($set_cookie_header);
73+
$this->assertContains($session_cookie_name, $set_cookie_header);
74+
$this->assertContains('my-session-id', $set_cookie_header);
75+
}
76+
}

test/src/GoogleAuthorizerTest.php

+7-6
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function setUp()
3434

3535
/**
3636
* @dataProvider providerInvalidCredentials
37-
* @expectedException ActiveCollab\Authentication\Exception\InvalidAuthenticationRequestException
37+
* @expectedException \ActiveCollab\Authentication\Exception\InvalidAuthenticationRequestException
3838
* @expectedExceptionMessage Authentication request data not valid
3939
*/
4040
public function testInvalidCredentialsThrowsException($credentials)
@@ -55,7 +55,7 @@ public function providerInvalidCredentials()
5555
}
5656

5757
/**
58-
* @expectedException RuntimeException
58+
* @expectedException \RuntimeException
5959
* @expectedExceptionMessage Unrecognized google_client
6060
*/
6161
public function testExceptionIsThrownForInvalidAud()
@@ -68,7 +68,7 @@ public function testExceptionIsThrownForInvalidAud()
6868
}
6969

7070
/**
71-
* @expectedException RuntimeException
71+
* @expectedException \RuntimeException
7272
* @expectedExceptionMessage Wrong issuer
7373
*/
7474
public function testExceptionIsThrownForInvalidIss()
@@ -83,7 +83,7 @@ public function testExceptionIsThrownForInvalidIss()
8383
}
8484

8585
/**
86-
* @expectedException RuntimeException
86+
* @expectedException \RuntimeException
8787
* @expectedExceptionMessage Email is not verified by Google
8888
*/
8989
public function testExceptionIsThrownForInvalidUsername()
@@ -98,7 +98,7 @@ public function testExceptionIsThrownForInvalidUsername()
9898
}
9999

100100
/**
101-
* @expectedException ActiveCollab\Authentication\Exception\UserNotFoundException
101+
* @expectedException \ActiveCollab\Authentication\Exception\UserNotFoundException
102102
* @expectedExceptionMessage User not found
103103
*/
104104
public function testExceptionIsThrownForNotFoundUser()
@@ -116,7 +116,7 @@ public function testExceptionIsThrownForNotFoundUser()
116116
}
117117

118118
/**
119-
* @expectedException ActiveCollab\Authentication\Exception\UserNotFoundException
119+
* @expectedException \ActiveCollab\Authentication\Exception\UserNotFoundException
120120
* @expectedExceptionMessage User not found
121121
*/
122122
public function testExceptionIsThrownForNotAuthenticatedUser()
@@ -157,6 +157,7 @@ public function testUserIsFoundAndVerified()
157157
$user = $google_authorizer->verifyCredentials(['token' => '123abacu', 'username' => '[email protected]']);
158158

159159
$this->assertSame(1, $user->getId());
160+
$this->assertSame($payload, $google_authorizer->getUserProfile());
160161
}
161162

162163
private function ensureTokenVerificationResult($token, $results)

test/src/RequestProcessorTest.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace ActiveCollab\Authentication\Test;
4+
5+
use ActiveCollab\Authentication\Authorizer\RequestProcessor\RequestProcessingResult\RequestProcessingResult;
6+
use ActiveCollab\Authentication\Test\TestCase\TestCase;
7+
8+
/**
9+
* @package ActiveCollab\Authentication\Test
10+
*/
11+
class RequestProcessorTest extends TestCase
12+
{
13+
public function testDefaultPayloadIsNull()
14+
{
15+
$this->assertNull((new RequestProcessingResult([]))->getDefaultPayload());
16+
}
17+
18+
public function testConstructRequestProcessingResult()
19+
{
20+
$credentials = [
21+
'username' => 'me',
22+
'password' => 'hard to guess, easy to remember',
23+
];
24+
25+
$payload = [1, 2, 3];
26+
27+
$result = new RequestProcessingResult($credentials, $payload);
28+
29+
$this->assertSame($credentials, $result->getCredentials());
30+
$this->assertSame($payload, $result->getDefaultPayload());
31+
}
32+
}

0 commit comments

Comments
 (0)