FOSSology  4.4.0
Open Source License Compliance by Open Source Software
AuthHelperTest.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 */
20 
25 use Mockery as M;
26 use Symfony\Component\HttpFoundation\Session\Session;
27 
32 class AuthHelperTest extends \PHPUnit\Framework\TestCase
33 {
34 
40 
45  private $authHelper;
46 
51  private $userDao;
52 
57  private $session;
58 
63  private $dbHelper;
64 
69  protected function setUp() : void
70  {
71  $this->userDao = M::mock(UserDao::class);
72  $this->session = M::mock(Session::class);
73  $this->dbHelper = M::mock(DbHelper::class);
74 
75  $this->session->shouldReceive('isStarted')->andReturn(true);
76 
77  $this->authHelper = new AuthHelper($this->userDao, $this->session,
78  $this->dbHelper);
79  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
80  }
81 
86  protected function tearDown() : void
87  {
88  $this->addToAssertionCount(
89  \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
90  M::close();
91  }
92 
102  public function testVerifyAuthToken()
103  {
104  $userId = null;
105  $expectedUser = 2;
106  $tokenScope = null;
107  $jti = "4.2";
108  $key = "mysecretkey";
109  $createdOn = strftime('%Y-%m-%d');
110  $expire = strftime('%Y-%m-%d', strtotime('+3 day'));
111  $authToken = $this->authHelper->generateJwtToken($expire, $createdOn, $jti,
112  "w", $key);
113  $authHeader = "Bearer " . $authToken;
114  $tokenRow = [
115  "token_key" => $key,
116  "created_on" => $createdOn,
117  "expire_on" => $expire,
118  "user_fk" => $expectedUser,
119  "active" => 't',
120  "token_scope" => "w"
121  ];
122 
123  $this->dbHelper->shouldReceive('getTokenKey')
124  ->withArgs(["4"])
125  ->andReturn($tokenRow);
126  $this->userDao->shouldReceive('isUserIdActive')
127  ->withArgs([$expectedUser])
128  ->andReturn(true);
129 
130  $GLOBALS['SysConf'] = ['AUTHENTICATION' => ['resttoken' => 'token']];
131  $this->authHelper->verifyAuthToken($authHeader, $userId,
132  $tokenScope);
133 
134  $this->assertEquals($expectedUser, $userId);
135  $this->assertEquals("write", $tokenScope);
136  }
137 
146  {
147  $userId = null;
148  $expectedUser = 2;
149  $tokenScope = null;
150  $jti = "4.2";
151  $key = "mysecretkey";
152  $createdOn = strftime('%Y-%m-%d');
153  $expire = strftime('%Y-%m-%d', strtotime('+3 day'));
154  $authToken = $this->authHelper->generateJwtToken($expire, $createdOn, $jti,
155  "w", $key);
156  $authHeader = "Bearer " . $authToken;
157  $tokenRow = [
158  "token_key" => $key,
159  "created_on" => $createdOn,
160  "expire_on" => $expire,
161  "user_fk" => $expectedUser,
162  "active" => 't',
163  "token_scope" => "w"
164  ];
165 
166  $this->dbHelper->shouldReceive('getTokenKey')
167  ->withArgs(["4"])
168  ->andReturn($tokenRow);
169  $this->userDao->shouldReceive('isUserIdActive')
170  ->withArgs([$expectedUser])
171  ->andReturn(false);
172 
173  $GLOBALS['SysConf'] = ['AUTHENTICATION' => ['resttoken' => 'token']];
174 
175  $this->expectException(HttpForbiddenException::class);
176 
177  $this->authHelper->verifyAuthToken($authHeader, $userId, $tokenScope);
178  }
179 
186  public function testIsTokenActive()
187  {
188  $key = "mysecretkey";
189  $createdOn = strftime('%Y-%m-%d');
190  $expire = strftime('%Y-%m-%d', strtotime('+3 day'));
191  $tokenId = 4;
192  $activeTokenRow = [
193  "token_key" => $key,
194  "created_on" => $createdOn,
195  "expire_on" => $expire,
196  "user_fk" => 2,
197  "active" => 't',
198  "token_scope" => "w"
199  ];
200  $expireTokenRow = [
201  "token_key" => $key,
202  "created_on" => $createdOn,
203  "expire_on" => $expire,
204  "user_fk" => 2,
205  "active" => 'f',
206  "token_scope" => "w"
207  ];
208 
209  $this->authHelper->isTokenActive($activeTokenRow, $tokenId);
210 
211  $this->expectException(HttpForbiddenException::class);
212 
213  $this->authHelper->isTokenActive($expireTokenRow, $tokenId);
214  }
215 
224  {
225  $key = "mysecretkey";
226  $createdOn = strftime('%Y-%m-%d', strtotime('-3 day'));
227  $expire = strftime('%Y-%m-%d', strtotime('-1 day'));
228  $tokenId = 4;
229  $tokenRow = [
230  "token_key" => $key,
231  "created_on" => $createdOn,
232  "expire_on" => $expire,
233  "user_fk" => 2,
234  "active" => 't',
235  "token_scope" => "w"
236  ];
237 
238  $this->dbHelper->shouldReceive('invalidateToken')
239  ->withArgs([$tokenId])->once();
240  $this->expectException(HttpForbiddenException::class);
241 
242  $this->authHelper->isTokenActive($tokenRow, $tokenId);
243  }
244 
251  public function testUserHasGroupAccess()
252  {
253  $userId = 3;
254  $groupName = 'fossy';
255  $groupMap = [
256  2 => 'fossy',
257  3 => 'read',
258  4 => 'write'
259  ];
260 
261  $this->userDao->shouldReceive('getGroupIdByName')
262  ->withArgs([$groupName])->andReturn(['group_pk' => 2])->once();
263  $this->userDao->shouldReceive('getUserGroupMap')
264  ->withArgs([$userId])->andReturn($groupMap)->twice();
265 
266  $this->authHelper->userHasGroupAccess($userId, $groupName);
267 
268  $groupName = 'random';
269  $this->userDao->shouldReceive('getGroupIdByName')
270  ->withArgs([$groupName])->andReturn(['group_pk' => 6])->once();
271 
272  $this->expectException(HttpForbiddenException::class);
273 
274  $this->authHelper->userHasGroupAccess($userId, $groupName);
275  }
276 }
Provides helper methods for REST api.
Definition: AuthHelper.php:38
Provides helper methods to access database for REST api.
Definition: DbHelper.php:38