Commit f3b13c76 authored by Thomas Müller's avatar Thomas Müller
Browse files

Merge pull request #22536 from owncloud/add-integration-tests-for-tags

Add integration tests for tags plus fix permissions
parents a70421ff 3bd95cca
......@@ -93,7 +93,11 @@ class Server {
$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(
......
......@@ -103,6 +103,7 @@ class SystemTagNode implements \Sabre\DAV\INode {
* @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) {
......
......@@ -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;
......@@ -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;
}
/**
......@@ -163,6 +179,13 @@ class SystemTagPlugin extends \Sabre\DAV\ServerPlugin {
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) {
......
......@@ -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 {
......@@ -46,6 +48,16 @@ class SystemTagPlugin extends \Test\TestCase {
*/
private $tagManager;
/**
* @var IGroupManager
*/
private $groupManager;
/**
* @var IUserSession
*/
private $userSession;
/**
* @var \OCA\DAV\SystemTag\SystemTagPlugin
*/
......@@ -60,8 +72,14 @@ class SystemTagPlugin extends \Test\TestCase {
$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);
}
......@@ -153,7 +171,204 @@ class SystemTagPlugin extends \Test\TestCase {
$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([
......@@ -214,6 +429,24 @@ class SystemTagPlugin extends \Test\TestCase {
}
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([
......@@ -307,9 +540,27 @@ class SystemTagPlugin extends \Test\TestCase {
/**
* @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,
......
......@@ -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
......
<?php
/**
* @author Lukas Reschke <lukas@owncloud.com>
*
* @copyright Copyright (c) 2016, ownCloud, Inc.
* @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 <http://www.gnu.org/licenses/>
*
*/
require __DIR__ . '/../../vendor/autoload.php';
use Behat\Gherkin\Node\TableNode;
use GuzzleHttp\Client;
use GuzzleHttp\Message\ResponseInterface;
class TagsContext implements \Behat\Behat\Context\Context {
/** @var string */
private $baseUrl;
/** @var Client */
private $client;
/** @var ResponseInterface */
private $response;
/**
* @param string $baseUrl
*/
public function __construct($baseUrl) {
$this->baseUrl = $baseUrl;
// in case of ci deployment we take the server url from the environment
$testServerUrl = getenv('TEST_SERVER_URL');
if ($testServerUrl !== false) {
$this->baseUrl = substr($testServerUrl, 0, -5);
}
}
/** @BeforeScenario */
public function tearUpScenario() {
$this->client = new Client();
}
/** @AfterScenario */
public function tearDownScenario() {
$user = 'admin';
$tags = $this->requestTagsForUser($user);
foreach($tags as $tagId => $tag) {
$this->response = $this->client->delete(
$this->baseUrl . '/remote.php/dav/systemtags/'.$tagId,
[
'auth' => [
$user,
$this->getPasswordForUser($user),
],
'headers' => [
'Content-Type' => 'application/json',
],
]
);
}
try {
$this->client->delete(
$this->baseUrl . '/remote.php/webdav/myFileToTag.txt',
[
'auth' => [
'user0',
'123456',
],
'headers' => [
'Content-Type' => 'application/json',
],
]
);
} catch (\GuzzleHttp\Exception\ClientException $e) {}
}
/**
* @param string $userName
* @return string
*/
private function getPasswordForUser($userName) {
if($userName === 'admin') {
return 'admin';
}
return '123456';
}
/**
* @When :user creates a :type tag with name :name
*/
public function createsATagWithName($user, $type, $name) {
$userVisible = 'true';
$userAssignable = 'true';
switch ($type) {
case 'normal':
break;
case 'not user-assignable':
$userAssignable = 'false';
break;
case 'not user-visible':
$userVisible = 'false';
break;
default:
throw new \Exception('Unsupported type');
}
try {
$this->response = $this->client->post(
$this->baseUrl . '/remote.php/dav/systemtags/',
[
'auth' => [
$user,
$this->getPasswordForUser($user),
],
'headers' => [
'Content-Type' => 'application/json',
],
'body' => '{"name":"'.$name.'","userVisible":'.$userVisible.',"userAssignable":'.$userAssignable.'}',
]
);
} catch (\GuzzleHttp\Exception\ClientException $e){
$this->response = $e->getResponse();
}
}
/**
* @Then The response should have a status code :statusCode
*/
public function theResponseShouldHaveAStatusCode($statusCode) {
if((int)$statusCode !== $this->response->getStatusCode()) {
throw new \Exception("Expected $statusCode, got ".$this->response->getStatusCode());
}
}
/**
* Returns all tags for a given user
*
* @param string $user
* @return array
*/
private function requestTagsForUser($user) {
try {
$request = $this->client->createRequest(
'PROPFIND',
$this->baseUrl . '/remote.php/dav/systemtags/',
[
'body' => '<?xml version="1.0"?>
<d:propfind xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns">
<d:prop>
<oc:id />
<oc:display-name />
<oc:user-visible />
<oc:user-assignable />
</d:prop>
</d:propfind>',
'auth' => [
$user,
$this->getPasswordForUser($user),
],
'headers' => [
'Content-Type' => 'application/json',
],
]
);
$this->response = $this->client->send($request);
} catch (\GuzzleHttp\Exception\ClientException $e) {
$this->response = $e->getResponse();
}
$tags = [];
$service = new Sabre\Xml\Service();
$parsed = $service->parse($this->response->getBody()->getContents());
foreach($parsed as $entry) {
$singleEntry = $entry['value'][1]['value'][0]['value'];
if(empty($singleEntry[0]['value'])) {
continue;
}
$tags[$singleEntry[0]['value']] = [
'display-name' => $singleEntry[1]['value'],
'user-visible' => $singleEntry[2]['value'],
'user-assignable' => $singleEntry[3]['value'],
];
}
return $tags;
}
/**
* @Then The following tags should exist for :user
*/
public function theFollowingTagsShouldExistFor($user, TableNode $table) {
$tags = $this->requestTagsForUser($user);
if(count($table->getRows()) !== count($tags)) {
throw new \Exception(
sprintf(
"Expected %s tags, got %s.",
count($table->getRows()),
count($tags)
)
);
}
foreach($table->getRowsHash() as $rowDisplayName => $row) {
foreach($tags as $key => $tag) {
if(
$tag['display-name'] === $rowDisplayName &&