FederationContext.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. use Behat\Behat\Context\Context;
  8. use Behat\Behat\Context\SnippetAcceptingContext;
  9. use Behat\Gherkin\Node\TableNode;
  10. require __DIR__ . '/../../vendor/autoload.php';
  11. /**
  12. * Federation context.
  13. */
  14. class FederationContext implements Context, SnippetAcceptingContext {
  15. use WebDav;
  16. use AppConfiguration;
  17. use CommandLine;
  18. /** @var string */
  19. private static $phpFederatedServerPid = '';
  20. /** @var string */
  21. private $lastAcceptedRemoteShareId;
  22. /**
  23. * @BeforeScenario
  24. * @AfterScenario
  25. *
  26. * The server is started also after the scenarios to ensure that it is
  27. * properly cleaned up if stopped.
  28. */
  29. public function startFederatedServer() {
  30. if (self::$phpFederatedServerPid !== '') {
  31. return;
  32. }
  33. $port = getenv('PORT_FED');
  34. self::$phpFederatedServerPid = exec('PHP_CLI_SERVER_WORKERS=2 php -S localhost:' . $port . ' -t ../../ >/dev/null & echo $!');
  35. }
  36. /**
  37. * @BeforeScenario
  38. */
  39. public function cleanupRemoteStorages() {
  40. // Ensure that dangling remote storages from previous tests will not
  41. // interfere with the current scenario.
  42. // The storages must be cleaned before each scenario; they can not be
  43. // cleaned after each scenario, as this hook is executed before the hook
  44. // that removes the users, so the shares would be still valid and thus
  45. // the storages would not be dangling yet.
  46. $this->runOcc(['sharing:cleanup-remote-storages']);
  47. }
  48. /**
  49. * @Given /^User "([^"]*)" from server "(LOCAL|REMOTE)" shares "([^"]*)" with user "([^"]*)" from server "(LOCAL|REMOTE)"$/
  50. *
  51. * @param string $sharerUser
  52. * @param string $sharerServer "LOCAL" or "REMOTE"
  53. * @param string $sharerPath
  54. * @param string $shareeUser
  55. * @param string $shareeServer "LOCAL" or "REMOTE"
  56. */
  57. public function federateSharing($sharerUser, $sharerServer, $sharerPath, $shareeUser, $shareeServer) {
  58. if ($shareeServer == 'REMOTE') {
  59. $shareWith = "$shareeUser@" . substr($this->remoteBaseUrl, 0, -4);
  60. } else {
  61. $shareWith = "$shareeUser@" . substr($this->localBaseUrl, 0, -4);
  62. }
  63. $previous = $this->usingServer($sharerServer);
  64. $this->createShare($sharerUser, $sharerPath, 6, $shareWith, null, null, null);
  65. $this->usingServer($previous);
  66. }
  67. /**
  68. * @Given /^User "([^"]*)" from server "(LOCAL|REMOTE)" shares "([^"]*)" with group "([^"]*)" from server "(LOCAL|REMOTE)"$/
  69. *
  70. * @param string $sharerUser
  71. * @param string $sharerServer "LOCAL" or "REMOTE"
  72. * @param string $sharerPath
  73. * @param string $shareeUser
  74. * @param string $shareeServer "LOCAL" or "REMOTE"
  75. */
  76. public function federateGroupSharing($sharerUser, $sharerServer, $sharerPath, $shareeGroup, $shareeServer) {
  77. if ($shareeServer == 'REMOTE') {
  78. $shareWith = "$shareeGroup@" . substr($this->remoteBaseUrl, 0, -4);
  79. } else {
  80. $shareWith = "$shareeGroup@" . substr($this->localBaseUrl, 0, -4);
  81. }
  82. $previous = $this->usingServer($sharerServer);
  83. $this->createShare($sharerUser, $sharerPath, 9, $shareWith, null, null, null);
  84. $this->usingServer($previous);
  85. }
  86. /**
  87. * @Then remote share :count is returned with
  88. *
  89. * @param int $number
  90. * @param TableNode $body
  91. */
  92. public function remoteShareXIsReturnedWith(int $number, TableNode $body) {
  93. $this->theHTTPStatusCodeShouldBe('200');
  94. $this->theOCSStatusCodeShouldBe('100');
  95. if (!($body instanceof TableNode)) {
  96. return;
  97. }
  98. $returnedShare = $this->getXmlResponse()->data[0];
  99. if ($returnedShare->element) {
  100. $returnedShare = $returnedShare->element[$number];
  101. }
  102. $defaultExpectedFields = [
  103. 'id' => 'A_NUMBER',
  104. 'remote_id' => 'A_NUMBER',
  105. 'accepted' => '1',
  106. ];
  107. $expectedFields = array_merge($defaultExpectedFields, $body->getRowsHash());
  108. foreach ($expectedFields as $field => $value) {
  109. $this->assertFieldIsInReturnedShare($field, $value, $returnedShare);
  110. }
  111. }
  112. /**
  113. * @When /^User "([^"]*)" from server "(LOCAL|REMOTE)" accepts last pending share$/
  114. * @param string $user
  115. * @param string $server
  116. */
  117. public function acceptLastPendingShare($user, $server) {
  118. $previous = $this->usingServer($server);
  119. $this->asAn($user);
  120. $this->sendingToWith('GET', '/apps/files_sharing/api/v1/remote_shares/pending', null);
  121. $this->theHTTPStatusCodeShouldBe('200');
  122. $this->theOCSStatusCodeShouldBe('100');
  123. $share_id = simplexml_load_string($this->response->getBody())->data[0]->element[0]->id;
  124. $this->sendingToWith('POST', "/apps/files_sharing/api/v1/remote_shares/pending/{$share_id}", null);
  125. $this->theHTTPStatusCodeShouldBe('200');
  126. $this->theOCSStatusCodeShouldBe('100');
  127. $this->usingServer($previous);
  128. $this->lastAcceptedRemoteShareId = $share_id;
  129. }
  130. /**
  131. * @When /^user "([^"]*)" deletes last accepted remote share$/
  132. * @param string $user
  133. */
  134. public function deleteLastAcceptedRemoteShare($user) {
  135. $this->asAn($user);
  136. $this->sendingToWith('DELETE', '/apps/files_sharing/api/v1/remote_shares/' . $this->lastAcceptedRemoteShareId, null);
  137. }
  138. /**
  139. * @When /^remote server is stopped$/
  140. */
  141. public function remoteServerIsStopped() {
  142. if (self::$phpFederatedServerPid === '') {
  143. return;
  144. }
  145. exec('kill ' . self::$phpFederatedServerPid);
  146. self::$phpFederatedServerPid = '';
  147. }
  148. protected function resetAppConfigs() {
  149. $this->deleteServerConfig('files_sharing', 'incoming_server2server_group_share_enabled');
  150. $this->deleteServerConfig('files_sharing', 'outgoing_server2server_group_share_enabled');
  151. }
  152. }