Test Failed
Push — master ( 42e257...dadfc2 )
by Charles
02:28
created

testEncryptedRequestWithPlaintextResponse()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 45
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 45
rs 9.424
c 0
b 0
f 0
cc 4
nc 2
nop 0
1
<?php declare(strict_types=1);
2
3
namespace ncryptf\Tests;
4
5
use DateTime;
6
use ncryptf\Authorization;
7
use ncryptf\Request;
8
use ncryptf\Token;
9
use ncryptf\middleware\NcryptfPayload;
10
use ncryptf\Tests\AbstractTest;
11
use ncryptf\Tests\mock\Authentication;
12
use ncryptf\Tests\mock\EncryptionKey;
13
14
use PHPUnit\Framework\TestCase;
15
16
use Middlewares\JsonPayload;
17
use Middlewares\Utils\Dispatcher;
18
use Middlewares\Utils\Factory;
19
20
use Psr\Http\Message\ResponseInterface;
21
use Psr\Http\Message\ServerRequestInterface;
22
use Psr\Http\Server\MiddlewareInterface;
23
use Psr\Http\Server\RequestHandlerInterface;
24
25
use WildWolf\Psr16MemoryCache;
26
27
final class AuthenticationTest extends AbstractTest
28
{
29
    public function testSuccessfulLogin()
30
    {
31
        foreach ($this->testCases as $k => $params) {
32
            $auth = new Authorization($params[0], $params[1], $this->token, new DateTime, $params[2]);
33
            $response = Dispatcher::run(
34
                [
35
                    new Authentication,
36
                    function ($request, $next) {
37
                        $this->assertInstanceOf('\ncryptf\Token', $request->getAttribute('ncryptf-token'));
38
                        $this->assertEquals(true, \is_array($request->getAttribute('ncryptf-user')));
39
                        return $next->handle($request);
40
                    }
41
                ],
42
                Factory::createServerRequest($params[0], $params[1])
43
                    ->withHeader('Authorization', $auth->getHeader())
44
                    ->withHeader('Content-Type', 'application/json')
45
                    ->withHeader('Accept', 'application/json')
46
                    ->withBody((function () use ($params) {
47
                        $stream = fopen('php://memory', 'r+');
48
                        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

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

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

50
                        return new \Zend\Diactoros\Stream(/** @scrutinizer ignore-type */ $stream);
Loading history...
51
                    })())
52
            );
53
54
            $this->assertSame(200, $response->getStatusCode());
55
        }
56
    }
57
58
    public function testEncryptedRequestWithPlaintextResponse()
59
    {
60
        foreach ($this->testCases as $k => $params) {
61
            $serverKey = EncryptionKey::generate();
62
            $myKey = EncryptionKey::generate();
63
            $cache = Psr16MemoryCache::instance();
64
            $cache->set($serverKey->getHashIdentifier(), $serverKey);
65
66
            $auth = new Authorization($params[0], $params[1], $this->token, new DateTime, $params[2]);
67
68
            $response = Dispatcher::run(
69
                [
70
                    new NcryptfPayload($cache),
71
                    new Authentication,
72
                    function ($request, $next) {
73
                        $this->assertInstanceOf('\ncryptf\Token', $request->getAttribute('ncryptf-token'));
74
                        $this->assertEquals(true, \is_array($request->getAttribute('ncryptf-user')));
75
                        return $next->handle($request);
76
                    }
77
                ],
78
                Factory::createServerRequest($params[0], $params[1])
79
                    ->withHeader('Authorization', $auth->getHeader())
80
                    ->withHeader('Content-Type', 'application/vnd.ncryptf+json')
81
                    ->withHeader('Accept', 'application/json')
82
                    ->withHeader('X-HashId', $serverKey->getHashIdentifier())
83
                    ->withBody((function () use ($params, $serverKey, $myKey) {
84
                        $data = \is_array($params[2]) ? \json_encode($params[2]): $params[2];
85
86
                        $request = new Request(
87
                            $myKey->getBoxSecretKey(),
88
                            $myKey->getSigningSecretKey()
89
                        );
90
91
                        $encryptedData = $request->encrypt(
92
                            $data,
93
                            $serverKey->getBoxPublicKey()
94
                        );
95
                        $stream = fopen('php://memory', 'r+');
96
                        fwrite($stream, $data === '' ? '' : \base64_encode($encryptedData));
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

96
                        fwrite(/** @scrutinizer ignore-type */ $stream, $data === '' ? '' : \base64_encode($encryptedData));
Loading history...
97
                        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

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

98
                        return new \Zend\Diactoros\Stream(/** @scrutinizer ignore-type */ $stream);
Loading history...
99
                    })())
100
            );
101
102
            $this->assertSame(200, $response->getStatusCode());
103
        }
104
    }
105
106
    public function testError()
107
    {
108
        $auth = new Authorization('GET', '/api/v1/user/index', $this->token, new DateTime, '{"foo":"bar"}');
109
        $response = Dispatcher::run(
110
            [
111
                new Authentication
112
            ],
113
            Factory::createServerRequest('GET', '/api/v1/user/index')
114
                ->withHeader('Authorization', $auth->getHeader())
115
                ->withHeader('Content-Type', 'application/json')
116
                ->withHeader('Accept', 'application/json')
117
        );
118
119
        $this->assertSame(401, $response->getStatusCode());
120
    }
121
}
122