Skip to content

Commit 182d206

Browse files
committed
feat: Use new password context in sharing API
Signed-off-by: Ferdinand Thiessen <[email protected]>
1 parent e3de860 commit 182d206

File tree

3 files changed

+55
-57
lines changed

3 files changed

+55
-57
lines changed

apps/files_sharing/lib/Controller/ShareController.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@
3333
use OCP\ISession;
3434
use OCP\IURLGenerator;
3535
use OCP\IUserManager;
36+
use OCP\Security\Events\GenerateSecurePasswordEvent;
3637
use OCP\Security\ISecureRandom;
38+
use OCP\Security\PasswordContext;
3739
use OCP\Share;
3840
use OCP\Share\Exceptions\ShareNotFound;
3941
use OCP\Share\IManager as ShareManager;
4042
use OCP\Share\IPublicShareTemplateFactory;
4143
use OCP\Share\IShare;
42-
use OCP\Template;
4344

4445
/**
4546
* @package OCA\Files_Sharing\Controllers
@@ -156,7 +157,7 @@ protected function validateIdentity(?string $identityToken = null): bool {
156157
* Generates a password for the share, respecting any password policy defined
157158
*/
158159
protected function generatePassword(): void {
159-
$event = new \OCP\Security\Events\GenerateSecurePasswordEvent();
160+
$event = new GenerateSecurePasswordEvent(PasswordContext::SHARING);
160161
$this->eventDispatcher->dispatchTyped($event);
161162
$password = $event->getPassword() ?? $this->secureRandom->generate(20);
162163

apps/sharebymail/lib/ShareByMailProvider.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use OCP\Security\Events\GenerateSecurePasswordEvent;
2929
use OCP\Security\IHasher;
3030
use OCP\Security\ISecureRandom;
31+
use OCP\Security\PasswordContext;
3132
use OCP\Share\Exceptions\GenericShareException;
3233
use OCP\Share\Exceptions\ShareNotFound;
3334
use OCP\Share\IAttributes;
@@ -131,7 +132,7 @@ protected function autoGeneratePassword(IShare $share): string {
131132
);
132133
}
133134

134-
$passwordEvent = new GenerateSecurePasswordEvent();
135+
$passwordEvent = new GenerateSecurePasswordEvent(PasswordContext::SHARING);
135136
$this->eventDispatcher->dispatchTyped($passwordEvent);
136137

137138
$password = $passwordEvent->getPassword();

apps/sharebymail/tests/ShareByMailProviderTest.php

Lines changed: 50 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
use OC\Mail\Message;
1010
use OCA\ShareByMail\Settings\SettingsManager;
1111
use OCA\ShareByMail\ShareByMailProvider;
12+
use OCP\Activity\IManager as IActivityManager;
1213
use OCP\Defaults;
1314
use OCP\EventDispatcher\IEventDispatcher;
1415
use OCP\Files\File;
1516
use OCP\Files\IRootFolder;
17+
use OCP\Files\Node;
1618
use OCP\IConfig;
1719
use OCP\IDBConnection;
1820
use OCP\IL10N;
@@ -25,9 +27,11 @@
2527
use OCP\Security\Events\GenerateSecurePasswordEvent;
2628
use OCP\Security\IHasher;
2729
use OCP\Security\ISecureRandom;
30+
use OCP\Security\PasswordContext;
2831
use OCP\Share\IAttributes;
2932
use OCP\Share\IManager;
3033
use OCP\Share\IShare;
34+
use PHPUnit\Framework\MockObject\MockObject;
3135
use Psr\Log\LoggerInterface;
3236
use Test\TestCase;
3337

