ShareByMailProviderTest.php 57 KB

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