FOSSology  4.4.0
Open Source License Compliance by Open Source Software
MaintenanceControllerTest.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2022 Samuel Dushimimana <dushsam100@gmail.com>
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
14 
27 use Mockery as M;
28 use Slim\Psr7\Factory\StreamFactory;
29 use Slim\Psr7\Uri;
30 use Slim\Psr7\Headers;
31 use Slim\Psr7\Request;
32 
33 require_once dirname(__DIR__, 4) . '/lib/php/Plugin/FO_Plugin.php';
34 
39 class MaintenanceControllerTest extends \PHPUnit\Framework\TestCase
40 {
45  const YAML_LOC = __DIR__ . '/../../../ui/api/documentation/openapi.yaml';
46 
52 
58 
63  private $dbHelper;
64 
69  private $restHelper;
70 
75  private $auth;
76 
82 
86  private $rq;
87 
91  private $OPTIONS =[];
96  protected function setUp() : void
97  {
98  global $container;
99  $this->rq = [
100  "options" => ["A","F","g","l","o"],
101  "logsDate"=>"2021-08-19",
102  "goldDate"=>"2022-07-16"
103  ];
104 
105  $this->OPTIONS =[
106  "A"=>"Run all maintenance operations.",
107  "F"=>"Validate folder contents.",
108  "g"=>"Remove orphaned gold files.",
109  "o"=>"Remove older gold files from repository.",
110  "l"=>"Remove older log files from repository."
111  ];
112  $container = M::mock('ContainerBuilder');
113  $this->dbHelper = M::mock(DbHelper::class);
114  $this->restHelper = M::mock(RestHelper::class);
115  $this->userDao = M::mock(UserDao::class);
116  $this->auth = M::mock(Auth::class);
117 
118  $this->maintagentPlugin = M::mock('maintagent');
119 
120  $this->restHelper->shouldReceive('getDbHelper')->andReturn($this->dbHelper);
121  $this->restHelper->shouldReceive('getUserDao')
122  ->andReturn($this->userDao);
123 
124  $this->restHelper->shouldReceive('getPlugin')
125  ->withArgs(array('maintagent'))
126  ->andReturn($this->maintagentPlugin);
127 
128  $this->auth->shouldReceive('isAdmin')->andReturn(true);
129 
130  $container->shouldReceive('get')->withArgs(array(
131  'helper.restHelper'))->andReturn($this->restHelper);
132  $this->maintenanceController = new MaintenanceController($container);
133  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
134  $this->dbManager = M::mock(DbManager::class);
135  $this->dbHelper->shouldReceive('getDbManager')->andReturn($this->dbManager);
136  $this->streamFactory = new StreamFactory();
137 
138  }
139 
146  private function getResponseJson($response)
147  {
148  $response->getBody()->seek(0);
149  return json_decode($response->getBody()->getContents(), true);
150  }
151 
157  public function testCreateMaintenance()
158  {
159  $_SESSION['UserLevel'] = 10;
160 
161  $rq = [
162  "options" => ["A","F","g","l","o"],
163  "logsDate"=>"2021-08-19",
164  "goldDate"=>"2022-07-16"
165  ];
166 
167  $OPTIONS =[
168  "A"=>"Run all maintenance operations.",
169  "F"=>"Validate folder contents.",
170  "g"=>"Remove orphaned gold files.",
171  "o"=>"Remove older gold files from repository.",
172  "l"=>"Remove older log files from repository."
173  ];
174 
175  $alteredOptions = array();
176  foreach ($rq['options'] as $key) {
177  $alteredOptions[$key] = $key;
178  }
179  $body = $rq;
180  $body['options'] = $alteredOptions;
181 
182  $this->maintagentPlugin->shouldReceive('getOptions')->andReturn($OPTIONS);
183 
184  $mess = _("The maintenance job has been queued");
185 
186  $this->maintagentPlugin->shouldReceive('handle')->withArgs([$body])->andReturn($mess);
187 
188  $info = new Info(201, $mess, InfoType::INFO);
189 
190 
191  $expectedResponse = (new ResponseHelper())->withJson($info->getArray(), $info->getCode());
192 
193  $reqBody = $this->streamFactory->createStream(json_encode(
194  $rq
195  ));
196 
197  $requestHeaders = new Headers();
198  $requestHeaders->setHeader('Content-Type', 'application/json');
199  $request = new Request("POST", new Uri("HTTP", "localhost"),
200  $requestHeaders, [], [], $reqBody);
201 
202 
203  $actualResponse = $this->maintenanceController->createMaintenance($request, new ResponseHelper(), null);
204 
205  $this->assertEquals($expectedResponse->getStatusCode(),
206  $actualResponse->getStatusCode());
207  $this->assertEquals($this->getResponseJson($expectedResponse),
208  $this->getResponseJson($actualResponse));
209  }
210 
217  {
218  $_SESSION['UserLevel'] = 0;
219 
220  $alteredOptions = array();
221  foreach ($this->rq['options'] as $key) {
222  $alteredOptions[$key] = $key;
223  }
224  $body = $this->rq;
225  $body['options'] = $alteredOptions;
226 
227  $this->maintagentPlugin->shouldReceive('getOptions')->andReturn($this->OPTIONS);
228 
229  $mess = _("The maintenance job has been queued");
230 
231  $this->maintagentPlugin->shouldReceive('handle')->withArgs([$body])->andReturn($mess);
232  $reqBody = $this->streamFactory->createStream(json_encode(
233  $this->rq
234  ));
235 
236  $requestHeaders = new Headers();
237  $requestHeaders->setHeader('Content-Type', 'application/json');
238  $request = new Request("POST", new Uri("HTTP", "localhost"),
239  $requestHeaders, [], [], $reqBody);
240 
241  $this->expectException(HttpForbiddenException::class);
242  $this->maintenanceController->createMaintenance($request, new ResponseHelper(), null);
243  }
244 
251  {
252  $_SESSION['UserLevel'] = 10;
253 
254  $this->rq["options"] = [];
255 
256  $reqBody = $this->streamFactory->createStream(json_encode(
257  $this->rq
258  ));
259  $requestHeaders = new Headers();
260  $requestHeaders->setHeader('Content-Type', 'application/json');
261  $request = new Request("POST", new Uri("HTTP", "localhost"),
262  $requestHeaders, [], [], $reqBody);
263 
264  $this->expectException(HttpBadRequestException::class);
265  $this->maintenanceController->createMaintenance($request, new ResponseHelper(), null);
266  }
267 
268 
275  {
276  $_SESSION['UserLevel'] = 10;
277 
278  array_push($this->rq["options"],"M");
279  $alteredOptions = array();
280  foreach ($this->rq['options'] as $key) {
281  $alteredOptions[$key] = $key;
282  }
283  $body = $this->rq;
284  $body['options'] = $alteredOptions;
285 
286  $this->maintagentPlugin->shouldReceive('getOptions')->andReturn($this->OPTIONS);
287 
288  $mess = _("The maintenance job has been queued");
289 
290  $this->maintagentPlugin->shouldReceive('handle')->withArgs([$body])->andReturn($mess);
291  $reqBody = $this->streamFactory->createStream(json_encode(
292  $this->rq
293  ));
294 
295  $requestHeaders = new Headers();
296  $requestHeaders->setHeader('Content-Type', 'application/json');
297  $request = new Request("POST", new Uri("HTTP", "localhost"),
298  $requestHeaders, [], [], $reqBody);
299 
300  $this->expectException(HttpNotFoundException::class);
301  $this->maintenanceController->createMaintenance($request, new ResponseHelper(), null);
302  }
303 
310  {
311  $_SESSION['UserLevel'] = 10;
312 
313  $req = $this->rq;
314  $req["goldDate"] = "";
315 
316 
317  $alteredOptions = array();
318  foreach ($req['options'] as $key) {
319  $alteredOptions[$key] = $key;
320  }
321  $body = $req;
322  $body['options'] = $alteredOptions;
323 
324  $this->maintagentPlugin->shouldReceive('getOptions')->andReturn($this->OPTIONS);
325 
326  $mess = _("The maintenance job has been queued");
327 
328  $this->maintagentPlugin->shouldReceive('handle')->withArgs([$body])->andReturn($mess);
329  $reqBody = $this->streamFactory->createStream(json_encode(
330  $req
331  ));
332 
333  $requestHeaders = new Headers();
334  $requestHeaders->setHeader('Content-Type', 'application/json');
335  $request = new Request("POST", new Uri("HTTP", "localhost"),
336  $requestHeaders, [], [], $reqBody);
337 
338  $this->expectException(HttpBadRequestException::class);
339  $this->maintenanceController->createMaintenance($request, new ResponseHelper(), null);
340  }
341 
342 
349  {
350  $_SESSION['UserLevel'] = 10;
351 
352  $req = $this->rq;
353  $req["logsDate"] = "";
354  $alteredOptions = array();
355  foreach ($req['options'] as $key) {
356  $alteredOptions[$key] = $key;
357  }
358  $body = $req;
359  $body['options'] = $alteredOptions;
360 
361  $this->maintagentPlugin->shouldReceive('getOptions')->andReturn($this->OPTIONS);
362 
363  $mess = _("The maintenance job has been queued");
364 
365  $this->maintagentPlugin->shouldReceive('handle')->withArgs([$body])->andReturn($mess);
366  $reqBody = $this->streamFactory->createStream(json_encode(
367  $req
368  ));
369 
370  $requestHeaders = new Headers();
371  $requestHeaders->setHeader('Content-Type', 'application/json');
372  $request = new Request("POST", new Uri("HTTP", "localhost"),
373  $requestHeaders, [], [], $reqBody);
374 
375  $this->expectException(HttpBadRequestException::class);
376  $this->maintenanceController->createMaintenance($request, new ResponseHelper(), null);
377  }
378 
379 }
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
Provides helper methods to access database for REST api.
Definition: DbHelper.php:38
Override Slim response for withJson function.
Provides various DAO helper functions for REST api.
Definition: RestHelper.php:32
Different type of infos provided by REST.
Definition: InfoType.php:16
Info model to contain general error and return values.
Definition: Info.php:19
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16