Passed
Push — master ( 3a301a...889d89 )
by Charles
02:26
created

MockAuthentication::getTokenFromAccessToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php declare(strict_types=1);
2
3
namespace ncryptf\Tests;
4
5
use DateTime;
6
use ncryptf\Authorization;
7
use ncryptf\Token;
8
use ncryptf\middleware\AbstractAuthentication;
9
use ncryptf\Tests\AbstractTest;
10
11
use PHPUnit\Framework\TestCase;
12
13
use Middlewares\Utils\Dispatcher;
14
use Middlewares\Utils\Factory;
15
16
use Psr\Http\Message\ResponseInterface;
17
use Psr\Http\Message\ServerRequestInterface;
18
use Psr\Http\Server\MiddlewareInterface;
19
use Psr\Http\Server\RequestHandlerInterface;
20
21
final class MockAuthentication extends AbstractAuthentication
22
{
23
    protected function getTokenFromAccessToken(string $accessToken) :? Token
24
    {
25
        // Return a fixed token
26
        return new Token(
27
            'x2gMeJ5Np0CcKpZav+i9iiXeQBtaYMQ/yeEtcOgY3J',
28
            'LRSEe5zHb1aq20Hr9te2sQF8sLReSkO8bS1eD/9LDM8',
29
            \base64_decode('f2mTaH9vkZZQyF7SxVeXDlOSDbVwjUzhdXv2T/YYO8k='),
30
            \base64_decode('waWBMawHD1zpAFRcX7e45L1aqsA3mEeSOwXqq4l1i3I='),
31
            \strtotime('+4 hours')
32
        );
33
    }
34
    
35
    protected function getUserFromToken(Token $token)
36
    {
37
        return [
38
            'id' => 1
39
        ];
40
    }
41
42
    protected function getRequestBody(ServerRequestInterface $request) : string
43
    {
44
        // Return the raw requestbody
45
        return $request->getBody()->getContents();
46
    }
47
}
48
49
final class AuthenticationTest extends AbstractTest
50
{
51
    public function testSuccessfulLogin()
52
    {
53
        foreach ($this->testCases as $k => $params) {
54
            $auth = new Authorization($params[0], $params[1], $this->token, new DateTime, $params[2]);
55
            $response = Dispatcher::run([
56
                    new MockAuthentication,
57
                    function ($request, $next) {
58
                        $this->assertInstanceOf('\ncryptf\Token', $request->getAttribute('ncryptf-token'));
59
                        $this->assertEquals(true, \is_array($request->getAttribute('ncryptf-user')));
60
                        return $next->handle($request);
61
                    }
62
                ],
63
                Factory::createServerRequest($params[0], $params[1])
64
                    ->withHeader('Authorization', $auth->getHeader())
65
                    ->withBody((function() use ($params) {
66
                        $stream = fopen('php://memory','r+');
67
                        fwrite($stream, \is_array($params[2]) ? \json_encode($params[2]): $params[2]);
0 ignored issues
show
Bug introduced by
It seems like $stream can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

67
                        fwrite(/** @scrutinizer ignore-type */ $stream, \is_array($params[2]) ? \json_encode($params[2]): $params[2]);
Loading history...
68
                        rewind($stream);
0 ignored issues
show
Bug introduced by
It seems like $stream can also be of type false; however, parameter $handle of rewind() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

68
                        rewind(/** @scrutinizer ignore-type */ $stream);
Loading history...
69
                        
70
                        return new \Zend\Diactoros\Stream($stream);                            
0 ignored issues
show
Bug introduced by
It seems like $stream can also be of type false; however, parameter $stream of Zend\Diactoros\Stream::__construct() does only seem to accept resource|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
                        return new \Zend\Diactoros\Stream(/** @scrutinizer ignore-type */ $stream);                            
Loading history...
71
                    })())
72
            );
73
    
74
            $this->assertSame(200, $response->getStatusCode());
75
        }
76
    }
77
78
    public function testError()
79
    {
80
        $auth = new Authorization('GET', '/api/v1/user/index', $this->token, new DateTime, '{"foo":"bar"}');
81
        $response = Dispatcher::run([
82
                new MockAuthentication
83
            ],
84
            Factory::createServerRequest('GET', '/api/v1/user/index')
85
                ->withHeader('Authorization', $auth->getHeader())
86
        );
87
88
        $this->assertSame(401, $response->getStatusCode());
89
    }
90
}
91