30 use Slim\Psr7\Factory\StreamFactory;
31 use Slim\Psr7\Headers;
32 use Slim\Psr7\Request;
76 protected function setUp() : void
79 $container = M::mock(
'ContainerBuilder');
80 $this->dbHelper = M::mock(DbHelper::class);
81 $this->restHelper = M::mock(RestHelper::class);
83 $this->restHelper->shouldReceive(
'getDbHelper')->andReturn($this->dbHelper);
85 $container->shouldReceive(
'get')->withArgs(array(
86 'helper.restHelper'))->andReturn($this->restHelper);
88 $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
89 $this->streamFactory =
new StreamFactory();
98 $this->addToAssertionCount(
99 \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
132 $authHelper = M::mock(AuthHelper::class);
133 $authHelper->shouldReceive(
'checkUsernameAndPassword')->withArgs([
134 'foss',
'foss'])->andReturn(
true);
135 $authHelper->shouldReceive(
'generateJwtToken')->withArgs([
136 '2020-01-01',
'2020-01-01',
'2.2',
'r', M::any()])
137 ->andReturn(
"sometoken");
138 $this->dbHelper->shouldReceive(
'insertNewTokenKey')->withArgs(array(
139 2,
'2020-01-01',
'r',
'test_token', M::any()))->andReturn([
141 "created_on" =>
'2020-01-01'
143 $this->restHelper->shouldReceive(
'validateTokenRequest')->withArgs(array(
144 '2020-01-01',
'test_token',
'r'))->andReturnNull();
145 $this->restHelper->shouldReceive(
'getAuthHelper')->andReturn($authHelper);
146 $this->restHelper->shouldReceive(
'getUserId')->andReturn(2);
147 $container->shouldReceive(
'get')->withArgs(array(
'helper.restHelper'))
148 ->andReturn($this->restHelper);
150 if ($version == ApiVersion::V2) {
152 "username" =>
"foss",
153 "password" =>
"foss",
154 "tokenName" =>
"test_token",
155 "tokenScope" =>
"read",
156 "tokenExpire" =>
"2020-01-01"
160 "username" =>
"foss",
161 "password" =>
"foss",
162 "token_name" =>
"test_token",
163 "token_scope" =>
"read",
164 "token_expire" =>
"2020-01-01"
167 $body = $this->streamFactory->createStream(json_encode($bodyContent));
168 $requestHeaders =
new Headers();
169 $requestHeaders->setHeader(
'Content-Type',
'application/json');
170 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost"),
171 $requestHeaders, [], [], $body);
172 if ($version == ApiVersion::V2) {
173 $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,
177 $GLOBALS[
'SysConf'] = [
'AUTHENTICATION' => [
'resttoken' =>
'token']];
178 $response = $this->authController->createNewJwtToken($request, $response,
180 $response->getBody()->seek(0);
181 $this->assertEquals([
"Authorization" =>
"Bearer sometoken"],
182 json_decode($response->getBody()->getContents(),
true));
183 $this->assertEquals(201, $response->getStatusCode());
196 $authHelper = M::mock(AuthHelper::class);
197 $authHelper->shouldReceive(
'checkUsernameAndPassword')->withArgs([
198 'foss',
'foss'])->andReturn(
true);
199 $authHelper->shouldReceive(
'generateJwtToken')->withArgs([
200 '2020-01-02',
'2020-01-01',
'2.2',
'r', M::any()])
201 ->andReturn(
"sometoken");
202 $this->dbHelper->shouldReceive(
'insertNewTokenKey')->withArgs(array(
203 2,
'2020-01-02',
'r',
'test_token', M::any()))->andReturn([
205 "created_on" =>
'2020-01-01'
207 $this->restHelper->shouldReceive(
'validateTokenRequest') ->withArgs(array(
208 '2020-01-02',
'test_token',
'r'))->andThrowExceptions([
212 $this->restHelper->shouldReceive(
'getAuthHelper')->andReturn($authHelper);
213 $this->restHelper->shouldReceive(
'getUserId')->andReturn(2);
214 $container->shouldReceive(
'get')->withArgs(array(
215 'helper.restHelper'))->andReturn($this->restHelper);
217 $body = $this->streamFactory->createStream(json_encode([
218 "username" =>
"foss",
219 "password" =>
"foss",
220 "token_name" =>
"test_token",
221 "token_scope" =>
"read",
222 "token_expire" =>
"2020-01-02"
224 $requestHeaders =
new Headers();
225 $requestHeaders->setHeader(
'Content-Type',
'application/json');
226 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost"), $requestHeaders,
229 $GLOBALS[
'SysConf'] = [
'AUTHENTICATION' => [
'resttoken' =>
'token']];
231 $this->expectException(HttpBadRequestException::class);
232 $this->expectExceptionCode(400);
234 $this->authController->createNewJwtToken($request, $response, []);
247 $authHelper = M::mock(AuthHelper::class);
248 $authHelper->shouldReceive(
'checkUsernameAndPassword')->withArgs([
249 'foss',
'foss'])->andReturn(
false);
250 $this->restHelper->shouldReceive(
'validateTokenRequest')->withArgs(array(
251 '2020-01-03',
'test_token',
'r'))->andReturnNull();
252 $this->restHelper->shouldReceive(
'getAuthHelper')->andReturn($authHelper);
253 $this->restHelper->shouldReceive(
'getUserId')->andReturn(2);
254 $container->shouldReceive(
'get')->withArgs(array(
255 'helper.restHelper'))->andReturn($this->restHelper);
257 $body = $this->streamFactory->createStream(json_encode([
258 "username" =>
"foss",
259 "password" =>
"foss",
260 "token_name" =>
"test_token",
261 "token_scope" =>
"read",
262 "token_expire" =>
"2020-01-03"
264 $requestHeaders =
new Headers();
265 $requestHeaders->setHeader(
'Content-Type',
'application/json');
266 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost"), $requestHeaders,
269 $GLOBALS[
'SysConf'] = [
'AUTHENTICATION' => [
'resttoken' =>
'token']];
270 $this->expectException(HttpNotFoundException::class);
271 $this->expectExceptionCode(404);
273 $this->authController->createNewJwtToken($request, $response, []);
Controller for Auth requests.
Provides helper methods for REST api.
Provides helper methods to access database for REST api.
Override Slim response for withJson function.
Provides various DAO helper functions for REST api.
tearDown()
Remove test objects.
testCreateNewJwtTokenInvalidPassword()
testCreateNewJwtToken(int $version)
testCreateNewJwtTokenExpiredToken()
setUp()
Setup test objects.
testCreateNewJwtTokenV2()
testCreateNewJwtTokenV1()