Skip to content

Commit

Permalink
Restores ShareByMailProviderTest unit tests from master
Browse files Browse the repository at this point in the history
Signed-off-by: Cyrille Bollu <cyrpub@bollu.be>
  • Loading branch information
StCyr committed Apr 8, 2022
1 parent efaed9a commit 231f79b
Showing 1 changed file with 62 additions and 10 deletions.
72 changes: 62 additions & 10 deletions apps/sharebymail/tests/ShareByMailProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\File;
use OCP\Files\IRootFolder;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IL10N;
use OCP\ILogger;
Expand All @@ -64,9 +63,6 @@
*/
class ShareByMailProviderTest extends TestCase {

/** @var IConfig */
private $config;

/** @var IDBConnection */
private $connection;

Expand Down Expand Up @@ -122,7 +118,6 @@ protected function setUp(): void {
->willReturnCallback(function ($text, $parameters = []) {
return vsprintf($text, $parameters);
});
$this->config = $this->getMockBuilder(IConfig::class)->getMock();
$this->logger = $this->getMockBuilder(ILogger::class)->getMock();
$this->rootFolder = $this->getMockBuilder('OCP\Files\IRootFolder')->getMock();
$this->userManager = $this->getMockBuilder(IUserManager::class)->getMock();
Expand Down Expand Up @@ -150,7 +145,6 @@ private function getInstance(array $mockedMethods = []) {
$instance = $this->getMockBuilder('OCA\ShareByMail\ShareByMailProvider')
->setConstructorArgs(
[
$this->config,
$this->connection,
$this->secureRandom,
$this->userManager,
Expand All @@ -174,7 +168,6 @@ private function getInstance(array $mockedMethods = []) {
}

return new ShareByMailProvider(
$this->config,
$this->connection,
$this->secureRandom,
$this->userManager,
Expand Down Expand Up @@ -274,9 +267,24 @@ public function testCreateSendPasswordByMailWithPasswordAndWithoutEnforcedPasswo
$this->hasher->expects($this->once())->method('hash')->with('password')->willReturn('passwordHashed');
$share->expects($this->once())->method('setPassword')->with('passwordHashed');

// The given password (but not the autogenerated password) should be
// mailed to the receiver of the share.
$this->shareManager->expects($this->any())->method('shareApiLinkEnforcePassword')->willReturn(false);
$this->settingsManager->expects($this->any())->method('sendPasswordByMail')->willReturn(true);
$instance->expects($this->never())->method('autoGeneratePassword');

$message = $this->createMock(IMessage::class);
$message->expects($this->once())->method('setTo')->with(['receiver@example.com']);
$this->mailer->expects($this->once())->method('createMessage')->willReturn($message);
$this->mailer->expects($this->once())->method('createEMailTemplate')->with('sharebymail.RecipientPasswordNotification', [
'filename' => 'filename',
'password' => 'password',
'initiator' => 'owner',
'initiatorEmail' => null,
'shareWith' => 'receiver@example.com',
]);
$this->mailer->expects($this->once())->method('send');

$this->assertSame('shareObject',
$instance->create($share)
);
Expand Down Expand Up @@ -312,9 +320,22 @@ public function testCreateSendPasswordByMailWithEnforcedPasswordProtection() {
$this->hasher->expects($this->once())->method('hash')->with('autogeneratedPassword')->willReturn('autogeneratedPasswordHashed');
$share->expects($this->once())->method('setPassword')->with('autogeneratedPasswordHashed');

// The autogenerated password should be mailed to the receiver of the share.
$this->shareManager->expects($this->any())->method('shareApiLinkEnforcePassword')->willReturn(true);
$this->settingsManager->expects($this->any())->method('sendPasswordByMail')->willReturn(true);

$message = $this->createMock(IMessage::class);
$message->expects($this->once())->method('setTo')->with(['receiver@example.com']);
$this->mailer->expects($this->once())->method('createMessage')->willReturn($message);
$this->mailer->expects($this->once())->method('createEMailTemplate')->with('sharebymail.RecipientPasswordNotification', [
'filename' => 'filename',
'password' => 'autogeneratedPassword',
'initiator' => 'owner',
'initiatorEmail' => null,
'shareWith' => 'receiver@example.com',
]);
$this->mailer->expects($this->once())->method('send');

$this->assertSame('shareObject',
$instance->create($share)
);
Expand Down Expand Up @@ -342,9 +363,24 @@ public function testCreateSendPasswordByMailWithPasswordAndWithEnforcedPasswordP
$this->hasher->expects($this->once())->method('hash')->with('password')->willReturn('passwordHashed');
$share->expects($this->once())->method('setPassword')->with('passwordHashed');

// The given password (but not the autogenerated password) should be
// mailed to the receiver of the share.
$this->shareManager->expects($this->any())->method('shareApiLinkEnforcePassword')->willReturn(true);
$this->settingsManager->expects($this->any())->method('sendPasswordByMail')->willReturn(true);
$instance->expects($this->never())->method('autoGeneratePassword');

$message = $this->createMock(IMessage::class);
$message->expects($this->once())->method('setTo')->with(['receiver@example.com']);
$this->mailer->expects($this->once())->method('createMessage')->willReturn($message);
$this->mailer->expects($this->once())->method('createEMailTemplate')->with('sharebymail.RecipientPasswordNotification', [
'filename' => 'filename',
'password' => 'password',
'initiator' => 'owner',
'initiatorEmail' => null,
'shareWith' => 'receiver@example.com',
]);
$this->mailer->expects($this->once())->method('send');

$this->assertSame('shareObject',
$instance->create($share)
);
Expand Down Expand Up @@ -372,9 +408,28 @@ public function testCreateSendPasswordByTalkWithEnforcedPasswordProtection() {
$this->hasher->expects($this->once())->method('hash')->with('autogeneratedPassword')->willReturn('autogeneratedPasswordHashed');
$share->expects($this->once())->method('setPassword')->with('autogeneratedPasswordHashed');

// The autogenerated password should be mailed to the owner of the share.
$this->shareManager->expects($this->any())->method('shareApiLinkEnforcePassword')->willReturn(true);
$this->settingsManager->expects($this->any())->method('sendPasswordByMail')->willReturn(true);
$instance->expects($this->once())->method('autoGeneratePassword')->with($share)->willReturn('autogeneratedPassword');

$message = $this->createMock(IMessage::class);
$message->expects($this->once())->method('setTo')->with(['owner@example.com' => 'Owner display name']);
$this->mailer->expects($this->once())->method('createMessage')->willReturn($message);
$this->mailer->expects($this->once())->method('createEMailTemplate')->with('sharebymail.OwnerPasswordNotification', [
'filename' => 'filename',
'password' => 'autogeneratedPassword',
'initiator' => 'Owner display name',
'initiatorEmail' => 'owner@example.com',
'shareWith' => 'receiver@example.com',
]);
$this->mailer->expects($this->once())->method('send');

$user = $this->createMock(IUser::class);
$this->userManager->expects($this->once())->method('get')->with('owner')->willReturn($user);
$user->expects($this->once())->method('getDisplayName')->willReturn('Owner display name');
$user->expects($this->once())->method('getEMailAddress')->willReturn('owner@example.com');

$this->assertSame('shareObject',
$instance->create($share)
);
Expand Down Expand Up @@ -476,7 +531,6 @@ public function testAddShareToDB() {
$hideDownload = true;
$label = 'label';
$expiration = new \DateTime();
$passwordExpirationTime = new \DateTime();


$instance = $this->getInstance();
Expand All @@ -492,7 +546,6 @@ public function testAddShareToDB() {
$permissions,
$token,
$password,
$passwordExpirationTime,
$sendPasswordByTalk,
$hideDownload,
$label,
Expand All @@ -519,7 +572,6 @@ public function testAddShareToDB() {
$this->assertSame($permissions, (int)$result[0]['permissions']);
$this->assertSame($token, $result[0]['token']);
$this->assertSame($password, $result[0]['password']);
$this->assertSame($passwordExpirationTime->getTimestamp(), \DateTime::createFromFormat('Y-m-d H:i:s', $result[0]['password_expiration_time'])->getTimestamp());
$this->assertSame($sendPasswordByTalk, (bool)$result[0]['password_by_talk']);
$this->assertSame($hideDownload, (bool)$result[0]['hide_download']);
$this->assertSame($label, $result[0]['label']);
Expand Down

0 comments on commit 231f79b

Please sign in to comment.