Completed
Push — master ( d24783...e203a5 )
by Joschi
04:10
created

AuthenticatorTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 2
c 2
b 0
f 1
lcom 0
cbo 7
dl 0
loc 58
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setUpBeforeClass() 0 12 1
B testBearerToken() 0 36 1
1
<?php
2
3
/**
4
 * apparat-server
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Server
8
 * @subpackage  Apparat\Server\Tests
9
 * @author      Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright   Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license     http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Apparat\Server\Tests;
38
39
use Apparat\Dev\Tests\AbstractTest;
40
use Apparat\Server\Ports\Authenticator\Bearer;
41
use Apparat\Server\Ports\Facade\ServerFacade;
42
use Apparat\Server\Ports\Route\RouteFactory;
43
use Apparat\Server\Ports\View\TYPO3FluidView;
44
use Psr\Http\Message\ResponseInterface;
45
use Zend\Diactoros\ServerRequest;
46
use Zend\Diactoros\Uri;
47
48
/**
49
 * Authenticator test
50
 *
51
 * @package Apparat\Server
52
 * @subpackage Apparat\Server\Tests
53
 */
54
class AuthenticatorTest extends AbstractTest
55
{
56
    /**
57
     * This method is called before the first test of this test class is run.
58
     */
59
    public static function setUpBeforeClass()
60
    {
61
        parent::setUpBeforeClass();
62
63
        // Register custom view resources
64
        $noneRepoPath = __DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'non-repo'.DIRECTORY_SEPARATOR;
65
        ServerFacade::setViewResources([
66
            TYPO3FluidView::LAYOUTS => $noneRepoPath.'Layouts'.DIRECTORY_SEPARATOR,
67
            TYPO3FluidView::TEMPLATES => $noneRepoPath.'Templates'.DIRECTORY_SEPARATOR,
68
            TYPO3FluidView::PARTIALS => $noneRepoPath.'Partials'.DIRECTORY_SEPARATOR,
69
        ]);
70
    }
71
72
    /**
73
     * Test the bearer token
74
     */
75
    public function testBearerToken()
76
    {
77
        $bearerToken = md5(microtime(true));
78
        $bearerAuthenticator = new Bearer(function ($currentToken) use ($bearerToken) {
79
            return $currentToken === $bearerToken;
80
        });
81
82
        //  Register a static route and add the bearer token authenticator
83
        $bearerRoute = RouteFactory::createStaticRoute('/bearer', 'Test/Bearer');
84
        $bearerRoute->setAuth([$bearerAuthenticator]);
85
        ServerFacade::registerRoute($bearerRoute);
86
87
        // Test authorization header
88
        $uri = new Uri('http://apparat/blog/bearer');
89
        $request = new ServerRequest();
90
        $request = $request->withUri($uri)->withAddedHeader('Authorization', 'Bearer '.$bearerToken);
91
        $response = ServerFacade::dispatchRequest($request);
92
        $this->assertInstanceOf(ResponseInterface::class, $response);
93
        $this->assertEquals('[(bearer)]', trim($response->getBody()));
94
95
        // Test "access_token" body parameter
96
        $uri = new Uri('http://apparat/blog/bearer');
97
        $request = new ServerRequest();
98
        $request = $request->withUri($uri)->withParsedBody(['access_token' => $bearerToken]);
99
        $response = ServerFacade::dispatchRequest($request);
100
        $this->assertInstanceOf(ResponseInterface::class, $response);
101
        $this->assertEquals('[(bearer)]', trim($response->getBody()));
102
103
        // Test "access_token" query parameter
104
        $uri = new Uri('http://apparat/blog/bearer');
105
        $request = new ServerRequest();
106
        $request = $request->withUri($uri)->withQueryParams(['access_token' => $bearerToken]);
107
        $response = ServerFacade::dispatchRequest($request);
108
        $this->assertInstanceOf(ResponseInterface::class, $response);
109
        $this->assertEquals('[(bearer)]', trim($response->getBody()));
110
    }
111
}
112