Skip to content
This repository has been archived by the owner on Jul 6, 2020. It is now read-only.

Commit

Permalink
add status api route
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernhard Posselt committed Feb 17, 2015
1 parent 77365fe commit 816a8be
Show file tree
Hide file tree
Showing 10 changed files with 230 additions and 67 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
owncloud-news (5.2.4)
* **Enhancement**: Add a new API route to check for the status and possible problems

owncloud-news (5.2.3)
* **Enhancement**: Push explore button at the bottom of the feed list
* **Enhancement**: When passing a negative batchSizes to the item API, all items will be returned
Expand Down
2 changes: 1 addition & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<author>Bernhard Posselt, Alessandro Cosentino, Jan-Christoph Borchardt</author>
<category>multimedia</category>
<licence>AGPL</licence>
<version>5.2.3</version>
<version>5.2.4</version>
<namespace>News</namespace>

<!-- resources -->
Expand Down
1 change: 1 addition & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@

// API 1.2
['name' => 'utility_api#version', 'url' => '/api/v1-2/version', 'verb' => 'GET'],
['name' => 'utility_api#status', 'url' => '/api/v1-2/status', 'verb' => 'GET'],
['name' => 'utility_api#before_update', 'url' => '/api/v1-2/cleanup/before-update', 'verb' => 'GET'],
['name' => 'utility_api#after_update', 'url' => '/api/v1-2/cleanup/after-update', 'verb' => 'GET'],
['name' => 'utility_api#preflighted_cors', 'url' => '/api/v1-2/{path}', 'verb' => 'OPTIONS', 'requirements' => ['path' => '.+']],
Expand Down
39 changes: 18 additions & 21 deletions controller/pagecontroller.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@

namespace OCA\News\Controller;

use \OCP\AppFramework\Http\TemplateResponse;
use \OCP\AppFramework\Http\JSONResponse;
use \OCP\IRequest;
use \OCP\IConfig;
use \OCP\IL10N;
use \OCP\IURLGenerator;
use \OCP\AppFramework\Controller;

use \OCA\News\Config\AppConfig;
use \OCA\News\Config\Config;
use \OCA\News\Explore\RecommendedSites;
use \OCA\News\Db\FeedType;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\AppFramework\Controller;

use OCA\News\Service\StatusService;
use OCA\News\Config\AppConfig;
use OCA\News\Config\Config;
use OCA\News\Explore\RecommendedSites;
use OCA\News\Db\FeedType;

