FakeLockerPluginTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\DAV\Tests\unit\Connector\Sabre;
  29. use OCA\DAV\Connector\Sabre\FakeLockerPlugin;
  30. use Sabre\DAV\INode;
  31. use Sabre\DAV\PropFind;
  32. use Sabre\DAV\Server;
  33. use Sabre\HTTP\RequestInterface;
  34. use Sabre\HTTP\Response;
  35. use Sabre\HTTP\ResponseInterface;
  36. use Test\TestCase;
  37. /**
  38. * Class FakeLockerPluginTest
  39. *
  40. * @package OCA\DAV\Tests\unit\Connector\Sabre
  41. */
  42. class FakeLockerPluginTest extends TestCase {
  43. /** @var FakeLockerPlugin */
  44. private $fakeLockerPlugin;
  45. protected function setUp(): void {
  46. parent::setUp();
  47. $this->fakeLockerPlugin = new FakeLockerPlugin();
  48. }
  49. public function testInitialize() {
  50. /** @var Server $server */
  51. $server = $this->getMockBuilder(Server::class)
  52. ->disableOriginalConstructor()
  53. ->getMock();
  54. $server
  55. ->expects($this->at(0))
  56. ->method('on')
  57. ->with('method:LOCK', [$this->fakeLockerPlugin, 'fakeLockProvider'], 1);
  58. $server
  59. ->expects($this->at(1))
  60. ->method('on')
  61. ->with('method:UNLOCK', [$this->fakeLockerPlugin, 'fakeUnlockProvider'], 1);
  62. $server
  63. ->expects($this->at(2))
  64. ->method('on')
  65. ->with('propFind', [$this->fakeLockerPlugin, 'propFind']);
  66. $server
  67. ->expects($this->at(3))
  68. ->method('on')
  69. ->with('validateTokens', [$this->fakeLockerPlugin, 'validateTokens']);
  70. $this->fakeLockerPlugin->initialize($server);
  71. }
  72. public function testGetHTTPMethods() {
  73. $expected = [
  74. 'LOCK',
  75. 'UNLOCK',
  76. ];
  77. $this->assertSame($expected, $this->fakeLockerPlugin->getHTTPMethods('Test'));
  78. }
  79. public function testGetFeatures() {
  80. $expected = [
  81. 2,
  82. ];
  83. $this->assertSame($expected, $this->fakeLockerPlugin->getFeatures());
  84. }
  85. public function testPropFind() {
  86. $propFind = $this->getMockBuilder(PropFind::class)
  87. ->disableOriginalConstructor()
  88. ->getMock();
  89. $node = $this->getMockBuilder(INode::class)
  90. ->disableOriginalConstructor()
  91. ->getMock();
  92. $propFind->expects($this->at(0))
  93. ->method('handle')
  94. ->with('{DAV:}supportedlock');
  95. $propFind->expects($this->at(1))
  96. ->method('handle')
  97. ->with('{DAV:}lockdiscovery');
  98. $this->fakeLockerPlugin->propFind($propFind, $node);
  99. }
  100. public function tokenDataProvider() {
  101. return [
  102. [
  103. [
  104. [
  105. 'tokens' => [
  106. [
  107. 'token' => 'aToken',
  108. 'validToken' => false,
  109. ],
  110. [],
  111. [
  112. 'token' => 'opaquelocktoken:asdf',
  113. 'validToken' => false,
  114. ]
  115. ],
  116. ]
  117. ],
  118. [
  119. [
  120. 'tokens' => [
  121. [
  122. 'token' => 'aToken',
  123. 'validToken' => false,
  124. ],
  125. [],
  126. [
  127. 'token' => 'opaquelocktoken:asdf',
  128. 'validToken' => true,
  129. ]
  130. ],
  131. ]
  132. ],
  133. ]
  134. ];
  135. }
  136. /**
  137. * @dataProvider tokenDataProvider
  138. * @param array $input
  139. * @param array $expected
  140. */
  141. public function testValidateTokens(array $input, array $expected) {
  142. $request = $this->getMockBuilder(RequestInterface::class)
  143. ->disableOriginalConstructor()
  144. ->getMock();
  145. $this->fakeLockerPlugin->validateTokens($request, $input);
  146. $this->assertSame($expected, $input);
  147. }
  148. public function testFakeLockProvider() {
  149. $request = $this->getMockBuilder(RequestInterface::class)
  150. ->disableOriginalConstructor()
  151. ->getMock();
  152. $response = new Response();
  153. $server = $this->getMockBuilder(Server::class)
  154. ->getMock();
  155. $this->fakeLockerPlugin->initialize($server);
  156. $request->expects($this->exactly(2))
  157. ->method('getPath')
  158. ->willReturn('MyPath');
  159. $this->assertSame(false, $this->fakeLockerPlugin->fakeLockProvider($request, $response));
  160. $expectedXml = '<?xml version="1.0" encoding="utf-8"?><d:prop xmlns:d="DAV:" xmlns:s="http://sabredav.org/ns"><d:lockdiscovery><d:activelock><d:lockscope><d:exclusive/></d:lockscope><d:locktype><d:write/></d:locktype><d:lockroot><d:href>MyPath</d:href></d:lockroot><d:depth>infinity</d:depth><d:timeout>Second-1800</d:timeout><d:locktoken><d:href>opaquelocktoken:fe4f7f2437b151fbcb4e9f5c8118c6b1</d:href></d:locktoken></d:activelock></d:lockdiscovery></d:prop>';
  161. $this->assertXmlStringEqualsXmlString($expectedXml, $response->getBody());
  162. }
  163. public function testFakeUnlockProvider() {
  164. $request = $this->getMockBuilder(RequestInterface::class)
  165. ->disableOriginalConstructor()
  166. ->getMock();
  167. $response = $this->getMockBuilder(ResponseInterface::class)
  168. ->disableOriginalConstructor()
  169. ->getMock();
  170. $response->expects($this->once())
  171. ->method('setStatus')
  172. ->with('204');
  173. $response->expects($this->once())
  174. ->method('setHeader')
  175. ->with('Content-Length', '0');
  176. $this->assertSame(false, $this->fakeLockerPlugin->fakeUnlockProvider($request, $response));
  177. }
  178. }