Skip to content

Commit

Permalink
Added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rullzer committed Mar 13, 2015
1 parent 58e9c95 commit dfb7542
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 4 deletions.
13 changes: 10 additions & 3 deletions lib/private/share/share.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

use OCP\IUserSession;
use OCP\IDBConnection;
use OCP\IConfig;

/**
* This class provides the ability for apps to share their content between users.
Expand Down Expand Up @@ -1117,6 +1118,7 @@ public static function setExpirationDate($itemType, $itemSource, $date, $shareTi
*
* @param IUserSession $userSession
* @param IDBConnection $connection
* @param IConfig $config
* @param string $itemType
* @param string $itemSource
* @param string $password
Expand All @@ -1125,6 +1127,7 @@ public static function setExpirationDate($itemType, $itemSource, $date, $shareTi
*/
public static function setPassword(IUserSession $userSession,
IDBConnection $connection,
IConfig $config,
$itemType, $itemSource, $password) {
$user = $userSession->getUser();
if (is_null($user)) {
Expand All @@ -1137,7 +1140,7 @@ public static function setPassword(IUserSession $userSession,
}

//If passwords are enforced the password can't be null
if (self::enforcePassword() && is_null($password)) {
if (self::enforcePassword($config) && is_null($password)) {
throw new \Exception('Cannot remove password');
}

Expand Down Expand Up @@ -2399,8 +2402,12 @@ public static function getExpireInterval() {
return (int)\OCP\Config::getAppValue('core', 'shareapi_expire_after_n_days', '7');
}

public static function enforcePassword() {
$enforcePassword = \OCP\Config::getAppValue('core', 'shareapi_enforce_links_password', 'no');
/**
* @param IConfig $config
* @return bool
*/
public static function enforcePassword(IConfig $config) {
$enforcePassword = $config->getAppValue('core', 'shareapi_enforce_links_password', 'no');
return ($enforcePassword === "yes") ? true : false;
}
}
3 changes: 2 additions & 1 deletion lib/public/share.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,8 @@ public static function setExpirationDate($itemType, $itemSource, $date, $shareTi
public static function setPassword($itemType, $itemSource, $password) {
$userSession = \OC::$server->getUserSession();
$connection = \OC::$server->getDatabaseConnection();
return \OC\Share\Share::setPassword($userSession, $connection, $itemType, $itemSource, $password);
$config = \OC::$server->getConfig();
return \OC\Share\Share::setPassword($userSession, $connection, $config, $itemType, $itemSource, $password);
}


Expand Down
110 changes: 110 additions & 0 deletions tests/lib/share/share.php
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,116 @@ function dataProviderTestGroupItems() {
),
);
}

/**
* Cannot set password is there is no user
*
* @expectedException Exception
* @expectedExceptionMessage User not logged in
*/
public function testSetPasswordNoUser() {
$userSession = $this->getMockBuilder('\OCP\IUserSession')
->disableOriginalConstructor()
->getMock();

$connection = $this->getMockBuilder('\OCP\IDBConnection')
->disableOriginalConstructor()
->getMock();

$config = $this->getMockBuilder('\OCP\IConfig')
->disableOriginalConstructor()
->getMock();

\OC\Share\Share::setPassword($userSession, $connection, $config, 'a', 'b', 'c');
}

/**
* Test setting a password when everything is fine
*/
public function testSetPassword() {
$user = $this->getMockBuilder('\OCP\IUser')
->disableOriginalConstructor()
->getMock();
$user->method('getUID')->willReturn('user');

$userSession = $this->getMockBuilder('\OCP\IUserSession')
->disableOriginalConstructor()
->getMock();
$userSession->method('getUser')->willReturn($user);


$ex = $this->getMockBuilder('\Doctrine\DBAL\Query\Expression\ExpressionBuilder')
->disableOriginalConstructor()
->getMock();
$qb = $this->getMockBuilder('\Doctrine\DBAL\Query\QueryBuilder')
->disableOriginalConstructor()
->getMock();
$qb->method('update')->will($this->returnSelf());
$qb->method('set')->will($this->returnSelf());
$qb->method('where')->will($this->returnSelf());
$qb->method('andWhere')->will($this->returnSelf());
$qb->method('expr')->willReturn($ex);


$connection = $this->getMockBuilder('\OCP\IDBConnection')
->disableOriginalConstructor()
->getMock();
$connection->method('createQueryBuilder')->willReturn($qb);

$config = $this->getMockBuilder('\OCP\IConfig')
->disableOriginalConstructor()
->getMock();


$res = \OC\Share\Share::setPassword($userSession, $connection, $config, 'a', 'b', 'c');

$this->assertTrue($res);
}

/**
* @expectedException Exception
* @expectedExceptionMessage Cannot remove password
*
* Test removing a password when password is enforced
*/
public function testSetPasswordRemove() {
$user = $this->getMockBuilder('\OCP\IUser')
->disableOriginalConstructor()
->getMock();
$user->method('getUID')->willReturn('user');

$userSession = $this->getMockBuilder('\OCP\IUserSession')
->disableOriginalConstructor()
->getMock();
$userSession->method('getUser')->willReturn($user);


$ex = $this->getMockBuilder('\Doctrine\DBAL\Query\Expression\ExpressionBuilder')
->disableOriginalConstructor()
->getMock();
$qb = $this->getMockBuilder('\Doctrine\DBAL\Query\QueryBuilder')
->disableOriginalConstructor()
->getMock();
$qb->method('update')->will($this->returnSelf());
$qb->method('set')->will($this->returnSelf());
$qb->method('where')->will($this->returnSelf());
$qb->method('andWhere')->will($this->returnSelf());
$qb->method('expr')->willReturn($ex);


$connection = $this->getMockBuilder('\OCP\IDBConnection')
->disableOriginalConstructor()
->getMock();
$connection->method('createQueryBuilder')->willReturn($qb);

$config = $this->getMockBuilder('\OCP\IConfig')
->disableOriginalConstructor()
->getMock();
$config->method('getAppValue')->willReturn('yes');

\OC\Share\Share::setPassword($userSession, $connection, $config, 'a', 'b', '');
}

}

class DummyShareClass extends \OC\Share\Share {
Expand Down

0 comments on commit dfb7542

Please sign in to comment.