class PageController extends Controller {

Expand All @@ -35,6 +36,7 @@ class PageController extends Controller {
private $urlGenerator;
private $config;
private $recommendedSites;
private $statusService;

public function __construct($AppName,
IRequest $request,
Expand All @@ -44,6 +46,7 @@ public function __construct($AppName,
Config $config,
IL10N $l10n,
RecommendedSites $recommendedSites,
StatusService $statusService,
$UserId){
parent::__construct($AppName, $request);
$this->settings = $settings;
Expand All @@ -53,6 +56,7 @@ public function __construct($AppName,
$this->userId = $UserId;
$this->config = $config;
$this->recommendedSites = $recommendedSites;
$this->statusService = $statusService;
}


Expand All @@ -61,17 +65,10 @@ public function __construct($AppName,
* @NoCSRFRequired
*/
public function index() {
$cronWarning = '';
$cronMode = $this->settings->getAppValue('core', 'backgroundjobs_mode');
$cronOn = $this->config->getUseCronUpdates();

// check for cron modes which may lead to problems
if ($cronMode !== 'cron' && $cronMode !== 'webcron' && $cronOn) {
$cronWarning = 'ajaxCron';
}
$status = $this->statusService->getStatus();

return new TemplateResponse($this->appName, 'index', [
'cronWarning' => $cronWarning
'cronWarning' => $status['warnings']['improperlyConfiguredCron']
]);
}

Expand Down
18 changes: 17 additions & 1 deletion controller/utilityapicontroller.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,24 @@
use \OCP\AppFramework\Http;

use \OCA\News\Utility\Updater;
use \OCA\News\Service\StatusService;


class UtilityApiController extends ApiController {

private $updater;
private $settings;
private $statusService;

public function __construct($AppName,
IRequest $request,
Updater $updater,
IConfig $settings){
IConfig $settings,
StatusService $statusService){
parent::__construct($AppName, $request);
$this->updater = $updater;
$this->settings = $settings;
$this->statusService = $statusService;
}


Expand All @@ -50,6 +54,7 @@ public function version() {

/**
* @NoCSRFRequired
* @CORS
*/
public function beforeUpdate() {
$this->updater->beforeUpdate();
Expand All @@ -58,10 +63,21 @@ public function beforeUpdate() {

/**
* @NoCSRFRequired
* @CORS
*/
public function afterUpdate() {
$this->updater->afterUpdate();
}


/**
* @CORS
* @NoCSRFRequired
* @NoAdminRequired
*/
public function status() {
return $this->statusService->getStatus();
}


}
59 changes: 59 additions & 0 deletions service/statusservice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* ownCloud - News
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Alessandro Cosentino <[email protected]>
* @author Bernhard Posselt <[email protected]>
* @copyright Alessandro Cosentino 2012
* @copyright Bernhard Posselt 2012, 2014
*/

namespace OCA\News\Service;

use OCP\IConfig;

use OCA\News\Config\Config;


class StatusService {

private $settings;
private $config;
private $appName;

public function __construct(IConfig $settings, Config $config, $AppName) {
$this->settings = $settings;
$this->config = $config;
$this->appName = $AppName;
}


public function getStatus() {
$improperlyConfiguredCron = false;

$version = $this->settings->getAppValue(
$this->appName, 'installed_version'
);
$cronMode = $this->settings->getAppValue(
'core', 'backgroundjobs_mode'
);
$cronOn = $this->config->getUseCronUpdates();

// check for cron modes which may lead to problems
if ($cronMode !== 'cron' && $cronMode !== 'webcron' && $cronOn) {
$improperlyConfiguredCron = true;
}


return [
'version' => $version,
'warnings' => [
'improperlyConfiguredCron' => $improperlyConfiguredCron
]
];
}

}
2 changes: 1 addition & 1 deletion templates/part.content.cronwarning.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php if ($_['cronWarning'] === 'ajaxCron') { ?>
<?php if ($_['cronWarning']) { ?>
<div id="cron-warning">
<p><?php p($l->t('Ajax cron mode detected! Your feeds will ' .
'not be updated correctly. It is recommended to either use ' .
Expand Down
65 changes: 23 additions & 42 deletions tests/unit/controller/PageControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class PageControllerTest extends \PHPUnit_Framework_TestCase {
private $configData;
private $config;
private $recommended;
private $status;

/**
* Gets run before each test
Expand Down Expand Up @@ -74,64 +75,44 @@ public function setUp(){
'\OCA\News\Explore\RecommendedSites')
->disableOriginalConstructor()
->getMock();
$this->status = $this->getMockBuilder(
'\OCA\News\Service\StatusService')
->disableOriginalConstructor()
->getMock();
$this->controller = new PageController($this->appName, $this->request,
$this->settings, $this->urlGenerator, $this->appConfig,
$this->config, $this->l10n, $this->recommended, $this->user);
$this->config, $this->l10n, $this->recommended, $this->status,
$this->user);
}


public function testIndex(){
$this->config->expects($this->once())
->method('getUseCronUpdates')
->will($this->returnValue(true));

$this->settings->expects($this->once())
->method('getAppValue')
->with(
$this->equalTo('core'),
$this->equalTo('backgroundjobs_mode')
)
->will($this->returnValue('webcron'));
$this->status->expects($this->once())
->method('getStatus')
->will($this->returnValue([
'warnings' => [
'improperlyConfiguredCron' => false
]
]));

$response = $this->controller->index();
$this->assertEquals('index', $response->getTemplateName());
$this->assertSame('', $response->getParams()['cronWarning']);
$this->assertSame(false, $response->getParams()['cronWarning']);
}


public function testIndexNoCorrectCronAjax(){
$this->config->expects($this->once())
->method('getUseCronUpdates')
->will($this->returnValue(true));

$this->settings->expects($this->once())
->method('getAppValue')
->with(
$this->equalTo('core'),
$this->equalTo('backgroundjobs_mode')
)
->will($this->returnValue('ajax'));

$response = $this->controller->index();
$this->assertEquals('ajaxCron', $response->getParams()['cronWarning']);
}


public function testIndexNoCorrectCronTurnedOff(){
$this->config->expects($this->once())
->method('getUseCronUpdates')
->will($this->returnValue(false));
$this->status->expects($this->once())
->method('getStatus')
->will($this->returnValue([
'warnings' => [
'improperlyConfiguredCron' => true
]
]));

$this->settings->expects($this->once())
->method('getAppValue')
->with(
$this->equalTo('core'),
$this->equalTo('backgroundjobs_mode')
)
->will($this->returnValue('ajax'));

$response = $this->controller->index();
$this->assertSame('', $response->getParams()['cronWarning']);
$this->assertEquals(true, $response->getParams()['cronWarning']);
}


Expand Down
19 changes: 18 additions & 1 deletion tests/unit/controller/UtilityApiControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class UtilityApiControllerTest extends \PHPUnit_Framework_TestCase {
private $newsAPI;
private $updater;
private $appName;
private $status;

protected function setUp() {
$this->appName = 'news';
Expand All @@ -36,8 +37,13 @@ protected function setUp() {
'\OCA\News\Utility\Updater')
->disableOriginalConstructor()
->getMock();
$this->status = $this->getMockBuilder(
'\OCA\News\Service\StatusService')
->disableOriginalConstructor()
->getMock();
$this->newsAPI = new UtilityApiController(
$this->appName, $this->request, $this->updater, $this->settings
$this->appName, $this->request, $this->updater, $this->settings,
$this->status
);
}

Expand Down Expand Up @@ -70,4 +76,15 @@ public function testAfterUpdate(){
}


public function testStatus(){
$in = 'hi';
$this->status->expects($this->once())
->method('getStatus')
->will($this->returnValue($in));
$result = $this->newsAPI->status();

$this->assertEquals($in, $result);
}


}
Loading

0 comments on commit 816a8be

Please sign in to comment.