ShareByMailProviderTest.php 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Bjoern Schiessle <bjoern@schiessle.org>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace OCA\ShareByMail\Tests;
  22. use OC\CapabilitiesManager;
  23. use OC\Mail\Message;
  24. use OCA\ShareByMail\Settings\SettingsManager;
  25. use OCA\ShareByMail\ShareByMailProvider;
  26. use OCP\Defaults;
  27. use OCP\Files\File;
  28. use OCP\Files\IRootFolder;
  29. use OCP\IDBConnection;
  30. use OCP\IL10N;
  31. use OCP\ILogger;
  32. use OCP\IURLGenerator;
  33. use OCP\IUser;
  34. use OCP\IUserManager;
  35. use OCP\Mail\IEMailTemplate;
  36. use OCP\Mail\IMailer;
  37. use OCP\Mail\IMessage;
  38. use OCP\Security\IHasher;
  39. use OCP\Security\ISecureRandom;
  40. use OCP\Share\IManager;
  41. use OCP\Share\IShare;
  42. use Test\TestCase;
  43. /**
  44. * Class ShareByMailProviderTest
  45. *
  46. * @package OCA\ShareByMail\Tests
  47. * @group DB
  48. */
  49. class ShareByMailProviderTest extends TestCase {
  50. /** @var IDBConnection */
  51. private $connection;
  52. /** @var IManager */
  53. private $shareManager;
  54. /** @var IL10N | \PHPUnit_Framework_MockObject_MockObject */
  55. private $l;
  56. /** @var ILogger | \PHPUnit_Framework_MockObject_MockObject */
  57. private $logger;
  58. /** @var IRootFolder | \PHPUnit_Framework_MockObject_MockObject */
  59. private $rootFolder;
  60. /** @var IUserManager | \PHPUnit_Framework_MockObject_MockObject */
  61. private $userManager;
  62. /** @var ISecureRandom | \PHPUnit_Framework_MockObject_MockObject */
  63. private $secureRandom;
  64. /** @var IMailer | \PHPUnit_Framework_MockObject_MockObject */
  65. private $mailer;
  66. /** @var IURLGenerator | \PHPUnit_Framework_MockObject_MockObject */
  67. private $urlGenerator;
  68. /** @var IShare | \PHPUnit_Framework_MockObject_MockObject */
  69. private $share;
  70. /** @var \OCP\Activity\IManager | \PHPUnit_Framework_MockObject_MockObject */
  71. private $activityManager;
  72. /** @var SettingsManager | \PHPUnit_Framework_MockObject_MockObject */
  73. private $settingsManager;
  74. /** @var Defaults|\PHPUnit_Framework_MockObject_MockObject */
  75. private $defaults;
  76. /** @var IHasher | \PHPUnit_Framework_MockObject_MockObject */
  77. private $hasher;
  78. /** @var CapabilitiesManager | \PHPUnit_Framework_MockObject_MockObject */
  79. private $capabilitiesManager;
  80. public function setUp() {
  81. parent::setUp();
  82. $this->shareManager = \OC::$server->getShareManager();
  83. $this->connection = \OC::$server->getDatabaseConnection();
  84. $this->l = $this->getMockBuilder(IL10N::class)->getMock();
  85. $this->l->method('t')
  86. ->will($this->returnCallback(function($text, $parameters = []) {
  87. return vsprintf($text, $parameters);
  88. }));
  89. $this->logger = $this->getMockBuilder(ILogger::class)->getMock();
  90. $this->rootFolder = $this->getMockBuilder('OCP\Files\IRootFolder')->getMock();
  91. $this->userManager = $this->getMockBuilder(IUserManager::class)->getMock();
  92. $this->secureRandom = $this->getMockBuilder('\OCP\Security\ISecureRandom')->getMock();
  93. $this->mailer = $this->getMockBuilder('\OCP\Mail\IMailer')->getMock();
  94. $this->urlGenerator = $this->getMockBuilder(IURLGenerator::class)->getMock();
  95. $this->share = $this->getMockBuilder(IShare::class)->getMock();
  96. $this->activityManager = $this->getMockBuilder('OCP\Activity\IManager')->getMock();
  97. $this->settingsManager = $this->getMockBuilder(SettingsManager::class)->disableOriginalConstructor()->getMock();
  98. $this->defaults = $this->createMock(Defaults::class);
  99. $this->hasher = $this->getMockBuilder(IHasher::class)->getMock();
  100. $this->capabilitiesManager = $this->getMockBuilder(CapabilitiesManager::class)->disableOriginalConstructor()->getMock();
  101. $this->userManager->expects($this->any())->method('userExists')->willReturn(true);
  102. }
  103. /**
  104. * get instance of Mocked ShareByMailProvider
  105. *
  106. * @param array $mockedMethods internal methods which should be mocked
  107. * @return \PHPUnit_Framework_MockObject_MockObject | ShareByMailProvider
  108. */
  109. private function getInstance(array $mockedMethods = []) {
  110. $instance = $this->getMockBuilder('OCA\ShareByMail\ShareByMailProvider')
  111. ->setConstructorArgs(
  112. [
  113. $this->connection,
  114. $this->secureRandom,
  115. $this->userManager,
  116. $this->rootFolder,
  117. $this->l,
  118. $this->logger,
  119. $this->mailer,
  120. $this->urlGenerator,
  121. $this->activityManager,
  122. $this->settingsManager,
  123. $this->defaults,
  124. $this->hasher,
  125. $this->capabilitiesManager
  126. ]
  127. );
  128. if (!empty($mockedMethods)) {
  129. $instance->setMethods($mockedMethods);
  130. return $instance->getMock();
  131. }
  132. return new ShareByMailProvider(
  133. $this->connection,
  134. $this->secureRandom,
  135. $this->userManager,
  136. $this->rootFolder,
  137. $this->l,
  138. $this->logger,
  139. $this->mailer,
  140. $this->urlGenerator,
  141. $this->activityManager,
  142. $this->settingsManager,
  143. $this->defaults,
  144. $this->hasher,
  145. $this->capabilitiesManager
  146. );
  147. }
  148. public function tearDown() {
  149. $this->connection->getQueryBuilder()->delete('share')->execute();
  150. return parent::tearDown();
  151. }
  152. public function testCreate() {
  153. $share = $this->getMockBuilder(IShare::class)->getMock();
  154. $share->expects($this->any())->method('getSharedWith')->willReturn('user1');
  155. $node = $this->getMockBuilder(File::class)->getMock();
  156. $node->expects($this->any())->method('getName')->willReturn('filename');
  157. $instance = $this->getInstance(['getSharedWith', 'createMailShare', 'getRawShare', 'createShareObject', 'createShareActivity', 'sendPassword']);
  158. $instance->expects($this->once())->method('getSharedWith')->willReturn([]);
  159. $instance->expects($this->once())->method('createMailShare')->with($share)->willReturn(42);
  160. $instance->expects($this->once())->method('createShareActivity')->with($share);
  161. $instance->expects($this->once())->method('getRawShare')->with(42)->willReturn('rawShare');
  162. $instance->expects($this->once())->method('createShareObject')->with('rawShare')->willReturn('shareObject');
  163. $instance->expects($this->any())->method('sendPassword')->willReturn(true);
  164. $share->expects($this->any())->method('getNode')->willReturn($node);
  165. $this->settingsManager->expects($this->any())->method('enforcePasswordProtection')->willReturn(false);
  166. $this->settingsManager->expects($this->any())->method('sendPasswordByMail')->willReturn(true);
  167. $this->assertSame('shareObject',
  168. $instance->create($share)
  169. );
  170. }
  171. public function testCreateSendPasswordByMailWithoutEnforcedPasswordProtection() {
  172. $share = $this->getMockBuilder(IShare::class)->getMock();
  173. $share->expects($this->any())->method('getSharedWith')->willReturn('receiver@example.com');
  174. $share->expects($this->any())->method('getSendPasswordByTalk')->willReturn(false);
  175. $share->expects($this->any())->method('getSharedBy')->willReturn('owner');
  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', 'autoGeneratePassword', 'createPasswordSendActivity']);
  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. $share->expects($this->any())->method('getNode')->willReturn($node);
  185. // The autogenerated password should not be mailed.
  186. $this->settingsManager->expects($this->any())->method('enforcePasswordProtection')->willReturn(false);
  187. $this->settingsManager->expects($this->any())->method('sendPasswordByMail')->willReturn(true);
  188. $instance->expects($this->never())->method('autoGeneratePassword');
  189. $this->mailer->expects($this->never())->method('send');
  190. $this->assertSame('shareObject',
  191. $instance->create($share)
  192. );
  193. }
  194. public function testCreateSendPasswordByMailWithEnforcedPasswordProtection() {
  195. $share = $this->getMockBuilder(IShare::class)->getMock();
  196. $share->expects($this->any())->method('getSharedWith')->willReturn('receiver@example.com');
  197. $share->expects($this->any())->method('getSendPasswordByTalk')->willReturn(false);
  198. $share->expects($this->any())->method('getSharedBy')->willReturn('owner');
  199. $node = $this->getMockBuilder(File::class)->getMock();
  200. $node->expects($this->any())->method('getName')->willReturn('filename');
  201. $instance = $this->getInstance(['getSharedWith', 'createMailShare', 'getRawShare', 'createShareObject', 'createShareActivity', 'autoGeneratePassword', 'createPasswordSendActivity']);
  202. $instance->expects($this->once())->method('getSharedWith')->willReturn([]);
  203. $instance->expects($this->once())->method('createMailShare')->with($share)->willReturn(42);
  204. $instance->expects($this->once())->method('createShareActivity')->with($share);
  205. $instance->expects($this->once())->method('getRawShare')->with(42)->willReturn('rawShare');
  206. $instance->expects($this->once())->method('createShareObject')->with('rawShare')->willReturn('shareObject');
  207. $share->expects($this->any())->method('getNode')->willReturn($node);
  208. // The autogenerated password should be mailed to the receiver of the share.
  209. $this->settingsManager->expects($this->any())->method('enforcePasswordProtection')->willReturn(true);
  210. $this->settingsManager->expects($this->any())->method('sendPasswordByMail')->willReturn(true);
  211. $instance->expects($this->once())->method('autoGeneratePassword')->with($share)->willReturn('password');
  212. $message = $this->createMock(IMessage::class);
  213. $message->expects($this->once())->method('setTo')->with(['receiver@example.com']);
  214. $this->mailer->expects($this->once())->method('createMessage')->willReturn($message);
  215. $this->mailer->expects($this->once())->method('send');
  216. $this->assertSame('shareObject',
  217. $instance->create($share)
  218. );
  219. }
  220. public function testCreateSendPasswordByTalkWithEnforcedPasswordProtection() {
  221. $share = $this->getMockBuilder(IShare::class)->getMock();
  222. $share->expects($this->any())->method('getSharedWith')->willReturn('receiver@example.com');
  223. $share->expects($this->any())->method('getSendPasswordByTalk')->willReturn(true);
  224. $share->expects($this->any())->method('getSharedBy')->willReturn('owner');
  225. $node = $this->getMockBuilder(File::class)->getMock();
  226. $node->expects($this->any())->method('getName')->willReturn('filename');
  227. $instance = $this->getInstance(['getSharedWith', 'createMailShare', 'getRawShare', 'createShareObject', 'createShareActivity', 'autoGeneratePassword', 'createPasswordSendActivity']);
  228. $instance->expects($this->once())->method('getSharedWith')->willReturn([]);
  229. $instance->expects($this->once())->method('createMailShare')->with($share)->willReturn(42);
  230. $instance->expects($this->once())->method('createShareActivity')->with($share);
  231. $instance->expects($this->once())->method('getRawShare')->with(42)->willReturn('rawShare');
  232. $instance->expects($this->once())->method('createShareObject')->with('rawShare')->willReturn('shareObject');
  233. $share->expects($this->any())->method('getNode')->willReturn($node);
  234. // The autogenerated password should be mailed to the owner of the share.
  235. $this->settingsManager->expects($this->any())->method('enforcePasswordProtection')->willReturn(true);
  236. $this->settingsManager->expects($this->any())->method('sendPasswordByMail')->willReturn(true);
  237. $instance->expects($this->once())->method('autoGeneratePassword')->with($share)->willReturn('password');
  238. $message = $this->createMock(IMessage::class);
  239. $message->expects($this->once())->method('setTo')->with(['owner@example.com' => 'Owner display name']);
  240. $this->mailer->expects($this->once())->method('createMessage')->willReturn($message);
  241. $this->mailer->expects($this->once())->method('send');
  242. $user = $this->createMock(IUser::class);
  243. $this->userManager->expects($this->once())->method('get')->with('owner')->willReturn($user);
  244. $user->expects($this->once())->method('getDisplayName')->willReturn('Owner display name');
  245. $user->expects($this->once())->method('getEMailAddress')->willReturn('owner@example.com');
  246. $this->assertSame('shareObject',
  247. $instance->create($share)
  248. );
  249. }
  250. /**
  251. * @expectedException \Exception
  252. */
  253. public function testCreateFailed() {
  254. $this->share->expects($this->once())->method('getSharedWith')->willReturn('user1');
  255. $node = $this->getMockBuilder('OCP\Files\Node')->getMock();
  256. $node->expects($this->any())->method('getName')->willReturn('fileName');
  257. $this->share->expects($this->any())->method('getNode')->willReturn($node);
  258. $instance = $this->getInstance(['getSharedWith', 'createMailShare', 'getRawShare', 'createShareObject']);
  259. $instance->expects($this->once())->method('getSharedWith')->willReturn(['found']);
  260. $instance->expects($this->never())->method('createMailShare');
  261. $instance->expects($this->never())->method('getRawShare');
  262. $instance->expects($this->never())->method('createShareObject');
  263. $this->assertSame('shareObject',
  264. $instance->create($this->share)
  265. );
  266. }
  267. public function testCreateMailShare() {
  268. $this->share->expects($this->any())->method('getToken')->willReturn('token');
  269. $this->share->expects($this->once())->method('setToken')->with('token');
  270. $node = $this->getMockBuilder('OCP\Files\Node')->getMock();
  271. $node->expects($this->any())->method('getName')->willReturn('fileName');
  272. $this->share->expects($this->any())->method('getNode')->willReturn($node);
  273. $instance = $this->getInstance(['generateToken', 'addShareToDB', 'sendMailNotification']);
  274. $instance->expects($this->once())->method('generateToken')->willReturn('token');
  275. $instance->expects($this->once())->method('addShareToDB')->willReturn(42);
  276. $instance->expects($this->once())->method('sendMailNotification');
  277. $this->urlGenerator->expects($this->once())->method('linkToRouteAbsolute')
  278. ->with('files_sharing.sharecontroller.showShare', ['token' => 'token']);
  279. $instance->expects($this->once())->method('sendMailNotification');
  280. $this->assertSame(42,
  281. $this->invokePrivate($instance, 'createMailShare', [$this->share])
  282. );
  283. }
  284. /**
  285. * @expectedException \OC\HintException
  286. */
  287. public function testCreateMailShareFailed() {
  288. $this->share->expects($this->any())->method('getToken')->willReturn('token');
  289. $this->share->expects($this->once())->method('setToken')->with('token');
  290. $node = $this->getMockBuilder('OCP\Files\Node')->getMock();
  291. $node->expects($this->any())->method('getName')->willReturn('fileName');
  292. $this->share->expects($this->any())->method('getNode')->willReturn($node);
  293. $instance = $this->getInstance(['generateToken', 'addShareToDB', 'sendMailNotification']);
  294. $instance->expects($this->once())->method('generateToken')->willReturn('token');
  295. $instance->expects($this->once())->method('addShareToDB')->willReturn(42);
  296. $instance->expects($this->once())->method('sendMailNotification');
  297. $this->urlGenerator->expects($this->once())->method('linkToRouteAbsolute')
  298. ->with('files_sharing.sharecontroller.showShare', ['token' => 'token']);
  299. $instance->expects($this->once())->method('sendMailNotification')
  300. ->willReturnCallback(
  301. function() {
  302. throw new \Exception('should be converted to a hint exception');
  303. }
  304. );
  305. $this->assertSame(42,
  306. $this->invokePrivate($instance, 'createMailShare', [$this->share])
  307. );
  308. }
  309. public function testGenerateToken() {
  310. $instance = $this->getInstance();
  311. $this->secureRandom->expects($this->once())->method('generate')->willReturn('token');
  312. $this->assertSame('token',
  313. $this->invokePrivate($instance, 'generateToken')
  314. );
  315. }
  316. public function testAddShareToDB() {
  317. $itemSource = 11;
  318. $itemType = 'file';
  319. $shareWith = 'user@server.com';
  320. $sharedBy = 'user1';
  321. $uidOwner = 'user2';
  322. $permissions = 1;
  323. $token = 'token';
  324. $password = 'password';
  325. $sendPasswordByTalk = true;
  326. $instance = $this->getInstance();
  327. $id = $this->invokePrivate(
  328. $instance,
  329. 'addShareToDB',
  330. [
  331. $itemSource,
  332. $itemType,
  333. $shareWith,
  334. $sharedBy,
  335. $uidOwner,
  336. $permissions,
  337. $token,
  338. $password,
  339. $sendPasswordByTalk
  340. ]
  341. );
  342. $qb = $this->connection->getQueryBuilder();
  343. $result = $qb->select('*')
  344. ->from('share')
  345. ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
  346. ->execute()->fetchAll();
  347. $this->assertSame(1, count($result));
  348. $this->assertSame($itemSource, (int)$result[0]['item_source']);
  349. $this->assertSame($itemType, $result[0]['item_type']);
  350. $this->assertSame($shareWith, $result[0]['share_with']);
  351. $this->assertSame($sharedBy, $result[0]['uid_initiator']);
  352. $this->assertSame($uidOwner, $result[0]['uid_owner']);
  353. $this->assertSame($permissions, (int)$result[0]['permissions']);
  354. $this->assertSame($token, $result[0]['token']);
  355. $this->assertSame($password, $result[0]['password']);
  356. $this->assertSame($sendPasswordByTalk, (bool)$result[0]['password_by_talk']);
  357. }
  358. public function testUpdate() {
  359. $itemSource = 11;
  360. $itemType = 'file';
  361. $shareWith = 'user@server.com';
  362. $sharedBy = 'user1';
  363. $uidOwner = 'user2';
  364. $permissions = 1;
  365. $token = 'token';
  366. $note = 'personal note';
  367. $instance = $this->getInstance();
  368. $id = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token, $note);
  369. $this->share->expects($this->once())->method('getPermissions')->willReturn($permissions + 1);
  370. $this->share->expects($this->once())->method('getShareOwner')->willReturn($uidOwner);
  371. $this->share->expects($this->once())->method('getSharedBy')->willReturn($sharedBy);
  372. $this->share->expects($this->any())->method('getNote')->willReturn($note);
  373. $this->share->expects($this->atLeastOnce())->method('getId')->willReturn($id);
  374. $this->assertSame($this->share,
  375. $instance->update($this->share)
  376. );
  377. $qb = $this->connection->getQueryBuilder();
  378. $result = $qb->select('*')
  379. ->from('share')
  380. ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
  381. ->execute()->fetchAll();
  382. $this->assertSame(1, count($result));
  383. $this->assertSame($itemSource, (int)$result[0]['item_source']);
  384. $this->assertSame($itemType, $result[0]['item_type']);
  385. $this->assertSame($shareWith, $result[0]['share_with']);
  386. $this->assertSame($sharedBy, $result[0]['uid_initiator']);
  387. $this->assertSame($uidOwner, $result[0]['uid_owner']);
  388. $this->assertSame($permissions + 1, (int)$result[0]['permissions']);
  389. $this->assertSame($token, $result[0]['token']);
  390. $this->assertSame($note, $result[0]['note']);
  391. }
  392. public function dataUpdateSendPassword() {
  393. return [
  394. ['password', 'hashed', 'hashed new', false, false, true],
  395. ['', 'hashed', 'hashed new', false, false, false],
  396. [null, 'hashed', 'hashed new', false, false, false],
  397. ['password', 'hashed', 'hashed', false, false, false],
  398. ['password', 'hashed', 'hashed new', false, true, false],
  399. ['password', 'hashed', 'hashed new', true, false, true],
  400. ['password', 'hashed', 'hashed', true, false, true],
  401. ];
  402. }
  403. /**
  404. * @dataProvider dataUpdateSendPassword
  405. *
  406. * @param string|null plainTextPassword
  407. * @param string originalPassword
  408. * @param string newPassword
  409. * @param string originalSendPasswordByTalk
  410. * @param string newSendPasswordByTalk
  411. * @param bool sendMail
  412. */
  413. public function testUpdateSendPassword($plainTextPassword, string $originalPassword, string $newPassword, string $originalSendPasswordByTalk, string $newSendPasswordByTalk, bool $sendMail) {
  414. $node = $this->getMockBuilder(File::class)->getMock();
  415. $node->expects($this->any())->method('getName')->willReturn('filename');
  416. $originalShare = $this->getMockBuilder(IShare::class)->getMock();
  417. $originalShare->expects($this->any())->method('getSharedWith')->willReturn('receiver@example.com');
  418. $originalShare->expects($this->any())->method('getNode')->willReturn($node);
  419. $originalShare->expects($this->any())->method('getId')->willReturn(42);
  420. $originalShare->expects($this->any())->method('getPassword')->willReturn($originalPassword);
  421. $originalShare->expects($this->any())->method('getSendPasswordByTalk')->willReturn($originalSendPasswordByTalk);
  422. $share = $this->getMockBuilder(IShare::class)->getMock();
  423. $share->expects($this->any())->method('getSharedWith')->willReturn('receiver@example.com');
  424. $share->expects($this->any())->method('getNode')->willReturn($node);
  425. $share->expects($this->any())->method('getId')->willReturn(42);
  426. $share->expects($this->any())->method('getPassword')->willReturn($newPassword);
  427. $share->expects($this->any())->method('getSendPasswordByTalk')->willReturn($newSendPasswordByTalk);
  428. if ($sendMail) {
  429. $this->mailer->expects($this->once())->method('send');
  430. } else {
  431. $this->mailer->expects($this->never())->method('send');
  432. }
  433. $instance = $this->getInstance(['getShareById', 'createPasswordSendActivity']);
  434. $instance->expects($this->once())->method('getShareById')->willReturn($originalShare);
  435. $this->assertSame($share,
  436. $instance->update($share, $plainTextPassword)
  437. );
  438. }
  439. public function testDelete() {
  440. $instance = $this->getInstance(['removeShareFromTable', 'createShareActivity']);
  441. $this->share->expects($this->once())->method('getId')->willReturn(42);
  442. $instance->expects($this->once())->method('removeShareFromTable')->with(42);
  443. $instance->expects($this->once())->method('createShareActivity')->with($this->share, 'unshare');
  444. $instance->delete($this->share);
  445. }
  446. public function testGetShareById() {
  447. $instance = $this->getInstance(['createShareObject']);
  448. $itemSource = 11;
  449. $itemType = 'file';
  450. $shareWith = 'user@server.com';
  451. $sharedBy = 'user1';
  452. $uidOwner = 'user2';
  453. $permissions = 1;
  454. $token = 'token';
  455. $this->createDummyShare($itemType, $itemSource, $shareWith, "user1wrong", "user2wrong", $permissions, $token);
  456. $id2 = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  457. $instance->expects($this->once())->method('createShareObject')
  458. ->willReturnCallback(
  459. function ($data) use ($uidOwner, $sharedBy, $id2) {
  460. $this->assertSame($uidOwner, $data['uid_owner']);
  461. $this->assertSame($sharedBy, $data['uid_initiator']);
  462. $this->assertSame($id2, (int)$data['id']);
  463. return $this->share;
  464. }
  465. );
  466. $result = $instance->getShareById($id2);
  467. $this->assertInstanceOf('OCP\Share\IShare', $result);
  468. }
  469. /**
  470. * @expectedException \OCP\Share\Exceptions\ShareNotFound
  471. */
  472. public function testGetShareByIdFailed() {
  473. $instance = $this->getInstance(['createShareObject']);
  474. $itemSource = 11;
  475. $itemType = 'file';
  476. $shareWith = 'user@server.com';
  477. $sharedBy = 'user1';
  478. $uidOwner = 'user2';
  479. $permissions = 1;
  480. $token = 'token';
  481. $id = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  482. $instance->getShareById($id+1);
  483. }
  484. public function testGetShareByPath() {
  485. $itemSource = 11;
  486. $itemType = 'file';
  487. $shareWith = 'user@server.com';
  488. $sharedBy = 'user1';
  489. $uidOwner = 'user2';
  490. $permissions = 1;
  491. $token = 'token';
  492. $node = $this->getMockBuilder('OCP\Files\Node')->getMock();
  493. $node->expects($this->once())->method('getId')->willReturn($itemSource);
  494. $instance = $this->getInstance(['createShareObject']);
  495. $this->createDummyShare($itemType, 111, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  496. $id = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  497. $instance->expects($this->once())->method('createShareObject')
  498. ->willReturnCallback(
  499. function ($data) use ($uidOwner, $sharedBy, $id) {
  500. $this->assertSame($uidOwner, $data['uid_owner']);
  501. $this->assertSame($sharedBy, $data['uid_initiator']);
  502. $this->assertSame($id, (int)$data['id']);
  503. return $this->share;
  504. }
  505. );
  506. $result = $instance->getSharesByPath($node);
  507. $this->assertTrue(is_array($result));
  508. $this->assertSame(1, count($result));
  509. $this->assertInstanceOf('OCP\Share\IShare', $result[0]);
  510. }
  511. public function testGetShareByToken() {
  512. $itemSource = 11;
  513. $itemType = 'file';
  514. $shareWith = 'user@server.com';
  515. $sharedBy = 'user1';
  516. $uidOwner = 'user2';
  517. $permissions = 1;
  518. $token = 'token';
  519. $instance = $this->getInstance(['createShareObject']);
  520. $idMail = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  521. $idPublic = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token, '', \OCP\Share::SHARE_TYPE_LINK);
  522. $this->assertTrue($idMail !== $idPublic);
  523. $instance->expects($this->once())->method('createShareObject')
  524. ->willReturnCallback(
  525. function ($data) use ($idMail) {
  526. $this->assertSame($idMail, (int)$data['id']);
  527. return $this->share;
  528. }
  529. );
  530. $result = $instance->getShareByToken('token');
  531. $this->assertInstanceOf('OCP\Share\IShare', $result);
  532. }
  533. /**
  534. * @expectedException \OCP\Share\Exceptions\ShareNotFound
  535. */
  536. public function testGetShareByTokenFailed() {
  537. $itemSource = 11;
  538. $itemType = 'file';
  539. $shareWith = 'user@server.com';
  540. $sharedBy = 'user1';
  541. $uidOwner = 'user2';
  542. $permissions = 1;
  543. $token = 'token';
  544. $instance = $this->getInstance(['createShareObject']);
  545. $idMail = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  546. $idPublic = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, "token2", '', \OCP\Share::SHARE_TYPE_LINK);
  547. $this->assertTrue($idMail !== $idPublic);
  548. $this->assertInstanceOf('OCP\Share\IShare',
  549. $instance->getShareByToken('token2')
  550. );
  551. }
  552. public function testRemoveShareFromTable() {
  553. $itemSource = 11;
  554. $itemType = 'file';
  555. $shareWith = 'user@server.com';
  556. $sharedBy = 'user1';
  557. $uidOwner = 'user2';
  558. $permissions = 1;
  559. $token = 'token';
  560. $instance = $this->getInstance();
  561. $id = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  562. $query = $this->connection->getQueryBuilder();
  563. $query->select('*')->from('share')
  564. ->where($query->expr()->eq('id', $query->createNamedParameter($id)));
  565. $before = $query->execute()->fetchAll();
  566. $this->assertTrue(is_array($before));
  567. $this->assertSame(1, count($before));
  568. $this->invokePrivate($instance, 'removeShareFromTable', [$id]);
  569. $query = $this->connection->getQueryBuilder();
  570. $query->select('*')->from('share')
  571. ->where($query->expr()->eq('id', $query->createNamedParameter($id)));
  572. $after = $query->execute()->fetchAll();
  573. $this->assertTrue(is_array($after));
  574. $this->assertEmpty($after);
  575. }
  576. public function testUserDeleted() {
  577. $itemSource = 11;
  578. $itemType = 'file';
  579. $shareWith = 'user@server.com';
  580. $sharedBy = 'user1';
  581. $uidOwner = 'user2';
  582. $permissions = 1;
  583. $token = 'token';
  584. $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  585. $id = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, 'user2Wrong', $permissions, $token);
  586. $query = $this->connection->getQueryBuilder();
  587. $query->select('*')->from('share');
  588. $before = $query->execute()->fetchAll();
  589. $this->assertTrue(is_array($before));
  590. $this->assertSame(2, count($before));
  591. $instance = $this->getInstance();
  592. $instance->userDeleted($uidOwner, \OCP\Share::SHARE_TYPE_EMAIL);
  593. $query = $this->connection->getQueryBuilder();
  594. $query->select('*')->from('share');
  595. $after = $query->execute()->fetchAll();
  596. $this->assertTrue(is_array($after));
  597. $this->assertSame(1, count($after));
  598. $this->assertSame($id, (int)$after[0]['id']);
  599. }
  600. public function testGetRawShare() {
  601. $itemSource = 11;
  602. $itemType = 'file';
  603. $shareWith = 'user@server.com';
  604. $sharedBy = 'user1';
  605. $uidOwner = 'user2';
  606. $permissions = 1;
  607. $token = 'token';
  608. $instance = $this->getInstance();
  609. $id = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  610. $result = $this->invokePrivate($instance, 'getRawShare', [$id]);
  611. $this->assertTrue(is_array($result));
  612. $this->assertSame($itemSource, (int)$result['item_source']);
  613. $this->assertSame($itemType, $result['item_type']);
  614. $this->assertSame($shareWith, $result['share_with']);
  615. $this->assertSame($sharedBy, $result['uid_initiator']);
  616. $this->assertSame($uidOwner, $result['uid_owner']);
  617. $this->assertSame($permissions, (int)$result['permissions']);
  618. $this->assertSame($token, $result['token']);
  619. }
  620. /**
  621. * @expectedException \OCP\Share\Exceptions\ShareNotFound
  622. */
  623. public function testGetRawShareFailed() {
  624. $itemSource = 11;
  625. $itemType = 'file';
  626. $shareWith = 'user@server.com';
  627. $sharedBy = 'user1';
  628. $uidOwner = 'user2';
  629. $permissions = 1;
  630. $token = 'token';
  631. $instance = $this->getInstance();
  632. $id = $this->createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
  633. $this->invokePrivate($instance, 'getRawShare', [$id+1]);
  634. }
  635. private function createDummyShare($itemType, $itemSource, $shareWith, $sharedBy, $uidOwner, $permissions, $token, $note='', $shareType = \OCP\Share::SHARE_TYPE_EMAIL) {
  636. $qb = $this->connection->getQueryBuilder();
  637. $qb->insert('share')
  638. ->setValue('share_type', $qb->createNamedParameter($shareType))
  639. ->setValue('item_type', $qb->createNamedParameter($itemType))
  640. ->setValue('item_source', $qb->createNamedParameter($itemSource))
  641. ->setValue('file_source', $qb->createNamedParameter($itemSource))
  642. ->setValue('share_with', $qb->createNamedParameter($shareWith))
  643. ->setValue('uid_owner', $qb->createNamedParameter($uidOwner))
  644. ->setValue('uid_initiator', $qb->createNamedParameter($sharedBy))
  645. ->setValue('permissions', $qb->createNamedParameter($permissions))
  646. ->setValue('token', $qb->createNamedParameter($token))
  647. ->setValue('note', $qb->createNamedParameter($note))
  648. ->setValue('stime', $qb->createNamedParameter(time()));
  649. /*
  650. * Added to fix https://github.com/owncloud/core/issues/22215
  651. * Can be removed once we get rid of ajax/share.php
  652. */
  653. $qb->setValue('file_target', $qb->createNamedParameter(''));
  654. $qb->execute();
  655. $id = $qb->getLastInsertId();
  656. return (int)$id;
  657. }
  658. public function testGetSharesInFolder() {
  659. $userManager = \OC::$server->getUserManager();
  660. $rootFolder = \OC::$server->getRootFolder();
  661. $provider = $this->getInstance(['sendMailNotification', 'createShareActivity']);
  662. $u1 = $userManager->createUser('testFed', md5(time()));
  663. $u2 = $userManager->createUser('testFed2', md5(time()));
  664. $folder1 = $rootFolder->getUserFolder($u1->getUID())->newFolder('foo');
  665. $file1 = $folder1->newFile('bar1');
  666. $file2 = $folder1->newFile('bar2');
  667. $share1 = $this->shareManager->newShare();
  668. $share1->setSharedWith('user@server.com')
  669. ->setSharedBy($u1->getUID())
  670. ->setShareOwner($u1->getUID())
  671. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  672. ->setNode($file1);
  673. $provider->create($share1);
  674. $share2 = $this->shareManager->newShare();
  675. $share2->setSharedWith('user@server.com')
  676. ->setSharedBy($u2->getUID())
  677. ->setShareOwner($u1->getUID())
  678. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  679. ->setNode($file2);
  680. $provider->create($share2);
  681. $result = $provider->getSharesInFolder($u1->getUID(), $folder1, false);
  682. $this->assertCount(1, $result);
  683. $this->assertCount(1, $result[$file1->getId()]);
  684. $result = $provider->getSharesInFolder($u1->getUID(), $folder1, true);
  685. $this->assertCount(2, $result);
  686. $this->assertCount(1, $result[$file1->getId()]);
  687. $this->assertCount(1, $result[$file2->getId()]);
  688. $u1->delete();
  689. $u2->delete();
  690. }
  691. public function testGetAccessList() {
  692. $userManager = \OC::$server->getUserManager();
  693. $rootFolder = \OC::$server->getRootFolder();
  694. $provider = $this->getInstance(['sendMailNotification', 'createShareActivity']);
  695. $u1 = $userManager->createUser('testFed', md5(time()));
  696. $u2 = $userManager->createUser('testFed2', md5(time()));
  697. $folder = $rootFolder->getUserFolder($u1->getUID())->newFolder('foo');
  698. $accessList = $provider->getAccessList([$folder], true);
  699. $this->assertArrayHasKey('public', $accessList);
  700. $this->assertFalse($accessList['public']);
  701. $accessList = $provider->getAccessList([$folder], false);
  702. $this->assertArrayHasKey('public', $accessList);
  703. $this->assertFalse($accessList['public']);
  704. $share1 = $this->shareManager->newShare();
  705. $share1->setSharedWith('user@server.com')
  706. ->setSharedBy($u1->getUID())
  707. ->setShareOwner($u1->getUID())
  708. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  709. ->setNode($folder);
  710. $share1 = $provider->create($share1);
  711. $share2 = $this->shareManager->newShare();
  712. $share2->setSharedWith('user2@server.com')
  713. ->setSharedBy($u2->getUID())
  714. ->setShareOwner($u1->getUID())
  715. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  716. ->setNode($folder);
  717. $share2 = $provider->create($share2);
  718. $accessList = $provider->getAccessList([$folder], true);
  719. $this->assertArrayHasKey('public', $accessList);
  720. $this->assertTrue($accessList['public']);
  721. $accessList = $provider->getAccessList([$folder], false);
  722. $this->assertArrayHasKey('public', $accessList);
  723. $this->assertTrue($accessList['public']);
  724. $provider->delete($share2);
  725. $accessList = $provider->getAccessList([$folder], true);
  726. $this->assertArrayHasKey('public', $accessList);
  727. $this->assertTrue($accessList['public']);
  728. $accessList = $provider->getAccessList([$folder], false);
  729. $this->assertArrayHasKey('public', $accessList);
  730. $this->assertTrue($accessList['public']);
  731. $provider->delete($share1);
  732. $accessList = $provider->getAccessList([$folder], true);
  733. $this->assertArrayHasKey('public', $accessList);
  734. $this->assertFalse($accessList['public']);
  735. $accessList = $provider->getAccessList([$folder], false);
  736. $this->assertArrayHasKey('public', $accessList);
  737. $this->assertFalse($accessList['public']);
  738. $u1->delete();
  739. $u2->delete();
  740. }
  741. public function testSendMailNotificationWithSameUserAndUserEmail() {
  742. $provider = $this->getInstance();
  743. $user = $this->createMock(IUser::class);
  744. $this->userManager
  745. ->expects($this->once())
  746. ->method('get')
  747. ->with('OwnerUser')
  748. ->willReturn($user);
  749. $user
  750. ->expects($this->once())
  751. ->method('getDisplayName')
  752. ->willReturn('Mrs. Owner User');
  753. $message = $this->createMock(Message::class);
  754. $this->mailer
  755. ->expects($this->once())
  756. ->method('createMessage')
  757. ->willReturn($message);
  758. $template = $this->createMock(IEMailTemplate::class);
  759. $this->mailer
  760. ->expects($this->once())
  761. ->method('createEMailTemplate')
  762. ->willReturn($template);
  763. $template
  764. ->expects($this->once())
  765. ->method('addHeader');
  766. $template
  767. ->expects($this->once())
  768. ->method('addHeading')
  769. ->with('Mrs. Owner User shared »file.txt« with you');
  770. $template
  771. ->expects($this->once())
  772. ->method('addBodyText')
  773. ->with(
  774. 'Mrs. Owner User shared »file.txt« with you. Click the button below to open it.',
  775. 'Mrs. Owner User shared »file.txt« with you.'
  776. );
  777. $template
  778. ->expects($this->once())
  779. ->method('addBodyButton')
  780. ->with(
  781. 'Open »file.txt«',
  782. 'https://example.com/file.txt'
  783. );
  784. $message
  785. ->expects($this->once())
  786. ->method('setTo')
  787. ->with(['john@doe.com']);
  788. $this->defaults
  789. ->expects($this->once())
  790. ->method('getName')
  791. ->willReturn('UnitTestCloud');
  792. $message
  793. ->expects($this->once())
  794. ->method('setFrom')
  795. ->with([
  796. \OCP\Util::getDefaultEmailAddress('UnitTestCloud') => 'Mrs. Owner User via UnitTestCloud'
  797. ]);
  798. $user
  799. ->expects($this->once())
  800. ->method('getEMailAddress')
  801. ->willReturn('owner@example.com');
  802. $message
  803. ->expects($this->once())
  804. ->method('setReplyTo')
  805. ->with(['owner@example.com' => 'Mrs. Owner User']);
  806. $this->defaults
  807. ->expects($this->exactly(2))
  808. ->method('getSlogan')
  809. ->willReturn('Testing like 1990');
  810. $template
  811. ->expects($this->once())
  812. ->method('addFooter')
  813. ->with('UnitTestCloud - Testing like 1990');
  814. $template
  815. ->expects($this->once())
  816. ->method('setSubject')
  817. ->with('Mrs. Owner User shared »file.txt« with you');
  818. $message
  819. ->expects($this->once())
  820. ->method('useTemplate')
  821. ->with($template);
  822. $this->mailer
  823. ->expects($this->once())
  824. ->method('send')
  825. ->with($message);
  826. self::invokePrivate(
  827. $provider,
  828. 'sendMailNotification',
  829. [
  830. 'file.txt',
  831. 'https://example.com/file.txt',
  832. 'OwnerUser',
  833. 'john@doe.com',
  834. null,
  835. ]);
  836. }
  837. public function testSendMailNotificationWithDifferentUserAndNoUserEmail() {
  838. $provider = $this->getInstance();
  839. $initiatorUser = $this->createMock(IUser::class);
  840. $this->userManager
  841. ->expects($this->once())
  842. ->method('get')
  843. ->with('InitiatorUser')
  844. ->willReturn($initiatorUser);
  845. $initiatorUser
  846. ->expects($this->once())
  847. ->method('getDisplayName')
  848. ->willReturn('Mr. Initiator User');
  849. $message = $this->createMock(Message::class);
  850. $this->mailer
  851. ->expects($this->once())
  852. ->method('createMessage')
  853. ->willReturn($message);
  854. $template = $this->createMock(IEMailTemplate::class);
  855. $this->mailer
  856. ->expects($this->once())
  857. ->method('createEMailTemplate')
  858. ->willReturn($template);
  859. $template
  860. ->expects($this->once())
  861. ->method('addHeader');
  862. $template
  863. ->expects($this->once())
  864. ->method('addHeading')
  865. ->with('Mr. Initiator User shared »file.txt« with you');
  866. $template
  867. ->expects($this->once())
  868. ->method('addBodyText')
  869. ->with(
  870. 'Mr. Initiator User shared »file.txt« with you. Click the button below to open it.',
  871. 'Mr. Initiator User shared »file.txt« with you.'
  872. );
  873. $template
  874. ->expects($this->once())
  875. ->method('addBodyButton')
  876. ->with(
  877. 'Open »file.txt«',
  878. 'https://example.com/file.txt'
  879. );
  880. $message
  881. ->expects($this->once())
  882. ->method('setTo')
  883. ->with(['john@doe.com']);
  884. $this->defaults
  885. ->expects($this->once())
  886. ->method('getName')
  887. ->willReturn('UnitTestCloud');
  888. $message
  889. ->expects($this->once())
  890. ->method('setFrom')
  891. ->with([
  892. \OCP\Util::getDefaultEmailAddress('UnitTestCloud') => 'Mr. Initiator User via UnitTestCloud'
  893. ]);
  894. $message
  895. ->expects($this->never())
  896. ->method('setReplyTo');
  897. $template
  898. ->expects($this->once())
  899. ->method('addFooter')
  900. ->with('');
  901. $template
  902. ->expects($this->once())
  903. ->method('setSubject')
  904. ->with('Mr. Initiator User shared »file.txt« with you');
  905. $message
  906. ->expects($this->once())
  907. ->method('useTemplate')
  908. ->with($template);
  909. $this->mailer
  910. ->expects($this->once())
  911. ->method('send')
  912. ->with($message);
  913. self::invokePrivate(
  914. $provider,
  915. 'sendMailNotification',
  916. [
  917. 'file.txt',
  918. 'https://example.com/file.txt',
  919. 'InitiatorUser',
  920. 'john@doe.com',
  921. null,
  922. ]);
  923. }
  924. }