Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add quota tests hypercube
  • Loading branch information
Vincent Petry committed Jul 4, 2017
commit bdf92242a487ebc1b7e67eac4ee671e5cdb8604f
161 changes: 158 additions & 3 deletions tests/integration/features/bootstrap/WebDav.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use GuzzleHttp\Message\ResponseInterface;
use Sabre\DAV\Client as SClient;
use Sabre\DAV\Xml\Property\ResourceType;
use GuzzleHttp\Exception\ServerException;

require __DIR__ . '/../../../../lib/composer/autoload.php';

Expand All @@ -17,6 +18,8 @@ trait WebDav {
private $usingOldDavPath = true;
/** @var ResponseInterface */
private $response;
/** @var ResponseInterface[] */
private $uploadResponses;
/** @var map with user as key and another map as value, which has path as key and etag as value */
private $storedETAG = NULL;
/** @var integer */
Expand Down Expand Up @@ -565,8 +568,7 @@ public function checkElementList($user, $expectedElements){
* @param string $source
* @param string $destination
*/
public function userUploadsAFileTo($user, $source, $destination)
{
public function userUploadsAFileTo($user, $source, $destination) {
$file = \GuzzleHttp\Stream\Stream::factory(fopen($source, 'r'));
try {
$this->response = $this->makeDavRequest($user, "PUT", $destination, [], $file);
Expand All @@ -576,6 +578,157 @@ public function userUploadsAFileTo($user, $source, $destination)
}
}

/**
* @When User :user uploads file :source to :destination with chunks
* @param string $user
* @param string $source
* @param string $destination
* @param string $chunkingVersion null for autodetect, "old" with old style, "new" for new style
*/
public function userUploadsAFileToWithChunks($user, $source, $destination, $chunkingVersion = null) {
$size = filesize($source);
$contents = file_get_contents($source);

// use two chunks for the sake of testing
$chunks = [];
$chunks[] = substr($contents, 0, $size / 2);
$chunks[] = substr($contents, $size / 2);

$this->uploadChunks($user, $chunks, $destination, $chunkingVersion);
}

public function uploadChunks($user, $chunks, $destination, $chunkingVersion = null) {
if ($chunkingVersion === null) {
if ($this->usingOldDavPath) {
$chunkingVersion = 'old';
} else {
$chunkingVersion = 'new';
}
}
if ($chunkingVersion === 'old') {
foreach ($chunks as $index => $chunk) {
$this->userUploadsChunkedFile($user, $index + 1, count($chunks), $chunk, $destination);
}
} else {
$id = 'chunking-43';
$this->userCreatesANewChunkingUploadWithId($user, $id);
foreach ($chunks as $index => $chunk) {
$this->userUploadsNewChunkFileOfWithToId($user, $index + 1, $chunk, $id);
}
$this->userMovesNewChunkFileWithIdToMychunkedfile($user, $id, $destination);
}
}

/**
* Uploading with old/new dav and chunked/non-chunked.
*
* @When User :user uploads file :source to :destination with all mechanisms
* @param string $user
* @param string $source
* @param string $destination
*/
public function userUploadsAFileToWithAllMechanisms($user, $source, $destination) {
$this->uploadResponses = $this->uploadWithAllMechanisms($user, $source, $destination, false);
}

/**
* Overwriting with old/new dav and chunked/non-chunked.
*
* @When User :user overwrites file :source to :destination with all mechanisms
* @param string $user
* @param string $source
* @param string $destination
*/
public function userOverwritesAFileToWithAllMechanisms($user, $source, $destination) {
$this->uploadResponses = $this->uploadWithAllMechanisms($user, $source, $destination, true);
}

/**
* Upload the same file multiple times with different mechanisms.
*
* @param string $user user who uploads
* @param string $source source file path
* @param string $destination destination path on the server
* @param bool $overwriteMode when false creates separate files to test uploading brand new files,
* when true it just overwrites the same file over and over again with the same name
*/
public function uploadWithAllMechanisms($user, $source, $destination, $overwriteMode = false) {
$responses = [];
foreach (['old', 'new'] as $dav) {
if ($dav === 'old') {
$this->usingOldDavPath();
} else {
$this->usingNewDavPath();
}

$suffix = '';

// regular upload
try {
if (!$overwriteMode) {
$suffix = '-' . $dav . 'dav-regular';
}
$this->userUploadsAFileTo($user, $source, $destination . $suffix);
$responses[] = $this->response;
} catch (ServerException $e) {
$responses[] = $e->getResponse();
}

// old chunking upload
if ($dav === 'old') {
if (!$overwriteMode) {
$suffix = '-' . $dav . 'dav-oldchunking';
}
try {
$this->userUploadsAFileToWithChunks($user, $source, $destination . $suffix, 'old');
$responses[] = $this->response;
} catch (ServerException $e) {
$responses[] = $e->getResponse();
}
}
if ($dav === 'new') {
// old chunking style applied to new endpoint 🙈
if (!$overwriteMode) {
$suffix = '-' . $dav . 'dav-oldchunking';
}
try {
// FIXME: prepending new dav path because the chunking utility functions are messed up
$this->userUploadsAFileToWithChunks($user, $source, '/files/' . $user . '/' . ltrim($destination, '/') . $suffix, 'old');
$responses[] = $this->response;
} catch (ServerException $e) {
$responses[] = $e->getResponse();
}

// new chunking style applied to new endpoint
if (!$overwriteMode) {
$suffix = '-' . $dav . 'dav-newchunking';
}
try {
$this->userUploadsAFileToWithChunks($user, $source, $destination . $suffix, 'new');
$responses[] = $this->response;
} catch (ServerException $e) {
$responses[] = $e->getResponse();
}
}
}

return $responses;
}

/**
* @Then /^the HTTP status code of all upload responses should be "([^"]*)"$/
* @param int $statusCode
*/
public function theHTTPStatusCodeOfAllUploadResponsesShouldBe($statusCode) {
foreach ($this->uploadResponses as $response) {
PHPUnit_Framework_Assert::assertEquals(
$statusCode,
$response->getStatusCode(),
'Response for ' . $response->getEffectiveUrl() . ' did not return expected status code'
);
}
}

/**
* @When User :user adds a file of :bytes bytes to :destination
* @param string $user
Expand Down Expand Up @@ -677,6 +830,8 @@ public function userCreatedAFolder($user, $destination){
}

/**
* Old style chunking upload
*
* @Given user :user uploads chunk file :num of :total with :data to :destination
* @param string $user
* @param int $num
Expand Down Expand Up @@ -739,7 +894,7 @@ public function userMovesNewChunkFileWithIdToMychunkedfile($user, $id, $dest)
{
$source = '/uploads/' . $user . '/' . $id . '/.file';
$destination = substr($this->baseUrl, 0, -4) . $this->getDavFilesPath($user) . $dest;
$this->makeDavRequest($user, 'MOVE', $source, [
$this->response = $this->makeDavRequest($user, 'MOVE', $source, [
'Destination' => $destination
], null, "uploads");
}
Expand Down
133 changes: 133 additions & 0 deletions tests/integration/features/quota.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
Feature: quota
Background:
Given using api version "1"

# Owner

Scenario: Uploading a file as owner having enough quota
Given using new dav path
And As an "admin"
And user "user0" exists
And user "user0" has a quota of "10 MB"
When User "user0" uploads file "data/textfile.txt" to "/testquota.txt" with all mechanisms
Then the HTTP status code of all upload responses should be "201"

Scenario: Uploading a file as owner having insufficient quota
Given using new dav path
And As an "admin"
And user "user0" exists
And user "user0" has a quota of "20 B"
When User "user0" uploads file "data/textfile.txt" to "/testquota.txt" with all mechanisms
Then the HTTP status code of all upload responses should be "507"
And as "user0" the file "/testquota.txt" does not exist

Scenario: Overwriting a file as owner having enough quota
Given using new dav path
And As an "admin"
And user "user0" exists
And user "user0" has a quota of "10 MB"
And User "user0" uploads file with content "test" to "/testquota.txt"
When User "user0" overwrites file "data/textfile.txt" to "/testquota.txt" with all mechanisms
Then the HTTP status code of all upload responses should be "204"

Scenario: Overwriting a file as owner having insufficient quota
Given using new dav path
And As an "admin"
And user "user0" exists
And user "user0" has a quota of "20 B"
And User "user0" uploads file with content "test" to "/testquota.txt"
When User "user0" overwrites file "data/textfile.txt" to "/testquota.txt" with all mechanisms
Then the HTTP status code of all upload responses should be "507"
And as "user0" the file "/testquota.txt" does not exist

# Received shared folder

Scenario: Uploading a file in received folder having enough quota
Given using new dav path
And As an "admin"
And user "user0" exists
And user "user1" exists
And user "user0" has a quota of "20 B"
And user "user1" has a quota of "10 MB"
And As an "user1"
And user "user1" created a folder "/testquota"
And folder "/testquota" of user "user1" is shared with user "user0" with permissions 31
And As an "user0"
When User "user0" uploads file "data/textfile.txt" to "/testquota/testquota.txt" with all mechanisms
Then the HTTP status code of all upload responses should be "201"

Scenario: Uploading a file in received folder having insufficient quota
Given using new dav path
And As an "admin"
And user "user0" exists
And user "user1" exists
And user "user0" has a quota of "10 MB"
And user "user1" has a quota of "20 B"
And As an "user1"
And user "user1" created a folder "/testquota"
And folder "/testquota" of user "user1" is shared with user "user0" with permissions 31
And As an "user0"
When User "user0" uploads file "data/textfile.txt" to "/testquota/testquota.txt" with all mechanisms
Then the HTTP status code of all upload responses should be "507"
And as "user0" the file "/testquota/testquota.txt" does not exist

Scenario: Overwriting a file in received folder having enough quota
Given using new dav path
And As an "admin"
And user "user0" exists
And user "user1" exists
And user "user0" has a quota of "20 B"
And user "user1" has a quota of "10 MB"
And As an "user1"
And user "user1" created a folder "/testquota"
And User "user1" uploads file with content "test" to "/testquota/testquota.txt"
And folder "/testquota" of user "user1" is shared with user "user0" with permissions 31
And As an "user0"
When User "user0" overwrites file "data/textfile.txt" to "/testquota/testquota.txt" with all mechanisms
Then the HTTP status code of all upload responses should be "204"

Scenario: Overwriting a file in received folder having insufficient quota
Given using new dav path
And As an "admin"
And user "user0" exists
And user "user1" exists
And user "user0" has a quota of "10 MB"
And user "user1" has a quota of "20 B"
And As an "user1"
And user "user1" created a folder "/testquota"
And User "user1" uploads file with content "test" to "/testquota/testquota.txt"
And folder "/testquota" of user "user1" is shared with user "user0" with permissions 31
And As an "user0"
When User "user0" overwrites file "data/textfile.txt" to "/testquota/testquota.txt" with all mechanisms
Then the HTTP status code of all upload responses should be "507"
And as "user0" the file "/testquota/testquota.txt" does not exist

# Received shared file

Scenario: Overwriting a received file having enough quota
Given using new dav path
And As an "admin"
And user "user0" exists
And user "user1" exists
And user "user0" has a quota of "20 B"
And user "user1" has a quota of "10 MB"
And As an "user1"
And User "user1" uploads file with content "test" to "/testquota.txt"
And file "/testquota.txt" of user "user1" is shared with user "user0" with permissions 19
And As an "user0"
When User "user0" overwrites file "data/textfile.txt" to "/testquota.txt" with all mechanisms
Then the HTTP status code of all upload responses should be "204"

Scenario: Overwriting a received file having insufficient quota
Given using new dav path
And As an "admin"
And user "user0" exists
And user "user1" exists
And user "user0" has a quota of "10 MB"
And user "user1" has a quota of "20 B"
And As an "user1"
And User "user1" moves file "/textfile0.txt" to "/testquota.txt"
And file "/testquota.txt" of user "user1" is shared with user "user0" with permissions 19
When User "user0" overwrites file "data/textfile.txt" to "/testquota.txt" with all mechanisms
Then the HTTP status code of all upload responses should be "507"

Loading