1
0

ChecksumsContext.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. require __DIR__ . '/../../vendor/autoload.php';
  8. use GuzzleHttp\Client;
  9. use GuzzleHttp\Message\ResponseInterface;
  10. class ChecksumsContext implements \Behat\Behat\Context\Context {
  11. /** @var string */
  12. private $baseUrl;
  13. /** @var Client */
  14. private $client;
  15. /** @var ResponseInterface */
  16. private $response;
  17. /**
  18. * @param string $baseUrl
  19. */
  20. public function __construct($baseUrl) {
  21. $this->baseUrl = $baseUrl;
  22. // in case of ci deployment we take the server url from the environment
  23. $testServerUrl = getenv('TEST_SERVER_URL');
  24. if ($testServerUrl !== false) {
  25. $this->baseUrl = substr($testServerUrl, 0, -5);
  26. }
  27. }
  28. /** @BeforeScenario */
  29. public function setUpScenario() {
  30. $this->client = new Client();
  31. }
  32. /** @AfterScenario */
  33. public function tearDownScenario() {
  34. }
  35. /**
  36. * @param string $userName
  37. * @return string
  38. */
  39. private function getPasswordForUser($userName) {
  40. if ($userName === 'admin') {
  41. return 'admin';
  42. }
  43. return '123456';
  44. }
  45. /**
  46. * @When user :user uploads file :source to :destination with checksum :checksum
  47. * @param string $user
  48. * @param string $source
  49. * @param string $destination
  50. * @param string $checksum
  51. */
  52. public function userUploadsFileToWithChecksum($user, $source, $destination, $checksum) {
  53. $file = \GuzzleHttp\Psr7\Utils::streamFor(fopen($source, 'r'));
  54. try {
  55. $this->response = $this->client->put(
  56. $this->baseUrl . '/remote.php/webdav' . $destination,
  57. [
  58. 'auth' => [
  59. $user,
  60. $this->getPasswordForUser($user)
  61. ],
  62. 'body' => $file,
  63. 'headers' => [
  64. 'OC-Checksum' => $checksum
  65. ]
  66. ]
  67. );
  68. } catch (\GuzzleHttp\Exception\ServerException $e) {
  69. // 4xx and 5xx responses cause an exception
  70. $this->response = $e->getResponse();
  71. }
  72. }
  73. /**
  74. * @Then The webdav response should have a status code :statusCode
  75. * @param int $statusCode
  76. * @throws \Exception
  77. */
  78. public function theWebdavResponseShouldHaveAStatusCode($statusCode) {
  79. if ((int)$statusCode !== $this->response->getStatusCode()) {
  80. throw new \Exception("Expected $statusCode, got " . $this->response->getStatusCode());
  81. }
  82. }
  83. /**
  84. * @When user :user request the checksum of :path via propfind
  85. * @param string $user
  86. * @param string $path
  87. */
  88. public function userRequestTheChecksumOfViaPropfind($user, $path) {
  89. $this->response = $this->client->request(
  90. 'PROPFIND',
  91. $this->baseUrl . '/remote.php/webdav' . $path,
  92. [
  93. 'body' => '<?xml version="1.0"?>
  94. <d:propfind xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns">
  95. <d:prop>
  96. <oc:checksums />
  97. </d:prop>
  98. </d:propfind>',
  99. 'auth' => [
  100. $user,
  101. $this->getPasswordForUser($user),
  102. ]
  103. ]
  104. );
  105. }
  106. /**
  107. * @Then The webdav checksum should match :checksum
  108. * @param string $checksum
  109. * @throws \Exception
  110. */
  111. public function theWebdavChecksumShouldMatch($checksum) {
  112. $service = new Sabre\Xml\Service();
  113. $parsed = $service->parse($this->response->getBody()->getContents());
  114. /*
  115. * Fetch the checksum array
  116. * Maybe we want to do this a bit cleaner ;)
  117. */
  118. $checksums = $parsed[0]['value'][1]['value'][0]['value'][0];
  119. if ($checksums['value'][0]['value'] !== $checksum) {
  120. throw new \Exception("Expected $checksum, got " . $checksums['value'][0]['value']);
  121. }
  122. }
  123. /**
  124. * @When user :user downloads the file :path
  125. * @param string $user
  126. * @param string $path
  127. */
  128. public function userDownloadsTheFile($user, $path) {
  129. $this->response = $this->client->get(
  130. $this->baseUrl . '/remote.php/webdav' . $path,
  131. [
  132. 'auth' => [
  133. $user,
  134. $this->getPasswordForUser($user),
  135. ]
  136. ]
  137. );
  138. }
  139. /**
  140. * @Then The header checksum should match :checksum
  141. * @param string $checksum
  142. * @throws \Exception
  143. */
  144. public function theHeaderChecksumShouldMatch($checksum) {
  145. if ($this->response->getHeader('OC-Checksum')[0] !== $checksum) {
  146. throw new \Exception("Expected $checksum, got " . $this->response->getHeader('OC-Checksum')[0]);
  147. }
  148. }
  149. /**
  150. * @Given User :user copied file :source to :destination
  151. * @param string $user
  152. * @param string $source
  153. * @param string $destination
  154. */
  155. public function userCopiedFileTo($user, $source, $destination) {
  156. $this->response = $this->client->request(
  157. 'MOVE',
  158. $this->baseUrl . '/remote.php/webdav' . $source,
  159. [
  160. 'auth' => [
  161. $user,
  162. $this->getPasswordForUser($user),
  163. ],
  164. 'headers' => [
  165. 'Destination' => $this->baseUrl . '/remote.php/webdav' . $destination,
  166. ],
  167. ]
  168. );
  169. }
  170. /**
  171. * @Then The webdav checksum should be empty
  172. */
  173. public function theWebdavChecksumShouldBeEmpty() {
  174. $service = new Sabre\Xml\Service();
  175. $parsed = $service->parse($this->response->getBody()->getContents());
  176. /*
  177. * Fetch the checksum array
  178. * Maybe we want to do this a bit cleaner ;)
  179. */
  180. $status = $parsed[0]['value'][1]['value'][1]['value'];
  181. if ($status !== 'HTTP/1.1 404 Not Found') {
  182. throw new \Exception("Expected 'HTTP/1.1 404 Not Found', got " . $status);
  183. }
  184. }
  185. /**
  186. * @Then The OC-Checksum header should not be there
  187. */
  188. public function theOcChecksumHeaderShouldNotBeThere() {
  189. if ($this->response->hasHeader('OC-Checksum')) {
  190. throw new \Exception('Expected no checksum header but got ' . $this->response->getHeader('OC-Checksum')[0]);
  191. }
  192. }
  193. }