From 092b767ef998a6afe2e01eb34aef1f8d21f6ec69 Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Fri, 12 Aug 2016 10:27:08 +0200 Subject: [PATCH] Move Apps to OCSController --- apps/provisioning_api/appinfo/routes.php | 16 ++--- .../AppsController.php} | 72 +++++++++++-------- .../AppsControllerTest.php} | 54 +++++++------- 3 files changed, 78 insertions(+), 64 deletions(-) rename apps/provisioning_api/lib/{Apps.php => Controller/AppsController.php} (54%) rename apps/provisioning_api/tests/{AppsTest.php => Controller/AppsControllerTest.php} (72%) 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/Controller/AppsController.php similarity index 54% rename from apps/provisioning_api/lib/Apps.php rename to apps/provisioning_api/lib/Controller/AppsController.php index f880e41905b..3821fc343ad 100644 --- a/apps/provisioning_api/lib/Apps.php +++ b/apps/provisioning_api/lib/Controller/AppsController.php @@ -23,89 +23,101 @@ * */ -namespace OCA\Provisioning_API; +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 Apps { +class AppsController extends OCSController { /** @var \OCP\App\IAppManager */ private $appManager; /** @var OCSClient */ private $ocsClient; /** - * @param \OCP\App\IAppManager $appManager + * @param string $appName + * @param IRequest $request + * @param IAppManager $appManager + * @param OCSClient $ocsClient */ - public function __construct(\OCP\App\IAppManager $appManager, - OCSClient $ocsClient) { + public function __construct( + $appName, + IRequest $request, + IAppManager $appManager, + OCSClient $ocsClient + ) { + parent::__construct($appName, $request); + $this->appManager = $appManager; $this->ocsClient = $ocsClient; } /** - * @param array $parameters - * @return \OC\OCS\Result + * @param string $filter + * @return DataResponse + * @throws OCSException */ - public function getApps($parameters) { + public function getApps($filter = null) { $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())); + return new DataResponse(['apps' => \OC_App::getEnabledApps()]); break; case 'disabled': $enabled = OC_App::getEnabledApps(); - return new \OC\OCS\Result(array('apps' => array_diff($list, $enabled))); + return new DataResponse(['apps' => array_diff($list, $enabled)]); break; default: // Invalid filter variable - return new \OC\OCS\Result(null, 101); - break; + throw new OCSException('', 101); } } else { - return new \OC\OCS\Result(array('apps' => $list)); + return new DataResponse(['apps' => $list]); } } /** - * @param array $parameters - * @return \OC\OCS\Result + * @param string $app + * @return DataResponse + * @throws OCSNotFoundException */ - public function getAppInfo($parameters) { - $app = $parameters['appid']; + public function getAppInfo($app) { $info = \OCP\App::getAppInfo($app); if(!is_null($info)) { - return new \OC\OCS\Result(OC_App::getAppInfo($app)); + return new DataResponse(OC_App::getAppInfo($app)); } else { - return new \OC\OCS\Result(null, \OCP\API::RESPOND_NOT_FOUND, 'The request app was not found'); + throw new OCSException('The request app was not found', \OCP\API::RESPOND_NOT_FOUND); } } /** - * @param array $parameters - * @return \OC\OCS\Result + * @param string $app + * @return DataResponse */ - public function enable($parameters) { - $app = $parameters['appid']; + public function enable($app) { $this->appManager->enableApp($app); - return new \OC\OCS\Result(null, 100); + return new DataResponse(); } /** - * @param array $parameters - * @return \OC\OCS\Result + * @param string $app + * @return DataResponse */ - public function disable($parameters) { - $app = $parameters['appid']; + public function disable($app) { $this->appManager->disableApp($app); - return new \OC\OCS\Result(null, 100); + return new DataResponse(); } } diff --git a/apps/provisioning_api/tests/AppsTest.php b/apps/provisioning_api/tests/Controller/AppsControllerTest.php similarity index 72% rename from apps/provisioning_api/tests/AppsTest.php rename to apps/provisioning_api/tests/Controller/AppsControllerTest.php index 35808b15816..9ac4a8290e4 100644 --- a/apps/provisioning_api/tests/AppsTest.php +++ b/apps/provisioning_api/tests/Controller/AppsControllerTest.php @@ -25,11 +25,11 @@ * */ -namespace OCA\Provisioning_API\Tests; +namespace OCA\Provisioning_API\Tests\Controller; use OC\OCSClient; -use OCA\Provisioning_API\Apps; +use OCA\Provisioning_API\Controller\AppsController; use OCP\API; use OCP\App\IAppManager; use OCP\IUserSession; @@ -41,10 +41,10 @@ use OCP\IUserSession; * * @package OCA\Provisioning_API\Tests */ -class AppsTest extends TestCase { +class AppsControllerTest extends \OCA\Provisioning_API\Tests\TestCase { /** @var IAppManager */ private $appManager; - /** @var Apps */ + /** @var AppsController */ private $api; /** @var IUserSession */ private $userSession; @@ -61,20 +61,30 @@ class AppsTest extends TestCase { ->disableOriginalConstructor() ->getMock(); - $this->api = new Apps($this->appManager, $this->ocsClient); + $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(['appid' => 'provisioning_api']); - $this->assertInstanceOf('\OC\OCS\Result', $result); - $this->assertTrue($result->succeeded()); + $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() { - $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()); + $this->api->getAppInfo('not_provisioning_api'); } public function testGetApps() { @@ -86,17 +96,14 @@ class AppsTest extends TestCase { $this->groupManager->get('admin')->addUser($user); $this->userSession->setUser($user); - $result = $this->api->getApps([]); + $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()); + $result = $this->api->getApps('enabled'); $data = $result->getData(); $this->assertEquals(count(\OC_App::getEnabledApps()), count($data['apps'])); } @@ -106,9 +113,7 @@ class AppsTest extends TestCase { ->expects($this->any()) ->method($this->anything()) ->will($this->returnValue(null)); - $_GET['filter'] = 'disabled'; - $result = $this->api->getApps(['filter' => 'disabled']); - $this->assertTrue($result->succeeded()); + $result = $this->api->getApps('disabled'); $data = $result->getData(); $apps = \OC_App::listAllApps(false, true, $this->ocsClient); $list = array(); @@ -119,10 +124,11 @@ class AppsTest extends TestCase { $this->assertEquals(count($disabled), count($data['apps'])); } + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 101 + */ public function testGetAppsInvalidFilter() { - $_GET['filter'] = 'foo'; - $result = $this->api->getApps([]); - $this->assertFalse($result->succeeded()); - $this->assertEquals(101, $result->getStatusCode()); + $this->api->getApps('foo'); } } -- 2.39.5