diff options
author | Vincent Petry <pvince81@owncloud.com> | 2015-12-16 17:35:53 +0100 |
---|---|---|
committer | Roeland Jago Douma <roeland@famdouma.nl> | 2016-10-24 21:45:00 +0200 |
commit | 59c5be1cc572793a8d50e87ab589e1cc4cf2ed12 (patch) | |
tree | 97b096fee075115bac45d69f2e0da7af5ffb41f2 /apps/dav/tests | |
parent | 4d01f23978549c2a33e9fdfdc3b9308cc9dc1078 (diff) | |
download | nextcloud-server-59c5be1cc572793a8d50e87ab589e1cc4cf2ed12.tar.gz nextcloud-server-59c5be1cc572793a8d50e87ab589e1cc4cf2ed12.zip |
Use Webdav PUT for uploads in the web browser
- uses PUT method with jquery.fileupload for regular and public file
lists
- for IE and browsers that don't support it, use POST with iframe
transport
- implemented Sabre plugin to handle iframe transport and redirect the
embedded PUT request to the proper handler
- added RFC5995 POST to file collection with "add-member" property to
make it possible to auto-rename conflicting file names
- remove obsolete ajax/upload.php and obsolete ajax routes
Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
Diffstat (limited to 'apps/dav/tests')
-rw-r--r-- | apps/dav/tests/unit/Connector/Sabre/FilesPluginTest.php | 85 | ||||
-rw-r--r-- | apps/dav/tests/unit/Connector/Sabre/IFrameTransportPluginTest.php | 164 |
2 files changed, 247 insertions, 2 deletions
diff --git a/apps/dav/tests/unit/Connector/Sabre/FilesPluginTest.php b/apps/dav/tests/unit/Connector/Sabre/FilesPluginTest.php index 282a5b2f626..43ca119abff 100644 --- a/apps/dav/tests/unit/Connector/Sabre/FilesPluginTest.php +++ b/apps/dav/tests/unit/Connector/Sabre/FilesPluginTest.php @@ -123,7 +123,7 @@ class FilesPluginTest extends TestCase { * @param string $class * @return \PHPUnit_Framework_MockObject_MockObject */ - private function createTestNode($class) { + private function createTestNode($class, $path = '/dummypath') { $node = $this->getMockBuilder($class) ->disableOriginalConstructor() ->getMock(); @@ -134,7 +134,7 @@ class FilesPluginTest extends TestCase { $this->tree->expects($this->any()) ->method('getNodeForPath') - ->with('/dummypath') + ->with($path) ->will($this->returnValue($node)); $node->expects($this->any()) @@ -547,4 +547,85 @@ class FilesPluginTest extends TestCase { $this->assertEquals("false", $propFind->get(self::HAS_PREVIEW_PROPERTYNAME)); } + + public function postCreateFileProvider() { + $baseUrl = 'http://example.com/owncloud/remote.php/webdav/subdir/'; + return [ + ['test.txt', 'some file.txt', 'some file.txt', $baseUrl . 'some%20file.txt'], + ['some file.txt', 'some file.txt', 'some file (2).txt', $baseUrl . 'some%20file%20%282%29.txt'], + ]; + } + + /** + * @dataProvider postCreateFileProvider + */ + public function testPostWithAddMember($existingFile, $wantedName, $deduplicatedName, $expectedLocation) { + $request = $this->getMock('Sabre\HTTP\RequestInterface'); + $response = $this->getMock('Sabre\HTTP\ResponseInterface'); + + $request->expects($this->any()) + ->method('getUrl') + ->will($this->returnValue('http://example.com/owncloud/remote.php/webdav/subdir/&' . $wantedName)); + + $request->expects($this->any()) + ->method('getPath') + ->will($this->returnValue('/subdir/&' . $wantedName)); + + $request->expects($this->once()) + ->method('getBodyAsStream') + ->will($this->returnValue(fopen('data://text/plain,hello', 'r'))); + + $this->view->expects($this->any()) + ->method('file_exists') + ->will($this->returnCallback(function($path) use ($existingFile) { + return ($path === '/subdir/' . $existingFile); + })); + + $node = $this->createTestNode('\OCA\DAV\Connector\Sabre\Directory', '/subdir'); + + $node->expects($this->once()) + ->method('createFile') + ->with($deduplicatedName, $this->isType('resource')); + + $response->expects($this->once()) + ->method('setStatus') + ->with(201); + $response->expects($this->once()) + ->method('setHeader') + ->with('Content-Location', $expectedLocation); + + $this->assertFalse($this->plugin->httpPost($request, $response)); + } + + public function testPostOnNonDirectory() { + $request = $this->getMock('Sabre\HTTP\RequestInterface'); + $response = $this->getMock('Sabre\HTTP\ResponseInterface'); + + $request->expects($this->any()) + ->method('getPath') + ->will($this->returnValue('/subdir/test.txt/&abc')); + + $this->createTestNode('\OCA\DAV\Connector\Sabre\File', '/subdir/test.txt'); + + $this->assertNull($this->plugin->httpPost($request, $response)); + } + + /** + * @expectedException \Sabre\DAV\Exception\BadRequest + */ + public function testPostWithoutAddMember() { + $request = $this->getMock('Sabre\HTTP\RequestInterface'); + $response = $this->getMock('Sabre\HTTP\ResponseInterface'); + + $request->expects($this->any()) + ->method('getPath') + ->will($this->returnValue('/subdir/&')); + + $node = $this->createTestNode('\OCA\DAV\Connector\Sabre\Directory', '/subdir'); + + $node->expects($this->never()) + ->method('createFile'); + + $this->plugin->httpPost($request, $response); + } } diff --git a/apps/dav/tests/unit/Connector/Sabre/IFrameTransportPluginTest.php b/apps/dav/tests/unit/Connector/Sabre/IFrameTransportPluginTest.php new file mode 100644 index 00000000000..485dd1b779e --- /dev/null +++ b/apps/dav/tests/unit/Connector/Sabre/IFrameTransportPluginTest.php @@ -0,0 +1,164 @@ +<?php + +namespace OCA\DAV\Tests\Unit\Connector\Sabre; + +/** + * Copyright (c) 2015 Vincent Petry <pvince81@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ +class IFrameTransportPluginTest extends \Test\TestCase { + + /** + * @var \Sabre\DAV\Server + */ + private $server; + + /** + * @var \OCA\DAV\Connector\Sabre\IFrameTransportPlugin + */ + private $plugin; + + public function setUp() { + parent::setUp(); + $this->server = $this->getMockBuilder('\Sabre\DAV\Server') + ->disableOriginalConstructor() + ->getMock(); + + $this->plugin = new \OCA\DAV\Connector\Sabre\IFrameTransportPlugin(); + $this->plugin->initialize($this->server); + } + + public function tearDown() { + $_FILES = null; + unset($_SERVER['CONTENT_LENGTH']); + } + + public function testPutConversion() { + $request = $this->getMock('Sabre\HTTP\RequestInterface'); + $response = $this->getMock('Sabre\HTTP\ResponseInterface'); + + $request->expects($this->once()) + ->method('getQueryParameters') + ->will($this->returnValue(['_method' => 'PUT'])); + + $postData = [ + 'headers' => json_encode([ + 'If-None-Match' => '*', + 'Disallowed-Header' => 'test', + ]), + ]; + + $request->expects($this->once()) + ->method('getPostData') + ->will($this->returnValue($postData)); + + $request->expects($this->once()) + ->method('getHeader') + ->with('Content-Type') + ->will($this->returnValue('multipart/form-data')); + + $tmpFileName = tempnam(sys_get_temp_dir(), 'tmpfile'); + $fh = fopen($tmpFileName, 'w'); + fwrite($fh, 'hello'); + fclose($fh); + + $_FILES = ['files' => [ + 'error' => [0], + 'tmp_name' => [$tmpFileName], + 'size' => [5], + ]]; + + $request->expects($this->any()) + ->method('setHeader') + ->withConsecutive( + ['If-None-Match', '*'], + ['Content-Length', 5] + ); + + $request->expects($this->once()) + ->method('setMethod') + ->with('PUT'); + + $this->server->expects($this->once()) + ->method('invokeMethod') + ->with($request, $response); + + // response data before conversion + $response->expects($this->once()) + ->method('getHeaders') + ->will($this->returnValue(['Test-Response-Header' => [123]])); + + $response->expects($this->any()) + ->method('getBody') + ->will($this->returnValue('test')); + + $response->expects($this->once()) + ->method('getStatus') + ->will($this->returnValue(201)); + + $responseBody = json_encode([ + 'status' => 201, + 'headers' => ['Test-Response-Header' => [123]], + 'data' => 'test', + ]); + + // response data after conversion + $response->expects($this->once()) + ->method('setBody') + ->with($responseBody); + + $response->expects($this->once()) + ->method('setStatus') + ->with(200); + + $response->expects($this->any()) + ->method('setHeader') + ->withConsecutive( + ['Content-Type', 'text/plain'], + ['Content-Length', strlen($responseBody)] + ); + + $this->assertFalse($this->plugin->handlePost($request, $response)); + + $this->assertEquals(5, $_SERVER['CONTENT_LENGTH']); + + $this->assertFalse(file_exists($tmpFileName)); + } + + public function testIgnoreNonPut() { + $request = $this->getMock('Sabre\HTTP\RequestInterface'); + $response = $this->getMock('Sabre\HTTP\ResponseInterface'); + + $request->expects($this->once()) + ->method('getQueryParameters') + ->will($this->returnValue(['_method' => 'PROPFIND'])); + + $this->server->expects($this->never()) + ->method('invokeMethod') + ->with($request, $response); + + $this->assertNull($this->plugin->handlePost($request, $response)); + } + + public function testIgnoreMismatchedContentType() { + $request = $this->getMock('Sabre\HTTP\RequestInterface'); + $response = $this->getMock('Sabre\HTTP\ResponseInterface'); + + $request->expects($this->once()) + ->method('getQueryParameters') + ->will($this->returnValue(['_method' => 'PUT'])); + + $request->expects($this->once()) + ->method('getHeader') + ->with('Content-Type') + ->will($this->returnValue('text/plain')); + + $this->server->expects($this->never()) + ->method('invokeMethod') + ->with($request, $response); + + $this->assertNull($this->plugin->handlePost($request, $response)); + } +} |