Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add integration tests for comments #22511

Merged
merged 1 commit into from
Feb 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build/integration/config/behat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ default:
- admin
- admin
regular_user_password: 123456
- CommentsContext:
baseUrl: http://localhost:8080
federation:
paths:
- %paths.base%/../federation_features
Expand Down
263 changes: 263 additions & 0 deletions build/integration/features/bootstrap/CommentsContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,263 @@
<?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';

class CommentsContext implements \Behat\Behat\Context\Context {
/** @var string */
private $baseUrl;
/** @var array */
private $response;
/** @var int */
private $commentId;
/** @var int */
private $fileId;

/**
* @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);
}
}

/** @AfterScenario */
public function teardownScenario(\Behat\Behat\Hook\Scope\AfterScenarioScope $scope) {
$client = new \GuzzleHttp\Client();
try {
$client->delete(
$this->baseUrl.'/remote.php/webdav/myFileToComment.txt',
[
'auth' => [
'user0',
'123456',
],
'headers' => [
'Content-Type' => 'application/json',
],
]
);
} catch (\GuzzleHttp\Exception\ClientException $e) {
$e->getResponse();
}
}

/**
* @return int
*/
private function getFileIdForPath($path) {
$url = $this->baseUrl.'/remote.php/webdav/'.$path;
$context = stream_context_create(array(
'http' => array(
'method' => 'PROPFIND',
'header' => "Authorization: Basic dXNlcjA6MTIzNDU2\r\nContent-Type: application/x-www-form-urlencoded",
'content' => '<?xml version="1.0"?>
<d:propfind xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns">
<d:prop>
<oc:fileid />
</d:prop>
</d:propfind>'
)
));

$response = file_get_contents($url, false, $context);
preg_match_all('/\<oc:fileid\>(.*)\<\/oc:fileid\>/', $response, $matches);
return (int)$matches[1][0];
}

/**
* @When :user posts a comment with content :content on the file named :fileName it should return :statusCode
*/
public function postsACommentWithContentOnTheFileNamedItShouldReturn($user, $content, $fileName, $statusCode) {
$fileId = $this->getFileIdForPath($fileName);
$this->fileId = (int)$fileId;
$url = $this->baseUrl.'/remote.php/dav/comments/files/'.$fileId.'/';

$client = new \GuzzleHttp\Client();
try {
$res = $client->post(
$url,
[
'body' => '{"actorId":"user0","actorDisplayName":"user0","actorType":"users","verb":"comment","message":"' . $content . '","creationDateTime":"Thu, 18 Feb 2016 17:04:18 GMT","objectType":"files"}',
'auth' => [
$user,
'123456',
],
'headers' => [
'Content-Type' => 'application/json',
],
]
);
} catch (\GuzzleHttp\Exception\ClientException $e) {
$res = $e->getResponse();
}

if($res->getStatusCode() !== (int)$statusCode) {
throw new \Exception("Response status code was not $statusCode (".$res->getStatusCode().")");
}
}


/**
* @Then As :user load all the comments of the file named :fileName it should return :statusCode
*/
public function asLoadloadAllTheCommentsOfTheFileNamedItShouldReturn($user, $fileName, $statusCode) {
$fileId = $this->getFileIdForPath($fileName);
$url = $this->baseUrl.'/remote.php/dav/comments/files/'.$fileId.'/';

try {
$client = new \GuzzleHttp\Client();
$res = $client->createRequest(
'REPORT',
$url,
[
'body' => '<?xml version="1.0" encoding="utf-8" ?>
<oc:filter-comments xmlns:D="DAV:" xmlns:oc="http://owncloud.org/ns">
<oc:limit>200</oc:limit>
<oc:offset>0</oc:offset>
</oc:filter-comments>
',
'auth' => [
$user,
'123456',
],
'headers' => [
'Content-Type' => 'application/json',
],
]
);
$res = $client->send($res);
} catch (\GuzzleHttp\Exception\ClientException $e) {
$res = $e->getResponse();
}

if($res->getStatusCode() !== (int)$statusCode) {
throw new \Exception("Response status code was not $statusCode (".$res->getStatusCode().")");
}

if($res->getStatusCode() === 207) {
$service = new Sabre\Xml\Service();
$this->response = $service->parse($res->getBody()->getContents());
$this->commentId = (int)$this->response[0]['value'][2]['value'][0]['value'][0]['value'];
}
}

/**
* @Given As :user sending :verb to :url with
*/
public function asUserSendingToWith($user, $verb, $url, \Behat\Gherkin\Node\TableNode $body) {
$client = new \GuzzleHttp\Client();
$options = [];
$options['auth'] = [$user, '123456'];
$fd = $body->getRowsHash();
$options['body'] = $fd;
$client->send($client->createRequest($verb, $this->baseUrl.'/ocs/v1.php/'.$url, $options));
}

/**
* @Then As :user delete the created comment it should return :statusCode
*/
public function asDeleteTheCreatedCommentItShouldReturn($user, $statusCode) {
$url = $this->baseUrl.'/remote.php/dav/comments/files/'.$this->fileId.'/'.$this->commentId;

$client = new \GuzzleHttp\Client();
try {
$res = $client->delete(
$url,
[
'auth' => [
$user,
'123456',
],
'headers' => [
'Content-Type' => 'application/json',
],
]
);
} catch (\GuzzleHttp\Exception\ClientException $e) {
$res = $e->getResponse();
}

if($res->getStatusCode() !== (int)$statusCode) {
throw new \Exception("Response status code was not $statusCode (".$res->getStatusCode().")");
}
}

/**
* @Then the response should contain a property :key with value :value
*/
public function theResponseShouldContainAPropertyWithValue($key, $value) {
$keys = $this->response[0]['value'][2]['value'][0]['value'];
$found = false;
foreach($keys as $singleKey) {
if($singleKey['name'] === '{http://owncloud.org/ns}'.substr($key, 3)) {
if($singleKey['value'] === $value) {
$found = true;
}
}
}
if($found === false) {
throw new \Exception("Cannot find property $key with $value");
}
}

/**
* @Then the response should contain only :number comments
*/
public function theResponseShouldContainOnlyComments($number) {
if(count($this->response) !== (int)$number) {
throw new \Exception("Found more comments than $number (".count($this->response).")");
}
}

/**
* @Then As :user edit the last created comment and set text to :text it should return :statusCode
*/
public function asEditTheLastCreatedCommentAndSetTextToItShouldReturn($user, $text, $statusCode) {
$client = new \GuzzleHttp\Client();
$options = [];
$options['auth'] = [$user, '123456'];
$options['body'] = '<?xml version="1.0"?>
<d:propertyupdate xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns">
<d:set>
<d:prop>
<oc:message>'.$text.'</oc:message>
</d:prop>
</d:set>
</d:propertyupdate>';
try {
$res = $client->send($client->createRequest('PROPPATCH', $this->baseUrl.'/remote.php/dav/comments/files/' . $this->fileId . '/' . $this->commentId, $options));
} catch (\GuzzleHttp\Exception\ClientException $e) {
$res = $e->getResponse();
}

if($res->getStatusCode() !== (int)$statusCode) {
throw new \Exception("Response status code was not $statusCode (".$res->getStatusCode().")");
}
}


}
44 changes: 44 additions & 0 deletions build/integration/features/bootstrap/Sharing.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,5 +370,49 @@ public function checkShareFields($body){
}
}

