26 use Symfony\Component\HttpFoundation\Session\Session;
69 protected function setUp() : void
71 $this->userDao = M::mock(UserDao::class);
72 $this->session = M::mock(Session::class);
73 $this->dbHelper = M::mock(DbHelper::class);
75 $this->session->shouldReceive(
'isStarted')->andReturn(
true);
77 $this->authHelper =
new AuthHelper($this->userDao, $this->session,
79 $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
88 $this->addToAssertionCount(
89 \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
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,
113 $authHeader =
"Bearer " . $authToken;
116 "created_on" => $createdOn,
117 "expire_on" => $expire,
118 "user_fk" => $expectedUser,
123 $this->dbHelper->shouldReceive(
'getTokenKey')
125 ->andReturn($tokenRow);
126 $this->userDao->shouldReceive(
'isUserIdActive')
127 ->withArgs([$expectedUser])
130 $GLOBALS[
'SysConf'] = [
'AUTHENTICATION' => [
'resttoken' =>
'token']];
131 $this->authHelper->verifyAuthToken($authHeader, $userId,
134 $this->assertEquals($expectedUser, $userId);
135 $this->assertEquals(
"write", $tokenScope);
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,
156 $authHeader =
"Bearer " . $authToken;
159 "created_on" => $createdOn,
160 "expire_on" => $expire,
161 "user_fk" => $expectedUser,
166 $this->dbHelper->shouldReceive(
'getTokenKey')
168 ->andReturn($tokenRow);
169 $this->userDao->shouldReceive(
'isUserIdActive')
170 ->withArgs([$expectedUser])
173 $GLOBALS[
'SysConf'] = [
'AUTHENTICATION' => [
'resttoken' =>
'token']];
175 $this->expectException(HttpForbiddenException::class);
177 $this->authHelper->verifyAuthToken($authHeader, $userId, $tokenScope);
188 $key =
"mysecretkey";
189 $createdOn = strftime(
'%Y-%m-%d');
190 $expire = strftime(
'%Y-%m-%d', strtotime(
'+3 day'));
194 "created_on" => $createdOn,
195 "expire_on" => $expire,
202 "created_on" => $createdOn,
203 "expire_on" => $expire,
209 $this->authHelper->isTokenActive($activeTokenRow, $tokenId);
211 $this->expectException(HttpForbiddenException::class);
213 $this->authHelper->isTokenActive($expireTokenRow, $tokenId);
225 $key =
"mysecretkey";
226 $createdOn = strftime(
'%Y-%m-%d', strtotime(
'-3 day'));
227 $expire = strftime(
'%Y-%m-%d', strtotime(
'-1 day'));
231 "created_on" => $createdOn,
232 "expire_on" => $expire,
238 $this->dbHelper->shouldReceive(
'invalidateToken')
239 ->withArgs([$tokenId])->once();
240 $this->expectException(HttpForbiddenException::class);
242 $this->authHelper->isTokenActive($tokenRow, $tokenId);
254 $groupName =
'fossy';
261 $this->userDao->shouldReceive(
'getGroupIdByName')
262 ->withArgs([$groupName])->andReturn([
'group_pk' => 2])->once();
263 $this->userDao->shouldReceive(
'getUserGroupMap')
264 ->withArgs([$userId])->andReturn($groupMap)->twice();
266 $this->authHelper->userHasGroupAccess($userId, $groupName);
268 $groupName =
'random';
269 $this->userDao->shouldReceive(
'getGroupIdByName')
270 ->withArgs([$groupName])->andReturn([
'group_pk' => 6])->once();
272 $this->expectException(HttpForbiddenException::class);
274 $this->authHelper->userHasGroupAccess($userId, $groupName);
Provides helper methods for REST api.
Provides helper methods to access database for REST api.
Test cases for AuthHelper.
tearDown()
Remove test objects.
testIsTokenActiveExpireOldToken()
setUp()
Setup test objects.
testVerifyAuthTokenInactiveUser()