Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jvillafanez committed Jul 11, 2018
1 parent 82ff3cc commit 61b75a5
Show file tree
Hide file tree
Showing 4 changed files with 473 additions and 1 deletion.
90 changes: 90 additions & 0 deletions tests/HooksHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use OCP\IUser;
use OCP\Security\IHasher;
use OCP\Notification\IManager;
use OCP\Notification\INotification;
use Symfony\Component\EventDispatcher\GenericEvent;
use Test\TestCase;

Expand Down Expand Up @@ -95,6 +96,37 @@ protected function setUp() {
$this->manager,
$this->unConfigHandler
);

$this->manager->method('createNotification')
->will($this->returnCallback(function() {
$holder = [];
$mock = $this->createMock(INotification::class);
$mock->method('setApp')->will($this->returnCallback(function($app) use (&$holder, $mock) {
$holder['app'] = $app;
return $mock;
}));
$mock->method('setUser')->will($this->returnCallback(function($user) use (&$holder, $mock) {
$holder['user'] = $user;
return $mock;
}));
$mock->method('setObject')->will($this->returnCallback(function($obj, $id) use (&$holder, $mock) {
$holder['object'] = [$obj, $id];
return $mock;
}));
$mock->method('getApp')->will($this->returnCallback(function() use (&$holder) {
return $holder['app'];
}));
$mock->method('getUser')->will($this->returnCallback(function() use (&$holder) {
return $holder['user'];
}));
$mock->method('getObjectType')->will($this->returnCallback(function() use (&$holder) {
return $holder['object'][0];
}));
$mock->method('getObjectId')->will($this->returnCallback(function() use (&$holder) {
return $holder['object'][1];
}));
return $mock;
}));
}

public function testGeneratePassword() {
Expand Down Expand Up @@ -194,6 +226,64 @@ public function testSaveOldPassword() {
$this->handler->saveOldPassword($event);
}

public function testSaveOldPasswordClearingNotifications() {
/** @var IUser | \PHPUnit_Framework_MockObject_MockObject $user */
$user = $this->createMock(IUser::class);
$user->method('getUID')->willReturn('testuid');

$this->hasher->expects($this->once())
->method('hash')
->with('secret')
->willReturn('somehash');

$this->timeFactory->expects($this->once())
->method('getTime')
->willReturn(12345);

$this->oldPasswordMapper->expects($this->once())
->method('insert')
->with($this->callback(function(OldPassword $oldPassword){
return $oldPassword->getPassword() === 'somehash' &&
$oldPassword->getUid() === 'testuid' &&
$oldPassword->getChangeTime() === 12345;
}));

$this->unConfigHandler->method('getMarkAboutToExpireNotificationSentFor')
->willReturn('222');
$this->unConfigHandler->method('getMarkExpiredNotificationSentFor')
->willReturn('333');

$this->unConfigHandler->expects($this->once())
->method('resetExpirationMarks');

$this->manager->expects($this->exactly(2))
->method('markProcessed')
->withConsecutive(
[
$this->callback(function($notif) {
return $notif->getApp() === 'password_policy' &&
$notif->getUser() === 'testuid' &&
$notif->getObjectType() === 'about_to_expire' &&
$notif->getObjectId() === '222';
})
],
[
$this->callback(function($notif) {
return $notif->getApp() === 'password_policy' &&
$notif->getUser() === 'testuid' &&
$notif->getObjectType() === 'expired' &&
$notif->getObjectId() === '333';
})
]
);

$event = new GenericEvent(null, [
'user' => $user,
'password' => 'secret'
]);
$this->handler->saveOldPassword($event);
}

/**
* @expectedException \UnexpectedValueException
*/
Expand Down
Loading

0 comments on commit 61b75a5

Please sign in to comment.