Skip to content

Commit 7445eda

Browse files
author
Verdict-as-a-Service Team
committed
More phpunit stuff
1 parent e5a7155 commit 7445eda

File tree

3 files changed

+154
-66
lines changed

3 files changed

+154
-66
lines changed

tests/integration/BaseIntegrationTest.php

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
use PHPUnit\Framework\TestCase;
1212
use RuntimeException;
1313

14-
abstract class BaseIntegrationTest extends TestCase {
14+
abstract class BaseIntegrationTest extends TestCase
15+
{
1516
protected string $hostname;
1617
protected string $folderPrefix;
1718
protected string $testUser;
@@ -24,7 +25,8 @@ abstract class BaseIntegrationTest extends TestCase {
2425

2526
protected static bool $setupCompleted = false;
2627

27-
protected function setUp(): void {
28+
protected function setUp(): void
29+
{
2830
parent::setUp();
2931

3032
// Load environment variables
@@ -44,7 +46,8 @@ protected function setUp(): void {
4446
}
4547
}
4648

47-
protected function setupEnvironment(): void {
49+
protected function setupEnvironment(): void
50+
{
4851
// Create temporary folder
4952
if (!is_dir($this->folderPrefix)) {
5053
mkdir($this->folderPrefix, 0755, true);
@@ -79,7 +82,8 @@ protected function setupEnvironment(): void {
7982
$this->executeDockerCommand('php occ app:enable gdatavaas');
8083
}
8184

82-
protected function executeDockerCommand(string $command, array $env = []): array {
85+
protected function executeDockerCommand(string $command, array $env = []): array
86+
{
8387
$envString = '';
8488
foreach ($env as $key => $value) {
8589
$envString .= " --env {$key}=\"{$value}\"";
@@ -96,7 +100,8 @@ protected function executeDockerCommand(string $command, array $env = []): array
96100
];
97101
}
98102

99-
protected function makeHttpRequest(string $method, string $url, array $options = []): array {
103+
protected function makeHttpRequest(string $method, string $url, array $options = []): array
104+
{
100105
$client = HttpClientBuilder::buildDefault();
101106
$request = new Request($url, $method);
102107

@@ -129,8 +134,6 @@ protected function makeHttpRequest(string $method, string $url, array $options =
129134
$request->setBody($options['file_handle']);
130135
}
131136

132-
var_dump($request);
133-
134137
try {
135138
$response = $client->request($request);
136139

@@ -148,7 +151,8 @@ protected function makeHttpRequest(string $method, string $url, array $options =
148151
}
149152
}
150153

151-
protected function uploadFileViaWebDAV(string $username, string $password, string $filename, string $content): array {
154+
protected function uploadFileViaWebDAV(string $username, string $password, string $filename, string $content): array
155+
{
152156
$url = "http://{$this->hostname}/remote.php/dav/files/{$username}/{$filename}";
153157

154158
return $this->makeHttpRequest('PUT', $url, [
@@ -158,15 +162,17 @@ protected function uploadFileViaWebDAV(string $username, string $password, strin
158162
]);
159163
}
160164

161-
protected function deleteFileViaWebDAV(string $username, string $password, string $filename): array {
165+
protected function deleteFileViaWebDAV(string $username, string $password, string $filename): array
166+
{
162167
$url = "http://{$this->hostname}/remote.php/dav/files/{$username}/{$filename}";
163168

164169
return $this->makeHttpRequest('DELETE', $url, [
165170
'auth' => ['username' => $username, 'password' => $password]
166171
]);
167172
}
168173

169-
protected function uploadFileFromDisk(string $username, string $password, string $filename, string $localPath): array {
174+
protected function uploadFileFromDisk(string $username, string $password, string $filename, string $localPath): array
175+
{
170176
if (!file_exists($localPath)) {
171177
throw new RuntimeException("File not found: {$localPath}");
172178
}
@@ -184,18 +190,19 @@ protected function uploadFileFromDisk(string $username, string $password, string
184190
]);
185191
}
186192

187-
protected function testGetEndpoint(string $endpoint, string $description, int $expectedHttpStatus = 200, string $username = 'admin', string $password = 'admin'): void {
193+
protected function testGetEndpoint(string $endpoint, string $description, int $expectedHttpStatus = 200, string $username = 'admin', string $password = 'admin'): void
194+
{
188195
$url = "http://{$this->hostname}/apps/gdatavaas/{$endpoint}";
189196

190197
$result = $this->makeHttpRequest('GET', $url, [
191198
'auth' => ['username' => $username, 'password' => $password]
192199
]);
193200

194-
echo "{$description} result: {$result['http_code']}\n";
195201
$this->assertEquals($expectedHttpStatus, $result['http_code'], "Failed: {$description}");
196202
}
197203

198-
protected function testPostEndpoint(string $endpoint, array $data, string $description, int $expectedHttpStatus = 200, string $username = 'admin', string $password = 'admin'): void {
204+
protected function testPostEndpoint(string $endpoint, array $data, string $description, int $expectedHttpStatus = 200, string $username = 'admin', string $password = 'admin'): void
205+
{
199206
$url = "http://{$this->hostname}/apps/gdatavaas/{$endpoint}";
200207

201208
$result = $this->makeHttpRequest('POST', $url, [
@@ -204,31 +211,35 @@ protected function testPostEndpoint(string $endpoint, array $data, string $descr
204211
'headers' => ['Content-Type: application/json']
205212
]);
206213

207-
echo "{$description} result: {$result['http_code']}\n";
208214
$this->assertEquals($expectedHttpStatus, $result['http_code'], "Failed: {$description}");
209215
}
210216

211-
protected function assertContainsVirusFound(array $response): void {
217+
protected function assertContainsVirusFound(array $response): void
218+
{
212219
$this->assertStringContainsString('Virus found', $response['body'], 'Expected "Virus found" in response body');
213220
}
214221

215-
protected function assertHttpCodeInRange(int $httpCode, int $min = 200, int $max = 299): void {
222+
protected function assertHttpCodeInRange(int $httpCode, int $min = 200, int $max = 299): void
223+
{
216224
$this->assertGreaterThanOrEqual($min, $httpCode, "HTTP code {$httpCode} is below expected range {$min}-{$max}");
217225
$this->assertLessThan($max + 1, $httpCode, "HTTP code {$httpCode} is above expected range {$min}-{$max}");
218226
}
219227

220-
protected function getTagsForFile(string $filePath): array {
228+
protected function getTagsForFile(string $filePath): array
229+
{
221230
$result = $this->executeDockerCommand("php occ gdatavaas:get-tags-for-file {$filePath}");
222231
return $result['output'];
223232
}
224233

225-
protected function assertHasTag(string $filePath, string $expectedTag): void {
234+
protected function assertHasTag(string $filePath, string $expectedTag): void
235+
{
226236
$tags = $this->getTagsForFile($filePath);
227237
$tagString = implode("\n", $tags);
228238
$this->assertStringContainsString($expectedTag, $tagString, "Expected tag '{$expectedTag}' not found in file tags");
229239
}
230240

231-
protected function assertTagCount(string $filePath, int $expectedCount): void {
241+
protected function assertTagCount(string $filePath, int $expectedCount): void
242+
{
232243
$tags = $this->getTagsForFile($filePath);
233244
// Filter out empty lines
234245
$nonEmptyTags = array_filter($tags, function ($line) {
@@ -237,7 +248,8 @@ protected function assertTagCount(string $filePath, int $expectedCount): void {
237248
$this->assertCount($expectedCount, $nonEmptyTags, "Expected {$expectedCount} tags, got " . count($nonEmptyTags));
238249
}
239250

240-
public static function tearDownAfterClass(): void {
251+
public static function tearDownAfterClass(): void
252+
{
241253
parent::tearDownAfterClass();
242254

243255
// Clean up temporary files
@@ -247,7 +259,8 @@ public static function tearDownAfterClass(): void {
247259
}
248260
}
249261

250-
private static function removeDirectory(string $dir): void {
262+
private static function removeDirectory(string $dir): void
263+
{
251264
if (!is_dir($dir)) {
252265
return;
253266
}

tests/integration/SettingsControllerTest.php

Lines changed: 119 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,55 @@ class SettingsControllerTest extends BaseIntegrationTest
1515
public static function adminGetRouteProvider(): array
1616
{
1717
return [
18-
['getCounters'],
1918
['getAuthMethod'],
2019
['getCache'],
2120
['getHashlookup'],
21+
];
22+
}
23+
24+
public static function adminPostRouteProvider(): array
25+
{
26+
return [
27+
// TODO: use default settings
28+
['adminSettings', [
29+
'username' => 'username',
30+
'password' => 'password',
31+
'clientId' => 'clientId',
32+
'clientSecret' => 'clientSecret',
33+
'authMethod' => 'authMethod',
34+
'maxScanSize' => 209715200,
35+
'timeout' => 900,
36+
'cache' => true,
37+
'hashlookup' => true
38+
]],
39+
// ['setadvancedconfig'],
40+
];
41+
}
42+
43+
public static function operatorGetRouteProvider(): array
44+
{
45+
return [
2246
['getSendMailOnVirusUpload'],
2347
['getAutoScan'],
2448
['getPrefixMalicious'],
2549
['getDisableUnscannedTag'],
26-
// ['setAutoScan'],
27-
// ['setPrefixMalicious'],
28-
// ['setSendMailOnVirusUpload'],
29-
// ['setDisableUnscannedTag'],
30-
// ['setadvancedconfig'],
31-
['operatorSettings'],
32-
['adminSettings'],
33-
// ['resetalltags'],
34-
// ['testsettings'],
35-
// ['scan']
50+
['getCounters'],
51+
];
52+
}
53+
54+
public static function operatorPostRouteProvider(): array
55+
{
56+
return [
57+
['operatorSettings', [
58+
'quarantineFolder' => '',
59+
'scanOnlyThis' => '',
60+
'doNotScanThis' => '',
61+
'notifyMails' => '',
62+
]],
63+
['setAutoScan', ['autoScanFiles' => true]],
64+
['setPrefixMalicious', ['prefixMalicious' => '[VIRUS] ']],
65+
['setSendMailOnVirusUpload', ['sendMailOnVirusUpload' => 'false']],
66+
['setDisableUnscannedTag', ['disableUnscannedTag' => 'false']],
3667
];
3768
}
3869

@@ -42,20 +73,85 @@ public function testAdminCanAccessAdminGetRoutes(string $route): void
4273
$this->testGetEndpoint($route, "Admin access to {$route}", 200);
4374
}
4475

45-
// testAdminCanAccessAdminGetRoutes
46-
// testAdminCanAccessAdminPostRoutes
47-
// testAdminCanAccessOperatorGetRoutes
48-
// testAdminCanAccessOperatorPostRoutes
4976

50-
// testOperatorCannotAccessAdminGetRoutes
51-
// testOperatorCannotAccessAdminPostRoutes
52-
// testOperatorCanAccessOperatorGetRoutes
53-
// testOperatorCanAccessOperatorPostRoutes
77+
#[DataProvider('adminPostRouteProvider')]
78+
public function testAdminCanAccessAdminPostRoutes(string $route, array $data): void
79+
{
80+
$this->testPostEndpoint($route, $data, "Admin access to {$route}", 200);
81+
}
82+
83+
#[DataProvider('operatorGetRouteProvider')]
84+
public function testAdminCanAccessOperatorGetRoutes(string $route): void
85+
{
86+
$this->testGetEndpoint($route, "Admin access to {$route}", 200);
87+
}
88+
89+
#[DataProvider('operatorPostRouteProvider')]
90+
public function testAdminCanAccessOperatorPostRoutes(string $route, array $data): void
91+
{
92+
$this->testPostEndpoint($route, $data, "Admin access to {$route}", 200);
93+
}
94+
95+
96+
#[DataProvider('adminGetRouteProvider')]
97+
public function testOperatorCannotAccessAdminGetRoutes(string $route): void
98+
{
99+
$this->testGetEndpoint($route, "Operator access to {$route}", 403, username: "vaas-operator", password: "gdatavaas-operator");
100+
}
101+
102+
103+
#[DataProvider('adminPostRouteProvider')]
104+
public function testOperatorCannotAccessAdminPostRoutes(string $route, array $data): void
105+
{
106+
$this->testPostEndpoint($route, $data, "Operator access to {$route}", 403, username: "vaas-operator", password: "gdatavaas-operator");
107+
}
108+
109+
#[DataProvider('operatorGetRouteProvider')]
110+
public function testOperatorCanAccessOperatorGetRoutes(string $route): void
111+
{
112+
$this->testGetEndpoint($route, "Operator access to {$route}", 200, username: "vaas-operator", password: "gdatavaas-operator");
113+
}
114+
115+
#[DataProvider('operatorPostRouteProvider')]
116+
public function testOperatorCanAccessOperatorPostRoutes(string $route, array $data): void
117+
{
118+
$this->testPostEndpoint($route, $data, "Operator access to {$route}", 200, username: "vaas-operator", password: "gdatavaas-operator");
119+
}
120+
121+
122+
#[DataProvider('adminGetRouteProvider')]
123+
public function testUserCannotAccessAdminGetRoutes(string $route): void
124+
{
125+
$this->testGetEndpoint($route, "User access to {$route}", 403, username: "user", password: "gdatavaas-user");
126+
}
127+
128+
129+
#[DataProvider('adminPostRouteProvider')]
130+
public function testUserCannotAccessAdminPostRoutes(string $route, array $data): void
131+
{
132+
$this->testPostEndpoint($route, $data, "Operator access to {$route}", 403, username: "user", password: "gdatavaas-user");
133+
}
134+
135+
#[DataProvider('operatorGetRouteProvider')]
136+
public function testUserCannotAccessOperatorGetRoutes(string $route): void
137+
{
138+
$this->testGetEndpoint($route, "Operator access to {$route}", 403, username: "user", password: "gdatavaas-user");
139+
}
140+
141+
#[DataProvider('operatorPostRouteProvider')]
142+
public function testUserCannotAccessOperatorPostRoutes(string $route, array $data): void
143+
{
144+
$this->testPostEndpoint($route, $data, "Operator access to {$route}", 403, username: "user", password: "gdatavaas-user");
145+
}
146+
147+
148+
#[DataProvider('adminPostRouteProvider')]
149+
#[DataProvider('operatorPostRouteProvider')]
150+
public function testPostRoutesReturn400ForEmptyBody(string $route, array $data): void
151+
{
152+
$this->testPostEndpoint($route, [], "Admin access to {$route}", 400);
153+
}
54154

55-
// testUserCannotAccessAdminGetRoutes
56-
// testUserCannotAccessAdminPostRoutes
57-
// testUserCannotAccessOperatorGetRoutes
58-
// testUserCannotAccessOperatorPostRoutes
59155

60156
// /**
61157
// * Test scan endpoint (POST /scan)
@@ -95,16 +191,6 @@ public function testAdminCanAccessAdminGetRoutes(string $route): void
95191
// }
96192
// }
97193

98-
// /**
99-
// * Test all GET endpoints
100-
// * @dataProvider getEndpointsProvider
101-
// */
102-
// public function testGetEndpoints(string $endpoint, string $description): void
103-
// {
104-
// echo "Testing {$endpoint}...\n";
105-
// $this->testGetEndpoint($endpoint, $description, 200);
106-
// }
107-
108194
// /**
109195
// * Data provider for GET endpoints
110196
// */
@@ -122,16 +208,6 @@ public function testAdminCanAccessAdminGetRoutes(string $route): void
122208
// ];
123209
// }
124210

125-
// /**
126-
// * Test all POST settings endpoints
127-
// * @dataProvider postEndpointsProvider
128-
// */
129-
// public function testPostSettingsEndpoints(string $endpoint, array $data, string $description): void
130-
// {
131-
// echo "Testing {$endpoint}...\n";
132-
// $this->testPostEndpoint($endpoint, $data, $description, 200);
133-
// }
134-
135211
// /**
136212
// * Data provider for POST settings endpoints
137213
// */

tests/integration/phpunit.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ SPDX-License-Identifier: AGPL-3.0-or-later
77
-->
88

99
<phpunit bootstrap="bootstrap.php"
10-
verbose="true"
11-
convertDeprecationsToExceptions="true"
1210
timeoutForSmallTests="900"
1311
timeoutForMediumTests="900"
1412
timeoutForLargeTests="900"
13+
displayDetailsOnTestsThatTriggerDeprecations="true"
1514
>
1615
<testsuite name='G DATA Antivirus Integration Tests'>
1716
<directory suffix='Test.php'>.</directory>

0 commit comments

Comments
 (0)