/**
* @Then As :user remove all shares from the file named :fileName
*/
public function asRemoveAllSharesFromTheFileNamed($user, $fileName) {
$url = $this->baseUrl.'v2.php/apps/files_sharing/api/v1/shares?format=json';
$client = new \GuzzleHttp\Client();
$res = $client->get(
$url,
[
'auth' => [
$user,
'123456',
],
'headers' => [
'Content-Type' => 'application/json',
],
]
);
$json = json_decode($res->getBody()->getContents(), true);
$deleted = false;
foreach($json['ocs']['data'] as $data) {
if (stripslashes($data['path']) === $fileName) {
$id = $data['id'];
$client->delete(
$this->baseUrl.'v2.php/apps/files_sharing/api/v1/shares/'.$id,
[
'auth' => [
$user,
'123456',
],
'headers' => [
'Content-Type' => 'application/json',
],
]
);
$deleted = true;
}
}

if($deleted === false) {
throw new \Exception("Could not delete file $fileName");
}
}

}

15 changes: 13 additions & 2 deletions build/integration/features/bootstrap/WebDav.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
require __DIR__ . '/../../vendor/autoload.php';


trait WebDav{

trait WebDav {
/** @var string*/
private $davPath = "remote.php/webdav";

Expand Down Expand Up @@ -162,6 +161,18 @@ public function userUploadsAFileTo($user, $source, $destination)
}
}

/**
* @When User :user deletes file :file
*/
public function userDeletesFile($user, $file) {
try {
$this->response = $this->makeDavRequest($user, 'DELETE', $file, []);
} catch (\GuzzleHttp\Exception\ServerException $e) {
// 4xx and 5xx responses cause an exception
$this->response = $e->getResponse();
}
}

/**
* @Given User :user created a folder :destination
*/
Expand Down
Loading