1
0

ChunkingPluginTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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(): void {
  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(): void {
  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->exactly(2))
  84. ->method('getNodeForPath')
  85. ->withConsecutive(
  86. ['source'],
  87. ['target'],
  88. )
  89. ->willReturnOnConsecutiveCalls(
  90. $sourceNode,
  91. $targetNode,
  92. );
  93. $this->response->expects($this->never())
  94. ->method('setStatus');
  95. $this->assertNull($this->plugin->beforeMove('source', 'target'));
  96. }
  97. public function testBeforeMoveFutureFileSkipNonExisting(): void {
  98. $sourceNode = $this->createMock(FutureFile::class);
  99. $sourceNode->expects($this->once())
  100. ->method('getSize')
  101. ->willReturn(4);
  102. $this->tree->expects($this->exactly(2))
  103. ->method('getNodeForPath')
  104. ->withConsecutive(
  105. ['source'],
  106. ['target'],
  107. )
  108. ->willReturnOnConsecutiveCalls(
  109. $sourceNode,
  110. $this->throwException(new NotFound()),
  111. );
  112. $this->tree->expects($this->any())
  113. ->method('nodeExists')
  114. ->with('target')
  115. ->willReturn(false);
  116. $this->response->expects($this->once())
  117. ->method('setHeader')
  118. ->with('Content-Length', '0');
  119. $this->response->expects($this->once())
  120. ->method('setStatus')
  121. ->with(201);
  122. $this->request->expects($this->once())
  123. ->method('getHeader')
  124. ->with('OC-Total-Length')
  125. ->willReturn(4);
  126. $this->assertFalse($this->plugin->beforeMove('source', 'target'));
  127. }
  128. public function testBeforeMoveFutureFileMoveIt(): void {
  129. $sourceNode = $this->createMock(FutureFile::class);
  130. $sourceNode->expects($this->once())
  131. ->method('getSize')
  132. ->willReturn(4);
  133. $this->tree->expects($this->exactly(2))
  134. ->method('getNodeForPath')
  135. ->withConsecutive(
  136. ['source'],
  137. ['target'],
  138. )
  139. ->willReturnOnConsecutiveCalls(
  140. $sourceNode,
  141. $this->throwException(new NotFound()),
  142. );
  143. $this->tree->expects($this->any())
  144. ->method('nodeExists')
  145. ->with('target')
  146. ->willReturn(true);
  147. $this->tree->expects($this->once())
  148. ->method('move')
  149. ->with('source', 'target');
  150. $this->response->expects($this->once())
  151. ->method('setHeader')
  152. ->with('Content-Length', '0');
  153. $this->response->expects($this->once())
  154. ->method('setStatus')
  155. ->with(204);
  156. $this->request->expects($this->once())
  157. ->method('getHeader')
  158. ->with('OC-Total-Length')
  159. ->willReturn('4');
  160. $this->assertFalse($this->plugin->beforeMove('source', 'target'));
  161. }
  162. public function testBeforeMoveSizeIsWrong(): void {
  163. $this->expectException(\Sabre\DAV\Exception\BadRequest::class);
  164. $this->expectExceptionMessage('Chunks on server do not sum up to 4 but to 3 bytes');
  165. $sourceNode = $this->createMock(FutureFile::class);
  166. $sourceNode->expects($this->once())
  167. ->method('getSize')
  168. ->willReturn(3);
  169. $this->tree->expects($this->exactly(2))
  170. ->method('getNodeForPath')
  171. ->withConsecutive(
  172. ['source'],
  173. ['target'],
  174. )
  175. ->willReturnOnConsecutiveCalls(
  176. $sourceNode,
  177. $this->throwException(new NotFound()),
  178. );
  179. $this->request->expects($this->once())
  180. ->method('getHeader')
  181. ->with('OC-Total-Length')
  182. ->willReturn('4');
  183. $this->assertFalse($this->plugin->beforeMove('source', 'target'));
  184. }
  185. }