Skip to content

Commit

Permalink
Merge pull request #22536 from owncloud/add-integration-tests-for-tags
Browse files Browse the repository at this point in the history
Add integration tests for tags plus fix permissions
  • Loading branch information
DeepDiver1975 committed Feb 22, 2016
2 parents a70421f + 3bd95cc commit f3b13c7
Show file tree
Hide file tree
Showing 7 changed files with 1,139 additions and 6 deletions.
6 changes: 5 additions & 1 deletion apps/dav/lib/server.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ public function __construct(IRequest $request, $baseUri) {
$this->server->addPlugin(new \OCA\DAV\CardDAV\Plugin());

// system tags plugins
$this->server->addPlugin(new \OCA\DAV\SystemTag\SystemTagPlugin(\OC::$server->getSystemTagManager()));
$this->server->addPlugin(new \OCA\DAV\SystemTag\SystemTagPlugin(
\OC::$server->getSystemTagManager(),
\OC::$server->getGroupManager(),
\OC::$server->getUserSession()
));

// comments plugin
$this->server->addPlugin(new \OCA\DAV\Comments\CommentsPlugin(
Expand Down
1 change: 1 addition & 0 deletions apps/dav/lib/systemtag/systemtagnode.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public function setName($name) {
* @param bool $userVisible user visible
* @param bool $userAssignable user assignable
* @throws NotFound whenever the given tag id does not exist
* @throws Forbidden whenever there is no permission to update said tag
* @throws Conflict whenever a tag already exists with the given attributes
*/
public function update($name, $userVisible, $userAssignable) {
Expand Down
29 changes: 26 additions & 3 deletions apps/dav/lib/systemtag/systemtagplugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/
namespace OCA\DAV\SystemTag;

use OCP\IGroupManager;
use OCP\IUserSession;
use Sabre\DAV\Exception\NotFound;
use Sabre\DAV\PropFind;
use Sabre\DAV\PropPatch;
Expand Down Expand Up @@ -61,12 +63,26 @@ class SystemTagPlugin extends \Sabre\DAV\ServerPlugin {
protected $tagManager;

/**
* System tags plugin
*
* @var IUserSession
*/
protected $userSession;

/**
* @var IGroupManager
*/
protected $groupManager;

/**
* @param ISystemTagManager $tagManager tag manager
* @param IGroupManager $groupManager
* @param IUserSession $userSession
*/
public function __construct(ISystemTagManager $tagManager) {
public function __construct(ISystemTagManager $tagManager,
IGroupManager $groupManager,
IUserSession $userSession) {
$this->tagManager = $tagManager;
$this->userSession = $userSession;
$this->groupManager = $groupManager;
}

/**
Expand Down Expand Up @@ -163,6 +179,13 @@ private function createTag($data, $contentType = 'application/json') {
if (isset($data['userAssignable'])) {
$userAssignable = (bool)$data['userAssignable'];
}

if($userVisible === false || $userAssignable === false) {
if(!$this->userSession->isLoggedIn() || !$this->groupManager->isAdmin($this->userSession->getUser()->getUID())) {
throw new BadRequest('Not sufficient permissions');
}
}

try {
return $this->tagManager->createTag($tagName, $userVisible, $userAssignable);
} catch (TagAlreadyExistsException $e) {
Expand Down
255 changes: 253 additions & 2 deletions apps/dav/tests/unit/systemtag/systemtagplugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
namespace OCA\DAV\Tests\Unit\SystemTag;

use OC\SystemTag\SystemTag;
use OCP\IGroupManager;
use OCP\IUserSession;
use OCP\SystemTag\TagAlreadyExistsException;

class SystemTagPlugin extends \Test\TestCase {
Expand All @@ -46,6 +48,16 @@ class SystemTagPlugin extends \Test\TestCase {
*/
private $tagManager;

/**
* @var IGroupManager
*/
private $groupManager;

/**
* @var IUserSession
*/
private $userSession;

/**
* @var \OCA\DAV\SystemTag\SystemTagPlugin
*/
Expand All @@ -60,8 +72,14 @@ public function setUp() {
$this->server = new \Sabre\DAV\Server($this->tree);

$this->tagManager = $this->getMock('\OCP\SystemTag\ISystemTagManager');
$this->groupManager = $this->getMock('\OCP\IGroupManager');
$this->userSession = $this->getMock('\OCP\IUserSession');

$this->plugin = new \OCA\DAV\SystemTag\SystemTagPlugin($this->tagManager);
$this->plugin = new \OCA\DAV\SystemTag\SystemTagPlugin(
$this->tagManager,
$this->groupManager,
$this->userSession
);
$this->plugin->initialize($this->server);
}

Expand Down Expand Up @@ -153,7 +171,204 @@ public function testUpdateProperties() {
$this->assertEquals(200, $result[self::USERVISIBLE_PROPERTYNAME]);
}

/**
* @expectedException \Sabre\DAV\Exception\BadRequest
* @expectedExceptionMessage Not sufficient permissions
*/
public function testCreateNotAssignableTagAsRegularUser() {
$user = $this->getMock('\OCP\IUser');
$user->expects($this->once())
->method('getUID')
->willReturn('admin');
$this->userSession
->expects($this->once())
->method('isLoggedIn')
->willReturn(true);
$this->userSession
->expects($this->once())
->method('getUser')
->willReturn($user);
$this->groupManager
->expects($this->once())
->method('isAdmin')
->with('admin')
->willReturn(false);

$requestData = json_encode([
'name' => 'Test',
'userVisible' => true,
'userAssignable' => false,
]);

$node = $this->getMockBuilder('\OCA\DAV\SystemTag\SystemTagsByIdCollection')
->disableOriginalConstructor()
->getMock();
$this->tagManager->expects($this->never())
->method('createTag');

$this->tree->expects($this->any())
->method('getNodeForPath')
->with('/systemtags')
->will($this->returnValue($node));

$request = $this->getMockBuilder('Sabre\HTTP\RequestInterface')
->disableOriginalConstructor()
->getMock();
$response = $this->getMockBuilder('Sabre\HTTP\ResponseInterface')
->disableOriginalConstructor()
->getMock();

$request->expects($this->once())
->method('getPath')
->will($this->returnValue('/systemtags'));

$request->expects($this->once())
->method('getBodyAsString')
->will($this->returnValue($requestData));

$request->expects($this->once())
->method('getHeader')
->with('Content-Type')
->will($this->returnValue('application/json'));

$this->plugin->httpPost($request, $response);
}

/**
* @expectedException \Sabre\DAV\Exception\BadRequest
* @expectedExceptionMessage Not sufficient permissions
*/
public function testCreateInvisibleTagAsRegularUser() {
$user = $this->getMock('\OCP\IUser');
$user->expects($this->once())
->method('getUID')
->willReturn('admin');
$this->userSession
->expects($this->once())
->method('isLoggedIn')
->willReturn(true);
$this->userSession
->expects($this->once())
->method('getUser')
->willReturn($user);
$this->groupManager
->expects($this->once())
->method('isAdmin')
->with('admin')
->willReturn(false);

$requestData = json_encode([
'name' => 'Test',
'userVisible' => false,
'userAssignable' => true,
]);

$node = $this->getMockBuilder('\OCA\DAV\SystemTag\SystemTagsByIdCollection')
->disableOriginalConstructor()
->getMock();
$this->tagManager->expects($this->never())
->method('createTag');

$this->tree->expects($this->any())
->method('getNodeForPath')
->with('/systemtags')
->will($this->returnValue($node));

$request = $this->getMockBuilder('Sabre\HTTP\RequestInterface')
->disableOriginalConstructor()
->getMock();
$response = $this->getMockBuilder('Sabre\HTTP\ResponseInterface')
->disableOriginalConstructor()
->getMock();

$request->expects($this->once())
->method('getPath')
->will($this->returnValue('/systemtags'));

$request->expects($this->once())
->method('getBodyAsString')
->will($this->returnValue($requestData));

$request->expects($this->once())
->method('getHeader')
->with('Content-Type')
->will($this->returnValue('application/json'));

$this->plugin->httpPost($request, $response);
}

public function testCreateTagInByIdCollectionAsRegularUser() {
$systemTag = new SystemTag(1, 'Test', true, false);

$requestData = json_encode([
'name' => 'Test',
'userVisible' => true,
'userAssignable' => true,
]);

$node = $this->getMockBuilder('\OCA\DAV\SystemTag\SystemTagsByIdCollection')
->disableOriginalConstructor()
->getMock();
$this->tagManager->expects($this->once())
->method('createTag')
->with('Test', true, true)
->will($this->returnValue($systemTag));

$this->tree->expects($this->any())
->method('getNodeForPath')
->with('/systemtags')
->will($this->returnValue($node));

$request = $this->getMockBuilder('Sabre\HTTP\RequestInterface')
->disableOriginalConstructor()
->getMock();
$response = $this->getMockBuilder('Sabre\HTTP\ResponseInterface')
->disableOriginalConstructor()
->getMock();

$request->expects($this->once())
->method('getPath')
->will($this->returnValue('/systemtags'));

$request->expects($this->once())
->method('getBodyAsString')
->will($this->returnValue($requestData));

$request->expects($this->once())
->method('getHeader')
->with('Content-Type')
->will($this->returnValue('application/json'));

$request->expects($this->once())
->method('getUrl')
->will($this->returnValue('http://example.com/dav/systemtags'));

$response->expects($this->once())
->method('setHeader')
->with('Content-Location', 'http://example.com/dav/systemtags/1');

$this->plugin->httpPost($request, $response);
}

public function testCreateTagInByIdCollection() {
$user = $this->getMock('\OCP\IUser');
$user->expects($this->once())
->method('getUID')
->willReturn('admin');
$this->userSession
->expects($this->once())
->method('isLoggedIn')
->willReturn(true);
$this->userSession
->expects($this->once())
->method('getUser')
->willReturn($user);
$this->groupManager
->expects($this->once())
->method('isAdmin')
->with('admin')
->willReturn(true);

$systemTag = new SystemTag(1, 'Test', true, false);

$requestData = json_encode([
Expand Down Expand Up @@ -214,6 +429,24 @@ public function nodeClassProvider() {
}

public function testCreateTagInMappingCollection() {
$user = $this->getMock('\OCP\IUser');
$user->expects($this->once())
->method('getUID')
->willReturn('admin');
$this->userSession
->expects($this->once())
->method('isLoggedIn')
->willReturn(true);
$this->userSession
->expects($this->once())
->method('getUser')
->willReturn($user);
$this->groupManager
->expects($this->once())
->method('isAdmin')
->with('admin')
->willReturn(true);

$systemTag = new SystemTag(1, 'Test', true, false);

$requestData = json_encode([
Expand Down Expand Up @@ -307,9 +540,27 @@ public function testCreateTagToUnknownNode() {

/**
* @dataProvider nodeClassProvider
* @expectedException Sabre\DAV\Exception\Conflict
* @expectedException \Sabre\DAV\Exception\Conflict
*/
public function testCreateTagConflict($nodeClass) {
$user = $this->getMock('\OCP\IUser');
$user->expects($this->once())
->method('getUID')
->willReturn('admin');
$this->userSession
->expects($this->once())
->method('isLoggedIn')
->willReturn(true);
$this->userSession
->expects($this->once())
->method('getUser')
->willReturn($user);
$this->groupManager
->expects($this->once())
->method('isAdmin')
->with('admin')
->willReturn(true);

$requestData = json_encode([
'name' => 'Test',
'userVisible' => true,
Expand Down
2 changes: 2 additions & 0 deletions build/integration/config/behat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ default:
regular_user_password: 123456
- CommentsContext:
baseUrl: http://localhost:8080
- TagsContext:
baseUrl: http://localhost:8080
federation:
paths:
- %paths.base%/../federation_features
Expand Down
Loading

0 comments on commit f3b13c7

Please sign in to comment.