Completed
Push — master ( d5f109...3a301a )
by Charles
03:33
created

AuthenticationTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 22
dl 0
loc 39
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testError() 0 11 1
A testSuccessfulLogin() 0 23 3
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 getRequestBody(ServerRequestInterface $request) : string
36
    {
37
        return $request->getBody()->getContents();
38
    }
39
}
40
41
final class AuthenticationTest extends AbstractTest
42
{
43
    public function testSuccessfulLogin()
44
    {
45
        foreach ($this->testCases as $k => $params) {
46
            $auth = new Authorization($params[0], $params[1], $this->token, new DateTime, $params[2]);
47
            $response = Dispatcher::run([
48
                    new MockAuthentication,
49
                    function ($request, $next) {
50
                        $this->assertInstanceOf('\ncryptf\Token', $request->getAttribute('ncryptf-token'));
51
                        return $next->handle($request);
52
                    }
53
                ],
54
                Factory::createServerRequest($params[0], $params[1])
55
                    ->withHeader('Authorization', $auth->getHeader())
56
                    ->withBody((function() use ($params) {
57
                        $stream = fopen('php://memory','r+');
58
                        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

58
                        fwrite(/** @scrutinizer ignore-type */ $stream, \is_array($params[2]) ? \json_encode($params[2]): $params[2]);
Loading history...
59
                        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

59
                        rewind(/** @scrutinizer ignore-type */ $stream);
Loading history...
60
                        
61
                        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

61
                        return new \Zend\Diactoros\Stream(/** @scrutinizer ignore-type */ $stream);                            
Loading history...
62
                    })())
63
            );
64
    
65
            $this->assertSame(200, $response->getStatusCode());
66
        }
67
    }
68
69
    public function testError()
70
    {
71
        $auth = new Authorization('GET', '/api/v1/user/index', $this->token, new DateTime, '{"foo":"bar"}');
72
        $response = Dispatcher::run([
73
                new MockAuthentication
74
            ],
75
            Factory::createServerRequest('GET', '/api/v1/user/index')
76
                ->withHeader('Authorization', $auth->getHeader())
77
        );
78
79
        $this->assertSame(401, $response->getStatusCode());
80
    }
81
}
82