ShareByMailProviderTest.php 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Bjoern Schiessle <bjoern@schiessle.org>
  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 Daniel Kesselberg <mail@danielkesselberg.de>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author John Molakvoæ <skjnldsv@protonmail.com>
  11. * @author Lukas Reschke <lukas@statuscode.ch>
  12. * @author Morris Jobke <hey@morrisjobke.de>
  13. * @author Nicolas SIMIDE <2083596+dems54@users.noreply.github.com>
  14. * @author Roeland Jago Douma <roeland@famdouma.nl>
  15. * @author Stephan Müller <mail@stephanmueller.eu>
  16. *
  17. * @license GNU AGPL version 3 or any later version
  18. *
  19. * This program is free software: you can redistribute it and/or modify
  20. * it under the terms of the GNU Affero General Public License as
  21. * published by the Free Software Foundation, either version 3 of the
  22. * License, or (at your option) any later version.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU Affero General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Affero General Public License
  30. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  31. *
  32. */
  33. namespace OCA\ShareByMail\Tests;
  34. use OC\Mail\Message;
  35. use OCA\ShareByMail\Settings\SettingsManager;
  36. use OCA\ShareByMail\ShareByMailProvider;
  37. use OCP\Defaults;
  38. use OCP\EventDispatcher\IEventDispatcher;
  39. use OCP\Files\File;
  40. use OCP\Files\IRootFolder;
  41. use OCP\IConfig;
  42. use OCP\IDBConnection;
  43. use OCP\IL10N;
  44. use OCP\IURLGenerator;
  45. use OCP\IUser;
  46. use OCP\IUserManager;
  47. use OCP\Mail\IEMailTemplate;
  48. use OCP\Mail\IMailer;
  49. use OCP\Mail\IMessage;
  50. use OCP\Security\Events\GenerateSecurePasswordEvent;
  51. use OCP\Security\IHasher;
  52. use OCP\Security\ISecureRandom;
  53. use OCP\Share\IManager;
  54. use OCP\Share\IShare;
  55. use Psr\Log\LoggerInterface;
  56. use Test\TestCase;
  57. /**
  58. * Class ShareByMailProviderTest
  59. *
  60. * @package OCA\ShareByMail\Tests
  61. * @group DB
  62. */
  63. class ShareByMailProviderTest extends TestCase {
  64. /** @var IConfig */
  65. private $config;
  66. /** @var IDBConnection */
  67. private $connection;
  68. /** @var IManager | \PHPUnit\Framework\MockObject\MockObject */
  69. private $shareManager;
  70. /** @var IL10N | \PHPUnit\Framework\MockObject\MockObject */
  71. private $l;
  72. /** @var LoggerInterface | \PHPUnit\Framework\MockObject\MockObject */
  73. private $logger;
  74. /** @var IRootFolder | \PHPUnit\Framework\MockObject\MockObject */
  75. private $rootFolder;
  76. /** @var IUserManager | \PHPUnit\Framework\MockObject\MockObject */
  77. private $userManager;
  78. /** @var ISecureRandom | \PHPUnit\Framework\MockObject\MockObject */
  79. private $secureRandom;
  80. /** @var IMailer | \PHPUnit\Framework\MockObject\MockObject */
  81. private $mailer;
  82. /** @var IURLGenerator | \PHPUnit\Framework\MockObject\MockObject */
  83. private $urlGenerator;
  84. /** @var IShare | \PHPUnit\Framework\MockObject\MockObject */
  85. private $share;
  86. /** @var \OCP\Activity\IManager | \PHPUnit\Framework\MockObject\MockObject */
  87. private $activityManager;
  88. /** @var SettingsManager | \PHPUnit\Framework\MockObject\MockObject */
  89. private $settingsManager;
  90. /** @var Defaults|\PHPUnit\Framework\MockObject\MockObject */
  91. private $defaults;
  92. /** @var IHasher | \PHPUnit\Framework\MockObject\MockObject */
  93. private $hasher;
  94. /** @var IEventDispatcher */
  95. private $eventDispatcher;
  96. protected function setUp(): void {
  97. parent::setUp();
  98. $this->config = $this->getMockBuilder(IConfig::class)->getMock();
  99. $this->connection = \OC::$server->getDatabaseConnection();
  100. $this->l = $this->getMockBuilder(IL10N::class)->getMock();
  101. $this->l->method('t')
  102. ->willReturnCallback(function ($text, $parameters = []) {
  103. return vsprintf($text, $parameters);
  104. });
  105. $this->logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
  106. $this->rootFolder = $this->getMockBuilder('OCP\Files\IRootFolder')->getMock();
  107. $this->userManager = $this->getMockBuilder(IUserManager::class)->getMock();
  108. $this->secureRandom = $this->getMockBuilder('\OCP\Security\ISecureRandom')->getMock();
  109. $this->mailer = $this->getMockBuilder('\OCP\Mail\IMailer')->getMock();
  110. $this->urlGenerator = $this->getMockBuilder(IURLGenerator::class)->getMock();
  111. $this->share = $this->getMockBuilder(IShare::class)->getMock();
  112. $this->activityManager = $this->getMockBuilder('OCP\Activity\IManager')->getMock();
  113. $this->settingsManager = $this->getMockBuilder(SettingsManager::class)->disableOriginalConstructor()->getMock();
  114. $this->defaults = $this->createMock(Defaults::class);
  115. $this->hasher = $this->getMockBuilder(IHasher::class)->getMock();
  116. $this->eventDispatcher = $this->getMockBuilder(IEventDispatcher::class)->getMock();
  117. $this->shareManager = $this->getMockBuilder(IManager::class)->getMock();
  118. $this->userManager->expects($this->any())->method('userExists')->willReturn(true);
  119. }
  120. /**
  121. * get instance of Mocked ShareByMailProvider
  122. *
  123. * @param array $mockedMethods internal methods which should be mocked
  124. * @return \PHPUnit\Framework\MockObject\MockObject | ShareByMailProvider
  125. */
  126. private function getInstance(array $mockedMethods = []) {
  127. $instance = $this->getMockBuilder('OCA\ShareByMail\ShareByMailProvider')
  128. ->setConstructorArgs(
  129. [
  130. $this->config,
  131. $this->connection,
  132. $this->secureRandom,
  133. $this->userManager,
  134. $this->rootFolder,
  135. $this->l,
  136. $this->logger,
  137. $this->mailer,
  138. $this->urlGenerator,
  139. $this->activityManager,
  140. $this->settingsManager,
  141. $this->defaults,
  142. $this->hasher,
  143. $this->eventDispatcher,
  144. $this->shareManager
  145. ]
  146. );
  147. if (!empty($mockedMethods)) {
  148. $instance->setMethods($mockedMethods);
  149. return $instance->getMock();
  150. }
  151. return new ShareByMailProvider(
  152. $this->config,
  153. $this->connection,
  154. $this->secureRandom,
  155. $this->userManager,
  156. $this->rootFolder,
  157. $this->l,
  158. $this->logger,
  159. $this->mailer,
  160. $this->urlGenerator,
  161. $this->activityManager,
  162. $this->settingsManager,
  163. $this->defaults,
  164. $this->hasher,
  165. $this->eventDispatcher,
  166. $this->shareManager
  167. );
  168. }
  169. protected function tearDown(): void {
  170. $this->connection->getQueryBuilder()->delete('share')->execute();
  171. parent::tearDown();
  172. }
  173. public function testCreate() {
  174. $expectedShare = $this->createMock(IShare::class);
  175. $share = $this->getMockBuilder(IShare::class)->getMock();
  176. $share->expects($this->any())->method('getSharedWith')->willReturn('user1');
  177. $node = $this->getMockBuilder(File::class)->getMock();
  178. $node->expects($this->any())->method('getName')->willReturn('filename');
  179. $instance = $this->getInstance(['getSharedWith', 'createMailShare', 'getRawShare', 'createShareObject', 'createShareActivity', 'sendPassword']);
  180. $instance->expects($this->once())->method('getSharedWith')->willReturn([]);
  181. $instance->expects($this->once())->method('createMailShare')->with($share)->willReturn(42);
  182. $instance->expects($this->once())->method('createShareActivity')->with($share);
  183. $instance->expects($this->once())->method('getRawShare')->with(42)->willReturn(['rawShare']);
  184. $instance->expects($this->once())->method('createShareObject')->with(['rawShare'])->willReturn($expectedShare);
  185. $instance->expects($this->any())->method('sendPassword')->willReturn(true);
  186. $share->expects($this->any())->method('getNode')->willReturn($node);
  187. $this->shareManager->expects($this->any())->method('shareApiLinkEnforcePassword')->willReturn(false);
  188. $this->settingsManager->expects($this->any())->method('sendPasswordByMail')->willReturn(true);
  189. $this->assertSame($expectedShare, $instance->create($share));
  190. }
  191. public function testCreateSendPasswordByMailWithoutEnforcedPasswordProtection() {
  192. $expectedShare = $this->createMock(IShare::class);
  193. $share = $this->getMockBuilder(IShare::class)->getMock();
  194. $share->expects($this->any())->method('getSharedWith')->willReturn('receiver@examplelölöl.com');
  195. $share->expects($this->any())->method('getSendPasswordByTalk')->willReturn(false);
  196. $share->expects($this->any())->method('getSharedBy')->willReturn('owner');
  197. $node = $this->getMockBuilder(File::class)->getMock();
  198. $node->expects($this->any())->method('getName')->willReturn('filename');
  199. $instance = $this->getInstance(['getSharedWith', 'createMailShare', 'getRawShare', 'createShareObject', 'createShareActivity', 'autoGeneratePassword', 'createPasswordSendActivity']);
  200. $instance->expects($this->once())->method('getSharedWith')->willReturn([]);
  201. $instance->expects($this->once())->method('createMailShare')->with($share)->willReturn(42);
  202. $instance->expects($this->once())->method('createShareActivity')->with($share);
  203. $instance->expects($this->once())->method('getRawShare')->with(42)->willReturn(['rawShare']);
  204. $instance->expects($this->once())->method('createShareObject')->with(['rawShare'])->willReturn($expectedShare);
  205. $share->expects($this->any())->method('getNode')->willReturn($node);
  206. // The autogenerated password should not be mailed.
  207. $this->shareManager->expects($this->any())->method('shareApiLinkEnforcePassword')->willReturn(false);
  208. $this->settingsManager->expects($this->any())->method('sendPasswordByMail')->willReturn(true);
  209. $instance->expects($this->never())->method('autoGeneratePassword');
  210. $this->mailer->expects($this->never())->method('send');
  211. $this->assertSame($expectedShare, $instance->create($share));
  212. }
  213. public function testCreateSendPasswordByMailWithPasswordAndWithoutEnforcedPasswordProtectionWithPermanentPassword() {
  214. $expectedShare = $this->createMock(IShare::class);
  215. $share = $this->getMockBuilder(IShare::class)->getMock();
  216. $share->expects($this->any())->method('getSharedWith')->willReturn('receiver@example.com');
  217. $share->expects($this->any())->method('getSendPasswordByTalk')->willReturn(false);
  218. $share->expects($this->any())->method('getSharedBy')->willReturn('owner');
  219. $node = $this->getMockBuilder(File::class)->getMock();
  220. $node->expects($this->any())->method('getName')->willReturn('filename');
  221. $instance = $this->getInstance(['getSharedWith', 'createMailShare', 'getRawShare', 'createShareObject', 'createShareActivity', 'autoGeneratePassword', 'createPasswordSendActivity']);
  222. $instance->expects($this->once())->method('getSharedWith')->willReturn([]);
  223. $instance->expects($this->once())->method('createMailShare')->with($share)->willReturn(42);
  224. $instance->expects($this->once())->method('createShareActivity')->with($share);
  225. $instance->expects($this->once())->method('getRawShare')->with(42)->willReturn(['rawShare']);
  226. $instance->expects($this->once())->method('createShareObject')->with(['rawShare'])->willReturn($expectedShare);
  227. $share->expects($this->any())->method('getNode')->willReturn($node);
  228. $share->expects($this->once())->method('getPassword')->willReturn('password');
  229. $this->hasher->expects($this->once())->method('hash')->with('password')->willReturn('passwordHashed');
  230. $share->expects($this->once())->method('setPassword')->with('passwordHashed');
  231. // The given password (but not the autogenerated password) should not be
  232. // mailed to the receiver of the share because permanent passwords are not enforced.
  233. $this->shareManager->expects($this->any())->method('shareApiLinkEnforcePassword')->willReturn(false);
  234. $this->config->expects($this->once())->method('getSystemValue')->with('sharing.enable_mail_link_password_expiration')->willReturn(false);
  235. $instance->expects($this->never())->method('autoGeneratePassword');
  236. $this->assertSame($expectedShare, $instance->create($share));
  237. }
  238. public function testCreateSendPasswordByMailWithPasswordAndWithoutEnforcedPasswordProtectionWithoutPermanentPassword() {
  239. $expectedShare = $this->createMock(IShare::class);
  240. $share = $this->getMockBuilder(IShare::class)->getMock();
  241. $share->expects($this->any())->method('getSharedWith')->willReturn('receiver@example.com');
  242. $share->expects($this->any())->method('getSendPasswordByTalk')->willReturn(false);
  243. $share->expects($this->any())->method('getSharedBy')->willReturn('owner');
  244. $node = $this->getMockBuilder(File::class)->getMock();
  245. $node->expects($this->any())->method('getName')->willReturn('filename');
  246. $instance = $this->getInstance(['getSharedWith', 'createMailShare', 'getRawShare', 'createShareObject', 'createShareActivity', 'autoGeneratePassword', 'createPasswordSendActivity']);
  247. $instance->expects($this->once())->method('getSharedWith')->willReturn([]);
  248. $instance->expects($this->once())->method('createMailShare')->with($share)->willReturn(42);
  249. $instance->expects($this->once())->method('createShareActivity')->with($share);
  250. $instance->expects($this->once())->method('getRawShare')->with(42)->willReturn(['rawShare']);
  251. $instance->expects($this->once())->method('createShareObject')->with(['rawShare'])->willReturn($expectedShare);
  252. $share->expects($this->any())->method('getNode')->willReturn($node);
  253. $share->expects($this->once())->method('getPassword')->willReturn('password');
  254. $this->hasher->expects($this->once())->method('hash')->with('password')->willReturn('passwordHashed');
  255. $share->expects($this->once())->method('setPassword')->with('passwordHashed');
  256. // The given password (but not the autogenerated password) should be
  257. // mailed to the receiver of the share because permanent passwords are enforced.
  258. $this->shareManager->expects($this->any())->method('shareApiLinkEnforcePassword')->willReturn(false);
  259. $instance->expects($this->never())->method('autoGeneratePassword');
  260. $this->config->expects($this->any())->method('getSystemValue')->withConsecutive(
  261. ['sharing.enable_mail_link_password_expiration'],
  262. ['sharing.enable_mail_link_password_expiration'],
  263. ['sharing.mail_link_password_expiration_interval']
  264. )->willReturnOnConsecutiveCalls(
  265. true,
  266. true,
  267. 3600
  268. );
  269. $this->assertSame($expectedShare, $instance->create($share));
  270. }
  271. public function testCreateSendPasswordByMailWithEnforcedPasswordProtectionWithPermanentPassword() {
  272. $expectedShare = $this->createMock(IShare::class);
  273. $share = $this->getMockBuilder(IShare::class)->getMock();
  274. $share->expects($this->any())->method('getSharedWith')->willReturn('receiver@example.com');
  275. $share->expects($this->any())->method('getSendPasswordByTalk')->willReturn(false);
  276. $share->expects($this->any())->method('getSharedBy')->willReturn('owner');
  277. $node = $this->getMockBuilder(File::class)->getMock();
  278. $node->expects($this->any())->method('getName')->willReturn('filename');
  279. $this->secureRandom->expects($this->once())
  280. ->method('generate')
  281. ->with(8, ISecureRandom::CHAR_HUMAN_READABLE)
  282. ->willReturn('autogeneratedPassword');
  283. $this->eventDispatcher->expects($this->once())
  284. ->method('dispatchTyped')
  285. ->with(new GenerateSecurePasswordEvent());
  286. $instance = $this->getInstance(['getSharedWith', 'createMailShare', 'getRawShare', 'createShareObject', 'createShareActivity', 'createPasswordSendActivity']);
  287. $instance->expects($this->once())->method('getSharedWith')->willReturn([]);
  288. $instance->expects($this->once())->method('createMailShare')->with($share)->willReturn(42);
  289. $instance->expects($this->once())->method('createShareActivity')->with($share);
  290. $instance->expects($this->once())->method('getRawShare')->with(42)->willReturn(['rawShare']);
  291. $instance->expects($this->once())->method('createShareObject')->with(['rawShare'])->willReturn($expectedShare);
  292. $share->expects($this->any())->method('getNode')->willReturn($node);
  293. $share->expects($this->once())->method('getPassword')->willReturn(null);
  294. $this->hasher->expects($this->once())->method('hash')->with('autogeneratedPassword')->willReturn('autogeneratedPasswordHashed');
  295. $share->expects($this->once())->method('setPassword')->with('autogeneratedPasswordHashed');
  296. // The autogenerated password should be mailed to the receiver of the share because permanent passwords are enforced.
  297. $this->shareManager->expects($this->any())->method('shareApiLinkEnforcePassword')->willReturn(true);
  298. $this->config->expects($this->any())->method('getSystemValue')->with('sharing.enable_mail_link_password_expiration')->willReturn(false);
  299. $this->settingsManager->expects($this->any())->method('sendPasswordByMail')->willReturn(true);
  300. $message = $this->createMock(IMessage::class);
  301. $message->expects($this->once())->method('setTo')->with(['receiver@example.com']);
  302. $this->mailer->expects($this->once())->method('createMessage')->willReturn($message);
  303. $this->mailer->expects($this->once())->method('createEMailTemplate')->with('sharebymail.RecipientPasswordNotification', [
  304. 'filename' => 'filename',
  305. 'password' => 'autogeneratedPassword',
  306. 'initiator' => 'owner',
  307. 'initiatorEmail' => null,
  308. 'shareWith' => 'receiver@example.com',
  309. ]);
  310. $this->mailer->expects($this->once())->method('send');
  311. $this->assertSame($expectedShare, $instance->create($share));
  312. }
  313. public function testCreateSendPasswordByMailWithPasswordAndWithEnforcedPasswordProtectionWithPermanentPassword() {
  314. $expectedShare = $this->createMock(IShare::class);
  315. $share = $this->getMockBuilder(IShare::class)->getMock();
  316. $share->expects($this->any())->method('getSharedWith')->willReturn('receiver@example.com');
  317. $share->expects($this->any())->method('getSendPasswordByTalk')->willReturn(false);
  318. $share->expects($this->any())->method('getSharedBy')->willReturn('owner');
  319. $node = $this->getMockBuilder(File::class)->getMock();
  320. $node->expects($this->any())->method('getName')->willReturn('filename');
  321. $instance = $this->getInstance(['getSharedWith', 'createMailShare', 'getRawShare', 'createShareObject', 'createShareActivity', 'autoGeneratePassword', 'createPasswordSendActivity']);
  322. $instance->expects($this->once())->method('getSharedWith')->willReturn([]);
  323. $instance->expects($this->once())->method('createMailShare')->with($share)->willReturn(42);
  324. $instance->expects($this->once())->method('createShareActivity')->with($share);
  325. $instance->expects($this->once())->method('getRawShare')->with(42)->willReturn(['rawShare']);
  326. $instance->expects($this->once())->method('createShareObject')->with(['rawShare'])->willReturn($expectedShare);
  327. $share->expects($this->any())->method('getNode')->willReturn($node);
  328. $share->expects($this->once())->method('getPassword')->willReturn('password');
  329. $this->hasher->expects($this->once())->method('hash')->with('password')->willReturn('passwordHashed');
  330. $share->expects($this->once())->method('setPassword')->with('passwordHashed');
  331. // The given password (but not the autogenerated password) should be
  332. // mailed to the receiver of the share.
  333. $this->shareManager->expects($this->any())->method('shareApiLinkEnforcePassword')->willReturn(true);
  334. $this->config->expects($this->any())->method('getSystemValue')->with('sharing.enable_mail_link_password_expiration')->willReturn(false);
  335. $this->settingsManager->expects($this->any())->method('sendPasswordByMail')->willReturn(true);
  336. $instance->expects($this->never())->method('autoGeneratePassword');
  337. $message = $this->createMock(IMessage::class);
  338. $message->expects($this->once())->method('setTo')->with(['receiver@example.com']);
  339. $this->mailer->expects($this->once())->method('createMessage')->willReturn($message);
  340. $this->mailer->expects($this->once())->method('createEMailTemplate')->with('sharebymail.RecipientPasswordNotification', [
  341. 'filename' => 'filename',
  342. 'password' => 'password',
  343. 'initiator' => 'owner',
  344. 'initiatorEmail' => null,
  345. 'shareWith' => 'receiver@example.com',
  346. ]);
  347. $this->mailer->expects($this->once())->method('send');
  348. $this->assertSame($expectedShare, $instance->create($share));
  349. }
  350. public function testCreateSendPasswordByTalkWithEnforcedPasswordProtectionWithPermanentPassword() {
  351. $expectedShare = $this->createMock(IShare::class);
  352. $share = $this->getMockBuilder(IShare::class)->getMock();
  353. $share->expects($this->any())->method('getSharedWith')->willReturn('receiver@example.com');
  354. $share->expects($this->any())->method('getSendPasswordByTalk')->willReturn(true);
  355. $share->expects($this->any())->method('getSharedBy')->willReturn('owner');
  356. $node = $this->getMockBuilder(File::class)->getMock();
  357. $node->expects($this->any())->method('getName')->willReturn('filename');
  358. $instance = $this->getInstance(['getSharedWith', 'createMailShare', 'getRawShare', 'createShareObject', 'createShareActivity', 'autoGeneratePassword', 'createPasswordSendActivity']);
  359. $instance->expects($this->once())->method('getSharedWith')->willReturn([]);
  360. $instance->expects($this->once())->method('createMailShare')->with($share)->willReturn(42);
  361. $instance->expects($this->once())->method('createShareActivity')->with($share);
  362. $instance->expects($this->once())->method('getRawShare')->with(42)->willReturn(['rawShare']);
  363. $instance->expects($this->once())->method('createShareObject')->with(['rawShare'])->willReturn($expectedShare);
  364. $share->expects($this->any())->method('getNode')->willReturn($node);
  365. $share->expects($this->once())->method('getPassword')->willReturn(null);
  366. $this->hasher->expects($this->once())->method('hash')->with('autogeneratedPassword')->willReturn('autogeneratedPasswordHashed');
  367. $share->expects($this->once())->method('setPassword')->with('autogeneratedPasswordHashed');
  368. // The autogenerated password should be mailed to the owner of the share.
  369. $this->shareManager->expects($this->any())->method('shareApiLinkEnforcePassword')->willReturn(true);
  370. $this->config->expects($this->any())->method('getSystemValue')->with('sharing.enable_mail_link_password_expiration')->willReturn(false);
  371. $this->settingsManager->expects($this->any())->method('sendPasswordByMail')->willReturn(true);
  372. $instance->expects($this->once())->method('autoGeneratePassword')->with($share)->willReturn('autogeneratedPassword');
  373. $message = $this->createMock(IMessage::class);
  374. $message->expects($this->once())->method('setTo')->with(['owner@example.com' => 'Owner display name']);
  375. $this->mailer->expects($this->once())->method('createMessage')->willReturn($message);
  376. $this->mailer->expects($this->once())->method('createEMailTemplate')->with('sharebymail.OwnerPasswordNotification', [
  377. 'filename' => 'filename',
  378. 'password' => 'autogeneratedPassword',
  379. 'initiator' => 'Owner display name',
  380. 'initiatorEmail' => 'owner@example.com',
  381. 'shareWith' => 'receiver@example.com',
  382. ]);
  383. $this->mailer->expects($this->once())->method('send');
  384. $user = $this->createMock(IUser::class);
  385. $this->userManager->expects($this->once())->method('get')->with('owner')->willReturn($user);
  386. $user->expects($this->once())->method('getDisplayName')->willReturn('Owner display name');
  387. $user->expects($this->once())->method('getEMailAddress')->willReturn('owner@example.com');
  388. $this->assertSame($expectedShare, $instance->create($share));
  389. }
  390. public function testCreateFailed() {
  391. $this->expectException(\Exception::class);
  392. $this->share->expects($this->once())->method('getSharedWith')->willReturn('user1');
  393. $node = $this->getMockBuilder('OCP\Files\Node')->getMock();
  394. $node->expects($this->any())->method('getName')->willReturn('fileName');
  395. $this->share->expects($this->any())->method('getNode')->willReturn($node);
  396. $instance = $this->getInstance(['getSharedWith', 'createMailShare', 'getRawShare', 'createShareObject']);
  397. $instance->expects($this->once())->method('getSharedWith')->willReturn(['found']);
  398. $instance->expects($this->never())->method('createMailShare');
  399. $instance->expects($this->never())->method('getRawShare');
  400. $instance->expects($this->never())->method('createShareObject');
  401. $this->assertSame('shareObject',
  402. $instance->create($this->share)
  403. );
  404. }
  405. public function testCreateMailShare() {
  406. $this->share->expects($this->any())->method('getToken')->willReturn('token');
  407. $this->share->expects($this->once())->method('setToken')->with('token');
  408. $this->share->expects($this->any())->method('getSharedBy')->willReturn('validby@valid.com');
  409. $this->share->expects($this->any())->method('getSharedWith')->willReturn('validwith@valid.com');
  410. $this->share->expects($this->any())->method('getNote')->willReturn('Check this!');
  411. $node = $this->getMockBuilder('OCP\Files\Node')->getMock();
  412. $node->expects($this->any())->method('getName')->willReturn('fileName');
  413. $this->share->expects($this->any())->method('getNode')->willReturn($node);
  414. $instance = $this->getInstance(['generateToken', 'addShareToDB', 'sendMailNotification']);
  415. $instance->expects($this->once())->method('generateToken')->willReturn('token');
  416. $instance->expects($this->once())->method('addShareToDB')->willReturn(42);
  417. $instance->expects($this->once())->method('sendMailNotification');
  418. $this->urlGenerator->expects($this->once())->method('linkToRouteAbsolute')
  419. ->with('files_sharing.sharecontroller.showShare', ['token' => 'token']);
  420. $instance->expects($this->once())->method('sendMailNotification');
  421. $this->assertSame(42,
  422. $this->invokePrivate($instance, 'createMailShare', [$this->share])
  423. );
  424. }
  425. public function testCreateMailShareFailed() {
  426. $this->expectException(\OCP\HintException::class);
  427. $this->share->expects($this->any())->method('getToken')->willReturn('token');
  428. $this->share->expects($this->once())->method('setToken')->with('token');
  429. $this->share->expects($this->any())->method('getSharedBy')->willReturn('validby@valid.com');
  430. $this->share->expects($this->any())->method('getSharedWith')->willReturn('validwith@valid.com');
  431. $this->share->expects($this->any())->method('getNote')->willReturn('Check this!');
  432. $node = $this->getMockBuilder('OCP\Files\Node')->getMock();
  433. $node->expects($this->any())->method('getName')->willReturn('fileName');
  434. $this->share->expects($this->any())->method('getNode')->willReturn($node);
  435. $instance = $this->getInstance(['generateToken', 'addShareToDB', 'sendMailNotification']);
  436. $instance->expects($this->once())->method('generateToken')->willReturn('token');
  437. $instance->expects($this->once())->method('addShareToDB')->willReturn(42);
  438. $instance->expects($this->once())->method('sendMailNotification');
  439. $this->urlGenerator->expects($this->once())->method('linkToRouteAbsolute')
  440. ->with('files_sharing.sharecontroller.showShare', ['token' => 'token']);
  441. $instance->expects($this->once())->method('sendMailNotification')
  442. ->willReturnCallback(
  443. function () {
  444. throw new \Exception('should be converted to a hint exception');
  445. }
  446. );
  447. $this->assertSame(42,
  448. $this->invokePrivate($instance, 'createMailShare', [$this->share])
  449. );
  450. }
  451. public function testGenerateToken() {
  452. $instance = $this->getInstance();
  453. $this->secureRandom->expects($this->once())->method('generate')->willReturn('token');
  454. $this->assertSame('token',
  455. $this->invokePrivate($instance, 'generateToken')
  456. );
  457. }
  458. public function testAddShareToDB() {
  459. $itemSource = 11;
  460. $itemType = 'file';
  461. $shareWith = 'user@server.com';
  462. $sharedBy = 'user1';
  463. $uidOwner = 'user2';
  464. $permissions = 1;
  465. $token = 'token';
  466. $password = 'password';
  467. $sendPasswordByTalk = true;
  468. $hideDownload = true;
  469. $label = 'label';
  470. $expiration = new \DateTime();
  471. $passwordExpirationTime = new \DateTime();
  472. $instance = $this->getInstance();
  473. $id = $this->invokePrivate(
  474. $instance,
  475. 'addShareToDB',
  476. [
  477. $itemSource,
  478. $itemType,
  479. $shareWith,
  480. $sharedBy,
  481. $uidOwner,
  482. $permissions,
  483. $token,
  484. $password,
  485. $passwordExpirationTime,
  486. $sendPasswordByTalk,
  487. $hideDownload,
  488. $label,
  489. $expiration
  490. ]
  491. );
  492. $qb = $this->connection->getQueryBuilder();
  493. $qb->select('*')
  494. ->from('share')
  495. ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)));
  496. $qResult = $qb->execute();
  497. $result = $qResult->fetchAll();
  498. $qResult->closeCursor();
  499. $this->assertSame(1, count($result));
  500. $this->assertSame($itemSource, (int)$result[0]['item_source']);
  501. $this->assertSame($itemType, $result[0]['item_type']);
  502. $this->assertSame($shareWith, $result[0]['share_with']);
  503. $this->assertSame($sharedBy, $result[0]['uid_initiator']);
  504. $this->assertSame($uidOwner, $result[0]['uid_owner']);
  505. $this->assertSame($permissions, (int)$result[0]['permissions']);
  506. $this->assertSame($token, $result[0]['token']);
  507. $this->assertSame($password, $result[0]['password']);
  508. $this->assertSame($passwordExpirationTime->getTimestamp(), \DateTime::createFromFormat('Y-m-d H:i:s', $result[0]['password_expiration_time'])->getTimestamp());
  509. $this->assertSame($sendPasswordByTalk, (bool)$result[0]['password_by_talk']);
  510. $this->assertSame($hideDownload, (bool)$result[0]['hide_download']);
  511. $this->assertSame($label, $result[0]['label']);
  512. $this->assertSame($expiration->getTimestamp(), \DateTime::createFromFormat('Y-m-d H:i:s', $result[0]['expiration'])->getTimestamp());
  513. }
  514. public function testUpdate() {
  515. $itemSource = 11;
  516. $itemType = 'file';
  517. $shareWith = 'user@server.com';
  518. $sharedBy = 'user1';
  519. $uidOwner = 'user2';
  520. $permissions = 1;
  521. $token = 'token';
  522. $note = 'personal note';
  523. $instance = $this->getInstance();
  524. $id = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token, $note);
  525. $this->share->expects($this->once())->method('getPermissions')->willReturn($permissions + 1);
  526. $this->share->expects($this->once())->method('getShareOwner')->willReturn($uidOwner);
  527. $this->share->expects($this->once())->method('getSharedBy')->willReturn($sharedBy);
  528. $this->share->expects($this->any())->method('getNote')->willReturn($note);
  529. $this->share->expects($this->atLeastOnce())->method('getId')->willReturn($id);
  530. $this->share->expects($this->atLeastOnce())->method('getNodeId')->willReturn($itemSource);
  531. $this->share->expects($this->once())->method('getSharedWith')->willReturn($shareWith);
  532. $this->assertSame($this->share,
  533. $instance->update($this->share)
  534. );
  535. $qb = $this->connection->getQueryBuilder();
  536. $qb->select('*')
  537. ->from('share')
  538. ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)));
  539. $qResult = $qb->execute();
  540. $result = $qResult->fetchAll();
  541. $qResult->closeCursor();
  542. $this->assertSame(1, count($result));
  543. $this->assertSame($itemSource, (int)$result[0]['item_source']);
  544. $this->assertSame($itemType, $result[0]['item_type']);
  545. $this->assertSame($shareWith, $result[0]['share_with']);
  546. $this->assertSame($sharedBy, $result[0]['uid_initiator']);
  547. $this->assertSame($uidOwner, $result[0]['uid_owner']);
  548. $this->assertSame($permissions + 1, (int)$result[0]['permissions']);
  549. $this->assertSame($token, $result[0]['token']);
  550. $this->assertSame($note, $result[0]['note']);
  551. }
  552. public function dataUpdateSendPassword() {
  553. return [
  554. ['password', 'hashed', 'hashed new', false, false, true],
  555. ['', 'hashed', 'hashed new', false, false, false],
  556. [null, 'hashed', 'hashed new', false, false, false],
  557. ['password', 'hashed', 'hashed', false, false, false],
  558. ['password', 'hashed', 'hashed new', false, true, false],
  559. ['password', 'hashed', 'hashed new', true, false, true],
  560. ['password', 'hashed', 'hashed', true, false, true],
  561. ];
  562. }
  563. /**
  564. * @dataProvider dataUpdateSendPassword
  565. *
  566. * @param string|null plainTextPassword
  567. * @param string originalPassword
  568. * @param string newPassword
  569. * @param string originalSendPasswordByTalk
  570. * @param string newSendPasswordByTalk
  571. * @param bool sendMail
  572. */
  573. public function testUpdateSendPassword($plainTextPassword, string $originalPassword, string $newPassword, $originalSendPasswordByTalk, $newSendPasswordByTalk, bool $sendMail) {
  574. $node = $this->getMockBuilder(File::class)->getMock();
  575. $node->expects($this->any())->method('getName')->willReturn('filename');
  576. $this->settingsManager->method('sendPasswordByMail')->willReturn(true);
  577. $originalShare = $this->getMockBuilder(IShare::class)->getMock();
  578. $originalShare->expects($this->any())->method('getSharedWith')->willReturn('receiver@example.com');
  579. $originalShare->expects($this->any())->method('getNode')->willReturn($node);
  580. $originalShare->expects($this->any())->method('getId')->willReturn(42);
  581. $originalShare->expects($this->any())->method('getPassword')->willReturn($originalPassword);
  582. $originalShare->expects($this->any())->method('getSendPasswordByTalk')->willReturn($originalSendPasswordByTalk);
  583. $share = $this->getMockBuilder(IShare::class)->getMock();
  584. $share->expects($this->any())->method('getSharedWith')->willReturn('receiver@example.com');
  585. $share->expects($this->any())->method('getNode')->willReturn($node);
  586. $share->expects($this->any())->method('getId')->willReturn(42);
  587. $share->expects($this->any())->method('getPassword')->willReturn($newPassword);
  588. $share->expects($this->any())->method('getSendPasswordByTalk')->willReturn($newSendPasswordByTalk);
  589. if ($sendMail) {
  590. $this->mailer->expects($this->once())->method('createEMailTemplate')->with('sharebymail.RecipientPasswordNotification', [
  591. 'filename' => 'filename',
  592. 'password' => $plainTextPassword,
  593. 'initiator' => null,
  594. 'initiatorEmail' => null,
  595. 'shareWith' => 'receiver@example.com',
  596. ]);
  597. $this->mailer->expects($this->once())->method('send');
  598. } else {
  599. $this->mailer->expects($this->never())->method('send');
  600. }
  601. $instance = $this->getInstance(['getShareById', 'createPasswordSendActivity']);
  602. $instance->expects($this->once())->method('getShareById')->willReturn($originalShare);
  603. $this->assertSame($share,
  604. $instance->update($share, $plainTextPassword)
  605. );
  606. }
  607. public function testDelete() {
  608. $instance = $this->getInstance(['removeShareFromTable', 'createShareActivity']);
  609. $this->share->expects($this->once())->method('getId')->willReturn(42);
  610. $instance->expects($this->once())->method('removeShareFromTable')->with(42);
  611. $instance->expects($this->once())->method('createShareActivity')->with($this->share, 'unshare');
  612. $instance->delete($this->share);
  613. }
  614. public function testGetShareById() {
  615. $instance = $this->getInstance(['createShareObject']);
  616. $itemSource = 11;
  617. $itemType = 'file';
  618. $shareWith = 'user@server.com';
  619. $sharedBy = 'user1';
  620. $uidOwner = 'user2';
  621. $permissions = 1;
  622. $token = 'token';
  623. $this->createDummyShare($itemType, $itemSource, $shareWith, "user1wrong", "user2wrong", $permissions, $token);
  624. $id2 = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  625. $instance->expects($this->once())->method('createShareObject')
  626. ->willReturnCallback(
  627. function ($data) use ($uidOwner, $sharedBy, $id2) {
  628. $this->assertSame($uidOwner, $data['uid_owner']);
  629. $this->assertSame($sharedBy, $data['uid_initiator']);
  630. $this->assertSame($id2, (int)$data['id']);
  631. return $this->share;
  632. }
  633. );
  634. $result = $instance->getShareById($id2);
  635. $this->assertInstanceOf('OCP\Share\IShare', $result);
  636. }
  637. public function testGetShareByIdFailed() {
  638. $this->expectException(\OCP\Share\Exceptions\ShareNotFound::class);
  639. $instance = $this->getInstance(['createShareObject']);
  640. $itemSource = 11;
  641. $itemType = 'file';
  642. $shareWith = 'user@server.com';
  643. $sharedBy = 'user1';
  644. $uidOwner = 'user2';
  645. $permissions = 1;
  646. $token = 'token';
  647. $id = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  648. $instance->getShareById($id + 1);
  649. }
  650. public function testGetShareByPath() {
  651. $itemSource = 11;
  652. $itemType = 'file';
  653. $shareWith = 'user@server.com';
  654. $sharedBy = 'user1';
  655. $uidOwner = 'user2';
  656. $permissions = 1;
  657. $token = 'token';
  658. $node = $this->getMockBuilder('OCP\Files\Node')->getMock();
  659. $node->expects($this->once())->method('getId')->willReturn($itemSource);
  660. $instance = $this->getInstance(['createShareObject']);
  661. $this->createDummyShare($itemType, 111, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  662. $id = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  663. $instance->expects($this->once())->method('createShareObject')
  664. ->willReturnCallback(
  665. function ($data) use ($uidOwner, $sharedBy, $id) {
  666. $this->assertSame($uidOwner, $data['uid_owner']);
  667. $this->assertSame($sharedBy, $data['uid_initiator']);
  668. $this->assertSame($id, (int)$data['id']);
  669. return $this->share;
  670. }
  671. );
  672. $result = $instance->getSharesByPath($node);
  673. $this->assertTrue(is_array($result));
  674. $this->assertSame(1, count($result));
  675. $this->assertInstanceOf('OCP\Share\IShare', $result[0]);
  676. }
  677. public function testGetShareByToken() {
  678. $itemSource = 11;
  679. $itemType = 'file';
  680. $shareWith = 'user@server.com';
  681. $sharedBy = 'user1';
  682. $uidOwner = 'user2';
  683. $permissions = 1;
  684. $token = 'token';
  685. $instance = $this->getInstance(['createShareObject']);
  686. $idMail = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  687. $idPublic = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token, '', IShare::TYPE_LINK);
  688. $this->assertTrue($idMail !== $idPublic);
  689. $instance->expects($this->once())->method('createShareObject')
  690. ->willReturnCallback(
  691. function ($data) use ($idMail) {
  692. $this->assertSame($idMail, (int)$data['id']);
  693. return $this->share;
  694. }
  695. );
  696. $result = $instance->getShareByToken('token');
  697. $this->assertInstanceOf('OCP\Share\IShare', $result);
  698. }
  699. public function testGetShareByTokenFailed() {
  700. $this->expectException(\OCP\Share\Exceptions\ShareNotFound::class);
  701. $itemSource = 11;
  702. $itemType = 'file';
  703. $shareWith = 'user@server.com';
  704. $sharedBy = 'user1';
  705. $uidOwner = 'user2';
  706. $permissions = 1;
  707. $token = 'token';
  708. $instance = $this->getInstance(['createShareObject']);
  709. $idMail = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  710. $idPublic = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, "token2", '', IShare::TYPE_LINK);
  711. $this->assertTrue($idMail !== $idPublic);
  712. $this->assertInstanceOf('OCP\Share\IShare',
  713. $instance->getShareByToken('token2')
  714. );
  715. }
  716. public function testRemoveShareFromTable() {
  717. $itemSource = 11;
  718. $itemType = 'file';
  719. $shareWith = 'user@server.com';
  720. $sharedBy = 'user1';
  721. $uidOwner = 'user2';
  722. $permissions = 1;
  723. $token = 'token';
  724. $instance = $this->getInstance();
  725. $id = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  726. $query = $this->connection->getQueryBuilder();
  727. $query->select('*')->from('share')
  728. ->where($query->expr()->eq('id', $query->createNamedParameter($id)));
  729. $result = $query->execute();
  730. $before = $result->fetchAll();
  731. $result->closeCursor();
  732. $this->assertTrue(is_array($before));
  733. $this->assertSame(1, count($before));
  734. $this->invokePrivate($instance, 'removeShareFromTable', [$id]);
  735. $query = $this->connection->getQueryBuilder();
  736. $query->select('*')->from('share')
  737. ->where($query->expr()->eq('id', $query->createNamedParameter($id)));
  738. $result = $query->execute();
  739. $after = $result->fetchAll();
  740. $result->closeCursor();
  741. $this->assertTrue(is_array($after));
  742. $this->assertEmpty($after);
  743. }
  744. public function testUserDeleted() {
  745. $itemSource = 11;
  746. $itemType = 'file';
  747. $shareWith = 'user@server.com';
  748. $sharedBy = 'user1';
  749. $uidOwner = 'user2';
  750. $permissions = 1;
  751. $token = 'token';
  752. $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  753. $id = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, 'user2Wrong', $permissions, $token);
  754. $query = $this->connection->getQueryBuilder();
  755. $query->select('*')->from('share');
  756. $result = $query->execute();
  757. $before = $result->fetchAll();
  758. $result->closeCursor();
  759. $this->assertTrue(is_array($before));
  760. $this->assertSame(2, count($before));
  761. $instance = $this->getInstance();
  762. $instance->userDeleted($uidOwner, IShare::TYPE_EMAIL);
  763. $query = $this->connection->getQueryBuilder();
  764. $query->select('*')->from('share');
  765. $result = $query->execute();
  766. $after = $result->fetchAll();
  767. $result->closeCursor();
  768. $this->assertTrue(is_array($after));
  769. $this->assertSame(1, count($after));
  770. $this->assertSame($id, (int)$after[0]['id']);
  771. }
  772. public function testGetRawShare() {
  773. $itemSource = 11;
  774. $itemType = 'file';
  775. $shareWith = 'user@server.com';
  776. $sharedBy = 'user1';
  777. $uidOwner = 'user2';
  778. $permissions = 1;
  779. $token = 'token';
  780. $instance = $this->getInstance();
  781. $id = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  782. $result = $this->invokePrivate($instance, 'getRawShare', [$id]);
  783. $this->assertTrue(is_array($result));
  784. $this->assertSame($itemSource, (int)$result['item_source']);
  785. $this->assertSame($itemType, $result['item_type']);
  786. $this->assertSame($shareWith, $result['share_with']);
  787. $this->assertSame($sharedBy, $result['uid_initiator']);
  788. $this->assertSame($uidOwner, $result['uid_owner']);
  789. $this->assertSame($permissions, (int)$result['permissions']);
  790. $this->assertSame($token, $result['token']);
  791. }
  792. public function testGetRawShareFailed() {
  793. $this->expectException(\OCP\Share\Exceptions\ShareNotFound::class);
  794. $itemSource = 11;
  795. $itemType = 'file';
  796. $shareWith = 'user@server.com';
  797. $sharedBy = 'user1';
  798. $uidOwner = 'user2';
  799. $permissions = 1;
  800. $token = 'token';
  801. $instance = $this->getInstance();
  802. $id = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  803. $this->invokePrivate($instance, 'getRawShare', [$id + 1]);
  804. }
  805. private function createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token, $note = '', $shareType = IShare::TYPE_EMAIL) {
  806. $qb = $this->connection->getQueryBuilder();
  807. $qb->insert('share')
  808. ->setValue('share_type', $qb->createNamedParameter($shareType))
  809. ->setValue('item_type', $qb->createNamedParameter($itemType))
  810. ->setValue('item_source', $qb->createNamedParameter($itemSource))
  811. ->setValue('file_source', $qb->createNamedParameter($itemSource))
  812. ->setValue('share_with', $qb->createNamedParameter($shareWith))
  813. ->setValue('uid_owner', $qb->createNamedParameter($uidOwner))
  814. ->setValue('uid_initiator', $qb->createNamedParameter($sharedBy))
  815. ->setValue('permissions', $qb->createNamedParameter($permissions))
  816. ->setValue('token', $qb->createNamedParameter($token))
  817. ->setValue('note', $qb->createNamedParameter($note))
  818. ->setValue('stime', $qb->createNamedParameter(time()));
  819. /*
  820. * Added to fix https://github.com/owncloud/core/issues/22215
  821. * Can be removed once we get rid of ajax/share.php
  822. */
  823. $qb->setValue('file_target', $qb->createNamedParameter(''));
  824. $qb->execute();
  825. $id = $qb->getLastInsertId();
  826. return (int)$id;
  827. }
  828. public function testGetSharesInFolder() {
  829. $userManager = \OC::$server->getUserManager();
  830. $rootFolder = \OC::$server->getRootFolder();
  831. $this->shareManager->expects($this->any())
  832. ->method('newShare')
  833. ->willReturn(new \OC\Share20\Share($rootFolder, $userManager));
  834. $provider = $this->getInstance(['sendMailNotification', 'createShareActivity']);
  835. $this->mailer->expects($this->any())->method('validateMailAddress')->willReturn(true);
  836. $u1 = $userManager->createUser('testFed', md5(time()));
  837. $u2 = $userManager->createUser('testFed2', md5(time()));
  838. $folder1 = $rootFolder->getUserFolder($u1->getUID())->newFolder('foo');
  839. $file1 = $folder1->newFile('bar1');
  840. $file2 = $folder1->newFile('bar2');
  841. $share1 = $this->shareManager->newShare();
  842. $share1->setSharedWith('user@server.com')
  843. ->setSharedBy($u1->getUID())
  844. ->setShareOwner($u1->getUID())
  845. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  846. ->setNode($file1);
  847. $provider->create($share1);
  848. $share2 = $this->shareManager->newShare();
  849. $share2->setSharedWith('user@server.com')
  850. ->setSharedBy($u2->getUID())
  851. ->setShareOwner($u1->getUID())
  852. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  853. ->setNode($file2);
  854. $provider->create($share2);
  855. $result = $provider->getSharesInFolder($u1->getUID(), $folder1, false);
  856. $this->assertCount(1, $result);
  857. $this->assertCount(1, $result[$file1->getId()]);
  858. $result = $provider->getSharesInFolder($u1->getUID(), $folder1, true);
  859. $this->assertCount(2, $result);
  860. $this->assertCount(1, $result[$file1->getId()]);
  861. $this->assertCount(1, $result[$file2->getId()]);
  862. $u1->delete();
  863. $u2->delete();
  864. }
  865. public function testGetAccessList() {
  866. $userManager = \OC::$server->getUserManager();
  867. $rootFolder = \OC::$server->getRootFolder();
  868. $this->shareManager->expects($this->any())
  869. ->method('newShare')
  870. ->willReturn(new \OC\Share20\Share($rootFolder, $userManager));
  871. $provider = $this->getInstance(['sendMailNotification', 'createShareActivity']);
  872. $this->mailer->expects($this->any())->method('validateMailAddress')->willReturn(true);
  873. $u1 = $userManager->createUser('testFed', md5(time()));
  874. $u2 = $userManager->createUser('testFed2', md5(time()));
  875. $folder = $rootFolder->getUserFolder($u1->getUID())->newFolder('foo');
  876. $accessList = $provider->getAccessList([$folder], true);
  877. $this->assertArrayHasKey('public', $accessList);
  878. $this->assertFalse($accessList['public']);
  879. $accessList = $provider->getAccessList([$folder], false);
  880. $this->assertArrayHasKey('public', $accessList);
  881. $this->assertFalse($accessList['public']);
  882. $share1 = $this->shareManager->newShare();
  883. $share1->setSharedWith('user@server.com')
  884. ->setSharedBy($u1->getUID())
  885. ->setShareOwner($u1->getUID())
  886. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  887. ->setNode($folder);
  888. $share1 = $provider->create($share1);
  889. $share2 = $this->shareManager->newShare();
  890. $share2->setSharedWith('user2@server.com')
  891. ->setSharedBy($u2->getUID())
  892. ->setShareOwner($u1->getUID())
  893. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  894. ->setNode($folder);
  895. $share2 = $provider->create($share2);
  896. $accessList = $provider->getAccessList([$folder], true);
  897. $this->assertArrayHasKey('public', $accessList);
  898. $this->assertTrue($accessList['public']);
  899. $accessList = $provider->getAccessList([$folder], false);
  900. $this->assertArrayHasKey('public', $accessList);
  901. $this->assertTrue($accessList['public']);
  902. $provider->delete($share2);
  903. $accessList = $provider->getAccessList([$folder], true);
  904. $this->assertArrayHasKey('public', $accessList);
  905. $this->assertTrue($accessList['public']);
  906. $accessList = $provider->getAccessList([$folder], false);
  907. $this->assertArrayHasKey('public', $accessList);
  908. $this->assertTrue($accessList['public']);
  909. $provider->delete($share1);
  910. $accessList = $provider->getAccessList([$folder], true);
  911. $this->assertArrayHasKey('public', $accessList);
  912. $this->assertFalse($accessList['public']);
  913. $accessList = $provider->getAccessList([$folder], false);
  914. $this->assertArrayHasKey('public', $accessList);
  915. $this->assertFalse($accessList['public']);
  916. $u1->delete();
  917. $u2->delete();
  918. }
  919. public function testSendMailNotificationWithSameUserAndUserEmail() {
  920. $provider = $this->getInstance();
  921. $user = $this->createMock(IUser::class);
  922. $this->settingsManager->expects($this->any())->method('replyToInitiator')->willReturn(true);
  923. $this->userManager
  924. ->expects($this->once())
  925. ->method('get')
  926. ->with('OwnerUser')
  927. ->willReturn($user);
  928. $user
  929. ->expects($this->once())
  930. ->method('getDisplayName')
  931. ->willReturn('Mrs. Owner User');
  932. $message = $this->createMock(Message::class);
  933. $this->mailer
  934. ->expects($this->once())
  935. ->method('createMessage')
  936. ->willReturn($message);
  937. $template = $this->createMock(IEMailTemplate::class);
  938. $this->mailer
  939. ->expects($this->once())
  940. ->method('createEMailTemplate')
  941. ->willReturn($template);
  942. $template
  943. ->expects($this->once())
  944. ->method('addHeader');
  945. $template
  946. ->expects($this->once())
  947. ->method('addHeading')
  948. ->with('Mrs. Owner User shared »file.txt« with you');
  949. $template
  950. ->expects($this->once())
  951. ->method('addBodyText')
  952. ->with(
  953. 'Mrs. Owner User shared »file.txt« with you. Click the button below to open it.',
  954. 'Mrs. Owner User shared »file.txt« with you.'
  955. );
  956. $template
  957. ->expects($this->once())
  958. ->method('addBodyButton')
  959. ->with(
  960. 'Open »file.txt«',
  961. 'https://example.com/file.txt'
  962. );
  963. $message
  964. ->expects($this->once())
  965. ->method('setTo')
  966. ->with(['john@doe.com']);
  967. $this->defaults
  968. ->expects($this->once())
  969. ->method('getName')
  970. ->willReturn('UnitTestCloud');
  971. $message
  972. ->expects($this->once())
  973. ->method('setFrom')
  974. ->with([
  975. \OCP\Util::getDefaultEmailAddress('UnitTestCloud') => 'Mrs. Owner User via UnitTestCloud'
  976. ]);
  977. $user
  978. ->expects($this->once())
  979. ->method('getEMailAddress')
  980. ->willReturn('owner@example.com');
  981. $message
  982. ->expects($this->once())
  983. ->method('setReplyTo')
  984. ->with(['owner@example.com' => 'Mrs. Owner User']);
  985. $this->defaults
  986. ->expects($this->exactly(2))
  987. ->method('getSlogan')
  988. ->willReturn('Testing like 1990');
  989. $template
  990. ->expects($this->once())
  991. ->method('addFooter')
  992. ->with('UnitTestCloud - Testing like 1990');
  993. $template
  994. ->expects($this->once())
  995. ->method('setSubject')
  996. ->with('Mrs. Owner User shared »file.txt« with you');
  997. $message
  998. ->expects($this->once())
  999. ->method('useTemplate')
  1000. ->with($template);
  1001. $this->mailer
  1002. ->expects($this->once())
  1003. ->method('send')
  1004. ->with($message);
  1005. self::invokePrivate(
  1006. $provider,
  1007. 'sendMailNotification',
  1008. [
  1009. 'file.txt',
  1010. 'https://example.com/file.txt',
  1011. 'OwnerUser',
  1012. 'john@doe.com',
  1013. null,
  1014. ''
  1015. ]);
  1016. }
  1017. public function testSendMailNotificationWithSameUserAndUserEmailAndNote() {
  1018. $provider = $this->getInstance();
  1019. $user = $this->createMock(IUser::class);
  1020. $this->settingsManager->expects($this->any())->method('replyToInitiator')->willReturn(true);
  1021. $this->userManager
  1022. ->expects($this->once())
  1023. ->method('get')
  1024. ->with('OwnerUser')
  1025. ->willReturn($user);
  1026. $user
  1027. ->expects($this->once())
  1028. ->method('getDisplayName')
  1029. ->willReturn('Mrs. Owner User');
  1030. $message = $this->createMock(Message::class);
  1031. $this->mailer
  1032. ->expects($this->once())
  1033. ->method('createMessage')
  1034. ->willReturn($message);
  1035. $template = $this->createMock(IEMailTemplate::class);
  1036. $this->mailer
  1037. ->expects($this->once())
  1038. ->method('createEMailTemplate')
  1039. ->willReturn($template);
  1040. $template
  1041. ->expects($this->once())
  1042. ->method('addHeader');
  1043. $template
  1044. ->expects($this->once())
  1045. ->method('addHeading')
  1046. ->with('Mrs. Owner User shared »file.txt« with you');
  1047. $template
  1048. ->expects($this->exactly(2))
  1049. ->method('addBodyText')
  1050. ->withConsecutive(
  1051. ['This is a note to the recipient', 'This is a note to the recipient'],
  1052. ['Mrs. Owner User shared »file.txt« with you. Click the button below to open it.', 'Mrs. Owner User shared »file.txt« with you.'],
  1053. );
  1054. $template
  1055. ->expects($this->once())
  1056. ->method('addBodyButton')
  1057. ->with(
  1058. 'Open »file.txt«',
  1059. 'https://example.com/file.txt'
  1060. );
  1061. $message
  1062. ->expects($this->once())
  1063. ->method('setTo')
  1064. ->with(['john@doe.com']);
  1065. $this->defaults
  1066. ->expects($this->once())
  1067. ->method('getName')
  1068. ->willReturn('UnitTestCloud');
  1069. $message
  1070. ->expects($this->once())
  1071. ->method('setFrom')
  1072. ->with([
  1073. \OCP\Util::getDefaultEmailAddress('UnitTestCloud') => 'Mrs. Owner User via UnitTestCloud'
  1074. ]);
  1075. $user
  1076. ->expects($this->once())
  1077. ->method('getEMailAddress')
  1078. ->willReturn('owner@example.com');
  1079. $message
  1080. ->expects($this->once())
  1081. ->method('setReplyTo')
  1082. ->with(['owner@example.com' => 'Mrs. Owner User']);
  1083. $this->defaults
  1084. ->expects($this->exactly(2))
  1085. ->method('getSlogan')
  1086. ->willReturn('Testing like 1990');
  1087. $template
  1088. ->expects($this->once())
  1089. ->method('addFooter')
  1090. ->with('UnitTestCloud - Testing like 1990');
  1091. $template
  1092. ->expects($this->once())
  1093. ->method('setSubject')
  1094. ->with('Mrs. Owner User shared »file.txt« with you');
  1095. $message
  1096. ->expects($this->once())
  1097. ->method('useTemplate')
  1098. ->with($template);
  1099. $this->mailer
  1100. ->expects($this->once())
  1101. ->method('send')
  1102. ->with($message);
  1103. self::invokePrivate(
  1104. $provider,
  1105. 'sendMailNotification',
  1106. [
  1107. 'file.txt',
  1108. 'https://example.com/file.txt',
  1109. 'OwnerUser',
  1110. 'john@doe.com',
  1111. null,
  1112. 'This is a note to the recipient'
  1113. ]);
  1114. }
  1115. public function testSendMailNotificationWithDifferentUserAndNoUserEmail() {
  1116. $provider = $this->getInstance();
  1117. $initiatorUser = $this->createMock(IUser::class);
  1118. $this->settingsManager->expects($this->any())->method('replyToInitiator')->willReturn(true);
  1119. $this->userManager
  1120. ->expects($this->once())
  1121. ->method('get')
  1122. ->with('InitiatorUser')
  1123. ->willReturn($initiatorUser);
  1124. $initiatorUser
  1125. ->expects($this->once())
  1126. ->method('getDisplayName')
  1127. ->willReturn('Mr. Initiator User');
  1128. $message = $this->createMock(Message::class);
  1129. $this->mailer
  1130. ->expects($this->once())
  1131. ->method('createMessage')
  1132. ->willReturn($message);
  1133. $template = $this->createMock(IEMailTemplate::class);
  1134. $this->mailer
  1135. ->expects($this->once())
  1136. ->method('createEMailTemplate')
  1137. ->willReturn($template);
  1138. $template
  1139. ->expects($this->once())
  1140. ->method('addHeader');
  1141. $template
  1142. ->expects($this->once())
  1143. ->method('addHeading')
  1144. ->with('Mr. Initiator User shared »file.txt« with you');
  1145. $template
  1146. ->expects($this->once())
  1147. ->method('addBodyText')
  1148. ->with(
  1149. 'Mr. Initiator User shared »file.txt« with you. Click the button below to open it.',
  1150. 'Mr. Initiator User shared »file.txt« with you.'
  1151. );
  1152. $template
  1153. ->expects($this->once())
  1154. ->method('addBodyButton')
  1155. ->with(
  1156. 'Open »file.txt«',
  1157. 'https://example.com/file.txt'
  1158. );
  1159. $message
  1160. ->expects($this->once())
  1161. ->method('setTo')
  1162. ->with(['john@doe.com']);
  1163. $this->defaults
  1164. ->expects($this->once())
  1165. ->method('getName')
  1166. ->willReturn('UnitTestCloud');
  1167. $message
  1168. ->expects($this->once())
  1169. ->method('setFrom')
  1170. ->with([
  1171. \OCP\Util::getDefaultEmailAddress('UnitTestCloud') => 'Mr. Initiator User via UnitTestCloud'
  1172. ]);
  1173. $message
  1174. ->expects($this->never())
  1175. ->method('setReplyTo');
  1176. $template
  1177. ->expects($this->once())
  1178. ->method('addFooter')
  1179. ->with('');
  1180. $template
  1181. ->expects($this->once())
  1182. ->method('setSubject')
  1183. ->with('Mr. Initiator User shared »file.txt« with you');
  1184. $message
  1185. ->expects($this->once())
  1186. ->method('useTemplate')
  1187. ->with($template);
  1188. $this->mailer
  1189. ->expects($this->once())
  1190. ->method('send')
  1191. ->with($message);
  1192. self::invokePrivate(
  1193. $provider,
  1194. 'sendMailNotification',
  1195. [
  1196. 'file.txt',
  1197. 'https://example.com/file.txt',
  1198. 'InitiatorUser',
  1199. 'john@doe.com',
  1200. null,
  1201. ]);
  1202. }
  1203. public function testSendMailNotificationWithSameUserAndUserEmailAndReplyToDesactivate() {
  1204. $provider = $this->getInstance();
  1205. $user = $this->createMock(IUser::class);
  1206. $this->settingsManager->expects($this->any())->method('replyToInitiator')->willReturn(false);
  1207. $this->userManager
  1208. ->expects($this->once())
  1209. ->method('get')
  1210. ->with('OwnerUser')
  1211. ->willReturn($user);
  1212. $user
  1213. ->expects($this->once())
  1214. ->method('getDisplayName')
  1215. ->willReturn('Mrs. Owner User');
  1216. $message = $this->createMock(Message::class);
  1217. $this->mailer
  1218. ->expects($this->once())
  1219. ->method('createMessage')
  1220. ->willReturn($message);
  1221. $template = $this->createMock(IEMailTemplate::class);
  1222. $this->mailer
  1223. ->expects($this->once())
  1224. ->method('createEMailTemplate')
  1225. ->willReturn($template);
  1226. $template
  1227. ->expects($this->once())
  1228. ->method('addHeader');
  1229. $template
  1230. ->expects($this->once())
  1231. ->method('addHeading')
  1232. ->with('Mrs. Owner User shared »file.txt« with you');
  1233. $template
  1234. ->expects($this->once())
  1235. ->method('addBodyText')
  1236. ->with(
  1237. 'Mrs. Owner User shared »file.txt« with you. Click the button below to open it.',
  1238. 'Mrs. Owner User shared »file.txt« with you.'
  1239. );
  1240. $template
  1241. ->expects($this->once())
  1242. ->method('addBodyButton')
  1243. ->with(
  1244. 'Open »file.txt«',
  1245. 'https://example.com/file.txt'
  1246. );
  1247. $message
  1248. ->expects($this->once())
  1249. ->method('setTo')
  1250. ->with(['john@doe.com']);
  1251. $this->defaults
  1252. ->expects($this->once())
  1253. ->method('getName')
  1254. ->willReturn('UnitTestCloud');
  1255. $message
  1256. ->expects($this->once())
  1257. ->method('setFrom')
  1258. ->with([
  1259. \OCP\Util::getDefaultEmailAddress('UnitTestCloud') => 'UnitTestCloud'
  1260. ]);
  1261. $user
  1262. ->expects($this->once())
  1263. ->method('getEMailAddress')
  1264. ->willReturn('owner@example.com');
  1265. $message
  1266. ->expects($this->never())
  1267. ->method('setReplyTo');
  1268. $template
  1269. ->expects($this->once())
  1270. ->method('addFooter')
  1271. ->with('');
  1272. $template
  1273. ->expects($this->once())
  1274. ->method('setSubject')
  1275. ->with('Mrs. Owner User shared »file.txt« with you');
  1276. $message
  1277. ->expects($this->once())
  1278. ->method('useTemplate')
  1279. ->with($template);
  1280. $this->mailer
  1281. ->expects($this->once())
  1282. ->method('send')
  1283. ->with($message);
  1284. self::invokePrivate(
  1285. $provider,
  1286. 'sendMailNotification',
  1287. [
  1288. 'file.txt',
  1289. 'https://example.com/file.txt',
  1290. 'OwnerUser',
  1291. 'john@doe.com',
  1292. null,
  1293. ]);
  1294. }
  1295. public function testSendMailNotificationWithDifferentUserAndNoUserEmailAndReplyToDesactivate() {
  1296. $provider = $this->getInstance();
  1297. $initiatorUser = $this->createMock(IUser::class);
  1298. $this->settingsManager->expects($this->any())->method('replyToInitiator')->willReturn(false);
  1299. $this->userManager
  1300. ->expects($this->once())
  1301. ->method('get')
  1302. ->with('InitiatorUser')
  1303. ->willReturn($initiatorUser);
  1304. $initiatorUser
  1305. ->expects($this->once())
  1306. ->method('getDisplayName')
  1307. ->willReturn('Mr. Initiator User');
  1308. $message = $this->createMock(Message::class);
  1309. $this->mailer
  1310. ->expects($this->once())
  1311. ->method('createMessage')
  1312. ->willReturn($message);
  1313. $template = $this->createMock(IEMailTemplate::class);
  1314. $this->mailer
  1315. ->expects($this->once())
  1316. ->method('createEMailTemplate')
  1317. ->willReturn($template);
  1318. $template
  1319. ->expects($this->once())
  1320. ->method('addHeader');
  1321. $template
  1322. ->expects($this->once())
  1323. ->method('addHeading')
  1324. ->with('Mr. Initiator User shared »file.txt« with you');
  1325. $template
  1326. ->expects($this->once())
  1327. ->method('addBodyText')
  1328. ->with(
  1329. 'Mr. Initiator User shared »file.txt« with you. Click the button below to open it.',
  1330. 'Mr. Initiator User shared »file.txt« with you.'
  1331. );
  1332. $template
  1333. ->expects($this->once())
  1334. ->method('addBodyButton')
  1335. ->with(
  1336. 'Open »file.txt«',
  1337. 'https://example.com/file.txt'
  1338. );
  1339. $message
  1340. ->expects($this->once())
  1341. ->method('setTo')
  1342. ->with(['john@doe.com']);
  1343. $this->defaults
  1344. ->expects($this->once())
  1345. ->method('getName')
  1346. ->willReturn('UnitTestCloud');
  1347. $message
  1348. ->expects($this->once())
  1349. ->method('setFrom')
  1350. ->with([
  1351. \OCP\Util::getDefaultEmailAddress('UnitTestCloud') => 'UnitTestCloud'
  1352. ]);
  1353. $message
  1354. ->expects($this->never())
  1355. ->method('setReplyTo');
  1356. $template
  1357. ->expects($this->once())
  1358. ->method('addFooter')
  1359. ->with('');
  1360. $template
  1361. ->expects($this->once())
  1362. ->method('setSubject')
  1363. ->with('Mr. Initiator User shared »file.txt« with you');
  1364. $message
  1365. ->expects($this->once())
  1366. ->method('useTemplate')
  1367. ->with($template);
  1368. $this->mailer
  1369. ->expects($this->once())
  1370. ->method('send')
  1371. ->with($message);
  1372. self::invokePrivate(
  1373. $provider,
  1374. 'sendMailNotification',
  1375. [
  1376. 'file.txt',
  1377. 'https://example.com/file.txt',
  1378. 'InitiatorUser',
  1379. 'john@doe.com',
  1380. null,
  1381. ]);
  1382. }
  1383. }