You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ChunkingPluginTest.php 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017, ownCloud GmbH
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  7. * @author Julius Härtl <jus@bitgrid.net>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\DAV\Tests\unit\Upload;
  28. use OCA\DAV\Connector\Sabre\Directory;
  29. use OCA\DAV\Upload\ChunkingPlugin;
  30. use OCA\DAV\Upload\FutureFile;
  31. use Sabre\DAV\Exception\NotFound;
  32. use Sabre\HTTP\RequestInterface;
  33. use Sabre\HTTP\ResponseInterface;
  34. use Test\TestCase;
  35. class ChunkingPluginTest extends TestCase {
  36. /**
  37. * @var \Sabre\DAV\Server | \PHPUnit\Framework\MockObject\MockObject
  38. */
  39. private $server;
  40. /**
  41. * @var \Sabre\DAV\Tree | \PHPUnit\Framework\MockObject\MockObject
  42. */
  43. private $tree;
  44. /**
  45. * @var ChunkingPlugin
  46. */
  47. private $plugin;
  48. /** @var RequestInterface | \PHPUnit\Framework\MockObject\MockObject */
  49. private $request;
  50. /** @var ResponseInterface | \PHPUnit\Framework\MockObject\MockObject */
  51. private $response;
  52. protected function setUp(): void {
  53. parent::setUp();
  54. $this->server = $this->getMockBuilder('\Sabre\DAV\Server')
  55. ->disableOriginalConstructor()
  56. ->getMock();
  57. $this->tree = $this->getMockBuilder('\Sabre\DAV\Tree')
  58. ->disableOriginalConstructor()
  59. ->getMock();
  60. $this->server->tree = $this->tree;
  61. $this->plugin = new ChunkingPlugin();
  62. $this->request = $this->createMock(RequestInterface::class);
  63. $this->response = $this->createMock(ResponseInterface::class);
  64. $this->server->httpRequest = $this->request;
  65. $this->server->httpResponse = $this->response;
  66. $this->plugin->initialize($this->server);
  67. }
  68. public function testBeforeMoveFutureFileSkip() {
  69. $node = $this->createMock(Directory::class);
  70. $this->tree->expects($this->any())
  71. ->method('getNodeForPath')
  72. ->with('source')
  73. ->willReturn($node);
  74. $this->response->expects($this->never())
  75. ->method('setStatus');
  76. $this->assertNull($this->plugin->beforeMove('source', 'target'));
  77. }
  78. public function testBeforeMoveDestinationIsDirectory() {
  79. $this->expectException(\Sabre\DAV\Exception\BadRequest::class);
  80. $this->expectExceptionMessage('The given destination target is a directory.');
  81. $sourceNode = $this->createMock(FutureFile::class);
  82. $targetNode = $this->createMock(Directory::class);
  83. $this->tree->expects($this->at(0))
  84. ->method('getNodeForPath')
  85. ->with('source')
  86. ->willReturn($sourceNode);
  87. $this->tree->expects($this->at(1))
  88. ->method('getNodeForPath')
  89. ->with('target')
  90. ->willReturn($targetNode);
  91. $this->response->expects($this->never())
  92. ->method('setStatus');
  93. $this->assertNull($this->plugin->beforeMove('source', 'target'));
  94. }
  95. public function testBeforeMoveFutureFileSkipNonExisting() {
  96. $sourceNode = $this->createMock(FutureFile::class);
  97. $sourceNode->expects($this->once())
  98. ->method('getSize')
  99. ->willReturn(4);
  100. $this->tree->expects($this->at(0))
  101. ->method('getNodeForPath')
  102. ->with('source')
  103. ->willReturn($sourceNode);
  104. $this->tree->expects($this->at(1))
  105. ->method('getNodeForPath')
  106. ->with('target')
  107. ->willThrowException(new NotFound());
  108. $this->tree->expects($this->any())
  109. ->method('nodeExists')
  110. ->with('target')
  111. ->willReturn(false);
  112. $this->response->expects($this->once())
  113. ->method('setHeader')
  114. ->with('Content-Length', '0');
  115. $this->response->expects($this->once())
  116. ->method('setStatus')
  117. ->with(201);
  118. $this->request->expects($this->once())
  119. ->method('getHeader')
  120. ->with('OC-Total-Length')
  121. ->willReturn(4);
  122. $this->assertFalse($this->plugin->beforeMove('source', 'target'));
  123. }
  124. public function testBeforeMoveFutureFileMoveIt() {
  125. $sourceNode = $this->createMock(FutureFile::class);
  126. $sourceNode->expects($this->once())
  127. ->method('getSize')
  128. ->willReturn(4);
  129. $this->tree->expects($this->at(0))
  130. ->method('getNodeForPath')
  131. ->with('source')
  132. ->willReturn($sourceNode);
  133. $this->tree->expects($this->at(1))
  134. ->method('getNodeForPath')
  135. ->with('target')
  136. ->willThrowException(new NotFound());
  137. $this->tree->expects($this->any())
  138. ->method('nodeExists')
  139. ->with('target')
  140. ->willReturn(true);
  141. $this->tree->expects($this->once())
  142. ->method('move')
  143. ->with('source', 'target');
  144. $this->response->expects($this->once())
  145. ->method('setHeader')
  146. ->with('Content-Length', '0');
  147. $this->response->expects($this->once())
  148. ->method('setStatus')
  149. ->with(204);
  150. $this->request->expects($this->once())
  151. ->method('getHeader')
  152. ->with('OC-Total-Length')
  153. ->willReturn('4');
  154. $this->assertFalse($this->plugin->beforeMove('source', 'target'));
  155. }
  156. public function testBeforeMoveSizeIsWrong() {
  157. $this->expectException(\Sabre\DAV\Exception\BadRequest::class);
  158. $this->expectExceptionMessage('Chunks on server do not sum up to 4 but to 3 bytes');
  159. $sourceNode = $this->createMock(FutureFile::class);
  160. $sourceNode->expects($this->once())
  161. ->method('getSize')
  162. ->willReturn(3);
  163. $this->tree->expects($this->at(0))
  164. ->method('getNodeForPath')
  165. ->with('source')
  166. ->willReturn($sourceNode);
  167. $this->tree->expects($this->at(1))
  168. ->method('getNodeForPath')
  169. ->with('target')
  170. ->willThrowException(new NotFound());
  171. $this->request->expects($this->once())
  172. ->method('getHeader')
  173. ->with('OC-Total-Length')
  174. ->willReturn('4');
  175. $this->assertFalse($this->plugin->beforeMove('source', 'target'));
  176. }
  177. }