Skip to content

Commit

Permalink
Adjust tests and fix code
Browse files Browse the repository at this point in the history
  • Loading branch information
jvillafanez committed Mar 26, 2018
1 parent 4a208ae commit 2895cac
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 32 deletions.
26 changes: 19 additions & 7 deletions lib/Controller/EndpointV2Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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);
Expand All @@ -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);
}

/**
Expand All @@ -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();

Expand All @@ -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();

Expand Down
55 changes: 30 additions & 25 deletions tests/Unit/Controller/EndpointV2ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
Expand Down Expand Up @@ -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']);
}
Expand Down Expand Up @@ -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']);
}
Expand Down Expand Up @@ -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']);
Expand Down Expand Up @@ -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']);
}
Expand Down

0 comments on commit 2895cac

Please sign in to comment.