@@ -38,65 +42,36 @@
3842
* @group DB
3943
*/
4044
class ShareByMailProviderTest extends TestCase {
41-
/** @var IConfig */
42-
private $config;
43-
44-
/** @var IDBConnection */
45-
private $connection;
46-
47-
/** @var IManager | \PHPUnit\Framework\MockObject\MockObject */
48-
private $shareManager;
49-
50-
/** @var IL10N | \PHPUnit\Framework\MockObject\MockObject */
51-
private $l;
52-
53-
/** @var LoggerInterface | \PHPUnit\Framework\MockObject\MockObject */
54-
private $logger;
55-
56-
/** @var IRootFolder | \PHPUnit\Framework\MockObject\MockObject */
57-
private $rootFolder;
58-
59-
/** @var IUserManager | \PHPUnit\Framework\MockObject\MockObject */
60-
private $userManager;
61-
62-
/** @var ISecureRandom | \PHPUnit\Framework\MockObject\MockObject */
63-
private $secureRandom;
64-
65-
/** @var IMailer | \PHPUnit\Framework\MockObject\MockObject */
66-
private $mailer;
67-
68-
/** @var IURLGenerator | \PHPUnit\Framework\MockObject\MockObject */
69-
private $urlGenerator;
70-
71-
/** @var IShare | \PHPUnit\Framework\MockObject\MockObject */
72-
private $share;
73-
74-
/** @var \OCP\Activity\IManager | \PHPUnit\Framework\MockObject\MockObject */
75-
private $activityManager;
76-
77-
/** @var SettingsManager | \PHPUnit\Framework\MockObject\MockObject */
78-
private $settingsManager;
79-
80-
/** @var Defaults|\PHPUnit\Framework\MockObject\MockObject */
81-
private $defaults;
82-
83-
/** @var IHasher | \PHPUnit\Framework\MockObject\MockObject */
84-
private $hasher;
85-
86-
/** @var IEventDispatcher */
87-
private $eventDispatcher;
45+
46+
private IDBConnection $connection;
47+
48+
private IL10N&MockObject $l;
49+
private IShare&MockObject $share;
50+
private IConfig&MockObject $config;
51+
private IMailer&MockObject $mailer;
52+
private IHasher&MockObject $hasher;
53+
private Defaults&MockObject $defaults;
54+
private IManager&MockObject $shareManager;
55+
private LoggerInterface&MockObject $logger;
56+
private IRootFolder&MockObject $rootFolder;
57+
private IUserManager&MockObject $userManager;
58+
private ISecureRandom&MockObject $secureRandom;
59+
private IURLGenerator&MockObject $urlGenerator;
60+
private SettingsManager&MockObject $settingsManager;
61+
private IActivityManager&MockObject $activityManager;
62+
private IEventDispatcher&MockObject $eventDispatcher;
8863

8964
protected function setUp(): void {
9065
parent::setUp();
9166

92-
$this->config = $this->getMockBuilder(IConfig::class)->getMock();
93-
$this->connection = \OC::$server->getDatabaseConnection();
67+
$this->connection = \OCP\Server::get(IDBConnection::class);
9468

9569
$this->l = $this->getMockBuilder(IL10N::class)->getMock();
9670
$this->l->method('t')
9771
->willReturnCallback(function ($text, $parameters = []) {
9872
return vsprintf($text, $parameters);
9973
});
74+
$this->config = $this->getMockBuilder(IConfig::class)->getMock();
10075
$this->logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
10176
$this->rootFolder = $this->getMockBuilder('OCP\Files\IRootFolder')->getMock();
10277
$this->userManager = $this->getMockBuilder(IUserManager::class)->getMock();
@@ -165,14 +140,15 @@ private function getInstance(array $mockedMethods = []) {
165140
}
166141

167142
protected function tearDown(): void {
168-
$this->connection->getQueryBuilder()->delete('share')->execute();
143+
$this->connection->getQueryBuilder()->delete('share')->executeStatement();
169144

170145
parent::tearDown();
171146
}
172147

173148
public function testCreate() {
174149
$expectedShare = $this->createMock(IShare::class);
175150

151+
/** @var IShare&MockObject */
176152
$share = $this->getMockBuilder(IShare::class)->getMock();
177153
$share->expects($this->any())->method('getSharedWith')->willReturn('user1');
178154

@@ -200,11 +176,13 @@ public function testCreate() {
200176
}
201177

202178
public function testCreateSendPasswordByMailWithoutEnforcedPasswordProtection() {
179+
/** @var IShare&MockObject */
203180
$expectedShare = $this->createMock(IShare::class);
204181

205182
$node = $this->getMockBuilder(File::class)->getMock();
206183
$node->expects($this->any())->method('getName')->willReturn('filename');
207184

185+
/** @var IShare&MockObject */
208186
$share = $this->getMockBuilder(IShare::class)->getMock();
209187
$share->expects($this->any())->method('getSharedWith')->willReturn('receiver@examplelölöl.com');
210188
$share->expects($this->any())->method('getSendPasswordByTalk')->willReturn(false);
@@ -242,11 +220,13 @@ public function testCreateSendPasswordByMailWithoutEnforcedPasswordProtection()
242220
}
243221

244222
public function testCreateSendPasswordByMailWithPasswordAndWithoutEnforcedPasswordProtectionWithPermanentPassword() {
223+
/** @var IShare&MockObject */
245224
$expectedShare = $this->createMock(IShare::class);
246225

247226
$node = $this->getMockBuilder(File::class)->getMock();
248227
$node->expects($this->any())->method('getName')->willReturn('filename');
249228

229+
/** @var IShare&MockObject */
250230
$share = $this->getMockBuilder(IShare::class)->getMock();
251231
$share->expects($this->any())->method('getSharedWith')->willReturn('[email protected]');
252232
$share->expects($this->any())->method('getSendPasswordByTalk')->willReturn(false);
@@ -288,11 +268,13 @@ public function testCreateSendPasswordByMailWithPasswordAndWithoutEnforcedPasswo
288268
}
289269

290270
public function testCreateSendPasswordByMailWithPasswordAndWithoutEnforcedPasswordProtectionWithoutPermanentPassword() {
271+
/** @var IShare&MockObject */
291272
$expectedShare = $this->createMock(IShare::class);
292273

293274
$node = $this->getMockBuilder(File::class)->getMock();
294275
$node->expects($this->any())->method('getName')->willReturn('filename');
295276

277+
/** @var IShare&MockObject */
296278
$share = $this->getMockBuilder(IShare::class)->getMock();
297279
$share->expects($this->any())->method('getSharedWith')->willReturn('[email protected]');
298280
$share->expects($this->any())->method('getSendPasswordByTalk')->willReturn(false);
@@ -305,7 +287,11 @@ public function testCreateSendPasswordByMailWithPasswordAndWithoutEnforcedPasswo
305287
// Assume the mail address is valid.
306288
$this->mailer->expects($this->any())->method('validateMailAddress')->willReturn(true);
307289

308-
$instance = $this->getInstance(['getSharedWith', 'createMailShare', 'getRawShare', 'createShareObject', 'createShareActivity', 'autoGeneratePassword', 'createPasswordSendActivity', 'sendEmail', 'sendPassword', 'sendPasswordToOwner']);
290+
$instance = $this->getInstance([
291+
'getSharedWith', 'createMailShare', 'getRawShare', 'createShareObject',
292+
'createShareActivity', 'autoGeneratePassword', 'createPasswordSendActivity',
293+
'sendEmail', 'sendPassword', 'sendPasswordToOwner',
294+
]);
309295

310296
$instance->expects($this->once())->method('getSharedWith')->willReturn([]);
311297
$instance->expects($this->once())->method('createMailShare')->with($share)->willReturn(42);
@@ -337,11 +323,13 @@ public function testCreateSendPasswordByMailWithPasswordAndWithoutEnforcedPasswo
337323
}
338324

339325
public function testCreateSendPasswordByMailWithEnforcedPasswordProtectionWithPermanentPassword() {
326+
/** @var IShare&MockObject */
340327
$expectedShare = $this->createMock(IShare::class);
341328

342329
$node = $this->getMockBuilder(File::class)->getMock();
343330
$node->expects($this->any())->method('getName')->willReturn('filename');
344331

332+
/** @var IShare&MockObject */
345333
$share = $this->getMockBuilder(IShare::class)->getMock();
346334
$share->expects($this->any())->method('getSharedWith')->willReturn('[email protected]');
347335
$share->expects($this->any())->method('getSendPasswordByTalk')->willReturn(false);
@@ -361,7 +349,7 @@ public function testCreateSendPasswordByMailWithEnforcedPasswordProtectionWithPe
361349
->willReturn('autogeneratedPassword');
362350
$this->eventDispatcher->expects($this->once())
363351
->method('dispatchTyped')
364-
->with(new GenerateSecurePasswordEvent());
352+
->with(new GenerateSecurePasswordEvent(PasswordContext::SHARING));
365353

366354
// Assume the mail address is valid.
367355
$this->mailer->expects($this->any())->method('validateMailAddress')->willReturn(true);
@@ -418,11 +406,13 @@ public function testCreateSendPasswordByMailWithEnforcedPasswordProtectionWithPe
418406
}
419407

420408
public function testCreateSendPasswordByMailWithPasswordAndWithEnforcedPasswordProtectionWithPermanentPassword() {
409+
/** @var IShare&MockObject */
421410
$expectedShare = $this->createMock(IShare::class);
422411

423412
$node = $this->getMockBuilder(File::class)->getMock();
424413
$node->expects($this->any())->method('getName')->willReturn('filename');
425414

415+
/** @var IShare&MockObject */
426416
$share = $this->getMockBuilder(IShare::class)->getMock();
427417
$share->expects($this->any())->method('getSharedWith')->willReturn('[email protected]');
428418
$share->expects($this->any())->method('getSendPasswordByTalk')->willReturn(false);
@@ -492,6 +482,7 @@ public function testCreateSendPasswordByMailWithPasswordAndWithEnforcedPasswordP
492482
}
493483

494484
public function testCreateSendPasswordByTalkWithEnforcedPasswordProtectionWithPermanentPassword() {
485+
/** @var IShare&MockObject */
495486
$expectedShare = $this->createMock(IShare::class);
496487

497488
// The owner of the share.
@@ -503,6 +494,7 @@ public function testCreateSendPasswordByTalkWithEnforcedPasswordProtectionWithPe
503494
$node = $this->getMockBuilder(File::class)->getMock();
504495
$node->expects($this->any())->method('getName')->willReturn('filename');
505496

497+
/** @var IShare&MockObject */
506498
$share = $this->getMockBuilder(IShare::class)->getMock();
507499
$share->expects($this->any())->method('getSharedWith')->willReturn('[email protected]');
508500
$share->expects($this->any())->method('getSendPasswordByTalk')->willReturn(true);
@@ -571,11 +563,13 @@ public function testCreateSendPasswordByTalkWithEnforcedPasswordProtectionWithPe
571563

572564
// If attributes is set to multiple emails, use them as BCC
573565
public function sendNotificationToMultipleEmails() {
566+
/** @var IShare&MockObject */
574567
$expectedShare = $this->createMock(IShare::class);
575568

576569
$node = $this->getMockBuilder(File::class)->getMock();
577570
$node->expects($this->any())->method('getName')->willReturn('filename');
578571

572+
/** @var IShare&MockObject */
579573
$share = $this->getMockBuilder(IShare::class)->getMock();
580574
$share->expects($this->any())->method('getSharedWith')->willReturn('');
581575
$share->expects($this->any())->method('getSendPasswordByTalk')->willReturn(false);
@@ -822,7 +816,7 @@ public function dataUpdateSendPassword() {
822816
* @param bool sendMail
823817
*/
824818
public function testUpdateSendPassword($plainTextPassword, string $originalPassword, string $newPassword, $originalSendPasswordByTalk, $newSendPasswordByTalk, bool $sendMail) {
825-
$node = $this->getMockBuilder(File::class)->getMock();
819+
$node = $this->createMock(File::class);
826820
$node->expects($this->any())->method('getName')->willReturn('filename');
827821

828822
$this->settingsManager->method('sendPasswordByMail')->willReturn(true);
@@ -834,6 +828,7 @@ public function testUpdateSendPassword($plainTextPassword, string $originalPassw
834828
$originalShare->expects($this->any())->method('getPassword')->willReturn($originalPassword);
835829
$originalShare->expects($this->any())->method('getSendPasswordByTalk')->willReturn($originalSendPasswordByTalk);
836830

831+
/** @var IShare&MockObject */
837832
$share = $this->getMockBuilder(IShare::class)->getMock();
838833
$share->expects($this->any())->method('getSharedWith')->willReturn('[email protected]');
839834
$share->expects($this->any())->method('getNode')->willReturn($node);
@@ -927,7 +922,8 @@ public function testGetShareByPath() {
927922
$permissions = 1;
928923
$token = 'token';
929924

930-
$node = $this->getMockBuilder('OCP\Files\Node')->getMock();
925+
/** @var Node&MockObject */
926+
$node = $this->createMock(Node::class);
931927
$node->expects($this->once())->method('getId')->willReturn($itemSource);
932928

933929

0 commit comments

Comments
 (0)