testHandleRunsChecksIfNoEnvironmentNameIsSet()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 8
c 3
b 1
f 0
dl 0
loc 16
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Admin\Http\Middleware;
6
7
use AbterPhp\Admin\Config\Routes as RoutesConfig;
8
use AbterPhp\Framework\Constant\Env;
9
use AbterPhp\Framework\Environments\Environment;
10
use AbterPhp\Framework\Exception\Security as SecurityException;
11
use Opulence\Cache\ICacheBridge;
12
use Opulence\Http\Requests\Request;
13
use Opulence\Http\Responses\Response;
14
use PHPUnit\Framework\MockObject\MockObject;
15
use PHPUnit\Framework\TestCase;
16
17
class SecurityTest extends TestCase
18
{
19
    /** @var Security - System Under Test */
20
    protected Security $sut;
21
22
    /** @var MockObject|ICacheBridge */
23
    protected $cacheBridgeMock;
24
25
    /** @var MockObject|RoutesConfig */
26
    protected $routesConfigMock;
27
28
    public function setUp(): void
29
    {
30
        $this->cacheBridgeMock  = $this->createMock(ICacheBridge::class);
31
        $this->routesConfigMock = $this->createMock(RoutesConfig::class);
32
33
        $this->sut = new Security($this->cacheBridgeMock, $this->routesConfigMock);
34
    }
35
36
    public function testHandleRunsChecksIfNoEnvironmentNameIsSet()
37
    {
38
        Environment::setVar(Env::ENV_NAME, Environment::PRODUCTION);
39
40
        $this->cacheBridgeMock->expects($this->once())->method('has')->willReturn(true);
41
42
        $requestStub  = new Request([], [], [], [], [], [], null);
43
        $responseStub = new Response();
44
45
        $next = function () use ($responseStub) {
46
            return $responseStub;
47
        };
48
49
        $actualResult = $this->sut->handle($requestStub, $next);
50
51
        $this->assertSame($responseStub, $actualResult);
52
    }
53
54
    public function testHandleSkipsChecksIfNotInProduction()
55
    {
56
        Environment::setVar(Env::ENV_NAME, Environment::STAGING);
57
58
        $this->cacheBridgeMock->expects($this->never())->method('has');
59
60
        $env          = [
61
            Env::ENV_NAME => Environment::STAGING,
62
        ];
63
        $requestStub  = new Request([], [], [], [], [], $env, null);
64
        $responseStub = new Response();
65
66
67
        $next = function () use ($responseStub) {
68
            return $responseStub;
69
        };
70
71
        $actualResult = $this->sut->handle($requestStub, $next);
72
73
        $this->assertSame($responseStub, $actualResult);
74
    }
75
76
    public function testHandleRunsChecksIfInProduction()
77
    {
78
        Environment::setVar(Env::ENV_NAME, Environment::PRODUCTION);
79
80
        $this->cacheBridgeMock->expects($this->once())->method('has')->willReturn(true);
81
82
        $env          = [
83
            Env::ENV_NAME => Environment::PRODUCTION,
84
        ];
85
        $requestStub  = new Request([], [], [], [], [], $env, null);
86
        $responseStub = new Response();
87
88
        $next = function () use ($responseStub) {
89
            return $responseStub;
90
        };
91
92
        $actualResult = $this->sut->handle($requestStub, $next);
93
94
        $this->assertSame($responseStub, $actualResult);
95
    }
96
97
    /**
98
     * @return string[][]
99
     */
100
    public function checksThrowSecurityExceptionProvider(): array
101
    {
102
        return [
103
            [Security::TEST_LOGIN_PATH, '/bar', '/baz', 'quix'],
104
            ['/foo', Security::TEST_ADMIN_BASE_PATH, '/baz', 'quix'],
105
            ['/foo', '/bar', Security::TEST_API_BASE_PATH, 'quix'],
106
            ['/foo', '/bar', '/baz', Security::TEST_OAUTH2_PRIVATE_KEY_PASSWORD],
107
        ];
108
    }
109
110
    /**
111
     * @dataProvider checksThrowSecurityExceptionProvider
112
     *
113
     * @param string $loginPath
114
     * @param string $adminBasePath
115
     * @param string $apiBasePath
116
     * @param string $oauth2PrivateKeyPassword
117
     */
118
    public function testHandleChecksThrowSecurityExceptionOnFailure(
119
        string $loginPath,
120
        string $adminBasePath,
121
        string $apiBasePath,
122
        string $oauth2PrivateKeyPassword
123
    ) {
124
        Environment::setVar(Env::ENV_NAME, Environment::PRODUCTION);
125
126
        $this->expectException(SecurityException::class);
127
128
        $this->routesConfigMock->expects($this->any())->method('getLoginPath')->willReturn($loginPath);
129
        $this->routesConfigMock->expects($this->any())->method('getAdminBasePath')->willReturn($adminBasePath);
130
        $this->routesConfigMock->expects($this->any())->method('getApiBasePath')->willReturn($apiBasePath);
131
132
        $this->cacheBridgeMock->expects($this->once())->method('has')->willReturn(false);
133
134
        $env          = [
135
            Env::ENV_NAME                    => Environment::PRODUCTION,
136
            Env::OAUTH2_PRIVATE_KEY_PASSWORD => $oauth2PrivateKeyPassword,
137
        ];
138
        $requestStub  = new Request([], [], [], [], [], $env, null);
139
        $responseStub = new Response();
140
141
        $next = function () use ($responseStub) {
142
            return $responseStub;
143
        };
144
145
        $this->sut->handle($requestStub, $next);
146
    }
147
148
    public function testHandleSetsSessionIfChecksWereRun()
149
    {
150
        $loginPath                = '/foo';
151
        $adminBasePath            = '/bar';
152
        $apiBasePath              = '/baz';
153
        $oauth2PrivateKeyPassword = 'quix';
154
155
        $this->routesConfigMock->expects($this->any())->method('getLoginPath')->willReturn($loginPath);
156
        $this->routesConfigMock->expects($this->any())->method('getAdminBasePath')->willReturn($adminBasePath);
157
        $this->routesConfigMock->expects($this->any())->method('getApiBasePath')->willReturn($apiBasePath);
158
159
        $this->cacheBridgeMock->expects($this->any())->method('has')->willReturn(false);
160
        $this->cacheBridgeMock->expects($this->once())->method('set')->willReturn(true);
161
162
        $env          = [
163
            Env::ENV_NAME                    => Environment::PRODUCTION,
164
            Env::OAUTH2_PRIVATE_KEY_PASSWORD => $oauth2PrivateKeyPassword,
165
        ];
166
        $requestStub  = new Request([], [], [], [], [], $env, null);
167
        $responseStub = new Response();
168
169
        $next = function () use ($responseStub) {
170
            return $responseStub;
171
        };
172
173
        $actualResult = $this->sut->handle($requestStub, $next);
174
175
        $this->assertSame($responseStub, $actualResult);
176
    }
177
}
178