Passed
Push — master ( b5e885...54d7bc )
by Charles
02:34
created

AuthenticationTest::testError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 17
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php declare(strict_types=1);
2
3
namespace ncryptf\Tests;
4
5
use DateTime;
6
use ncryptf\Token;
7
use ncryptf\Request;
8
use ncryptf\Response;
9
use ncryptf\Authorization;
10
use Middlewares\Utils\Factory;
11
use WildWolf\Psr16MemoryCache;
12
use ncryptf\Tests\AbstractTest;
13
use PHPUnit\Framework\TestCase;
14
15
use Middlewares\Utils\Dispatcher;
16
use ncryptf\Tests\mock\EchoResponse;
17
use ncryptf\Tests\mock\EncryptionKey;
18
use ncryptf\Tests\mock\Authentication;
19
use Psr\Http\Message\ResponseInterface;
20
use Psr\Http\Server\MiddlewareInterface;
21
use Psr\Http\Message\ServerRequestInterface;
22
use Psr\Http\Server\RequestHandlerInterface;
23
use Zend\Diactoros\Response\JsonResponse;
24
25
final class AuthenticationTest extends AbstractTest
26
{
27
    public function testSuccessfulLogin()
28
    {
29
        foreach ($this->testCases as $k => $params) {
30
            $auth = new Authorization($params[0], $params[1], $this->token, new DateTime, $params[2]);
31
            $response = Dispatcher::run(
32
                [
33
                    new Authentication,
34
                    function ($request, $next) {
35
                        $this->assertInstanceOf('\ncryptf\Token', $request->getAttribute('ncryptf-token'));
36
                        $this->assertEquals(true, \is_array($request->getAttribute('ncryptf-user')));
37
                        return $next->handle($request);
38
                    }
39
                ],
40
                Factory::createServerRequest($params[0], $params[1])
41
                    ->withHeader('Authorization', $auth->getHeader())
42
                    ->withHeader('Content-Type', 'application/json')
43
                    ->withHeader('Accept', 'application/json')
44
                    ->withBody((function () use ($params) {
45
                        $stream = fopen('php://memory', 'r+');
46
                        fwrite($stream, \is_array($params[2]) ? \json_encode($params[2]): $params[2]);
47
                        rewind($stream);
48
                        return new \Zend\Diactoros\Stream($stream);
49
                    })())
50
            );
51
52
            $this->assertSame(200, $response->getStatusCode());
53
        }
54
    }
55
56
    public function testError()
57
    {
58
        $auth = new Authorization('GET', '/api/v1/user/index', $this->token, new DateTime, '{"foo":"bar"}');
59
        $response = Dispatcher::run(
60
            [
61
                new Authentication,
62
                function ($request, $next) {
63
                    return new JsonResponse(['foo' => 'bar']);
64
                }
65
            ],
66
            Factory::createServerRequest('GET', '/api/v1/user/index')
67
                ->withHeader('Authorization', $auth->getHeader())
68
                ->withHeader('Content-Type', 'application/json')
69
                ->withHeader('Accept', 'application/json')
70
        );
71
72
        $this->assertSame(401, $response->getStatusCode());
73
    }
74
}
75