From 2895cac0beb8371ec8d3724692962bc074e556e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Pablo=20Villaf=C3=A1=C3=B1ez?= Date: Mon, 26 Mar 2018 11:57:09 +0200 Subject: [PATCH] Adjust tests and fix code --- lib/Controller/EndpointV2Controller.php | 26 ++++++--- .../Controller/EndpointV2ControllerTest.php | 55 ++++++++++--------- 2 files changed, 49 insertions(+), 32 deletions(-) diff --git a/lib/Controller/EndpointV2Controller.php b/lib/Controller/EndpointV2Controller.php index 65554ee0..84d59e07 100644 --- a/lib/Controller/EndpointV2Controller.php +++ b/lib/Controller/EndpointV2Controller.php @@ -72,7 +72,9 @@ public function __construct(Handler $handler, IManager $manager, IUserSession $u public function listNotifications($id = null, $fetch = 'desc', $limit = self::ENFORCED_LIST_LIMIT, $format = 'json') { $userObject = $this->userSession->getUser(); if ($userObject === null) { - return new OCSResponse($format, Http::STATUS_FORBIDDEN, null, [], null, null, true); + $ocsResponse = new OCSResponse($format, Http::STATUS_FORBIDDEN, null, [], null, null, true); + $ocsResponse->setStatus(Http::STATUS_FORBIDDEN); + return $ocsResponse; } $userid = $userObject->getUID(); @@ -146,14 +148,18 @@ public function listNotifications($id = null, $fetch = 'desc', $limit = self::EN public function getNotification($id, $format = 'json') { $userObject = $this->userSession->getUser(); if ($userObject === null) { - return new OCSResponse($format, Http::STATUS_FORBIDDEN, null, [], null, null, true); + $ocsResponse = new OCSResponse($format, Http::STATUS_FORBIDDEN, null, [], null, null, true); + $ocsResponse->setStatus(Http::STATUS_FORBIDDEN); + return $ocsResponse; } $userid = $userObject->getUID(); $notification = $this->handler->getById($id, $userid); if (!($notification instanceof INotification)) { - return new OCSResponse($format, Http::STATUS_NOT_FOUND, null, [], null, null, true); + $ocsResponse = new OCSResponse($format, Http::STATUS_NOT_FOUND, null, [], null, null, true); + $ocsResponse->setStatus(Http::STATUS_NOT_FOUND); + return $ocsResponse; } $language = $this->config->getUserValue($userid, 'core', 'lang', null); @@ -162,11 +168,13 @@ public function getNotification($id, $format = 'json') { $notification = $this->manager->prepare($notification, $language); } catch (\InvalidArgumentException $e) { // The app was disabled - return new OCSResponse($format, Http::STATUS_NOT_FOUND, null, [], null, null, true); + $ocsResponse = new OCSResponse($format, Http::STATUS_NOT_FOUND, null, [], null, null, true); + $ocsResponse->setStatus(Http::STATUS_NOT_FOUND); + return $ocsResponse; } return new OCSResponse($format, Http::STATUS_OK, null, - $this->notificationToArray($id, $notificationToArray), null, null, true); + $this->notificationToArray($id, $notification), null, null, true); } /** @@ -180,7 +188,9 @@ public function getNotification($id, $format = 'json') { public function deleteNotification($id, $format = 'json') { $userObject = $this->userSession->getUser(); if ($userObject === null) { - return new OCSResponse($format, Http::STATUS_FORBIDDEN, null, [], null, null, true); + $ocsResponse = new OCSResponse($format, Http::STATUS_FORBIDDEN, null, [], null, null, true); + $ocsResponse->setStatus(Http::STATUS_FORBIDDEN); + return $ocsResponse; } $userid = $userObject->getUID(); @@ -198,7 +208,9 @@ public function deleteNotification($id, $format = 'json') { public function getLastNotificationId($format = 'json') { $userObject = $this->userSession->getUser(); if ($userObject === null) { - return new OCSResponse($format, Http::STATUS_FORBIDDEN, null, [], null, null, true); + $ocsResponse = new OCSResponse($format, Http::STATUS_FORBIDDEN, null, [], null, null, true); + $ocsResponse->setStatus(Http::STATUS_FORBIDDEN); + return $ocsResponse; } $userid = $userObject->getUID(); diff --git a/tests/Unit/Controller/EndpointV2ControllerTest.php b/tests/Unit/Controller/EndpointV2ControllerTest.php index 83322527..5ea7e691 100644 --- a/tests/Unit/Controller/EndpointV2ControllerTest.php +++ b/tests/Unit/Controller/EndpointV2ControllerTest.php @@ -159,16 +159,17 @@ public function testListNotificationsDescendent() { ->willReturn('http://server/owncloud/route?id=20&fetch=desc&limit=20'); // we won't check the url returned by the urlGenerator, any path will do - $jsonResult = $this->controller->listNotifications(); - $this->assertEquals(Http::STATUS_OK, $jsonResult->getStatus()); + $ocsResult = $this->controller->listNotifications(); + $this->assertEquals(Http::STATUS_OK, $ocsResult->getStatus()); - $rawData = $jsonResult->getData(); - $this->assertEquals($maxResults, count($rawData['data'])); - $this->assertTrue($this->isKeySortedTopToBottom($rawData['data'])); + $rawData = json_decode($ocsResult->render(), true); + $rawData = $rawData['ocs']['data']; + $this->assertEquals($maxResults, count($rawData['notifications'])); + $this->assertTrue($this->isKeySortedTopToBottom($rawData['notifications'])); $this->assertArrayHasKey('next', $rawData); $this->assertEquals('http://server/owncloud/route?id=20&fetch=desc&limit=20', $rawData['next']); - $resultHeaders = $jsonResult->getHeaders(); + $resultHeaders = $ocsResult->getHeaders(); $this->assertArrayHasKey('OC-Last-Notification', $resultHeaders); $this->assertEquals(123, $resultHeaders['OC-Last-Notification']); } @@ -202,16 +203,17 @@ public function testListNotificationsAscendent() { ->willReturn('http://server/owncloud/route?id=20&fetch=asc&limit=20'); // we won't check the url returned by the urlGenerator, any path will do - $jsonResult = $this->controller->listNotifications(null, 'asc'); - $this->assertEquals(Http::STATUS_OK, $jsonResult->getStatus()); + $ocsResult = $this->controller->listNotifications(null, 'asc'); + $this->assertEquals(Http::STATUS_OK, $ocsResult->getStatus()); - $rawData = $jsonResult->getData(); - $this->assertEquals($maxResults, count($rawData['data'])); - $this->assertTrue($this->isKeySortedBottomToTop($rawData['data'])); + $rawData = json_decode($ocsResult->render(), true); + $rawData = $rawData['ocs']['data']; + $this->assertEquals($maxResults, count($rawData['notifications'])); + $this->assertTrue($this->isKeySortedBottomToTop($rawData['notifications'])); $this->assertArrayHasKey('next', $rawData); $this->assertEquals('http://server/owncloud/route?id=20&fetch=asc&limit=20', $rawData['next']); - $resultHeaders = $jsonResult->getHeaders(); + $resultHeaders = $ocsResult->getHeaders(); $this->assertArrayHasKey('OC-Last-Notification', $resultHeaders); $this->assertEquals(123, $resultHeaders['OC-Last-Notification']); } @@ -239,15 +241,16 @@ public function testListNotificationsDescendentNoMoreResults() { $this->manager->method('prepare') ->willReturn($this->returnArgument(0)); - $jsonResult = $this->controller->listNotifications(); - $this->assertEquals(Http::STATUS_OK, $jsonResult->getStatus()); + $ocsResult = $this->controller->listNotifications(); + $this->assertEquals(Http::STATUS_OK, $ocsResult->getStatus()); - $rawData = $jsonResult->getData(); - $this->assertLessThan($maxResults, count($rawData['data'])); - $this->assertTrue($this->isKeySortedTopToBottom($rawData['data'])); + $rawData = json_decode($ocsResult->render(), true); + $rawData = $rawData['ocs']['data']; + $this->assertLessThan($maxResults, count($rawData['notifications'])); + $this->assertTrue($this->isKeySortedTopToBottom($rawData['notifications'])); $this->assertArrayNotHasKey('next', $rawData); - $resultHeaders = $jsonResult->getHeaders(); + $resultHeaders = $ocsResult->getHeaders(); $this->assertArrayHasKey('OC-Last-Notification', $resultHeaders); $this->assertEquals(123, $resultHeaders['OC-Last-Notification']); } @@ -334,10 +337,11 @@ public function testGetNotification() { $this->manager->method('prepare') ->willReturn($notification); - $jsonResponse = $this->controller->getNotification(5); - $this->assertEquals(Http::STATUS_OK, $jsonResponse->getStatus()); + $ocsResult = $this->controller->getNotification(5); + $this->assertEquals(Http::STATUS_OK, $ocsResult->getStatus()); - $rawData = $jsonResponse->getData(); + $rawData = json_decode($ocsResult->render(), true); + $rawData = $rawData['ocs']['data']; $this->assertEquals('5', $rawData['notification_id']); $this->assertEquals($datetime->format('c'), $rawData['datetime']); $this->assertEquals('the parsed subject', $rawData['subject']); @@ -382,13 +386,14 @@ public function testGetLastNotification() { $this->handler->method('getMaxNotificationId') ->willReturn(321); - $jsonResponse = $this->controller->getLastNotificationId(); - $this->assertEquals(Http::STATUS_OK, $jsonResponse->getStatus()); + $ocsResult = $this->controller->getLastNotificationId(); + $this->assertEquals(Http::STATUS_OK, $ocsResult->getStatus()); - $rawData = $jsonResponse->getData(); + $rawData = json_decode($ocsResult->render(), true); + $rawData = $rawData['ocs']['data']; $this->assertEquals(['id' => 321], $rawData); - $resultHeaders = $jsonResponse->getHeaders(); + $resultHeaders = $ocsResult->getHeaders(); $this->assertArrayHasKey('OC-Last-Notification', $resultHeaders); $this->assertEquals(321, $resultHeaders['OC-Last-Notification']); }