-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathInjectionMiddlewareTest.php
More file actions
235 lines (223 loc) · 7.21 KB
/
InjectionMiddlewareTest.php
File metadata and controls
235 lines (223 loc) · 7.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
declare(strict_types=1);
namespace OCA\Libresign\Tests\Unit\Middleware;
use OC\AppFramework\Bootstrap\Coordinator;
use OC\AppFramework\Services\InitialState;
use OC\InitialStateService;
use OCA\Libresign\Db\FileMapper;
use OCA\Libresign\Db\SignRequestMapper;
use OCA\Libresign\Exception\LibresignException;
use OCA\Libresign\Exception\PageException;
use OCA\Libresign\Handler\CertificateEngine\CertificateEngineFactory;
use OCA\Libresign\Helper\ValidateHelper;
use OCA\Libresign\Middleware\InjectionMiddleware;
use OCA\Libresign\Service\SignFileService;
use OCA\Libresign\Service\UuidResolverService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\RedirectResponse;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
use OCP\IAppConfig;
use OCP\IL10N;
use OCP\IRequest;
use OCP\IServerContainer;
use OCP\ISession;
use OCP\IURLGenerator;
use OCP\IUserSession;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
/**
* SPDX-FileCopyrightText: 2020-2024 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
final class InjectionMiddlewareTest extends \OCA\Libresign\Tests\Unit\TestCase {
private IRequest&MockObject $request;
private ISession&MockObject $session;
private IUserSession&MockObject $userSession;
private ValidateHelper&MockObject $validateHelper;
private SignRequestMapper&MockObject $signRequestMapper;
private CertificateEngineFactory $certificateEngineFactory;
private FileMapper&MockObject $fileMapper;
private IInitialState $initialState;
private SignFileService&MockObject $signFileService;
private UuidResolverService&MockObject $uuidResolverService;
private IL10N&MockObject $l10n;
private IappConfig&MockObject $appConfig;
private IurlGenerator&MockObject $urlGenerator;
private ?string $userId = null;
private InitialStateService $initialStateService;
public function setUp(): void {
$this->request = $this->createMock(IRequest::class);
$this->session = $this->createMock(ISession::class);
$this->userSession = $this->createMock(IUserSession::class);
$this->validateHelper = $this->createMock(ValidateHelper::class);
$this->signRequestMapper = $this->createMock(SignRequestMapper::class);
$this->certificateEngineFactory = $this->createMock(CertificateEngineFactory::class);
$this->fileMapper = $this->createMock(FileMapper::class);
$this->appConfig = $this->createMock(IAppConfig::class);
$this->urlGenerator = $this->createMock(IURLGenerator::class);
$this->initialStateService = new InitialStateService(
$this->createMock(LoggerInterface::class),
$this->createMock(Coordinator::class),
$this->createMock(IServerContainer::class)
);
$this->initialState = new InitialState($this->initialStateService, 'libresign');
$this->signFileService = $this->createMock(SignFileService::class);
$this->uuidResolverService = $this->createMock(UuidResolverService::class);
$this->l10n = $this->createMock(IL10N::class);
$this->userId = null;
}
public function getInjectionMiddleware(): InjectionMiddleware {
return new InjectionMiddleware(
$this->request,
$this->session,
$this->userSession,
$this->validateHelper,
$this->signRequestMapper,
$this->certificateEngineFactory,
$this->fileMapper,
$this->initialState,
$this->signFileService,
$this->uuidResolverService,
$this->l10n,
$this->appConfig,
$this->urlGenerator,
$this->userId,
);
}
/**
* @dataProvider providerAfterException
*/
public function testAfterException(string $message, int $code, string $exception, callable $expected): void {
$controller = $this->createMock(Controller::class);
$methodName = 'fake';
try {
throw new $exception($message, $code);
} catch (\Throwable $exception) {
}
if ($exception instanceof PageException) {
$this->request
->method('getHeader')
->with('Accept')
->willReturn('text/html');
}
$injectionMiddleware = $this->getInjectionMiddleware();
$actual = $injectionMiddleware->afterException($controller, $methodName, $exception);
$expected($this, $message, $code, $actual);
}
public static function providerAfterException(): array {
return [
[
json_encode(['action' => 1000]), 1, LibresignException::class,
function (self $self, $message, int $code, $actual):void {
/** @var JSONResponse $actual */
$self->assertInstanceOf(
JSONResponse::class,
$actual,
'The response need to be JSONResponse'
);
$self->assertJsonStringEqualsJsonString(
$message,
json_encode($actual->getData()),
'Invalid response json content'
);
$self->assertEquals(
$code,
$actual->getStatus(),
'Invalid response status code'
);
},
],
[
'a text here', 1, LibresignException::class,
function (self $self, $message, int $code, $actual):void {
/** @var JSONResponse $actual */
$self->assertInstanceOf(
JSONResponse::class,
$actual,
'The response need to be JSONResponse'
);
$self->assertJsonStringEqualsJsonString(
json_encode(['message' => $message]),
json_encode($actual->getData()),
'Invalid response json content'
);
$self->assertEquals(
$code,
$actual->getStatus(),
'Invalid response status code'
);
},
],
[
'a text here', 1, PageException::class,
function (self $self, $message, int $code, $actual):void {
/** @var TemplateResponse $actual */
$self->assertInstanceOf(
TemplateResponse::class,
$actual,
'The response need to be TemplateResponse'
);
$states = $self->initialStateService->getInitialStates();
$self->assertArrayHasKey('libresign-error', $states);
$self->assertJsonStringEqualsJsonString(
json_encode(['message' => $message]),
$states['libresign-error'],
'Invalid response params content'
);
$self->assertEquals(
$code,
$actual->getStatus(),
'Invalid response status code'
);
},
],
[
json_encode(['action' => 1000]), 1, PageException::class,
function (self $self, $message, int $code, $actual):void {
/** @var TemplateResponse $actual */
$self->assertInstanceOf(
TemplateResponse::class,
$actual,
'The response need to be TemplateResponse'
);
$states = $self->initialStateService->getInitialStates();
$self->assertJsonStringEqualsJsonString(
json_encode([
'libresign-action' => '1000',
]),
json_encode($states),
'Invalid response params content'
);
$self->assertEquals(
$code,
$actual->getStatus(),
'Invalid response status code'
);
},
],
[
json_encode(['action' => 1000, 'redirect' => 'http://fake.url']), 1, PageException::class,
function (self $self, $message, int $code, $actual):void {
/** @var RedirectResponse $actual */
$self->assertInstanceOf(
RedirectResponse::class,
$actual,
'The response need to be RedirectResponse'
);
$self->assertEquals(
'http://fake.url',
$actual->getRedirectURL(),
'Invalid redirect URL'
);
$self->assertEquals(
303,
$actual->getStatus(),
'Invalid response status code'
);
},
],
];
}
}