FederationContext.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Sergio Bertolin <sbertolin@solidgear.es>
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Daniel Calviño Sánchez <danxuliu@gmail.com>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Sergio Bertolin <sbertolin@solidgear.es>
  11. * @author Sergio Bertolín <sbertolin@solidgear.es>
  12. *
  13. * @license GNU AGPL version 3 or any later version
  14. *
  15. * This program is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License as
  17. * published by the Free Software Foundation, either version 3 of the
  18. * License, or (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. *
  28. */
  29. use Behat\Behat\Context\Context;
  30. use Behat\Behat\Context\SnippetAcceptingContext;
  31. use Behat\Gherkin\Node\TableNode;
  32. require __DIR__ . '/../../vendor/autoload.php';
  33. /**
  34. * Federation context.
  35. */
  36. class FederationContext implements Context, SnippetAcceptingContext {
  37. use WebDav;
  38. use AppConfiguration;
  39. use CommandLine;
  40. /** @var string */
  41. private static $phpFederatedServerPid = '';
  42. /** @var string */
  43. private $lastAcceptedRemoteShareId;
  44. /**
  45. * @BeforeScenario
  46. * @AfterScenario
  47. *
  48. * The server is started also after the scenarios to ensure that it is
  49. * properly cleaned up if stopped.
  50. */
  51. public function startFederatedServer() {
  52. if (self::$phpFederatedServerPid !== '') {
  53. return;
  54. }
  55. $port = getenv('PORT_FED');
  56. self::$phpFederatedServerPid = exec('php -S localhost:' . $port . ' -t ../../ >/dev/null & echo $!');
  57. }
  58. /**
  59. * @BeforeScenario
  60. */
  61. public function cleanupRemoteStorages() {
  62. // Ensure that dangling remote storages from previous tests will not
  63. // interfere with the current scenario.
  64. // The storages must be cleaned before each scenario; they can not be
  65. // cleaned after each scenario, as this hook is executed before the hook
  66. // that removes the users, so the shares would be still valid and thus
  67. // the storages would not be dangling yet.
  68. $this->runOcc(['sharing:cleanup-remote-storages']);
  69. }
  70. /**
  71. * @Given /^User "([^"]*)" from server "(LOCAL|REMOTE)" shares "([^"]*)" with user "([^"]*)" from server "(LOCAL|REMOTE)"$/
  72. *
  73. * @param string $sharerUser
  74. * @param string $sharerServer "LOCAL" or "REMOTE"
  75. * @param string $sharerPath
  76. * @param string $shareeUser
  77. * @param string $shareeServer "LOCAL" or "REMOTE"
  78. */
  79. public function federateSharing($sharerUser, $sharerServer, $sharerPath, $shareeUser, $shareeServer) {
  80. if ($shareeServer == "REMOTE") {
  81. $shareWith = "$shareeUser@" . substr($this->remoteBaseUrl, 0, -4);
  82. } else {
  83. $shareWith = "$shareeUser@" . substr($this->localBaseUrl, 0, -4);
  84. }
  85. $previous = $this->usingServer($sharerServer);
  86. $this->createShare($sharerUser, $sharerPath, 6, $shareWith, null, null, null);
  87. $this->usingServer($previous);
  88. }
  89. /**
  90. * @Given /^User "([^"]*)" from server "(LOCAL|REMOTE)" shares "([^"]*)" with group "([^"]*)" from server "(LOCAL|REMOTE)"$/
  91. *
  92. * @param string $sharerUser
  93. * @param string $sharerServer "LOCAL" or "REMOTE"
  94. * @param string $sharerPath
  95. * @param string $shareeUser
  96. * @param string $shareeServer "LOCAL" or "REMOTE"
  97. */
  98. public function federateGroupSharing($sharerUser, $sharerServer, $sharerPath, $shareeGroup, $shareeServer) {
  99. if ($shareeServer == "REMOTE") {
  100. $shareWith = "$shareeGroup@" . substr($this->remoteBaseUrl, 0, -4);
  101. } else {
  102. $shareWith = "$shareeGroup@" . substr($this->localBaseUrl, 0, -4);
  103. }
  104. $previous = $this->usingServer($sharerServer);
  105. $this->createShare($sharerUser, $sharerPath, 9, $shareWith, null, null, null);
  106. $this->usingServer($previous);
  107. }
  108. /**
  109. * @Then remote share :count is returned with
  110. *
  111. * @param int $number
  112. * @param TableNode $body
  113. */
  114. public function remoteShareXIsReturnedWith(int $number, TableNode $body) {
  115. $this->theHTTPStatusCodeShouldBe('200');
  116. $this->theOCSStatusCodeShouldBe('100');
  117. if (!($body instanceof TableNode)) {
  118. return;
  119. }
  120. $returnedShare = $this->getXmlResponse()->data[0];
  121. if ($returnedShare->element) {
  122. $returnedShare = $returnedShare->element[$number];
  123. }
  124. $defaultExpectedFields = [
  125. 'id' => 'A_NUMBER',
  126. 'remote_id' => 'A_NUMBER',
  127. 'accepted' => '1',
  128. ];
  129. $expectedFields = array_merge($defaultExpectedFields, $body->getRowsHash());
  130. foreach ($expectedFields as $field => $value) {
  131. $this->assertFieldIsInReturnedShare($field, $value, $returnedShare);
  132. }
  133. }
  134. /**
  135. * @When /^User "([^"]*)" from server "(LOCAL|REMOTE)" accepts last pending share$/
  136. * @param string $user
  137. * @param string $server
  138. */
  139. public function acceptLastPendingShare($user, $server) {
  140. $previous = $this->usingServer($server);
  141. $this->asAn($user);
  142. $this->sendingToWith('GET', "/apps/files_sharing/api/v1/remote_shares/pending", null);
  143. $this->theHTTPStatusCodeShouldBe('200');
  144. $this->theOCSStatusCodeShouldBe('100');
  145. $share_id = simplexml_load_string($this->response->getBody())->data[0]->element[0]->id;
  146. $this->sendingToWith('POST', "/apps/files_sharing/api/v1/remote_shares/pending/{$share_id}", null);
  147. $this->theHTTPStatusCodeShouldBe('200');
  148. $this->theOCSStatusCodeShouldBe('100');
  149. $this->usingServer($previous);
  150. $this->lastAcceptedRemoteShareId = $share_id;
  151. }
  152. /**
  153. * @When /^user "([^"]*)" deletes last accepted remote share$/
  154. * @param string $user
  155. */
  156. public function deleteLastAcceptedRemoteShare($user) {
  157. $this->asAn($user);
  158. $this->sendingToWith('DELETE', "/apps/files_sharing/api/v1/remote_shares/" . $this->lastAcceptedRemoteShareId, null);
  159. }
  160. /**
  161. * @When /^remote server is stopped$/
  162. */
  163. public function remoteServerIsStopped() {
  164. if (self::$phpFederatedServerPid === '') {
  165. return;
  166. }
  167. exec('kill ' . self::$phpFederatedServerPid);
  168. self::$phpFederatedServerPid = '';
  169. }
  170. protected function resetAppConfigs() {
  171. $this->deleteServerConfig('files_sharing', 'incoming_server2server_group_share_enabled');
  172. $this->deleteServerConfig('files_sharing', 'outgoing_server2server_group_share_enabled');
  173. }
  174. }