FOSSology  4.4.0
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 
29 $GLOBALS['globalSession'] = new Session();
30 $GLOBALS['globalSession']->set('t','t');
31 
32 function plugin_find($plugin)
33 {
34  return RestHelperTest::$functions->plugin_find($plugin);
35 }
36 
41 class RestHelperTest extends \PHPUnit\Framework\TestCase
42 {
47  public static $functions;
52  private $uploadDao;
57  private $dbHelper;
67  private $folderDao;
72  private $userDao;
77  private $jobDao;
82  private $showJobDao;
87  private $authHelper;
92  private $session;
97  private $restHelper;
102  private $userId;
107  private $groupId;
118 
123  protected function setUp() : void
124  {
125  $this->uploadPermissionDao = M::mock(UploadPermissionDao::class);
126  $this->uploadDao = M::mock(UploadDao::class);
127  $this->userDao = M::mock(UserDao::class);
128  $this->folderDao = M::mock(FolderDao::class);
129  $this->dbHelper = M::mock(DbHelper::class);
130  $this->authHelper = M::mock(AuthHelper::class);
131  $this->jobDao = M::mock(JobDao::class);
132  $this->showJobDao = M::mock(ShowJobsDao::class);
133  $this->session = $GLOBALS['globalSession'];
134  $this->userId = 2;
135  $this->groupId = 2;
136  self::$functions = M::mock();
137  $this->contentMovePlugin = M::mock('AdminContentMove');
138 
139  $this->session->set(Auth::USER_ID, $this->userId);
140  $this->session->set(Auth::GROUP_ID, $this->groupId);
141  $this->authHelper->shouldReceive('getSession')->andReturn($this->session);
142 
143  self::$functions->shouldReceive('plugin_find')
144  ->withArgs(['content_move'])
145  ->andReturn($this->contentMovePlugin);
146 
147  $this->restHelper = new RestHelper(
148  $this->uploadPermissionDao,
149  $this->uploadDao,
150  $this->userDao,
151  $this->folderDao,
152  $this->dbHelper,
153  $this->authHelper,
154  $this->jobDao,
155  $this->showJobDao);
156  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
157  }
158 
163  protected function tearDown() : void
164  {
165  $this->addToAssertionCount(
166  \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
167  M::close();
168  }
169 
175  public function testCopyUpload()
176  {
177  $uploadId = 5;
178  $newFolderId = 10;
179  $isCopy = true;
180  $uploadContentId = 44;
181 
182  $this->folderDao->shouldReceive('isFolderAccessible')
183  ->withArgs([$newFolderId, $this->userId])
184  ->once()
185  ->andReturn(true);
186  $this->uploadPermissionDao->shouldReceive('isAccessible')
187  ->withArgs([$uploadId, $this->groupId])
188  ->once()
189  ->andReturn(true);
190  $this->folderDao->shouldReceive('getFolderContentsId')
191  ->withArgs([$uploadId, 2])
192  ->once()
193  ->andReturn($uploadContentId);
194  $this->contentMovePlugin->shouldReceive('copyContent')
195  ->withArgs([[$uploadContentId], $newFolderId, $isCopy])
196  ->once()
197  ->andReturn("");
198 
199  $expected = new Info(202, "Upload $uploadId will be copied to folder " .
200  $newFolderId, InfoType::INFO);
201  $actual = $this->restHelper->copyUpload($uploadId, $newFolderId, $isCopy);
202 
203  $this->assertEquals($expected, $actual);
204  }
205 
211  public function testValidateTokenRequest()
212  {
213  $tokenExpire = strftime('%Y-%m-%d', strtotime('+3 day'));
214  $tokenName = "myTok";
215  $tokenScope = "r";
216  $tokenValidity = 30;
217 
218  $this->authHelper->shouldReceive('getMaxTokenValidity')
219  ->andReturn($tokenValidity);
220  $this->restHelper->validateTokenRequest($tokenExpire, $tokenName,
221  $tokenScope);
222  }
223 
230  {
231  $tokenExpire = strftime('%Y-%m-%d', strtotime('+10 day'));
232  $tokenName = "myTok";
233  $tokenScope = "read";
234  $tokenValidity = 3;
235 
236  $this->authHelper->shouldReceive('getMaxTokenValidity')
237  ->andReturn($tokenValidity);
238  $this->expectException(HttpBadRequestException::class);
239 
240  $this->restHelper->validateTokenRequest($tokenExpire, $tokenName,
241  $tokenScope);
242  }
243 
250  {
251  $tokenExpire = strftime('%d-%m-%Y', strtotime('+10 day'));
252  $tokenName = "myTok";
253  $tokenScope = "read";
254  $tokenValidity = 30;
255 
256  $this->authHelper->shouldReceive('getMaxTokenValidity')
257  ->andReturn($tokenValidity);
258  $this->expectException(HttpBadRequestException::class);
259 
260  $this->restHelper->validateTokenRequest($tokenExpire, $tokenName,
261  $tokenScope);
262  }
263 
270  {
271  $tokenExpire = strftime('%Y-%m-%d', strtotime('+10 day'));
272  $tokenName = "myTok";
273  $tokenScope = "rread";
274  $tokenValidity = 30;
275 
276  $this->authHelper->shouldReceive('getMaxTokenValidity')
277  ->andReturn($tokenValidity);
278  $this->expectException(HttpBadRequestException::class);
279 
280  $this->restHelper->validateTokenRequest($tokenExpire, $tokenName,
281  $tokenScope);
282  }
283 }
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($x)
Mock function to get decider plugin required by BulkReuser.
REST api helper classes.