From: Roeland Jago Douma Date: Fri, 12 Aug 2016 08:27:08 +0000 (+0200) Subject: Move Apps to OCSController X-Git-Tag: v11.0RC2~821^2~1 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=092b767ef998a6afe2e01eb34aef1f8d21f6ec69;p=nextcloud-server.git Move Apps to OCSController --- diff --git a/apps/provisioning_api/appinfo/routes.php b/apps/provisioning_api/appinfo/routes.php index 473ae2ff716..a7366a32a06 100644 --- a/apps/provisioning_api/appinfo/routes.php +++ b/apps/provisioning_api/appinfo/routes.php @@ -33,6 +33,12 @@ use OCP\API; $app = new \OCA\Provisioning_API\AppInfo\Application(); $app->registerRoutes($this, [ 'ocs' => [ + // Apps + ['root' => '/cloud', 'name' => 'Apps#getApps', 'url' => '/apps', 'verb' => 'GET'], + ['root' => '/cloud', 'name' => 'Apps#getAppInfo', 'url' => '/apps/{app}', 'verb' => 'GET'], + ['root' => '/cloud', 'name' => 'Apps#enable', 'url' => '/apps/{app}', 'verb' => 'POST'], + ['root' => '/cloud', 'name' => 'Apps#disable', 'url' => '/apps/{app}', 'verb' => 'DELETE'], + // Groups ['root' => '/cloud', 'name' => 'Groups#getGroups', 'url' => '/groups', 'verb' => 'GET'], ['root' => '/cloud', 'name' => 'Groups#getGroup', 'url' => '/groups/{groupId}', 'verb' => 'GET'], @@ -57,13 +63,3 @@ $app->registerRoutes($this, [ ], ]); - -// Apps -$apps = new Apps( - \OC::$server->getAppManager(), - \OC::$server->getOcsClient() -); -API::register('get', '/cloud/apps', [$apps, 'getApps'], 'provisioning_api', API::ADMIN_AUTH); -API::register('get', '/cloud/apps/{appid}', [$apps, 'getAppInfo'], 'provisioning_api', API::ADMIN_AUTH); -API::register('post', '/cloud/apps/{appid}', [$apps, 'enable'], 'provisioning_api', API::ADMIN_AUTH); -API::register('delete', '/cloud/apps/{appid}', [$apps, 'disable'], 'provisioning_api', API::ADMIN_AUTH); diff --git a/apps/provisioning_api/lib/Apps.php b/apps/provisioning_api/lib/Apps.php deleted file mode 100644 index f880e41905b..00000000000 --- a/apps/provisioning_api/lib/Apps.php +++ /dev/null @@ -1,111 +0,0 @@ - - * @author Morris Jobke - * @author Roeland Jago Douma - * @author Tom Needham - * - * @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\Provisioning_API; - -use OC\OCSClient; -use \OC_App; - -class Apps { - /** @var \OCP\App\IAppManager */ - private $appManager; - /** @var OCSClient */ - private $ocsClient; - - /** - * @param \OCP\App\IAppManager $appManager - */ - public function __construct(\OCP\App\IAppManager $appManager, - OCSClient $ocsClient) { - $this->appManager = $appManager; - $this->ocsClient = $ocsClient; - } - - /** - * @param array $parameters - * @return \OC\OCS\Result - */ - public function getApps($parameters) { - $apps = OC_App::listAllApps(false, true, $this->ocsClient); - $list = []; - foreach($apps as $app) { - $list[] = $app['id']; - } - $filter = isset($_GET['filter']) ? $_GET['filter'] : false; - if($filter){ - switch($filter){ - case 'enabled': - return new \OC\OCS\Result(array('apps' => \OC_App::getEnabledApps())); - break; - case 'disabled': - $enabled = OC_App::getEnabledApps(); - return new \OC\OCS\Result(array('apps' => array_diff($list, $enabled))); - break; - default: - // Invalid filter variable - return new \OC\OCS\Result(null, 101); - break; - } - - } else { - return new \OC\OCS\Result(array('apps' => $list)); - } - } - - /** - * @param array $parameters - * @return \OC\OCS\Result - */ - public function getAppInfo($parameters) { - $app = $parameters['appid']; - $info = \OCP\App::getAppInfo($app); - if(!is_null($info)) { - return new \OC\OCS\Result(OC_App::getAppInfo($app)); - } else { - return new \OC\OCS\Result(null, \OCP\API::RESPOND_NOT_FOUND, 'The request app was not found'); - } - } - - /** - * @param array $parameters - * @return \OC\OCS\Result - */ - public function enable($parameters) { - $app = $parameters['appid']; - $this->appManager->enableApp($app); - return new \OC\OCS\Result(null, 100); - } - - /** - * @param array $parameters - * @return \OC\OCS\Result - */ - public function disable($parameters) { - $app = $parameters['appid']; - $this->appManager->disableApp($app); - return new \OC\OCS\Result(null, 100); - } - -} diff --git a/apps/provisioning_api/lib/Controller/AppsController.php b/apps/provisioning_api/lib/Controller/AppsController.php new file mode 100644 index 00000000000..3821fc343ad --- /dev/null +++ b/apps/provisioning_api/lib/Controller/AppsController.php @@ -0,0 +1,123 @@ + + * @author Morris Jobke + * @author Roeland Jago Douma + * @author Tom Needham + * + * @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\Provisioning_API\Controller; + +use OC\OCSClient; +use \OC_App; +use OCP\App\IAppManager; +use OCP\AppFramework\Http\DataResponse; +use OCP\AppFramework\OCS\OCSException; +use OCP\AppFramework\OCS\OCSNotFoundException; +use OCP\AppFramework\OCSController; +use OCP\IRequest; + +class AppsController extends OCSController { + /** @var \OCP\App\IAppManager */ + private $appManager; + /** @var OCSClient */ + private $ocsClient; + + /** + * @param string $appName + * @param IRequest $request + * @param IAppManager $appManager + * @param OCSClient $ocsClient + */ + public function __construct( + $appName, + IRequest $request, + IAppManager $appManager, + OCSClient $ocsClient + ) { + parent::__construct($appName, $request); + + $this->appManager = $appManager; + $this->ocsClient = $ocsClient; + } + + /** + * @param string $filter + * @return DataResponse + * @throws OCSException + */ + public function getApps($filter = null) { + $apps = OC_App::listAllApps(false, true, $this->ocsClient); + $list = []; + foreach($apps as $app) { + $list[] = $app['id']; + } + if($filter){ + switch($filter){ + case 'enabled': + return new DataResponse(['apps' => \OC_App::getEnabledApps()]); + break; + case 'disabled': + $enabled = OC_App::getEnabledApps(); + return new DataResponse(['apps' => array_diff($list, $enabled)]); + break; + default: + // Invalid filter variable + throw new OCSException('', 101); + } + + } else { + return new DataResponse(['apps' => $list]); + } + } + + /** + * @param string $app + * @return DataResponse + * @throws OCSNotFoundException + */ + public function getAppInfo($app) { + $info = \OCP\App::getAppInfo($app); + if(!is_null($info)) { + return new DataResponse(OC_App::getAppInfo($app)); + } else { + throw new OCSException('The request app was not found', \OCP\API::RESPOND_NOT_FOUND); + } + } + + /** + * @param string $app + * @return DataResponse + */ + public function enable($app) { + $this->appManager->enableApp($app); + return new DataResponse(); + } + + /** + * @param string $app + * @return DataResponse + */ + public function disable($app) { + $this->appManager->disableApp($app); + return new DataResponse(); + } + +} diff --git a/apps/provisioning_api/tests/AppsTest.php b/apps/provisioning_api/tests/AppsTest.php deleted file mode 100644 index 35808b15816..00000000000 --- a/apps/provisioning_api/tests/AppsTest.php +++ /dev/null @@ -1,128 +0,0 @@ - - * @author Lukas Reschke - * @author Morris Jobke - * @author Roeland Jago Douma - * @author Thomas Müller - * @author Tom Needham - * - * @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\Provisioning_API\Tests; - - -use OC\OCSClient; -use OCA\Provisioning_API\Apps; -use OCP\API; -use OCP\App\IAppManager; -use OCP\IUserSession; - -/** - * Class AppsTest - * - * @group DB - * - * @package OCA\Provisioning_API\Tests - */ -class AppsTest extends TestCase { - /** @var IAppManager */ - private $appManager; - /** @var Apps */ - private $api; - /** @var IUserSession */ - private $userSession; - /** @var OCSClient|\PHPUnit_Framework_MockObject_MockObject */ - private $ocsClient; - - protected function setUp() { - parent::setUp(); - - $this->appManager = \OC::$server->getAppManager(); - $this->groupManager = \OC::$server->getGroupManager(); - $this->userSession = \OC::$server->getUserSession(); - $this->ocsClient = $this->getMockBuilder('OC\OCSClient') - ->disableOriginalConstructor() - ->getMock(); - - $this->api = new Apps($this->appManager, $this->ocsClient); - } - - public function testGetAppInfo() { - $result = $this->api->getAppInfo(['appid' => 'provisioning_api']); - $this->assertInstanceOf('\OC\OCS\Result', $result); - $this->assertTrue($result->succeeded()); - } - - public function testGetAppInfoOnBadAppID() { - $result = $this->api->getAppInfo(['appid' => 'not_provisioning_api']); - $this->assertInstanceOf('\OC\OCS\Result', $result); - $this->assertFalse($result->succeeded()); - $this->assertEquals(API::RESPOND_NOT_FOUND, $result->getStatusCode()); - } - - public function testGetApps() { - $this->ocsClient - ->expects($this->any()) - ->method($this->anything()) - ->will($this->returnValue(null)); - $user = $this->generateUsers(); - $this->groupManager->get('admin')->addUser($user); - $this->userSession->setUser($user); - - $result = $this->api->getApps([]); - - $this->assertTrue($result->succeeded()); - $data = $result->getData(); - $this->assertEquals(count(\OC_App::listAllApps(false, true, $this->ocsClient)), count($data['apps'])); - } - - public function testGetAppsEnabled() { - $_GET['filter'] = 'enabled'; - $result = $this->api->getApps(['filter' => 'enabled']); - $this->assertTrue($result->succeeded()); - $data = $result->getData(); - $this->assertEquals(count(\OC_App::getEnabledApps()), count($data['apps'])); - } - - public function testGetAppsDisabled() { - $this->ocsClient - ->expects($this->any()) - ->method($this->anything()) - ->will($this->returnValue(null)); - $_GET['filter'] = 'disabled'; - $result = $this->api->getApps(['filter' => 'disabled']); - $this->assertTrue($result->succeeded()); - $data = $result->getData(); - $apps = \OC_App::listAllApps(false, true, $this->ocsClient); - $list = array(); - foreach($apps as $app) { - $list[] = $app['id']; - } - $disabled = array_diff($list, \OC_App::getEnabledApps()); - $this->assertEquals(count($disabled), count($data['apps'])); - } - - public function testGetAppsInvalidFilter() { - $_GET['filter'] = 'foo'; - $result = $this->api->getApps([]); - $this->assertFalse($result->succeeded()); - $this->assertEquals(101, $result->getStatusCode()); - } -} diff --git a/apps/provisioning_api/tests/Controller/AppsControllerTest.php b/apps/provisioning_api/tests/Controller/AppsControllerTest.php new file mode 100644 index 00000000000..9ac4a8290e4 --- /dev/null +++ b/apps/provisioning_api/tests/Controller/AppsControllerTest.php @@ -0,0 +1,134 @@ + + * @author Lukas Reschke + * @author Morris Jobke + * @author Roeland Jago Douma + * @author Thomas Müller + * @author Tom Needham + * + * @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\Provisioning_API\Tests\Controller; + + +use OC\OCSClient; +use OCA\Provisioning_API\Controller\AppsController; +use OCP\API; +use OCP\App\IAppManager; +use OCP\IUserSession; + +/** + * Class AppsTest + * + * @group DB + * + * @package OCA\Provisioning_API\Tests + */ +class AppsControllerTest extends \OCA\Provisioning_API\Tests\TestCase { + /** @var IAppManager */ + private $appManager; + /** @var AppsController */ + private $api; + /** @var IUserSession */ + private $userSession; + /** @var OCSClient|\PHPUnit_Framework_MockObject_MockObject */ + private $ocsClient; + + protected function setUp() { + parent::setUp(); + + $this->appManager = \OC::$server->getAppManager(); + $this->groupManager = \OC::$server->getGroupManager(); + $this->userSession = \OC::$server->getUserSession(); + $this->ocsClient = $this->getMockBuilder('OC\OCSClient') + ->disableOriginalConstructor() + ->getMock(); + + $request = $this->getMockBuilder('OCP\IRequest') + ->disableOriginalConstructor() + ->getMock(); + + $this->api = new AppsController( + 'provisioning_api', + $request, + $this->appManager, + $this->ocsClient + ); + } + + public function testGetAppInfo() { + $result = $this->api->getAppInfo('provisioning_api'); + $expected = \OC_App::getAppInfo('provisioning_api'); + $this->assertEquals($expected, $result->getData()); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 998 + */ + public function testGetAppInfoOnBadAppID() { + $this->api->getAppInfo('not_provisioning_api'); + } + + public function testGetApps() { + $this->ocsClient + ->expects($this->any()) + ->method($this->anything()) + ->will($this->returnValue(null)); + $user = $this->generateUsers(); + $this->groupManager->get('admin')->addUser($user); + $this->userSession->setUser($user); + + $result = $this->api->getApps(); + + $data = $result->getData(); + $this->assertEquals(count(\OC_App::listAllApps(false, true, $this->ocsClient)), count($data['apps'])); + } + + public function testGetAppsEnabled() { + $result = $this->api->getApps('enabled'); + $data = $result->getData(); + $this->assertEquals(count(\OC_App::getEnabledApps()), count($data['apps'])); + } + + public function testGetAppsDisabled() { + $this->ocsClient + ->expects($this->any()) + ->method($this->anything()) + ->will($this->returnValue(null)); + $result = $this->api->getApps('disabled'); + $data = $result->getData(); + $apps = \OC_App::listAllApps(false, true, $this->ocsClient); + $list = array(); + foreach($apps as $app) { + $list[] = $app['id']; + } + $disabled = array_diff($list, \OC_App::getEnabledApps()); + $this->assertEquals(count($disabled), count($data['apps'])); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 101 + */ + public function testGetAppsInvalidFilter() { + $this->api->getApps('foo'); + } +}