From 96ba051c5c8b357a37161ee417dab04786e83ca1 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 8 Dec 2015 14:42:05 +0100 Subject: Add tests for empty notifications list 200/204 --- tests/integration/controller.php | 91 ++++++++++++++++++++++ .../features/bootstrap/FeatureContext.php | 43 ++++++++++ tests/integration/features/notifications.feature | 9 +++ tests/integration/run.sh | 8 +- tests/unit/controller/EndpointControllerTest.php | 4 + 5 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 tests/integration/controller.php (limited to 'tests') diff --git a/tests/integration/controller.php b/tests/integration/controller.php new file mode 100644 index 0000000..ec3297f --- /dev/null +++ b/tests/integration/controller.php @@ -0,0 +1,91 @@ + + * + * @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\IManager; +use OCP\AppFramework\Http; +use OCP\IConfig; +use OCP\IRequest; + +class Controller extends \OCP\AppFramework\Controller { + + /** @var IConfig */ + private $config; + + /** @var IManager */ + private $manager; + + /** + * @param string $appName + * @param IRequest $request + * @param IConfig $config + * @param IManager $manager + */ + public function __construct($appName, IRequest $request, IConfig $config, IManager $manager) { + parent::__construct($appName, $request); + + $this->config = $config; + $this->manager = $manager; + } + + /** + * @NoCSRFRequired + * + * @return \OC_OCS_Result + */ + public function fillNotifiers() { + if (!$this->config->getAppValue('notifications', 'debug')) { + return new \OC_OCS_Result(null, Http::STATUS_FORBIDDEN); + } + + $this->config->setAppValue('notifications', 'forceHasNotifiers', 'true'); + return new \OC_OCS_Result(); + } + + /** + * @NoCSRFRequired + * + * @return \OC_OCS_Result + */ + public function clearNotifiers() { + if (!$this->config->getAppValue('notifications', 'debug')) { + return new \OC_OCS_Result(null, Http::STATUS_FORBIDDEN); + } + + $this->config->setAppValue('notifications', 'forceHasNotifiers', 'false'); + return new \OC_OCS_Result(); + } + + /** + * @NoCSRFRequired + * + * @return \OC_OCS_Result + */ + public function reset() { + if (!$this->config->getAppValue('notifications', 'debug')) { + return new \OC_OCS_Result(null, Http::STATUS_FORBIDDEN); + } + + $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 321a4de..6b9cfb9 100644 --- a/tests/integration/features/bootstrap/FeatureContext.php +++ b/tests/integration/features/bootstrap/FeatureContext.php @@ -12,9 +12,25 @@ use GuzzleHttp\Message\ResponseInterface; * Defines application features from the specific context. */ class FeatureContext implements Context, SnippetAcceptingContext { + use BasicStructure; use Provisioning; + /** + * @Given /^list of notifiers (is|is not) empty$/ + */ + public function hasNotifiers($noNotifiers) { + if ($noNotifiers === 'is') { + $response = $this->setTestingValue('DELETE', 'apps/notifications/testing/notifiers', null); + PHPUnit_Framework_Assert::assertEquals(200, $response->getStatusCode()); + PHPUnit_Framework_Assert::assertEquals(200, (int) $this->getOCSResponse($response)); + } else { + $response = $this->setTestingValue('POST', 'apps/notifications/testing/notifiers', null); + PHPUnit_Framework_Assert::assertEquals(200, $response->getStatusCode()); + PHPUnit_Framework_Assert::assertEquals(200, (int) $this->getOCSResponse($response)); + } + } + /** * @BeforeSuite */ @@ -28,4 +44,31 @@ class FeatureContext implements Context, SnippetAcceptingContext { public static function removeFilesFromSkeleton() { // The path to the skeleton files does not match, and we don't need them } + + /** + * @AfterScenario + */ + public function removeDebugConfigs() { + $response = $this->setTestingValue('DELETE', 'apps/notifications/testing', null); + PHPUnit_Framework_Assert::assertEquals(200, $response->getStatusCode()); + PHPUnit_Framework_Assert::assertEquals(200, (int) $this->getOCSResponse($response)); + } + + protected function setTestingValue($verb, $url, $body) { + $fullUrl = $this->baseUrl . "v2.php/" . $url; + $client = new Client(); + $options = [ + 'auth' => $this->adminUser, + ]; + if ($body instanceof \Behat\Gherkin\Node\TableNode) { + $fd = $body->getRowsHash(); + $options['body'] = $fd; + } + + try { + return $client->send($client->createRequest($verb, $fullUrl, $options)); + } catch (\GuzzleHttp\Exception\ClientException $ex) { + return $ex->getResponse(); + } + } } diff --git a/tests/integration/features/notifications.feature b/tests/integration/features/notifications.feature index 08b669a..01f90c9 100644 --- a/tests/integration/features/notifications.feature +++ b/tests/integration/features/notifications.feature @@ -5,6 +5,15 @@ Feature: notifications 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/run.sh b/tests/integration/run.sh index 41c7f7a..c7f9d5c 100644 --- a/tests/integration/run.sh +++ b/tests/integration/run.sh @@ -1,7 +1,9 @@ #!/usr/bin/env bash +APP_NAME=notifications + APP_INTEGRATION_DIR=$PWD -ROOT_DIR=../../../.. +ROOT_DIR=${APP_INTEGRATION_DIR}/../../../.. cd ${ROOT_DIR}/build/integration composer install @@ -11,10 +13,14 @@ php -S localhost:8080 -t ${ROOT_DIR} & PHPPID=$! echo $PHPPID +${ROOT_DIR}/occ config:app:set ${APP_NAME} debug --value="on" + export TEST_SERVER_URL="http://localhost:8080/ocs/" ${ROOT_DIR}/build/integration/vendor/bin/behat -f junit -f pretty RESULT=$? kill $PHPPID +${ROOT_DIR}/occ config:app:delete ${APP_NAME} debug + exit $RESULT diff --git a/tests/unit/controller/EndpointControllerTest.php b/tests/unit/controller/EndpointControllerTest.php index e09a733..e875cff 100644 --- a/tests/unit/controller/EndpointControllerTest.php +++ b/tests/unit/controller/EndpointControllerTest.php @@ -69,6 +69,10 @@ class EndpointControllerTest extends TestCase { $this->config = $this->getMockBuilder('OCP\IConfig') ->disableOriginalConstructor() ->getMock(); + $this->config->expects($this->any()) + ->method('getAppValue') + ->with('notifications', 'forceHasNotifiers', $this->anything()) + ->willReturnArgument(2); /** @var \OCP\IUserSession|\PHPUnit_Framework_MockObject_MockObject */ $this->session = $this->getMockBuilder('OCP\IUserSession') -- cgit v1.2.3