FOSSology  4.7.1
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 {
41  public $dbManager;
42  public $streamFactory;
43  public $userDao;
44 
49  const YAML_LOC = __DIR__ . '/../../../ui/api/documentation/openapi.yaml';
50 
56 
62 
67  private $dbHelper;
68 
73  private $restHelper;
74 
79  private $auth;
80 
86 
90  private $rq;
91 
95  private $OPTIONS =[];
100  protected function setUp() : void
101  {
102  global $container;
103  $this->rq = [
104  "options" => ["A","F","g","l","o"],
105  "logsDate"=>"2021-08-19",
106  "goldDate"=>"2022-07-16"
107  ];
108 
109  $this->OPTIONS =[
110  "A"=>"Run all maintenance operations.",
111  "F"=>"Validate folder contents.",
112  "g"=>"Remove orphaned gold files.",
113  "o"=>"Remove older gold files from repository.",
114  "l"=>"Remove older log files from repository."
115  ];
116  $container = M::mock('ContainerBuilder');
117  $this->dbHelper = M::mock(DbHelper::class);
118  $this->restHelper = M::mock(RestHelper::class);
119  $this->userDao = M::mock(UserDao::class);
120  $this->auth = M::mock(Auth::class);
121 
122  $this->maintagentPlugin = M::mock('maintagent');
123 
124  $this->restHelper->shouldReceive('getDbHelper')->andReturn($this->dbHelper);
125  $this->restHelper->shouldReceive('getUserDao')
126  ->andReturn($this->userDao);
127 
128  $this->restHelper->shouldReceive('getPlugin')
129  ->withArgs(array('maintagent'))
130  ->andReturn($this->maintagentPlugin);
131 
132  $this->auth->shouldReceive('isAdmin')->andReturn(true);
133 
134  $container->shouldReceive('get')->withArgs(array(
135  'helper.restHelper'))->andReturn($this->restHelper);
136  $this->maintenanceController = new MaintenanceController($container);
137  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
138  $this->dbManager = M::mock(DbManager::class);
139  $this->dbHelper->shouldReceive('getDbManager')->andReturn($this->dbManager);
140  $this->streamFactory = new StreamFactory();
141 
142  }
143 
150  private function getResponseJson($response)
151  {
152  $response->getBody()->seek(0);
153  return json_decode($response->getBody()->getContents(), true);
154  }
155 
161  public function testCreateMaintenance()
162  {
163  $_SESSION['UserLevel'] = 10;
164 
165  $rq = [
166  "options" => ["A","F","g","l","o"],
167  "logsDate"=>"2021-08-19",
168  "goldDate"=>"2022-07-16"
169  ];
170 
171  $OPTIONS =[
172  "A"=>"Run all maintenance operations.",
173  "F"=>"Validate folder contents.",
174  "g"=>"Remove orphaned gold files.",
175  "o"=>"Remove older gold files from repository.",
176  "l"=>"Remove older log files from repository."
177  ];
178 
179  $alteredOptions = array();
180  foreach ($rq['options'] as $key) {
181  $alteredOptions[$key] = $key;
182  }
183  $body = $rq;
184  $body['options'] = $alteredOptions;
185 
186  $this->maintagentPlugin->shouldReceive('getOptions')->andReturn($OPTIONS);
187 
188  $mess = _("The maintenance job has been queued");
189 
190  $this->maintagentPlugin->shouldReceive('handle')->withArgs([$body])->andReturn($mess);
191 
192  $info = new Info(201, $mess, InfoType::INFO);
193 
194 
195  $expectedResponse = (new ResponseHelper())->withJson($info->getArray(), $info->getCode());
196 
197  $reqBody = $this->streamFactory->createStream(json_encode(
198  $rq
199  ));
200 
201  $requestHeaders = new Headers();
202  $requestHeaders->setHeader('Content-Type', 'application/json');
203  $request = new Request("POST", new Uri("HTTP", "localhost"),
204  $requestHeaders, [], [], $reqBody);
205 
206 
207  $actualResponse = $this->maintenanceController->createMaintenance($request, new ResponseHelper(), null);
208 
209  $this->assertEquals($expectedResponse->getStatusCode(),
210  $actualResponse->getStatusCode());
211  $this->assertEquals($this->getResponseJson($expectedResponse),
212  $this->getResponseJson($actualResponse));
213  }
214 
221  {
222  $_SESSION['UserLevel'] = 0;
223 
224  $alteredOptions = array();
225  foreach ($this->rq['options'] as $key) {
226  $alteredOptions[$key] = $key;
227  }
228  $body = $this->rq;
229  $body['options'] = $alteredOptions;
230 
231  $this->maintagentPlugin->shouldReceive('getOptions')->andReturn($this->OPTIONS);
232 
233  $mess = _("The maintenance job has been queued");
234 
235  $this->maintagentPlugin->shouldReceive('handle')->withArgs([$body])->andReturn($mess);
236  $reqBody = $this->streamFactory->createStream(json_encode(
237  $this->rq
238  ));
239 
240  $requestHeaders = new Headers();
241  $requestHeaders->setHeader('Content-Type', 'application/json');
242  $request = new Request("POST", new Uri("HTTP", "localhost"),
243  $requestHeaders, [], [], $reqBody);
244 
245  $this->expectException(HttpForbiddenException::class);
246  $this->maintenanceController->createMaintenance($request, new ResponseHelper(), null);
247  }
248 
255  {
256  $_SESSION['UserLevel'] = 10;
257 
258  $this->rq["options"] = [];
259 
260  $reqBody = $this->streamFactory->createStream(json_encode(
261  $this->rq
262  ));
263  $requestHeaders = new Headers();
264  $requestHeaders->setHeader('Content-Type', 'application/json');
265  $request = new Request("POST", new Uri("HTTP", "localhost"),
266  $requestHeaders, [], [], $reqBody);
267 
268  $this->expectException(HttpBadRequestException::class);
269  $this->maintenanceController->createMaintenance($request, new ResponseHelper(), null);
270  }
271 
272 
279  {
280  $_SESSION['UserLevel'] = 10;
281 
282  array_push($this->rq["options"],"M");
283  $alteredOptions = array();
284  foreach ($this->rq['options'] as $key) {
285  $alteredOptions[$key] = $key;
286  }
287  $body = $this->rq;
288  $body['options'] = $alteredOptions;
289 
290  $this->maintagentPlugin->shouldReceive('getOptions')->andReturn($this->OPTIONS);
291 
292  $mess = _("The maintenance job has been queued");
293 
294  $this->maintagentPlugin->shouldReceive('handle')->withArgs([$body])->andReturn($mess);
295  $reqBody = $this->streamFactory->createStream(json_encode(
296  $this->rq
297  ));
298 
299  $requestHeaders = new Headers();
300  $requestHeaders->setHeader('Content-Type', 'application/json');
301  $request = new Request("POST", new Uri("HTTP", "localhost"),
302  $requestHeaders, [], [], $reqBody);
303 
304  $this->expectException(HttpNotFoundException::class);
305  $this->maintenanceController->createMaintenance($request, new ResponseHelper(), null);
306  }
307 
314  {
315  $_SESSION['UserLevel'] = 10;
316 
317  $req = $this->rq;
318  $req["goldDate"] = "";
319 
320 
321  $alteredOptions = array();
322  foreach ($req['options'] as $key) {
323  $alteredOptions[$key] = $key;
324  }
325  $body = $req;
326  $body['options'] = $alteredOptions;
327 
328  $this->maintagentPlugin->shouldReceive('getOptions')->andReturn($this->OPTIONS);
329 
330  $mess = _("The maintenance job has been queued");
331 
332  $this->maintagentPlugin->shouldReceive('handle')->withArgs([$body])->andReturn($mess);
333  $reqBody = $this->streamFactory->createStream(json_encode(
334  $req
335  ));
336 
337  $requestHeaders = new Headers();
338  $requestHeaders->setHeader('Content-Type', 'application/json');
339  $request = new Request("POST", new Uri("HTTP", "localhost"),
340  $requestHeaders, [], [], $reqBody);
341 
342  $this->expectException(HttpBadRequestException::class);
343  $this->maintenanceController->createMaintenance($request, new ResponseHelper(), null);
344  }
345 
346 
353  {
354  $_SESSION['UserLevel'] = 10;
355 
356  $req = $this->rq;
357  $req["logsDate"] = "";
358  $alteredOptions = array();
359  foreach ($req['options'] as $key) {
360  $alteredOptions[$key] = $key;
361  }
362  $body = $req;
363  $body['options'] = $alteredOptions;
364 
365  $this->maintagentPlugin->shouldReceive('getOptions')->andReturn($this->OPTIONS);
366 
367  $mess = _("The maintenance job has been queued");
368 
369  $this->maintagentPlugin->shouldReceive('handle')->withArgs([$body])->andReturn($mess);
370  $reqBody = $this->streamFactory->createStream(json_encode(
371  $req
372  ));
373 
374  $requestHeaders = new Headers();
375  $requestHeaders->setHeader('Content-Type', 'application/json');
376  $request = new Request("POST", new Uri("HTTP", "localhost"),
377  $requestHeaders, [], [], $reqBody);
378 
379  $this->expectException(HttpBadRequestException::class);
380  $this->maintenanceController->createMaintenance($request, new ResponseHelper(), null);
381  }
382 
388  public function testGetMaintenanceInfo()
389  {
390  $_SESSION['UserLevel'] = 10;
391 
392  $this->maintagentPlugin->shouldReceive('getLastMaintenanceRunTime')
393  ->andReturn("2022-07-16 10:15:30");
394 
395  $request = new Request("GET", new Uri("HTTP", "localhost"),
396  new Headers(), [], [], $this->streamFactory->createStream());
397 
398  $actualResponse = $this->maintenanceController->getMaintenanceInfo($request, new ResponseHelper(), null);
399 
400  $this->assertEquals(200, $actualResponse->getStatusCode());
401  $this->assertEquals(
402  ['lastRun' => date(DATE_ATOM, strtotime("2022-07-16 10:15:30"))],
403  $this->getResponseJson($actualResponse));
404  }
405 
412  {
413  $_SESSION['UserLevel'] = 10;
414 
415  $this->maintagentPlugin->shouldReceive('getLastMaintenanceRunTime')
416  ->andReturn(null);
417 
418  $request = new Request("GET", new Uri("HTTP", "localhost"),
419  new Headers(), [], [], $this->streamFactory->createStream());
420 
421  $actualResponse = $this->maintenanceController->getMaintenanceInfo($request, new ResponseHelper(), null);
422 
423  $this->assertEquals(200, $actualResponse->getStatusCode());
424  $this->assertEquals(['lastRun' => null], $this->getResponseJson($actualResponse));
425  }
426 
433  {
434  $_SESSION['UserLevel'] = 0;
435 
436  $request = new Request("GET", new Uri("HTTP", "localhost"),
437  new Headers(), [], [], $this->streamFactory->createStream());
438 
439  $this->expectException(HttpForbiddenException::class);
440  $this->maintenanceController->getMaintenanceInfo($request, new ResponseHelper(), null);
441  }
442 
443 }
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