From 76b0740bec3866538840e8b05cf240ea18f0f73b Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 8 Dec 2015 16:13:30 +0100 Subject: Extend integration tests to test all status codes --- tests/integration/controller.php | 28 ++++++++++ .../features/bootstrap/FeatureContext.php | 29 ++++++++++ tests/integration/features/notifications.feature | 20 ------- tests/integration/features/statuscodes.feature | 34 +++++++++++ tests/integration/notifier.php | 65 ++++++++++++++++++++++ 5 files changed, 156 insertions(+), 20 deletions(-) delete mode 100644 tests/integration/features/notifications.feature create mode 100644 tests/integration/features/statuscodes.feature create mode 100644 tests/integration/notifier.php (limited to 'tests') diff --git a/tests/integration/controller.php b/tests/integration/controller.php index ec3297f..549244a 100644 --- a/tests/integration/controller.php +++ b/tests/integration/controller.php @@ -75,6 +75,30 @@ class Controller extends \OCP\AppFramework\Controller { return new \OC_OCS_Result(); } + /** + * @NoCSRFRequired + * + * @return \OC_OCS_Result + */ + public function addNotification() { + if (!$this->config->getAppValue('notifications', 'debug')) { + return new \OC_OCS_Result(null, Http::STATUS_FORBIDDEN); + } + + $notification = $this->manager->createNotification(); + $notification->setApp('testing') + ->setDateTime(\DateTime::createFromFormat('U', 1449585176)) // 2015-12-08T14:32:56+00:00 + ->setUser('test1') + ->setSubject('testing') + ->setLink('https://www.owncloud.org/') + ->setMessage('message') + ->setObject('object', 23); + + $this->manager->notify($notification); + + return new \OC_OCS_Result(); + } + /** * @NoCSRFRequired * @@ -85,6 +109,10 @@ class Controller extends \OCP\AppFramework\Controller { return new \OC_OCS_Result(null, Http::STATUS_FORBIDDEN); } + $notification = $this->manager->createNotification(); + $notification->setApp('testing'); + $this->manager->markProcessed($notification); + $this->config->deleteAppValue('notifications', 'forceHasNotifiers'); return new \OC_OCS_Result(); } diff --git a/tests/integration/features/bootstrap/FeatureContext.php b/tests/integration/features/bootstrap/FeatureContext.php index 6b9cfb9..66576ce 100644 --- a/tests/integration/features/bootstrap/FeatureContext.php +++ b/tests/integration/features/bootstrap/FeatureContext.php @@ -31,6 +31,35 @@ class FeatureContext implements Context, SnippetAcceptingContext { } } + /** + * @Given /^user "([^"]*)" has notifications$/ + */ + public function hasNotifications($user) { + if ($user === 'test1') { + $response = $this->setTestingValue('POST', 'apps/notifications/testing/notifications', null); + PHPUnit_Framework_Assert::assertEquals(200, $response->getStatusCode()); + PHPUnit_Framework_Assert::assertEquals(200, (int) $this->getOCSResponse($response)); + } + } + + /** + * @Then /^list of notifications has (\d+) entries$/ + */ + public function checkNumNotifications($numNotifications) { + $notifications = $this->getArrayOfNotificationsResponded($this->response); + PHPUnit_Framework_Assert::assertCount((int) $numNotifications, $notifications); + } + + /** + * Parses the xml answer to get the array of users returned. + * @param ResponseInterface $resp + * @return array + */ + public function getArrayOfNotificationsResponded($resp) { + $jsonResponse = json_decode($resp->getBody()->getContents(), 1); + return $jsonResponse['ocs']['data']; + } + /** * @BeforeSuite */ diff --git a/tests/integration/features/notifications.feature b/tests/integration/features/notifications.feature deleted file mode 100644 index 01f90c9..0000000 --- a/tests/integration/features/notifications.feature +++ /dev/null @@ -1,20 +0,0 @@ -Feature: notifications - Background: - Given using api version "2" - - Scenario: Read notifications without Notifiers - Given user "test1" exists - Given As an "test1" - Given list of notifiers is not empty - When sending "GET" to "/apps/notifications/api/v1/notifications" - # "200 OK" - Because there is no notifier - Then the HTTP status code should be "200" - - Scenario: Read notifications with Notifiers - Given user "test1" exists - Given As an "test1" - Given list of notifiers is empty - When sending "GET" to "/apps/notifications/api/v1/notifications" - # "204 No Content" - Because there is no notifier - Then the HTTP status code should be "204" - diff --git a/tests/integration/features/statuscodes.feature b/tests/integration/features/statuscodes.feature new file mode 100644 index 0000000..b4f3034 --- /dev/null +++ b/tests/integration/features/statuscodes.feature @@ -0,0 +1,34 @@ +Feature: statuscodes + Background: + Given using api version "2" + Given user "test1" exists + Given As an "test1" + + Scenario: Status code when reading notifications without notifiers + Given list of notifiers is empty + When sending "GET" to "/apps/notifications/api/v1/notifications?format=json" + # "204 No Content" + Then the HTTP status code should be "204" + # Request-Body is empty: And list of notifications has 0 entries + + Scenario: Status code when reading notifications with notifiers and without notifications + Given list of notifiers is not empty + When sending "GET" to "/apps/notifications/api/v1/notifications?format=json" + Then the HTTP status code should be "200" + And list of notifications has 0 entries + + Scenario: Status code when reading notifications with notifiers and notification + Given list of notifiers is not empty + Given user "test1" has notifications + When sending "GET" to "/apps/notifications/api/v1/notifications?format=json" + Then the HTTP status code should be "200" + And list of notifications has 1 entries + + Scenario: Status code when reading notifications with notifiers and notifications + Given list of notifiers is not empty + Given user "test1" has notifications + Given user "test1" has notifications + Given user "test1" has notifications + When sending "GET" to "/apps/notifications/api/v1/notifications?format=json" + Then the HTTP status code should be "200" + And list of notifications has 3 entries diff --git a/tests/integration/notifier.php b/tests/integration/notifier.php new file mode 100644 index 0000000..96cd45a --- /dev/null +++ b/tests/integration/notifier.php @@ -0,0 +1,65 @@ + + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see + * + */ + +namespace OCA\Notifications\Tests\Integration; + +use OC\Notification\INotification; +use OC\Notification\INotifier; +use OCP\IConfig; + +class Notifier implements INotifier { + + /** @var IConfig */ + private $config; + + /** + * @param IConfig $config + */ + public function __construct(IConfig $config) { + $this->config = $config; + } + + /** + * @return bool + */ + protected function isDebugMode() { + if ($this->config->getAppValue('notifications', 'debug', '') !== '' && $this->config->getAppValue('notifications', 'forceHasNotifiers', '') !== '') { + return $this->config->getAppValue('notifications', 'forceHasNotifiers') === 'true'; + } + return false; + } + + /** + * @param INotification $notification + * @param string $languageCode The code of the language that should be used to prepare the notification + * @return INotification + * @throws \InvalidArgumentException When the notification was not prepared by a notifier + */ + public function prepare(INotification $notification, $languageCode) { + if ($this->isDebugMode() && $notification->getApp() === 'testing') { + $notification->setParsedSubject($notification->getSubject()); + $notification->setParsedMessage($notification->getMessage()); + return $notification; + } + + throw new \InvalidArgumentException(); + } +} -- cgit v1.2.3