summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorJoas Schilling <coding@schilljs.com>2021-04-21 08:58:35 +0200
committerJoas Schilling <coding@schilljs.com>2021-05-12 08:16:07 +0200
commitb6c6527705695a343b055f89bdde5ec497914ff1 (patch)
treeb52adc3a0b203add9a971cd1e2bf0ef9666af23a /tests
parent0599a8060ceb6518bb3981c88fc14f215d80f562 (diff)
downloadnextcloud-server-b6c6527705695a343b055f89bdde5ec497914ff1.tar.gz
nextcloud-server-b6c6527705695a343b055f89bdde5ec497914ff1.zip
Fix unauthorized OCS status in provisioning
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/lib/AppFramework/Middleware/OCSMiddlewareTest.php99
1 files changed, 49 insertions, 50 deletions
diff --git a/tests/lib/AppFramework/Middleware/OCSMiddlewareTest.php b/tests/lib/AppFramework/Middleware/OCSMiddlewareTest.php
index 1ee6da9ca30..83b764f6c65 100644
--- a/tests/lib/AppFramework/Middleware/OCSMiddlewareTest.php
+++ b/tests/lib/AppFramework/Middleware/OCSMiddlewareTest.php
@@ -98,29 +98,24 @@ class OCSMiddlewareTest extends \Test\TestCase {
$OCSMiddleware = new OCSMiddleware($this->request);
$OCSMiddleware->beforeController($controller, 'method');
- try {
- $result = $OCSMiddleware->afterException($controller, 'method', $exception);
- $this->assertFalse($forward);
+ if ($forward) {
+ $this->expectException(get_class($exception));
+ $this->expectExceptionMessage($exception->getMessage());
+ }
- $this->assertInstanceOf(V1Response::class, $result);
+ $result = $OCSMiddleware->afterException($controller, 'method', $exception);
- $this->assertSame($message, $this->invokePrivate($result, 'statusMessage'));
+ $this->assertInstanceOf(V1Response::class, $result);
- if ($exception->getCode() === 0) {
- $this->assertSame(\OCP\AppFramework\OCSController::RESPOND_UNKNOWN_ERROR, $result->getOCSStatus());
- } else {
- $this->assertSame($code, $result->getOCSStatus());
- }
+ $this->assertSame($message, $this->invokePrivate($result, 'statusMessage'));
- if ($exception instanceof OCSForbiddenException) {
- $this->assertSame(Http::STATUS_UNAUTHORIZED, $result->getStatus());
- } else {
- $this->assertSame(Http::STATUS_OK, $result->getStatus());
- }
- } catch (\Exception $e) {
- $this->assertTrue($forward);
- $this->assertEquals($exception, $e);
+ if ($exception->getCode() === 0) {
+ $this->assertSame(\OCP\AppFramework\OCSController::RESPOND_UNKNOWN_ERROR, $result->getOCSStatus());
+ } else {
+ $this->assertSame($code, $result->getOCSStatus());
}
+
+ $this->assertSame(Http::STATUS_OK, $result->getStatus());
}
/**
@@ -139,23 +134,22 @@ class OCSMiddlewareTest extends \Test\TestCase {
$OCSMiddleware = new OCSMiddleware($this->request);
$OCSMiddleware->beforeController($controller, 'method');
- try {
- $result = $OCSMiddleware->afterException($controller, 'method', $exception);
- $this->assertFalse($forward);
+ if ($forward) {
+ $this->expectException(get_class($exception));
+ $this->expectExceptionMessage($exception->getMessage());
+ }
- $this->assertInstanceOf(V2Response::class, $result);
+ $result = $OCSMiddleware->afterException($controller, 'method', $exception);
- $this->assertSame($message, $this->invokePrivate($result, 'statusMessage'));
- if ($exception->getCode() === 0) {
- $this->assertSame(\OCP\AppFramework\OCSController::RESPOND_UNKNOWN_ERROR, $result->getOCSStatus());
- } else {
- $this->assertSame($code, $result->getOCSStatus());
- }
- $this->assertSame($code, $result->getStatus());
- } catch (\Exception $e) {
- $this->assertTrue($forward);
- $this->assertEquals($exception, $e);
+ $this->assertInstanceOf(V2Response::class, $result);
+
+ $this->assertSame($message, $this->invokePrivate($result, 'statusMessage'));
+ if ($exception->getCode() === 0) {
+ $this->assertSame(\OCP\AppFramework\OCSController::RESPOND_UNKNOWN_ERROR, $result->getOCSStatus());
+ } else {
+ $this->assertSame($code, $result->getOCSStatus());
}
+ $this->assertSame($code, $result->getStatus());
}
/**
@@ -174,23 +168,22 @@ class OCSMiddlewareTest extends \Test\TestCase {
$OCSMiddleware = new OCSMiddleware($this->request);
$OCSMiddleware->beforeController($controller, 'method');
- try {
- $result = $OCSMiddleware->afterException($controller, 'method', $exception);
- $this->assertFalse($forward);
+ if ($forward) {
+ $this->expectException(get_class($exception));
+ $this->expectExceptionMessage($exception->getMessage());
+ }
- $this->assertInstanceOf(V2Response::class, $result);
+ $result = $OCSMiddleware->afterException($controller, 'method', $exception);
- $this->assertSame($message, $this->invokePrivate($result, 'statusMessage'));
- if ($exception->getCode() === 0) {
- $this->assertSame(\OCP\AppFramework\OCSController::RESPOND_UNKNOWN_ERROR, $result->getOCSStatus());
- } else {
- $this->assertSame($code, $result->getOCSStatus());
- }
- $this->assertSame($code, $result->getStatus());
- } catch (\Exception $e) {
- $this->assertTrue($forward);
- $this->assertEquals($exception, $e);
+ $this->assertInstanceOf(V2Response::class, $result);
+
+ $this->assertSame($message, $this->invokePrivate($result, 'statusMessage'));
+ if ($exception->getCode() === 0) {
+ $this->assertSame(\OCP\AppFramework\OCSController::RESPOND_UNKNOWN_ERROR, $result->getOCSStatus());
+ } else {
+ $this->assertSame($code, $result->getOCSStatus());
}
+ $this->assertSame($code, $result->getStatus());
}
public function dataAfterController() {
@@ -205,7 +198,7 @@ class OCSMiddlewareTest extends \Test\TestCase {
[$OCSController, new Http\Response(), false],
[$OCSController, new Http\JSONResponse(), false],
[$OCSController, new Http\JSONResponse(['message' => 'foo']), false],
- [$OCSController, new Http\JSONResponse(['message' => 'foo'], Http::STATUS_UNAUTHORIZED), true],
+ [$OCSController, new Http\JSONResponse(['message' => 'foo'], Http::STATUS_UNAUTHORIZED), true, OCSController::RESPOND_UNAUTHORISED],
[$OCSController, new Http\JSONResponse(['message' => 'foo'], Http::STATUS_FORBIDDEN), true],
[$controller, new Http\Response(), false],
@@ -223,8 +216,9 @@ class OCSMiddlewareTest extends \Test\TestCase {
* @param Controller $controller
* @param Http\Response $response
* @param bool $converted
+ * @param int $convertedOCSStatus
*/
- public function testAfterController($controller, $response, $converted) {
+ public function testAfterController($controller, $response, $converted, $convertedOCSStatus = 0) {
$OCSMiddleware = new OCSMiddleware($this->request);
$newResponse = $OCSMiddleware->afterController($controller, 'foo', $response);
@@ -233,8 +227,13 @@ class OCSMiddlewareTest extends \Test\TestCase {
} else {
$this->assertInstanceOf(BaseResponse::class, $newResponse);
$this->assertSame($response->getData()['message'], $this->invokePrivate($newResponse, 'statusMessage'));
- $this->assertSame(\OCP\AppFramework\OCSController::RESPOND_UNAUTHORISED, $newResponse->getOCSStatus());
- $this->assertSame(Http::STATUS_UNAUTHORIZED, $newResponse->getStatus());
+
+ if ($convertedOCSStatus) {
+ $this->assertSame($convertedOCSStatus, $newResponse->getOCSStatus());
+ } else {
+ $this->assertSame($response->getStatus(), $newResponse->getOCSStatus());
+ }
+ $this->assertSame($response->getStatus(), $newResponse->getStatus());
}
}
}