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 {
41 
42 
47  const YAML_LOC = __DIR__ . '/../../../ui/api/documentation/openapi.yaml';
48 
54 
60 
65  private $dbHelper;
66 
71  private $restHelper;
72 
77  private $auth;
78 
84 
88  private $rq;
89 
93  private $OPTIONS =[];
103  protected function setUp() : void
104  {
105  global $container;
106  $this->rq = [
107  "options" => ["A","F","g","l","o"],
108  "logsDate"=>"2021-08-19",
109  "goldDate"=>"2022-07-16"
110  ];
111 
112  $this->OPTIONS =[
113  "A"=>"Run all maintenance operations.",
114  "F"=>"Validate folder contents.",
115  "g"=>"Remove orphaned gold files.",
116  "o"=>"Remove older gold files from repository.",
117  "l"=>"Remove older log files from repository."
118  ];
119  $container = M::mock('ContainerBuilder');
120  $this->dbHelper = M::mock(DbHelper::class);
121  $this->restHelper = M::mock(RestHelper::class);
122  $this->userDao = M::mock(UserDao::class);
123  $this->auth = M::mock(Auth::class);
124 
125  $this->maintagentPlugin = M::mock('maintagent');
126 
127  $this->restHelper->shouldReceive('getDbHelper')->andReturn($this->dbHelper);
128  $this->restHelper->shouldReceive('getUserDao')
129  ->andReturn($this->userDao);
130 
131  $this->restHelper->shouldReceive('getPlugin')
132  ->withArgs(array('maintagent'))
133  ->andReturn($this->maintagentPlugin);
134 
135  $this->auth->shouldReceive('isAdmin')->andReturn(true);
136 
137  $container->shouldReceive('get')->withArgs(array(
138  'helper.restHelper'))->andReturn($this->restHelper);
139  $this->maintenanceController = new MaintenanceController($container);
140  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
141  $this->dbManager = M::mock(DbManager::class);
142  $this->dbHelper->shouldReceive('getDbManager')->andReturn($this->dbManager);
143  $this->streamFactory = new StreamFactory();
144 
145  }
146 
153  private function getResponseJson($response)
154  {
155  $response->getBody()->seek(0);
156  return json_decode($response->getBody()->getContents(), true);
157  }
158 
164  public function testCreateMaintenance()
165  {
166  $_SESSION['UserLevel'] = 10;
167 
168  $rq = [
169  "options" => ["A","F","g","l","o"],
170  "logsDate"=>"2021-08-19",
171  "goldDate"=>"2022-07-16"
172  ];
173 
174  $OPTIONS =[
175  "A"=>"Run all maintenance operations.",
176  "F"=>"Validate folder contents.",
177  "g"=>"Remove orphaned gold files.",
178  "o"=>"Remove older gold files from repository.",
179  "l"=>"Remove older log files from repository."
180  ];
181 
182  $alteredOptions = array();
183  foreach ($rq['options'] as $key) {
184  $alteredOptions[$key] = $key;
185  }
186  $body = $rq;
187  $body['options'] = $alteredOptions;
188 
189  $this->maintagentPlugin->shouldReceive('getOptions')->andReturn($OPTIONS);
190 
191  $mess = _("The maintenance job has been queued");
192 
193  $this->maintagentPlugin->shouldReceive('handle')->withArgs([$body])->andReturn($mess);
194 
195  $info = new Info(201, $mess, InfoType::INFO);
196 
197 
198  $expectedResponse = (new ResponseHelper())->withJson($info->getArray(), $info->getCode());
199 
200  $reqBody = $this->streamFactory->createStream(json_encode(
201  $rq
202  ));
203 
204  $requestHeaders = new Headers();
205  $requestHeaders->setHeader('Content-Type', 'application/json');
206  $request = new Request("POST", new Uri("HTTP", "localhost"),
207  $requestHeaders, [], [], $reqBody);
208 
209 
210  $actualResponse = $this->maintenanceController->createMaintenance($request, new ResponseHelper(), null);
211 
212  $this->assertEquals($expectedResponse->getStatusCode(),
213  $actualResponse->getStatusCode());
214  $this->assertEquals($this->getResponseJson($expectedResponse),
215  $this->getResponseJson($actualResponse));
216  }
217 
224  {
225  $_SESSION['UserLevel'] = 0;
226 
227  $alteredOptions = array();
228  foreach ($this->rq['options'] as $key) {
229  $alteredOptions[$key] = $key;
230  }
231  $body = $this->rq;
232  $body['options'] = $alteredOptions;
233 
234  $this->maintagentPlugin->shouldReceive('getOptions')->andReturn($this->OPTIONS);
235 
236  $mess = _("The maintenance job has been queued");
237 
238  $this->maintagentPlugin->shouldReceive('handle')->withArgs([$body])->andReturn($mess);
239  $reqBody = $this->streamFactory->createStream(json_encode(
240  $this->rq
241  ));
242 
243  $requestHeaders = new Headers();
244  $requestHeaders->setHeader('Content-Type', 'application/json');
245  $request = new Request("POST", new Uri("HTTP", "localhost"),
246  $requestHeaders, [], [], $reqBody);
247 
248  $this->expectException(HttpForbiddenException::class);
249  $this->maintenanceController->createMaintenance($request, new ResponseHelper(), null);
250  }
251 
258  {
259  $_SESSION['UserLevel'] = 10;
260 
261  $this->rq["options"] = [];
262 
263  $reqBody = $this->streamFactory->createStream(json_encode(
264  $this->rq
265  ));
266  $requestHeaders = new Headers();
267  $requestHeaders->setHeader('Content-Type', 'application/json');
268  $request = new Request("POST", new Uri("HTTP", "localhost"),
269  $requestHeaders, [], [], $reqBody);
270 
271  $this->expectException(HttpBadRequestException::class);
272  $this->maintenanceController->createMaintenance($request, new ResponseHelper(), null);
273  }
274 
275 
282  {
283  $_SESSION['UserLevel'] = 10;
284 
285  array_push($this->rq["options"],"M");
286  $alteredOptions = array();
287  foreach ($this->rq['options'] as $key) {
288  $alteredOptions[$key] = $key;
289  }
290  $body = $this->rq;
291  $body['options'] = $alteredOptions;
292 
293  $this->maintagentPlugin->shouldReceive('getOptions')->andReturn($this->OPTIONS);
294 
295  $mess = _("The maintenance job has been queued");
296 
297  $this->maintagentPlugin->shouldReceive('handle')->withArgs([$body])->andReturn($mess);
298  $reqBody = $this->streamFactory->createStream(json_encode(
299  $this->rq
300  ));
301 
302  $requestHeaders = new Headers();
303  $requestHeaders->setHeader('Content-Type', 'application/json');
304  $request = new Request("POST", new Uri("HTTP", "localhost"),
305  $requestHeaders, [], [], $reqBody);
306 
307  $this->expectException(HttpNotFoundException::class);
308  $this->maintenanceController->createMaintenance($request, new ResponseHelper(), null);
309  }
310 
317  {
318  $_SESSION['UserLevel'] = 10;
319 
320  $req = $this->rq;
321  $req["goldDate"] = "";
322 
323 
324  $alteredOptions = array();
325  foreach ($req['options'] as $key) {
326  $alteredOptions[$key] = $key;
327  }
328  $body = $req;
329  $body['options'] = $alteredOptions;
330 
331  $this->maintagentPlugin->shouldReceive('getOptions')->andReturn($this->OPTIONS);
332 
333  $mess = _("The maintenance job has been queued");
334 
335  $this->maintagentPlugin->shouldReceive('handle')->withArgs([$body])->andReturn($mess);
336  $reqBody = $this->streamFactory->createStream(json_encode(
337  $req
338  ));
339 
340  $requestHeaders = new Headers();
341  $requestHeaders->setHeader('Content-Type', 'application/json');
342  $request = new Request("POST", new Uri("HTTP", "localhost"),
343  $requestHeaders, [], [], $reqBody);
344 
345  $this->expectException(HttpBadRequestException::class);
346  $this->maintenanceController->createMaintenance($request, new ResponseHelper(), null);
347  }
348 
349 
356  {
357  $_SESSION['UserLevel'] = 10;
358 
359  $req = $this->rq;
360  $req["logsDate"] = "";
361  $alteredOptions = array();
362  foreach ($req['options'] as $key) {
363  $alteredOptions[$key] = $key;
364  }
365  $body = $req;
366  $body['options'] = $alteredOptions;
367 
368  $this->maintagentPlugin->shouldReceive('getOptions')->andReturn($this->OPTIONS);
369 
370  $mess = _("The maintenance job has been queued");
371 
372  $this->maintagentPlugin->shouldReceive('handle')->withArgs([$body])->andReturn($mess);
373  $reqBody = $this->streamFactory->createStream(json_encode(
374  $req
375  ));
376 
377  $requestHeaders = new Headers();
378  $requestHeaders->setHeader('Content-Type', 'application/json');
379  $request = new Request("POST", new Uri("HTTP", "localhost"),
380  $requestHeaders, [], [], $reqBody);
381 
382  $this->expectException(HttpBadRequestException::class);
383  $this->maintenanceController->createMaintenance($request, new ResponseHelper(), null);
384  }
385 
386 }
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