|
2 | 2 |
|
3 | 3 | namespace OCA\Solid\Controller; |
4 | 4 |
|
| 5 | +use Laminas\Diactoros\Response; |
5 | 6 | use OC\AppFramework\Http; |
6 | 7 | use OCA\Solid\AppInfo\Application; |
7 | 8 | use OCA\Solid\Service\UserService; |
@@ -310,6 +311,68 @@ public function testRegisterWithRedirectUris() |
310 | 311 | ], $actual); |
311 | 312 | } |
312 | 313 |
|
| 314 | + /** |
| 315 | + * @testdox ServerController should consume Post, Server, and Session variables when generating a token |
| 316 | + * |
| 317 | + * @covers ::token |
| 318 | + */ |
| 319 | + public function testToken() |
| 320 | + { |
| 321 | + $_POST['client_id'] = self::MOCK_CLIENT_ID; |
| 322 | + $_POST['code'] = ''; |
| 323 | + $_SERVER['HTTP_DPOP'] = 'mock dpop'; |
| 324 | + $_SESSION['nonce'] = 'mock nonce'; |
| 325 | + |
| 326 | + $parameters = $this->createMockConstructorParameters(); |
| 327 | + |
| 328 | + // @FIXME: Use actual TokenGenerator when we know how to make a valid 'code' for the test |
| 329 | + $mockTokenGenerator = $this->createMock(\Pdsinterop\Solid\Auth\TokenGenerator::class); |
| 330 | + $mockTokenGenerator->method('getCodeInfo')->willReturn(['user_id' => self::MOCK_USER_ID]); |
| 331 | + $mockTokenGenerator->expects($this->once()) |
| 332 | + ->method('addIdTokenToResponse') |
| 333 | + ->with( |
| 334 | + $this->isInstanceOf(Response::class), |
| 335 | + $_POST['client_id'], |
| 336 | + self::MOCK_USER_ID, |
| 337 | + $_SESSION['nonce'], |
| 338 | + self::$privateKey, |
| 339 | + $_SERVER['HTTP_DPOP'], |
| 340 | + ) |
| 341 | + ->willReturn(new Response('php://memory', Http::STATUS_IM_A_TEAPOT, [ |
| 342 | + 'Content-Type' => 'mock application type' |
| 343 | + ])); |
| 344 | + |
| 345 | + $controller = new ServerController(...$parameters); |
| 346 | + |
| 347 | + $reflectionObject = new \ReflectionObject($controller); |
| 348 | + $reflectionProperty = $reflectionObject->getProperty('tokenGenerator'); |
| 349 | + $reflectionProperty->setAccessible(true); |
| 350 | + $reflectionProperty->setValue($controller, $mockTokenGenerator); |
| 351 | + |
| 352 | + $tokenResponse = $controller->token(); |
| 353 | + |
| 354 | + $expected = [ |
| 355 | + 'data' => "I'm a teapot", |
| 356 | + 'headers' => [ |
| 357 | + 'Cache-Control' => 'no-cache, no-store, must-revalidate', |
| 358 | + 'Content-Security-Policy' => "default-src 'none';base-uri 'none';manifest-src 'self';frame-ancestors 'none'", |
| 359 | + 'Feature-Policy' => "autoplay 'none';camera 'none';fullscreen 'none';geolocation 'none';microphone 'none';payment 'none'", |
| 360 | + 'X-Robots-Tag' => 'noindex, nofollow', |
| 361 | + 'Content-Type' => 'application/json; charset=utf-8', |
| 362 | + ], |
| 363 | + 'status' => Http::STATUS_IM_A_TEAPOT, |
| 364 | + ]; |
| 365 | + |
| 366 | + $actual = [ |
| 367 | + 'data' => $tokenResponse->getData(), |
| 368 | + 'headers' => $tokenResponse->getHeaders(), |
| 369 | + 'status' => $tokenResponse->getStatus(), |
| 370 | + ]; |
| 371 | + unset($actual['headers']['X-Request-Id']); |
| 372 | + |
| 373 | + |
| 374 | + $this->assertEquals($expected, $actual); |
| 375 | + } |
313 | 376 | ////////////////////////////// MOCKS AND STUBS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\ |
314 | 377 |
|
315 | 378 | public function createMockConfig($clientData): IConfig|MockObject |
|
0 commit comments