ShareByMailProviderTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2022 Richard Steinmetz <richard@steinmetz.cloud>
  5. *
  6. * @author Richard Steinmetz <richard@steinmetz.cloud>
  7. *
  8. * @license AGPL-3.0-or-later
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace Test\Share20;
  24. use OC\Files\Node\Node;
  25. use OCA\ShareByMail\Settings\SettingsManager;
  26. use OCA\ShareByMail\ShareByMailProvider;
  27. use OCP\DB\QueryBuilder\IQueryBuilder;
  28. use OCP\Defaults;
  29. use OCP\EventDispatcher\IEventDispatcher;
  30. use OCP\Files\IRootFolder;
  31. use OCP\IConfig;
  32. use OCP\IDBConnection;
  33. use OCP\IL10N;
  34. use OCP\IURLGenerator;
  35. use OCP\IUserManager;
  36. use OCP\Mail\IMailer;
  37. use OCP\Security\IHasher;
  38. use OCP\Security\ISecureRandom;
  39. use OCP\Share\IShare;
  40. use PHPUnit\Framework\MockObject\MockObject;
  41. use Psr\Log\LoggerInterface;
  42. use Test\TestCase;
  43. /**
  44. * Class ShareByMailProviderTest
  45. *
  46. * @package Test\Share20
  47. * @group DB
  48. */
  49. class ShareByMailProviderTest extends TestCase {
  50. /** @var IDBConnection */
  51. protected $dbConn;
  52. /** @var IUserManager | \PHPUnit\Framework\MockObject\MockObject */
  53. protected $userManager;
  54. /** @var IRootFolder | \PHPUnit\Framework\MockObject\MockObject */
  55. protected $rootFolder;
  56. /** @var ShareByMailProvider */
  57. protected $provider;
  58. /** @var \PHPUnit\Framework\MockObject\MockObject|IMailer */
  59. protected $mailer;
  60. /** @var \PHPUnit\Framework\MockObject\MockObject|IL10N */
  61. protected $l10n;
  62. /** @var \PHPUnit\Framework\MockObject\MockObject|Defaults */
  63. protected $defaults;
  64. /** @var \PHPUnit\Framework\MockObject\MockObject|IURLGenerator */
  65. protected $urlGenerator;
  66. /** @var IConfig|MockObject */
  67. protected $config;
  68. /** @var LoggerInterface|MockObject */
  69. private $logger;
  70. /** @var IHasher|MockObject */
  71. private $hasher;
  72. /** @var \OCP\Activity\IManager|MockObject */
  73. private $activityManager;
  74. /** @var IEventDispatcher|MockObject */
  75. private $eventDispatcher;
  76. /** @var \OCP\Share\IManager|MockObject */
  77. private $shareManager;
  78. /** @var ISecureRandom|MockObject */
  79. private $secureRandom;
  80. /** @var SettingsManager|MockObject */
  81. private $settingsManager;
  82. protected function setUp(): void {
  83. $this->dbConn = \OC::$server->getDatabaseConnection();
  84. $this->userManager = $this->createMock(IUserManager::class);
  85. $this->rootFolder = $this->createMock(IRootFolder::class);
  86. $this->mailer = $this->createMock(IMailer::class);
  87. $this->l10n = $this->createMock(IL10N::class);
  88. $this->defaults = $this->getMockBuilder(Defaults::class)->disableOriginalConstructor()->getMock();
  89. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  90. $this->logger = $this->createMock(LoggerInterface::class);
  91. $this->activityManager = $this->createMock(\OCP\Activity\IManager::class);
  92. $this->settingsManager = $this->createMock(SettingsManager::class);
  93. $this->hasher = $this->createMock(IHasher::class);
  94. $this->eventDispatcher = $this->createMock(IEventDispatcher::class);
  95. $this->shareManager = $this->createMock(\OCP\Share\IManager::class);
  96. $this->secureRandom = $this->createMock(ISecureRandom::class);
  97. $this->config = $this->createMock(IConfig::class);
  98. // Empty share table
  99. $this->dbConn->getQueryBuilder()->delete('share')->execute();
  100. $this->provider = new ShareByMailProvider(
  101. $this->config,
  102. $this->dbConn,
  103. $this->secureRandom,
  104. $this->userManager,
  105. $this->rootFolder,
  106. $this->l10n,
  107. $this->logger,
  108. $this->mailer,
  109. $this->urlGenerator,
  110. $this->activityManager,
  111. $this->settingsManager,
  112. $this->defaults,
  113. $this->hasher,
  114. $this->eventDispatcher,
  115. $this->shareManager,
  116. );
  117. }
  118. protected function tearDown(): void {
  119. $this->dbConn->getQueryBuilder()->delete('share')->execute();
  120. $this->dbConn->getQueryBuilder()->delete('filecache')->execute();
  121. $this->dbConn->getQueryBuilder()->delete('storages')->execute();
  122. }
  123. /**
  124. * @param int $shareType
  125. * @param string $sharedWith
  126. * @param string $sharedBy
  127. * @param string $shareOwner
  128. * @param string $itemType
  129. * @param int $fileSource
  130. * @param string $fileTarget
  131. * @param int $permissions
  132. * @param $token
  133. * @param $expiration
  134. * @param $parent
  135. * @return int
  136. *
  137. * @throws \OCP\DB\Exception
  138. */
  139. private function addShareToDB($shareType, $sharedWith, $sharedBy, $shareOwner,
  140. $itemType, $fileSource, $fileTarget, $permissions, $token, $expiration,
  141. $parent) {
  142. $qb = $this->dbConn->getQueryBuilder();
  143. $qb->insert('share');
  144. if ($shareType) {
  145. $qb->setValue('share_type', $qb->expr()->literal($shareType));
  146. }
  147. if ($sharedWith) {
  148. $qb->setValue('share_with', $qb->expr()->literal($sharedWith));
  149. }
  150. if ($sharedBy) {
  151. $qb->setValue('uid_initiator', $qb->expr()->literal($sharedBy));
  152. }
  153. if ($shareOwner) {
  154. $qb->setValue('uid_owner', $qb->expr()->literal($shareOwner));
  155. }
  156. if ($itemType) {
  157. $qb->setValue('item_type', $qb->expr()->literal($itemType));
  158. }
  159. if ($fileSource) {
  160. $qb->setValue('file_source', $qb->expr()->literal($fileSource));
  161. }
  162. if ($fileTarget) {
  163. $qb->setValue('file_target', $qb->expr()->literal($fileTarget));
  164. }
  165. if ($permissions) {
  166. $qb->setValue('permissions', $qb->expr()->literal($permissions));
  167. }
  168. if ($token) {
  169. $qb->setValue('token', $qb->expr()->literal($token));
  170. }
  171. if ($expiration) {
  172. $qb->setValue('expiration', $qb->createNamedParameter($expiration, IQueryBuilder::PARAM_DATE));
  173. }
  174. if ($parent) {
  175. $qb->setValue('parent', $qb->expr()->literal($parent));
  176. }
  177. $this->assertEquals(1, $qb->execute());
  178. return $qb->getLastInsertId();
  179. }
  180. public function testGetSharesByWithResharesAndNoNode() {
  181. $this->addShareToDB(
  182. IShare::TYPE_EMAIL,
  183. 'external.one@domain.tld',
  184. 'user1',
  185. 'user1',
  186. 'folder',
  187. 42,
  188. null,
  189. 17,
  190. 'foobar',
  191. null,
  192. null,
  193. );
  194. $this->addShareToDB(
  195. IShare::TYPE_EMAIL,
  196. 'external.two@domain.tld',
  197. 'user2',
  198. 'user2',
  199. 'folder',
  200. 42,
  201. null,
  202. 17,
  203. 'barfoo',
  204. null,
  205. null,
  206. );
  207. // Return own shares only if not asked for a specific node
  208. /** @var IShare[] $actual */
  209. $actual = $this->provider->getSharesBy(
  210. 'user1',
  211. IShare::TYPE_EMAIL,
  212. null,
  213. true,
  214. -1,
  215. 0,
  216. );
  217. $this->assertCount(1, $actual);
  218. $this->assertEquals(IShare::TYPE_EMAIL, $actual[0]->getShareType());
  219. $this->assertEquals('user1', $actual[0]->getSharedBy());
  220. $this->assertEquals('user1', $actual[0]->getShareOwner());
  221. $this->assertEquals('external.one@domain.tld', $actual[0]->getSharedWith());
  222. }
  223. public function testGetSharesByWithResharesAndNode() {
  224. $this->addShareToDB(
  225. IShare::TYPE_EMAIL,
  226. 'external.one@domain.tld',
  227. 'user1',
  228. 'user1',
  229. 'folder',
  230. 42,
  231. null,
  232. 17,
  233. 'foobar',
  234. null,
  235. null,
  236. );
  237. $this->addShareToDB(
  238. IShare::TYPE_EMAIL,
  239. 'external.two@domain.tld',
  240. 'user2',
  241. 'user2',
  242. 'folder',
  243. 42,
  244. null,
  245. 17,
  246. 'barfoo',
  247. null,
  248. null,
  249. );
  250. $node = $this->createMock(Node::class);
  251. $node->expects($this->once())
  252. ->method('getId')
  253. ->willReturn(42);
  254. // Return all shares if asked for specific node
  255. /** @var IShare[] $actual */
  256. $actual = $this->provider->getSharesBy(
  257. 'user1',
  258. IShare::TYPE_EMAIL,
  259. $node,
  260. true,
  261. -1,
  262. 0,
  263. );
  264. $this->assertCount(2, $actual);
  265. $this->assertEquals(IShare::TYPE_EMAIL, $actual[0]->getShareType());
  266. $this->assertEquals('user1', $actual[0]->getSharedBy());
  267. $this->assertEquals('user1', $actual[0]->getShareOwner());
  268. $this->assertEquals('external.one@domain.tld', $actual[0]->getSharedWith());
  269. $this->assertEquals(IShare::TYPE_EMAIL, $actual[1]->getShareType());
  270. $this->assertEquals('user2', $actual[1]->getSharedBy());
  271. $this->assertEquals('user2', $actual[1]->getShareOwner());
  272. $this->assertEquals('external.two@domain.tld', $actual[1]->getSharedWith());
  273. }
  274. }