1
0

ShareByMailProviderTest.php 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616
  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->assertSame($this->share,
  531. $instance->update($this->share)
  532. );
  533. $qb = $this->connection->getQueryBuilder();
  534. $qb->select('*')
  535. ->from('share')
  536. ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)));
  537. $qResult = $qb->execute();
  538. $result = $qResult->fetchAll();
  539. $qResult->closeCursor();
  540. $this->assertSame(1, count($result));
  541. $this->assertSame($itemSource, (int)$result[0]['item_source']);
  542. $this->assertSame($itemType, $result[0]['item_type']);
  543. $this->assertSame($shareWith, $result[0]['share_with']);
  544. $this->assertSame($sharedBy, $result[0]['uid_initiator']);
  545. $this->assertSame($uidOwner, $result[0]['uid_owner']);
  546. $this->assertSame($permissions + 1, (int)$result[0]['permissions']);
  547. $this->assertSame($token, $result[0]['token']);
  548. $this->assertSame($note, $result[0]['note']);
  549. }
  550. public function dataUpdateSendPassword() {
  551. return [
  552. ['password', 'hashed', 'hashed new', false, false, true],
  553. ['', 'hashed', 'hashed new', false, false, false],
  554. [null, 'hashed', 'hashed new', false, false, false],
  555. ['password', 'hashed', 'hashed', false, false, false],
  556. ['password', 'hashed', 'hashed new', false, true, false],
  557. ['password', 'hashed', 'hashed new', true, false, true],
  558. ['password', 'hashed', 'hashed', true, false, true],
  559. ];
  560. }
  561. /**
  562. * @dataProvider dataUpdateSendPassword
  563. *
  564. * @param string|null plainTextPassword
  565. * @param string originalPassword
  566. * @param string newPassword
  567. * @param string originalSendPasswordByTalk
  568. * @param string newSendPasswordByTalk
  569. * @param bool sendMail
  570. */
  571. public function testUpdateSendPassword($plainTextPassword, string $originalPassword, string $newPassword, $originalSendPasswordByTalk, $newSendPasswordByTalk, bool $sendMail) {
  572. $node = $this->getMockBuilder(File::class)->getMock();
  573. $node->expects($this->any())->method('getName')->willReturn('filename');
  574. $this->settingsManager->method('sendPasswordByMail')->willReturn(true);
  575. $originalShare = $this->getMockBuilder(IShare::class)->getMock();
  576. $originalShare->expects($this->any())->method('getSharedWith')->willReturn('receiver@example.com');
  577. $originalShare->expects($this->any())->method('getNode')->willReturn($node);
  578. $originalShare->expects($this->any())->method('getId')->willReturn(42);
  579. $originalShare->expects($this->any())->method('getPassword')->willReturn($originalPassword);
  580. $originalShare->expects($this->any())->method('getSendPasswordByTalk')->willReturn($originalSendPasswordByTalk);
  581. $share = $this->getMockBuilder(IShare::class)->getMock();
  582. $share->expects($this->any())->method('getSharedWith')->willReturn('receiver@example.com');
  583. $share->expects($this->any())->method('getNode')->willReturn($node);
  584. $share->expects($this->any())->method('getId')->willReturn(42);
  585. $share->expects($this->any())->method('getPassword')->willReturn($newPassword);
  586. $share->expects($this->any())->method('getSendPasswordByTalk')->willReturn($newSendPasswordByTalk);
  587. if ($sendMail) {
  588. $this->mailer->expects($this->once())->method('createEMailTemplate')->with('sharebymail.RecipientPasswordNotification', [
  589. 'filename' => 'filename',
  590. 'password' => $plainTextPassword,
  591. 'initiator' => null,
  592. 'initiatorEmail' => null,
  593. 'shareWith' => 'receiver@example.com',
  594. ]);
  595. $this->mailer->expects($this->once())->method('send');
  596. } else {
  597. $this->mailer->expects($this->never())->method('send');
  598. }
  599. $instance = $this->getInstance(['getShareById', 'createPasswordSendActivity']);
  600. $instance->expects($this->once())->method('getShareById')->willReturn($originalShare);
  601. $this->assertSame($share,
  602. $instance->update($share, $plainTextPassword)
  603. );
  604. }
  605. public function testDelete() {
  606. $instance = $this->getInstance(['removeShareFromTable', 'createShareActivity']);
  607. $this->share->expects($this->once())->method('getId')->willReturn(42);
  608. $instance->expects($this->once())->method('removeShareFromTable')->with(42);
  609. $instance->expects($this->once())->method('createShareActivity')->with($this->share, 'unshare');
  610. $instance->delete($this->share);
  611. }
  612. public function testGetShareById() {
  613. $instance = $this->getInstance(['createShareObject']);
  614. $itemSource = 11;
  615. $itemType = 'file';
  616. $shareWith = 'user@server.com';
  617. $sharedBy = 'user1';
  618. $uidOwner = 'user2';
  619. $permissions = 1;
  620. $token = 'token';
  621. $this->createDummyShare($itemType, $itemSource, $shareWith, "user1wrong", "user2wrong", $permissions, $token);
  622. $id2 = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  623. $instance->expects($this->once())->method('createShareObject')
  624. ->willReturnCallback(
  625. function ($data) use ($uidOwner, $sharedBy, $id2) {
  626. $this->assertSame($uidOwner, $data['uid_owner']);
  627. $this->assertSame($sharedBy, $data['uid_initiator']);
  628. $this->assertSame($id2, (int)$data['id']);
  629. return $this->share;
  630. }
  631. );
  632. $result = $instance->getShareById($id2);
  633. $this->assertInstanceOf('OCP\Share\IShare', $result);
  634. }
  635. public function testGetShareByIdFailed() {
  636. $this->expectException(\OCP\Share\Exceptions\ShareNotFound::class);
  637. $instance = $this->getInstance(['createShareObject']);
  638. $itemSource = 11;
  639. $itemType = 'file';
  640. $shareWith = 'user@server.com';
  641. $sharedBy = 'user1';
  642. $uidOwner = 'user2';
  643. $permissions = 1;
  644. $token = 'token';
  645. $id = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  646. $instance->getShareById($id + 1);
  647. }
  648. public function testGetShareByPath() {
  649. $itemSource = 11;
  650. $itemType = 'file';
  651. $shareWith = 'user@server.com';
  652. $sharedBy = 'user1';
  653. $uidOwner = 'user2';
  654. $permissions = 1;
  655. $token = 'token';
  656. $node = $this->getMockBuilder('OCP\Files\Node')->getMock();
  657. $node->expects($this->once())->method('getId')->willReturn($itemSource);
  658. $instance = $this->getInstance(['createShareObject']);
  659. $this->createDummyShare($itemType, 111, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  660. $id = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  661. $instance->expects($this->once())->method('createShareObject')
  662. ->willReturnCallback(
  663. function ($data) use ($uidOwner, $sharedBy, $id) {
  664. $this->assertSame($uidOwner, $data['uid_owner']);
  665. $this->assertSame($sharedBy, $data['uid_initiator']);
  666. $this->assertSame($id, (int)$data['id']);
  667. return $this->share;
  668. }
  669. );
  670. $result = $instance->getSharesByPath($node);
  671. $this->assertTrue(is_array($result));
  672. $this->assertSame(1, count($result));
  673. $this->assertInstanceOf('OCP\Share\IShare', $result[0]);
  674. }
  675. public function testGetShareByToken() {
  676. $itemSource = 11;
  677. $itemType = 'file';
  678. $shareWith = 'user@server.com';
  679. $sharedBy = 'user1';
  680. $uidOwner = 'user2';
  681. $permissions = 1;
  682. $token = 'token';
  683. $instance = $this->getInstance(['createShareObject']);
  684. $idMail = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  685. $idPublic = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token, '', IShare::TYPE_LINK);
  686. $this->assertTrue($idMail !== $idPublic);
  687. $instance->expects($this->once())->method('createShareObject')
  688. ->willReturnCallback(
  689. function ($data) use ($idMail) {
  690. $this->assertSame($idMail, (int)$data['id']);
  691. return $this->share;
  692. }
  693. );
  694. $result = $instance->getShareByToken('token');
  695. $this->assertInstanceOf('OCP\Share\IShare', $result);
  696. }
  697. public function testGetShareByTokenFailed() {
  698. $this->expectException(\OCP\Share\Exceptions\ShareNotFound::class);
  699. $itemSource = 11;
  700. $itemType = 'file';
  701. $shareWith = 'user@server.com';
  702. $sharedBy = 'user1';
  703. $uidOwner = 'user2';
  704. $permissions = 1;
  705. $token = 'token';
  706. $instance = $this->getInstance(['createShareObject']);
  707. $idMail = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  708. $idPublic = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, "token2", '', IShare::TYPE_LINK);
  709. $this->assertTrue($idMail !== $idPublic);
  710. $this->assertInstanceOf('OCP\Share\IShare',
  711. $instance->getShareByToken('token2')
  712. );
  713. }
  714. public function testRemoveShareFromTable() {
  715. $itemSource = 11;
  716. $itemType = 'file';
  717. $shareWith = 'user@server.com';
  718. $sharedBy = 'user1';
  719. $uidOwner = 'user2';
  720. $permissions = 1;
  721. $token = 'token';
  722. $instance = $this->getInstance();
  723. $id = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  724. $query = $this->connection->getQueryBuilder();
  725. $query->select('*')->from('share')
  726. ->where($query->expr()->eq('id', $query->createNamedParameter($id)));
  727. $result = $query->execute();
  728. $before = $result->fetchAll();
  729. $result->closeCursor();
  730. $this->assertTrue(is_array($before));
  731. $this->assertSame(1, count($before));
  732. $this->invokePrivate($instance, 'removeShareFromTable', [$id]);
  733. $query = $this->connection->getQueryBuilder();
  734. $query->select('*')->from('share')
  735. ->where($query->expr()->eq('id', $query->createNamedParameter($id)));
  736. $result = $query->execute();
  737. $after = $result->fetchAll();
  738. $result->closeCursor();
  739. $this->assertTrue(is_array($after));
  740. $this->assertEmpty($after);
  741. }
  742. public function testUserDeleted() {
  743. $itemSource = 11;
  744. $itemType = 'file';
  745. $shareWith = 'user@server.com';
  746. $sharedBy = 'user1';
  747. $uidOwner = 'user2';
  748. $permissions = 1;
  749. $token = 'token';
  750. $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  751. $id = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, 'user2Wrong', $permissions, $token);
  752. $query = $this->connection->getQueryBuilder();
  753. $query->select('*')->from('share');
  754. $result = $query->execute();
  755. $before = $result->fetchAll();
  756. $result->closeCursor();
  757. $this->assertTrue(is_array($before));
  758. $this->assertSame(2, count($before));
  759. $instance = $this->getInstance();
  760. $instance->userDeleted($uidOwner, IShare::TYPE_EMAIL);
  761. $query = $this->connection->getQueryBuilder();
  762. $query->select('*')->from('share');
  763. $result = $query->execute();
  764. $after = $result->fetchAll();
  765. $result->closeCursor();
  766. $this->assertTrue(is_array($after));
  767. $this->assertSame(1, count($after));
  768. $this->assertSame($id, (int)$after[0]['id']);
  769. }
  770. public function testGetRawShare() {
  771. $itemSource = 11;
  772. $itemType = 'file';
  773. $shareWith = 'user@server.com';
  774. $sharedBy = 'user1';
  775. $uidOwner = 'user2';
  776. $permissions = 1;
  777. $token = 'token';
  778. $instance = $this->getInstance();
  779. $id = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  780. $result = $this->invokePrivate($instance, 'getRawShare', [$id]);
  781. $this->assertTrue(is_array($result));
  782. $this->assertSame($itemSource, (int)$result['item_source']);
  783. $this->assertSame($itemType, $result['item_type']);
  784. $this->assertSame($shareWith, $result['share_with']);
  785. $this->assertSame($sharedBy, $result['uid_initiator']);
  786. $this->assertSame($uidOwner, $result['uid_owner']);
  787. $this->assertSame($permissions, (int)$result['permissions']);
  788. $this->assertSame($token, $result['token']);
  789. }
  790. public function testGetRawShareFailed() {
  791. $this->expectException(\OCP\Share\Exceptions\ShareNotFound::class);
  792. $itemSource = 11;
  793. $itemType = 'file';
  794. $shareWith = 'user@server.com';
  795. $sharedBy = 'user1';
  796. $uidOwner = 'user2';
  797. $permissions = 1;
  798. $token = 'token';
  799. $instance = $this->getInstance();
  800. $id = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  801. $this->invokePrivate($instance, 'getRawShare', [$id + 1]);
  802. }
  803. private function createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token, $note = '', $shareType = IShare::TYPE_EMAIL) {
  804. $qb = $this->connection->getQueryBuilder();
  805. $qb->insert('share')
  806. ->setValue('share_type', $qb->createNamedParameter($shareType))
  807. ->setValue('item_type', $qb->createNamedParameter($itemType))
  808. ->setValue('item_source', $qb->createNamedParameter($itemSource))
  809. ->setValue('file_source', $qb->createNamedParameter($itemSource))
  810. ->setValue('share_with', $qb->createNamedParameter($shareWith))
  811. ->setValue('uid_owner', $qb->createNamedParameter($uidOwner))
  812. ->setValue('uid_initiator', $qb->createNamedParameter($sharedBy))
  813. ->setValue('permissions', $qb->createNamedParameter($permissions))
  814. ->setValue('token', $qb->createNamedParameter($token))
  815. ->setValue('note', $qb->createNamedParameter($note))
  816. ->setValue('stime', $qb->createNamedParameter(time()));
  817. /*
  818. * Added to fix https://github.com/owncloud/core/issues/22215
  819. * Can be removed once we get rid of ajax/share.php
  820. */
  821. $qb->setValue('file_target', $qb->createNamedParameter(''));
  822. $qb->execute();
  823. $id = $qb->getLastInsertId();
  824. return (int)$id;
  825. }
  826. public function testGetSharesInFolder() {
  827. $userManager = \OC::$server->getUserManager();
  828. $rootFolder = \OC::$server->getRootFolder();
  829. $this->shareManager->expects($this->any())
  830. ->method('newShare')
  831. ->willReturn(new \OC\Share20\Share($rootFolder, $userManager));
  832. $provider = $this->getInstance(['sendMailNotification', 'createShareActivity']);
  833. $this->mailer->expects($this->any())->method('validateMailAddress')->willReturn(true);
  834. $u1 = $userManager->createUser('testFed', md5(time()));
  835. $u2 = $userManager->createUser('testFed2', md5(time()));
  836. $folder1 = $rootFolder->getUserFolder($u1->getUID())->newFolder('foo');
  837. $file1 = $folder1->newFile('bar1');
  838. $file2 = $folder1->newFile('bar2');
  839. $share1 = $this->shareManager->newShare();
  840. $share1->setSharedWith('user@server.com')
  841. ->setSharedBy($u1->getUID())
  842. ->setShareOwner($u1->getUID())
  843. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  844. ->setNode($file1);
  845. $provider->create($share1);
  846. $share2 = $this->shareManager->newShare();
  847. $share2->setSharedWith('user@server.com')
  848. ->setSharedBy($u2->getUID())
  849. ->setShareOwner($u1->getUID())
  850. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  851. ->setNode($file2);
  852. $provider->create($share2);
  853. $result = $provider->getSharesInFolder($u1->getUID(), $folder1, false);
  854. $this->assertCount(1, $result);
  855. $this->assertCount(1, $result[$file1->getId()]);
  856. $result = $provider->getSharesInFolder($u1->getUID(), $folder1, true);
  857. $this->assertCount(2, $result);
  858. $this->assertCount(1, $result[$file1->getId()]);
  859. $this->assertCount(1, $result[$file2->getId()]);
  860. $u1->delete();
  861. $u2->delete();
  862. }
  863. public function testGetAccessList() {
  864. $userManager = \OC::$server->getUserManager();
  865. $rootFolder = \OC::$server->getRootFolder();
  866. $this->shareManager->expects($this->any())
  867. ->method('newShare')
  868. ->willReturn(new \OC\Share20\Share($rootFolder, $userManager));
  869. $provider = $this->getInstance(['sendMailNotification', 'createShareActivity']);
  870. $this->mailer->expects($this->any())->method('validateMailAddress')->willReturn(true);
  871. $u1 = $userManager->createUser('testFed', md5(time()));
  872. $u2 = $userManager->createUser('testFed2', md5(time()));
  873. $folder = $rootFolder->getUserFolder($u1->getUID())->newFolder('foo');
  874. $accessList = $provider->getAccessList([$folder], true);
  875. $this->assertArrayHasKey('public', $accessList);
  876. $this->assertFalse($accessList['public']);
  877. $accessList = $provider->getAccessList([$folder], false);
  878. $this->assertArrayHasKey('public', $accessList);
  879. $this->assertFalse($accessList['public']);
  880. $share1 = $this->shareManager->newShare();
  881. $share1->setSharedWith('user@server.com')
  882. ->setSharedBy($u1->getUID())
  883. ->setShareOwner($u1->getUID())
  884. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  885. ->setNode($folder);
  886. $share1 = $provider->create($share1);
  887. $share2 = $this->shareManager->newShare();
  888. $share2->setSharedWith('user2@server.com')
  889. ->setSharedBy($u2->getUID())
  890. ->setShareOwner($u1->getUID())
  891. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  892. ->setNode($folder);
  893. $share2 = $provider->create($share2);
  894. $accessList = $provider->getAccessList([$folder], true);
  895. $this->assertArrayHasKey('public', $accessList);
  896. $this->assertTrue($accessList['public']);
  897. $accessList = $provider->getAccessList([$folder], false);
  898. $this->assertArrayHasKey('public', $accessList);
  899. $this->assertTrue($accessList['public']);
  900. $provider->delete($share2);
  901. $accessList = $provider->getAccessList([$folder], true);
  902. $this->assertArrayHasKey('public', $accessList);
  903. $this->assertTrue($accessList['public']);
  904. $accessList = $provider->getAccessList([$folder], false);
  905. $this->assertArrayHasKey('public', $accessList);
  906. $this->assertTrue($accessList['public']);
  907. $provider->delete($share1);
  908. $accessList = $provider->getAccessList([$folder], true);
  909. $this->assertArrayHasKey('public', $accessList);
  910. $this->assertFalse($accessList['public']);
  911. $accessList = $provider->getAccessList([$folder], false);
  912. $this->assertArrayHasKey('public', $accessList);
  913. $this->assertFalse($accessList['public']);
  914. $u1->delete();
  915. $u2->delete();
  916. }
  917. public function testSendMailNotificationWithSameUserAndUserEmail() {
  918. $provider = $this->getInstance();
  919. $user = $this->createMock(IUser::class);
  920. $this->settingsManager->expects($this->any())->method('replyToInitiator')->willReturn(true);
  921. $this->userManager
  922. ->expects($this->once())
  923. ->method('get')
  924. ->with('OwnerUser')
  925. ->willReturn($user);
  926. $user
  927. ->expects($this->once())
  928. ->method('getDisplayName')
  929. ->willReturn('Mrs. Owner User');
  930. $message = $this->createMock(Message::class);
  931. $this->mailer
  932. ->expects($this->once())
  933. ->method('createMessage')
  934. ->willReturn($message);
  935. $template = $this->createMock(IEMailTemplate::class);
  936. $this->mailer
  937. ->expects($this->once())
  938. ->method('createEMailTemplate')
  939. ->willReturn($template);
  940. $template
  941. ->expects($this->once())
  942. ->method('addHeader');
  943. $template
  944. ->expects($this->once())
  945. ->method('addHeading')
  946. ->with('Mrs. Owner User shared »file.txt« with you');
  947. $template
  948. ->expects($this->once())
  949. ->method('addBodyText')
  950. ->with(
  951. 'Mrs. Owner User shared »file.txt« with you. Click the button below to open it.',
  952. 'Mrs. Owner User shared »file.txt« with you.'
  953. );
  954. $template
  955. ->expects($this->once())
  956. ->method('addBodyButton')
  957. ->with(
  958. 'Open »file.txt«',
  959. 'https://example.com/file.txt'
  960. );
  961. $message
  962. ->expects($this->once())
  963. ->method('setTo')
  964. ->with(['john@doe.com']);
  965. $this->defaults
  966. ->expects($this->once())
  967. ->method('getName')
  968. ->willReturn('UnitTestCloud');
  969. $message
  970. ->expects($this->once())
  971. ->method('setFrom')
  972. ->with([
  973. \OCP\Util::getDefaultEmailAddress('UnitTestCloud') => 'Mrs. Owner User via UnitTestCloud'
  974. ]);
  975. $user
  976. ->expects($this->once())
  977. ->method('getEMailAddress')
  978. ->willReturn('owner@example.com');
  979. $message
  980. ->expects($this->once())
  981. ->method('setReplyTo')
  982. ->with(['owner@example.com' => 'Mrs. Owner User']);
  983. $this->defaults
  984. ->expects($this->exactly(2))
  985. ->method('getSlogan')
  986. ->willReturn('Testing like 1990');
  987. $template
  988. ->expects($this->once())
  989. ->method('addFooter')
  990. ->with('UnitTestCloud - Testing like 1990');
  991. $template
  992. ->expects($this->once())
  993. ->method('setSubject')
  994. ->with('Mrs. Owner User shared »file.txt« with you');
  995. $message
  996. ->expects($this->once())
  997. ->method('useTemplate')
  998. ->with($template);
  999. $this->mailer
  1000. ->expects($this->once())
  1001. ->method('send')
  1002. ->with($message);
  1003. self::invokePrivate(
  1004. $provider,
  1005. 'sendMailNotification',
  1006. [
  1007. 'file.txt',
  1008. 'https://example.com/file.txt',
  1009. 'OwnerUser',
  1010. 'john@doe.com',
  1011. null,
  1012. ''
  1013. ]);
  1014. }
  1015. public function testSendMailNotificationWithSameUserAndUserEmailAndNote() {
  1016. $provider = $this->getInstance();
  1017. $user = $this->createMock(IUser::class);
  1018. $this->settingsManager->expects($this->any())->method('replyToInitiator')->willReturn(true);
  1019. $this->userManager
  1020. ->expects($this->once())
  1021. ->method('get')
  1022. ->with('OwnerUser')
  1023. ->willReturn($user);
  1024. $user
  1025. ->expects($this->once())
  1026. ->method('getDisplayName')
  1027. ->willReturn('Mrs. Owner User');
  1028. $message = $this->createMock(Message::class);
  1029. $this->mailer
  1030. ->expects($this->once())
  1031. ->method('createMessage')
  1032. ->willReturn($message);
  1033. $template = $this->createMock(IEMailTemplate::class);
  1034. $this->mailer
  1035. ->expects($this->once())
  1036. ->method('createEMailTemplate')
  1037. ->willReturn($template);
  1038. $template
  1039. ->expects($this->once())
  1040. ->method('addHeader');
  1041. $template
  1042. ->expects($this->once())
  1043. ->method('addHeading')
  1044. ->with('Mrs. Owner User shared »file.txt« with you');
  1045. $template
  1046. ->expects($this->exactly(2))
  1047. ->method('addBodyText')
  1048. ->withConsecutive(
  1049. ['This is a note to the recipient', 'This is a note to the recipient'],
  1050. ['Mrs. Owner User shared »file.txt« with you. Click the button below to open it.', 'Mrs. Owner User shared »file.txt« with you.'],
  1051. );
  1052. $template
  1053. ->expects($this->once())
  1054. ->method('addBodyButton')
  1055. ->with(
  1056. 'Open »file.txt«',
  1057. 'https://example.com/file.txt'
  1058. );
  1059. $message
  1060. ->expects($this->once())
  1061. ->method('setTo')
  1062. ->with(['john@doe.com']);
  1063. $this->defaults
  1064. ->expects($this->once())
  1065. ->method('getName')
  1066. ->willReturn('UnitTestCloud');
  1067. $message
  1068. ->expects($this->once())
  1069. ->method('setFrom')
  1070. ->with([
  1071. \OCP\Util::getDefaultEmailAddress('UnitTestCloud') => 'Mrs. Owner User via UnitTestCloud'
  1072. ]);
  1073. $user
  1074. ->expects($this->once())
  1075. ->method('getEMailAddress')
  1076. ->willReturn('owner@example.com');
  1077. $message
  1078. ->expects($this->once())
  1079. ->method('setReplyTo')
  1080. ->with(['owner@example.com' => 'Mrs. Owner User']);
  1081. $this->defaults
  1082. ->expects($this->exactly(2))
  1083. ->method('getSlogan')
  1084. ->willReturn('Testing like 1990');
  1085. $template
  1086. ->expects($this->once())
  1087. ->method('addFooter')
  1088. ->with('UnitTestCloud - Testing like 1990');
  1089. $template
  1090. ->expects($this->once())
  1091. ->method('setSubject')
  1092. ->with('Mrs. Owner User shared »file.txt« with you');
  1093. $message
  1094. ->expects($this->once())
  1095. ->method('useTemplate')
  1096. ->with($template);
  1097. $this->mailer
  1098. ->expects($this->once())
  1099. ->method('send')
  1100. ->with($message);
  1101. self::invokePrivate(
  1102. $provider,
  1103. 'sendMailNotification',
  1104. [
  1105. 'file.txt',
  1106. 'https://example.com/file.txt',
  1107. 'OwnerUser',
  1108. 'john@doe.com',
  1109. null,
  1110. 'This is a note to the recipient'
  1111. ]);
  1112. }
  1113. public function testSendMailNotificationWithDifferentUserAndNoUserEmail() {
  1114. $provider = $this->getInstance();
  1115. $initiatorUser = $this->createMock(IUser::class);
  1116. $this->settingsManager->expects($this->any())->method('replyToInitiator')->willReturn(true);
  1117. $this->userManager
  1118. ->expects($this->once())
  1119. ->method('get')
  1120. ->with('InitiatorUser')
  1121. ->willReturn($initiatorUser);
  1122. $initiatorUser
  1123. ->expects($this->once())
  1124. ->method('getDisplayName')
  1125. ->willReturn('Mr. Initiator User');
  1126. $message = $this->createMock(Message::class);
  1127. $this->mailer
  1128. ->expects($this->once())
  1129. ->method('createMessage')
  1130. ->willReturn($message);
  1131. $template = $this->createMock(IEMailTemplate::class);
  1132. $this->mailer
  1133. ->expects($this->once())
  1134. ->method('createEMailTemplate')
  1135. ->willReturn($template);
  1136. $template
  1137. ->expects($this->once())
  1138. ->method('addHeader');
  1139. $template
  1140. ->expects($this->once())
  1141. ->method('addHeading')
  1142. ->with('Mr. Initiator User shared »file.txt« with you');
  1143. $template
  1144. ->expects($this->once())
  1145. ->method('addBodyText')
  1146. ->with(
  1147. 'Mr. Initiator User shared »file.txt« with you. Click the button below to open it.',
  1148. 'Mr. Initiator User shared »file.txt« with you.'
  1149. );
  1150. $template
  1151. ->expects($this->once())
  1152. ->method('addBodyButton')
  1153. ->with(
  1154. 'Open »file.txt«',
  1155. 'https://example.com/file.txt'
  1156. );
  1157. $message
  1158. ->expects($this->once())
  1159. ->method('setTo')
  1160. ->with(['john@doe.com']);
  1161. $this->defaults
  1162. ->expects($this->once())
  1163. ->method('getName')
  1164. ->willReturn('UnitTestCloud');
  1165. $message
  1166. ->expects($this->once())
  1167. ->method('setFrom')
  1168. ->with([
  1169. \OCP\Util::getDefaultEmailAddress('UnitTestCloud') => 'Mr. Initiator User via UnitTestCloud'
  1170. ]);
  1171. $message
  1172. ->expects($this->never())
  1173. ->method('setReplyTo');
  1174. $template
  1175. ->expects($this->once())
  1176. ->method('addFooter')
  1177. ->with('');
  1178. $template
  1179. ->expects($this->once())
  1180. ->method('setSubject')
  1181. ->with('Mr. Initiator User shared »file.txt« with you');
  1182. $message
  1183. ->expects($this->once())
  1184. ->method('useTemplate')
  1185. ->with($template);
  1186. $this->mailer
  1187. ->expects($this->once())
  1188. ->method('send')
  1189. ->with($message);
  1190. self::invokePrivate(
  1191. $provider,
  1192. 'sendMailNotification',
  1193. [
  1194. 'file.txt',
  1195. 'https://example.com/file.txt',
  1196. 'InitiatorUser',
  1197. 'john@doe.com',
  1198. null,
  1199. ]);
  1200. }
  1201. public function testSendMailNotificationWithSameUserAndUserEmailAndReplyToDesactivate() {
  1202. $provider = $this->getInstance();
  1203. $user = $this->createMock(IUser::class);
  1204. $this->settingsManager->expects($this->any())->method('replyToInitiator')->willReturn(false);
  1205. $this->userManager
  1206. ->expects($this->once())
  1207. ->method('get')
  1208. ->with('OwnerUser')
  1209. ->willReturn($user);
  1210. $user
  1211. ->expects($this->once())
  1212. ->method('getDisplayName')
  1213. ->willReturn('Mrs. Owner User');
  1214. $message = $this->createMock(Message::class);
  1215. $this->mailer
  1216. ->expects($this->once())
  1217. ->method('createMessage')
  1218. ->willReturn($message);
  1219. $template = $this->createMock(IEMailTemplate::class);
  1220. $this->mailer
  1221. ->expects($this->once())
  1222. ->method('createEMailTemplate')
  1223. ->willReturn($template);
  1224. $template
  1225. ->expects($this->once())
  1226. ->method('addHeader');
  1227. $template
  1228. ->expects($this->once())
  1229. ->method('addHeading')
  1230. ->with('Mrs. Owner User shared »file.txt« with you');
  1231. $template
  1232. ->expects($this->once())
  1233. ->method('addBodyText')
  1234. ->with(
  1235. 'Mrs. Owner User shared »file.txt« with you. Click the button below to open it.',
  1236. 'Mrs. Owner User shared »file.txt« with you.'
  1237. );
  1238. $template
  1239. ->expects($this->once())
  1240. ->method('addBodyButton')
  1241. ->with(
  1242. 'Open »file.txt«',
  1243. 'https://example.com/file.txt'
  1244. );
  1245. $message
  1246. ->expects($this->once())
  1247. ->method('setTo')
  1248. ->with(['john@doe.com']);
  1249. $this->defaults
  1250. ->expects($this->once())
  1251. ->method('getName')
  1252. ->willReturn('UnitTestCloud');
  1253. $message
  1254. ->expects($this->once())
  1255. ->method('setFrom')
  1256. ->with([
  1257. \OCP\Util::getDefaultEmailAddress('UnitTestCloud') => 'UnitTestCloud'
  1258. ]);
  1259. $user
  1260. ->expects($this->once())
  1261. ->method('getEMailAddress')
  1262. ->willReturn('owner@example.com');
  1263. $message
  1264. ->expects($this->never())
  1265. ->method('setReplyTo');
  1266. $template
  1267. ->expects($this->once())
  1268. ->method('addFooter')
  1269. ->with('');
  1270. $template
  1271. ->expects($this->once())
  1272. ->method('setSubject')
  1273. ->with('Mrs. Owner User shared »file.txt« with you');
  1274. $message
  1275. ->expects($this->once())
  1276. ->method('useTemplate')
  1277. ->with($template);
  1278. $this->mailer
  1279. ->expects($this->once())
  1280. ->method('send')
  1281. ->with($message);
  1282. self::invokePrivate(
  1283. $provider,
  1284. 'sendMailNotification',
  1285. [
  1286. 'file.txt',
  1287. 'https://example.com/file.txt',
  1288. 'OwnerUser',
  1289. 'john@doe.com',
  1290. null,
  1291. ]);
  1292. }
  1293. public function testSendMailNotificationWithDifferentUserAndNoUserEmailAndReplyToDesactivate() {
  1294. $provider = $this->getInstance();
  1295. $initiatorUser = $this->createMock(IUser::class);
  1296. $this->settingsManager->expects($this->any())->method('replyToInitiator')->willReturn(false);
  1297. $this->userManager
  1298. ->expects($this->once())
  1299. ->method('get')
  1300. ->with('InitiatorUser')
  1301. ->willReturn($initiatorUser);
  1302. $initiatorUser
  1303. ->expects($this->once())
  1304. ->method('getDisplayName')
  1305. ->willReturn('Mr. Initiator User');
  1306. $message = $this->createMock(Message::class);
  1307. $this->mailer
  1308. ->expects($this->once())
  1309. ->method('createMessage')
  1310. ->willReturn($message);
  1311. $template = $this->createMock(IEMailTemplate::class);
  1312. $this->mailer
  1313. ->expects($this->once())
  1314. ->method('createEMailTemplate')
  1315. ->willReturn($template);
  1316. $template
  1317. ->expects($this->once())
  1318. ->method('addHeader');
  1319. $template
  1320. ->expects($this->once())
  1321. ->method('addHeading')
  1322. ->with('Mr. Initiator User shared »file.txt« with you');
  1323. $template
  1324. ->expects($this->once())
  1325. ->method('addBodyText')
  1326. ->with(
  1327. 'Mr. Initiator User shared »file.txt« with you. Click the button below to open it.',
  1328. 'Mr. Initiator User shared »file.txt« with you.'
  1329. );
  1330. $template
  1331. ->expects($this->once())
  1332. ->method('addBodyButton')
  1333. ->with(
  1334. 'Open »file.txt«',
  1335. 'https://example.com/file.txt'
  1336. );
  1337. $message
  1338. ->expects($this->once())
  1339. ->method('setTo')
  1340. ->with(['john@doe.com']);
  1341. $this->defaults
  1342. ->expects($this->once())
  1343. ->method('getName')
  1344. ->willReturn('UnitTestCloud');
  1345. $message
  1346. ->expects($this->once())
  1347. ->method('setFrom')
  1348. ->with([
  1349. \OCP\Util::getDefaultEmailAddress('UnitTestCloud') => 'UnitTestCloud'
  1350. ]);
  1351. $message
  1352. ->expects($this->never())
  1353. ->method('setReplyTo');
  1354. $template
  1355. ->expects($this->once())
  1356. ->method('addFooter')
  1357. ->with('');
  1358. $template
  1359. ->expects($this->once())
  1360. ->method('setSubject')
  1361. ->with('Mr. Initiator User shared »file.txt« with you');
  1362. $message
  1363. ->expects($this->once())
  1364. ->method('useTemplate')
  1365. ->with($template);
  1366. $this->mailer
  1367. ->expects($this->once())
  1368. ->method('send')
  1369. ->with($message);
  1370. self::invokePrivate(
  1371. $provider,
  1372. 'sendMailNotification',
  1373. [
  1374. 'file.txt',
  1375. 'https://example.com/file.txt',
  1376. 'InitiatorUser',
  1377. 'john@doe.com',
  1378. null,
  1379. ]);
  1380. }
  1381. }