FOSSology  4.8.0-rc1
Open Source License Compliance by Open Source Software
RestHelperTest.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2020 Siemens AG
4  Author: Gaurav Mishra <mishra.gaurav@siemens.com>
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
13 namespace Fossology\UI\Api\Helper;
14 
15 
26 use Mockery as M;
27 use Symfony\Component\HttpFoundation\Session\Session;
28 use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
29 
30 $GLOBALS['globalSession'] = new Session(new MockArraySessionStorage());
31 $GLOBALS['globalSession']->set('t','t');
32 
33 function plugin_find($plugin)
34 {
35  return RestHelperTest::$functions->plugin_find($plugin);
36 }
37 
42 class RestHelperTest extends \PHPUnit\Framework\TestCase
43 {
48  public static $functions;
53  private $uploadDao;
58  private $dbHelper;
68  private $folderDao;
73  private $userDao;
78  private $jobDao;
83  private $showJobDao;
88  private $authHelper;
93  private $session;
98  private $restHelper;
103  private $userId;
108  private $groupId;
119 
124  protected function setUp() : void
125  {
126  $this->uploadPermissionDao = M::mock(UploadPermissionDao::class);
127  $this->uploadDao = M::mock(UploadDao::class);
128  $this->userDao = M::mock(UserDao::class);
129  $this->folderDao = M::mock(FolderDao::class);
130  $this->dbHelper = M::mock(DbHelper::class);
131  $this->authHelper = M::mock(AuthHelper::class);
132  $this->jobDao = M::mock(JobDao::class);
133  $this->showJobDao = M::mock(ShowJobsDao::class);
134  $this->session = $GLOBALS['globalSession'];
135  $this->userId = 2;
136  $this->groupId = 2;
137  self::$functions = M::mock();
138  $this->contentMovePlugin = M::mock('AdminContentMove');
139 
140  $this->session->set(Auth::USER_ID, $this->userId);
141  $this->session->set(Auth::GROUP_ID, $this->groupId);
142  $this->authHelper->shouldReceive('getSession')->andReturn($this->session);
143 
144  self::$functions->shouldReceive('plugin_find')
145  ->withArgs(['content_move'])
146  ->andReturn($this->contentMovePlugin);
147 
148  $this->restHelper = new RestHelper(
149  $this->uploadPermissionDao,
150  $this->uploadDao,
151  $this->userDao,
152  $this->folderDao,
153  $this->dbHelper,
154  $this->authHelper,
155  $this->jobDao,
156  $this->showJobDao);
157  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
158  }
159 
164  protected function tearDown() : void
165  {
166  $this->addToAssertionCount(
167  \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
168  M::close();
169  }
170 
176  public function testCopyUpload()
177  {
178  $uploadId = 5;
179  $newFolderId = 10;
180  $isCopy = true;
181  $uploadContentId = 44;
182 
183  $this->folderDao->shouldReceive('isFolderAccessible')
184  ->withArgs([$newFolderId, $this->userId])
185  ->once()
186  ->andReturn(true);
187  $this->uploadPermissionDao->shouldReceive('isAccessible')
188  ->withArgs([$uploadId, $this->groupId])
189  ->once()
190  ->andReturn(true);
191  $this->folderDao->shouldReceive('getFolderContentsId')
192  ->withArgs([$uploadId, 2])
193  ->once()
194  ->andReturn($uploadContentId);
195  $this->contentMovePlugin->shouldReceive('copyContent')
196  ->withArgs([[$uploadContentId], $newFolderId, $isCopy])
197  ->once()
198  ->andReturn("");
199 
200  $expected = new Info(202, "Upload $uploadId will be copied to folder " .
201  $newFolderId, InfoType::INFO);
202  $actual = $this->restHelper->copyUpload($uploadId, $newFolderId, $isCopy);
203 
204  $this->assertEquals($expected, $actual);
205  }
206 
212  public function testValidateTokenRequest()
213  {
214  $tokenExpire = date('Y-m-d', strtotime('+3 day'));
215  $tokenName = "myTok";
216  $tokenScope = "r";
217  $tokenValidity = 30;
218 
219  $this->authHelper->shouldReceive('getMaxTokenValidity')
220  ->andReturn($tokenValidity);
221  $this->restHelper->validateTokenRequest($tokenExpire, $tokenName,
222  $tokenScope);
223  }
224 
231  {
232  $tokenExpire = date('Y-m-d', strtotime('+10 day'));
233  $tokenName = "myTok";
234  $tokenScope = "read";
235  $tokenValidity = 3;
236 
237  $this->authHelper->shouldReceive('getMaxTokenValidity')
238  ->andReturn($tokenValidity);
239  $this->expectException(HttpBadRequestException::class);
240 
241  $this->restHelper->validateTokenRequest($tokenExpire, $tokenName,
242  $tokenScope);
243  }
244 
251  {
252  $tokenExpire = date('d-m-Y', strtotime('+10 day'));
253  $tokenName = "myTok";
254  $tokenScope = "read";
255  $tokenValidity = 30;
256 
257  $this->authHelper->shouldReceive('getMaxTokenValidity')
258  ->andReturn($tokenValidity);
259  $this->expectException(HttpBadRequestException::class);
260 
261  $this->restHelper->validateTokenRequest($tokenExpire, $tokenName,
262  $tokenScope);
263  }
264 
271  {
272  $tokenExpire = date('Y-m-d', strtotime('+10 day'));
273  $tokenName = "myTok";
274  $tokenScope = "rread";
275  $tokenValidity = 30;
276 
277  $this->authHelper->shouldReceive('getMaxTokenValidity')
278  ->andReturn($tokenValidity);
279  $this->expectException(HttpBadRequestException::class);
280 
281  $this->restHelper->validateTokenRequest($tokenExpire, $tokenName,
282  $tokenScope);
283  }
284 }
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
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
plugin_find($pluginName)
Given the official name of a plugin, return the $Plugins object.
REST api helper classes.