Move Apps to OCSController

This commit is contained in:
Roeland Jago Douma 2016-08-12 10:27:08 +02:00
parent 8f4adebab7
commit 092b767ef9
No known key found for this signature in database
GPG Key ID: 1E152838F164D13B
3 changed files with 78 additions and 64 deletions

View File

@ -33,6 +33,12 @@ use OCP\API;
$app = new \OCA\Provisioning_API\AppInfo\Application(); $app = new \OCA\Provisioning_API\AppInfo\Application();
$app->registerRoutes($this, [ $app->registerRoutes($this, [
'ocs' => [ '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 // Groups
['root' => '/cloud', 'name' => 'Groups#getGroups', 'url' => '/groups', 'verb' => 'GET'], ['root' => '/cloud', 'name' => 'Groups#getGroups', 'url' => '/groups', 'verb' => 'GET'],
['root' => '/cloud', 'name' => 'Groups#getGroup', 'url' => '/groups/{groupId}', '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);

View File

@ -23,89 +23,101 @@
* *
*/ */
namespace OCA\Provisioning_API; namespace OCA\Provisioning_API\Controller;
use OC\OCSClient; use OC\OCSClient;
use \OC_App; 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 */ /** @var \OCP\App\IAppManager */
private $appManager; private $appManager;
/** @var OCSClient */ /** @var OCSClient */
private $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, public function __construct(
OCSClient $ocsClient) { $appName,
IRequest $request,
IAppManager $appManager,
OCSClient $ocsClient
) {
parent::__construct($appName, $request);
$this->appManager = $appManager; $this->appManager = $appManager;
$this->ocsClient = $ocsClient; $this->ocsClient = $ocsClient;
} }
/** /**
* @param array $parameters * @param string $filter
* @return \OC\OCS\Result * @return DataResponse
* @throws OCSException
*/ */
public function getApps($parameters) { public function getApps($filter = null) {
$apps = OC_App::listAllApps(false, true, $this->ocsClient); $apps = OC_App::listAllApps(false, true, $this->ocsClient);
$list = []; $list = [];
foreach($apps as $app) { foreach($apps as $app) {
$list[] = $app['id']; $list[] = $app['id'];
} }
$filter = isset($_GET['filter']) ? $_GET['filter'] : false;
if($filter){ if($filter){
switch($filter){ switch($filter){
case 'enabled': case 'enabled':
return new \OC\OCS\Result(array('apps' => \OC_App::getEnabledApps())); return new DataResponse(['apps' => \OC_App::getEnabledApps()]);
break; break;
case 'disabled': case 'disabled':
$enabled = OC_App::getEnabledApps(); $enabled = OC_App::getEnabledApps();
return new \OC\OCS\Result(array('apps' => array_diff($list, $enabled))); return new DataResponse(['apps' => array_diff($list, $enabled)]);
break; break;
default: default:
// Invalid filter variable // Invalid filter variable
return new \OC\OCS\Result(null, 101); throw new OCSException('', 101);
break;
} }
} else { } else {
return new \OC\OCS\Result(array('apps' => $list)); return new DataResponse(['apps' => $list]);
} }
} }
/** /**
* @param array $parameters * @param string $app
* @return \OC\OCS\Result * @return DataResponse
* @throws OCSNotFoundException
*/ */
public function getAppInfo($parameters) { public function getAppInfo($app) {
$app = $parameters['appid'];
$info = \OCP\App::getAppInfo($app); $info = \OCP\App::getAppInfo($app);
if(!is_null($info)) { if(!is_null($info)) {
return new \OC\OCS\Result(OC_App::getAppInfo($app)); return new DataResponse(OC_App::getAppInfo($app));
} else { } 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 * @param string $app
* @return \OC\OCS\Result * @return DataResponse
*/ */
public function enable($parameters) { public function enable($app) {
$app = $parameters['appid'];
$this->appManager->enableApp($app); $this->appManager->enableApp($app);
return new \OC\OCS\Result(null, 100); return new DataResponse();
} }
/** /**
* @param array $parameters * @param string $app
* @return \OC\OCS\Result * @return DataResponse
*/ */
public function disable($parameters) { public function disable($app) {
$app = $parameters['appid'];
$this->appManager->disableApp($app); $this->appManager->disableApp($app);
return new \OC\OCS\Result(null, 100); return new DataResponse();
} }
} }

View File

@ -25,11 +25,11 @@
* *
*/ */
namespace OCA\Provisioning_API\Tests; namespace OCA\Provisioning_API\Tests\Controller;
use OC\OCSClient; use OC\OCSClient;
use OCA\Provisioning_API\Apps; use OCA\Provisioning_API\Controller\AppsController;
use OCP\API; use OCP\API;
use OCP\App\IAppManager; use OCP\App\IAppManager;
use OCP\IUserSession; use OCP\IUserSession;
@ -41,10 +41,10 @@ use OCP\IUserSession;
* *
* @package OCA\Provisioning_API\Tests * @package OCA\Provisioning_API\Tests
*/ */
class AppsTest extends TestCase { class AppsControllerTest extends \OCA\Provisioning_API\Tests\TestCase {
/** @var IAppManager */ /** @var IAppManager */
private $appManager; private $appManager;
/** @var Apps */ /** @var AppsController */
private $api; private $api;
/** @var IUserSession */ /** @var IUserSession */
private $userSession; private $userSession;
@ -61,20 +61,30 @@ class AppsTest extends TestCase {
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->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() { public function testGetAppInfo() {
$result = $this->api->getAppInfo(['appid' => 'provisioning_api']); $result = $this->api->getAppInfo('provisioning_api');
$this->assertInstanceOf('\OC\OCS\Result', $result); $expected = \OC_App::getAppInfo('provisioning_api');
$this->assertTrue($result->succeeded()); $this->assertEquals($expected, $result->getData());
} }
/**
* @expectedException \OCP\AppFramework\OCS\OCSException
* @expectedExceptionCode 998
*/
public function testGetAppInfoOnBadAppID() { public function testGetAppInfoOnBadAppID() {
$result = $this->api->getAppInfo(['appid' => 'not_provisioning_api']); $this->api->getAppInfo('not_provisioning_api');
$this->assertInstanceOf('\OC\OCS\Result', $result);
$this->assertFalse($result->succeeded());
$this->assertEquals(API::RESPOND_NOT_FOUND, $result->getStatusCode());
} }
public function testGetApps() { public function testGetApps() {
@ -86,17 +96,14 @@ class AppsTest extends TestCase {
$this->groupManager->get('admin')->addUser($user); $this->groupManager->get('admin')->addUser($user);
$this->userSession->setUser($user); $this->userSession->setUser($user);
$result = $this->api->getApps([]); $result = $this->api->getApps();
$this->assertTrue($result->succeeded());
$data = $result->getData(); $data = $result->getData();
$this->assertEquals(count(\OC_App::listAllApps(false, true, $this->ocsClient)), count($data['apps'])); $this->assertEquals(count(\OC_App::listAllApps(false, true, $this->ocsClient)), count($data['apps']));
} }
public function testGetAppsEnabled() { public function testGetAppsEnabled() {
$_GET['filter'] = 'enabled'; $result = $this->api->getApps('enabled');
$result = $this->api->getApps(['filter' => 'enabled']);
$this->assertTrue($result->succeeded());
$data = $result->getData(); $data = $result->getData();
$this->assertEquals(count(\OC_App::getEnabledApps()), count($data['apps'])); $this->assertEquals(count(\OC_App::getEnabledApps()), count($data['apps']));
} }
@ -106,9 +113,7 @@ class AppsTest extends TestCase {
->expects($this->any()) ->expects($this->any())
->method($this->anything()) ->method($this->anything())
->will($this->returnValue(null)); ->will($this->returnValue(null));
$_GET['filter'] = 'disabled'; $result = $this->api->getApps('disabled');
$result = $this->api->getApps(['filter' => 'disabled']);
$this->assertTrue($result->succeeded());
$data = $result->getData(); $data = $result->getData();
$apps = \OC_App::listAllApps(false, true, $this->ocsClient); $apps = \OC_App::listAllApps(false, true, $this->ocsClient);
$list = array(); $list = array();
@ -119,10 +124,11 @@ class AppsTest extends TestCase {
$this->assertEquals(count($disabled), count($data['apps'])); $this->assertEquals(count($disabled), count($data['apps']));
} }
/**
* @expectedException \OCP\AppFramework\OCS\OCSException
* @expectedExceptionCode 101
*/
public function testGetAppsInvalidFilter() { public function testGetAppsInvalidFilter() {
$_GET['filter'] = 'foo'; $this->api->getApps('foo');
$result = $this->api->getApps([]);
$this->assertFalse($result->succeeded());
$this->assertEquals(101, $result->getStatusCode());
} }
} }