sharecontroller.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * @author Lukas Reschke <lukas@owncloud.com>
  4. * @copyright 2014 Lukas Reschke
  5. *
  6. * This file is licensed under the Affero General Public License version 3 or
  7. * later.
  8. * See the COPYING-README file.
  9. */
  10. namespace OCA\Files_Sharing\Controllers;
  11. use OC\Files\Filesystem;
  12. use OCA\Files_Sharing\Application;
  13. use OCP\AppFramework\IAppContainer;
  14. use OCP\Files;
  15. use OCP\AppFramework\Http\RedirectResponse;
  16. use OCP\AppFramework\Http\TemplateResponse;
  17. use OCP\Security\ISecureRandom;
  18. use OC\Files\View;
  19. use OCP\Share;
  20. use OC\URLGenerator;
  21. /**
  22. * @package OCA\Files_Sharing\Controllers
  23. */
  24. class ShareControllerTest extends \PHPUnit_Framework_TestCase {
  25. /** @var IAppContainer */
  26. private $container;
  27. /** @var string */
  28. private $user;
  29. /** @var string */
  30. private $token;
  31. /** @var string */
  32. private $oldUser;
  33. /** @var ShareController */
  34. private $shareController;
  35. /** @var URLGenerator */
  36. private $urlGenerator;
  37. protected function setUp() {
  38. $app = new Application();
  39. $this->container = $app->getContainer();
  40. $this->container['Config'] = $this->getMockBuilder('\OCP\IConfig')
  41. ->disableOriginalConstructor()->getMock();
  42. $this->container['AppName'] = 'files_sharing';
  43. $this->container['UserSession'] = $this->getMockBuilder('\OC\User\Session')
  44. ->disableOriginalConstructor()->getMock();
  45. $this->container['URLGenerator'] = $this->getMockBuilder('\OC\URLGenerator')
  46. ->disableOriginalConstructor()->getMock();
  47. $this->urlGenerator = $this->container['URLGenerator'];
  48. $this->shareController = $this->container['ShareController'];
  49. // Store current user
  50. $this->oldUser = \OC_User::getUser();
  51. // Create a dummy user
  52. $this->user = \OC::$server->getSecureRandom()->getLowStrengthGenerator()->generate(12, ISecureRandom::CHAR_LOWER);
  53. \OC_User::createUser($this->user, $this->user);
  54. \OC_Util::tearDownFS();
  55. \OC_User::setUserId('');
  56. Filesystem::tearDown();
  57. \OC_User::setUserId($this->user);
  58. \OC_Util::setupFS($this->user);
  59. // Create a dummy shared file
  60. $view = new View('/'. $this->user . '/files');
  61. $view->file_put_contents('file1.txt', 'I am such an awesome shared file!');
  62. $this->token = \OCP\Share::shareItem(
  63. Filesystem::getFileInfo('file1.txt')->getType(),
  64. Filesystem::getFileInfo('file1.txt')->getId(),
  65. \OCP\Share::SHARE_TYPE_LINK,
  66. 'IAmPasswordProtected!',
  67. 1
  68. );
  69. }
  70. protected function tearDown() {
  71. \OC_Util::tearDownFS();
  72. \OC_User::setUserId('');
  73. Filesystem::tearDown();
  74. \OC_User::deleteUser($this->user);
  75. \OC_User::setIncognitoMode(false);
  76. \OC::$server->getSession()->set('public_link_authenticated', '');
  77. // Set old user
  78. \OC_User::setUserId($this->oldUser);
  79. \OC_Util::setupFS($this->oldUser);
  80. }
  81. public function testShowAuthenticate() {
  82. $linkItem = \OCP\Share::getShareByToken($this->token, false);
  83. // Test without being authenticated
  84. $response = $this->shareController->showAuthenticate($this->token);
  85. $expectedResponse = new TemplateResponse($this->container['AppName'], 'authenticate', array(), 'guest');
  86. $this->assertEquals($expectedResponse, $response);
  87. // Test with being authenticated for another file
  88. \OC::$server->getSession()->set('public_link_authenticated', $linkItem['id']-1);
  89. $response = $this->shareController->showAuthenticate($this->token);
  90. $expectedResponse = new TemplateResponse($this->container['AppName'], 'authenticate', array(), 'guest');
  91. $this->assertEquals($expectedResponse, $response);
  92. // Test with being authenticated for the correct file
  93. \OC::$server->getSession()->set('public_link_authenticated', $linkItem['id']);
  94. $response = $this->shareController->showAuthenticate($this->token);
  95. $expectedResponse = new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.showShare', array('token' => $this->token)));
  96. $this->assertEquals($expectedResponse, $response);
  97. }
  98. public function testAuthenticate() {
  99. // Test without a not existing token
  100. $response = $this->shareController->authenticate('ThisTokenShouldHopefullyNeverExistSoThatTheUnitTestWillAlwaysPass :)');
  101. $expectedResponse = new TemplateResponse('core', '404', array(), 'guest');
  102. $this->assertEquals($expectedResponse, $response);
  103. // Test with a valid password
  104. $response = $this->shareController->authenticate($this->token, 'IAmPasswordProtected!');
  105. $expectedResponse = new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.showShare', array('token' => $this->token)));
  106. $this->assertEquals($expectedResponse, $response);
  107. // Test with a invalid password
  108. $response = $this->shareController->authenticate($this->token, 'WrongPw!');
  109. $expectedResponse = new TemplateResponse($this->container['AppName'], 'authenticate', array('wrongpw' => true), 'guest');
  110. $this->assertEquals($expectedResponse, $response);
  111. }
  112. public function testShowShare() {
  113. // Test without a not existing token
  114. $response = $this->shareController->showShare('ThisTokenShouldHopefullyNeverExistSoThatTheUnitTestWillAlwaysPass :)');
  115. $expectedResponse = new TemplateResponse('core', '404', array(), 'guest');
  116. $this->assertEquals($expectedResponse, $response);
  117. // Test with a password protected share and no authentication
  118. $response = $this->shareController->showShare($this->token);
  119. $expectedResponse = new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.authenticate', array('token' => $this->token)));
  120. $this->assertEquals($expectedResponse, $response);
  121. // Test with password protected share and authentication
  122. $linkItem = Share::getShareByToken($this->token, false);
  123. \OC::$server->getSession()->set('public_link_authenticated', $linkItem['id']);
  124. $response = $this->shareController->showShare($this->token);
  125. $sharedTmplParams = array(
  126. 'displayName' => $this->user,
  127. 'filename' => 'file1.txt',
  128. 'directory_path' => '/file1.txt',
  129. 'mimetype' => 'text/plain',
  130. 'dirToken' => $this->token,
  131. 'sharingToken' => $this->token,
  132. 'server2serversharing' => true,
  133. 'protected' => 'true',
  134. 'dir' => '',
  135. 'downloadURL' => null,
  136. 'fileSize' => '33 B'
  137. );
  138. $expectedResponse = new TemplateResponse($this->container['AppName'], 'public', $sharedTmplParams, 'base');
  139. $this->assertEquals($expectedResponse, $response);
  140. }
  141. public function testDownloadShare() {
  142. // Test with a password protected share and no authentication
  143. $response = $this->shareController->downloadShare($this->token);
  144. $expectedResponse = new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.authenticate',
  145. array('token' => $this->token)));
  146. $this->assertEquals($expectedResponse, $response);
  147. }
  148. }