FOSSology  4.4.0
Open Source License Compliance by Open Source Software
AuthControllerTest.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 
29 use Mockery as M;
30 use Slim\Psr7\Factory\StreamFactory;
31 use Slim\Psr7\Headers;
32 use Slim\Psr7\Request;
33 use Slim\Psr7\Uri;
34 
39 class AuthControllerTest extends \PHPUnit\Framework\TestCase
40 {
41 
46  private $dbHelper;
47 
52  private $restHelper;
53 
58  private $authController;
59 
65 
70  private $streamFactory;
71 
76  protected function setUp() : void
77  {
78  global $container;
79  $container = M::mock('ContainerBuilder');
80  $this->dbHelper = M::mock(DbHelper::class);
81  $this->restHelper = M::mock(RestHelper::class);
82 
83  $this->restHelper->shouldReceive('getDbHelper')->andReturn($this->dbHelper);
84 
85  $container->shouldReceive('get')->withArgs(array(
86  'helper.restHelper'))->andReturn($this->restHelper);
87  $this->authController = new AuthController($container);
88  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
89  $this->streamFactory = new StreamFactory();
90  }
91 
96  protected function tearDown() : void
97  {
98  $this->addToAssertionCount(
99  \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
100  M::close();
101  }
102 
109  public function testCreateNewJwtTokenV1()
110  {
111  $this->testCreateNewJwtToken(ApiVersion::V1);
112  }
113 
120  public function testCreateNewJwtTokenV2()
121  {
122  $this->testCreateNewJwtToken(ApiVersion::V2);
123  }
124 
129  private function testCreateNewJwtToken(int $version)
130  {
131  global $container;
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([
140  "jti" => "2.2",
141  "created_on" => '2020-01-01'
142  ]);
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);
149 
150  if ($version == ApiVersion::V2) {
151  $bodyContent = [
152  "username" => "foss",
153  "password" => "foss",
154  "tokenName" => "test_token",
155  "tokenScope" => "read",
156  "tokenExpire" => "2020-01-01"
157  ];
158  } else {
159  $bodyContent = [
160  "username" => "foss",
161  "password" => "foss",
162  "token_name" => "test_token",
163  "token_scope" => "read",
164  "token_expire" => "2020-01-01"
165  ];
166  }
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,
174  ApiVersion::V2);
175  }
176  $response = new ResponseHelper();
177  $GLOBALS['SysConf'] = ['AUTHENTICATION' => ['resttoken' => 'token']];
178  $response = $this->authController->createNewJwtToken($request, $response,
179  []);
180  $response->getBody()->seek(0);
181  $this->assertEquals(["Authorization" => "Bearer sometoken"],
182  json_decode($response->getBody()->getContents(), true));
183  $this->assertEquals(201, $response->getStatusCode());
184  }
185 
194  {
195  global $container;
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([
204  "jti" => "2.2",
205  "created_on" => '2020-01-01'
206  ]);
207  $this->restHelper->shouldReceive('validateTokenRequest') ->withArgs(array(
208  '2020-01-02', 'test_token', 'r'))->andThrowExceptions([
210  "bad req"
211  )]);
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);
216 
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"
223  ]));
224  $requestHeaders = new Headers();
225  $requestHeaders->setHeader('Content-Type', 'application/json');
226  $request = new Request("POST", new Uri("HTTP", "localhost"), $requestHeaders,
227  [], [], $body);
228  $response = new ResponseHelper();
229  $GLOBALS['SysConf'] = ['AUTHENTICATION' => ['resttoken' => 'token']];
230 
231  $this->expectException(HttpBadRequestException::class);
232  $this->expectExceptionCode(400);
233 
234  $this->authController->createNewJwtToken($request, $response, []);
235  }
236 
245  {
246  global $container;
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);
256 
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"
263  ]));
264  $requestHeaders = new Headers();
265  $requestHeaders->setHeader('Content-Type', 'application/json');
266  $request = new Request("POST", new Uri("HTTP", "localhost"), $requestHeaders,
267  [], [], $body);
268  $response = new ResponseHelper();
269  $GLOBALS['SysConf'] = ['AUTHENTICATION' => ['resttoken' => 'token']];
270  $this->expectException(HttpNotFoundException::class);
271  $this->expectExceptionCode(404);
272 
273  $this->authController->createNewJwtToken($request, $response, []);
274  }
275 }
Provides helper methods for REST api.
Definition: AuthHelper.php:38
Provides helper methods to access database for REST api.
Definition: DbHelper.php:38
Override Slim response for withJson function.
Provides various DAO helper functions for REST api.
Definition: RestHelper.php:32