Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
christeredvartsen committed Mar 5, 2016
1 parent 7f4f9a1 commit 2997c89
Showing 1 changed file with 182 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@
namespace ImboUnitTest\Auth\AccessControl\Adapter;

use Imbo\Auth\AccessControl\Adapter\ArrayAdapter,
Imbo\Auth\AccessControl\GroupQuery,
Imbo\Resource;

/**
* @covers Imbo\Auth\AccessControl\Adapter\ArrayAdapter
* @group unit
*/
class ArrayAdapterTest extends \PHPUnit_Framework_TestCase {
/**
* @covers Imbo\Auth\AccessControl\Adapter\ArrayAdapter::__construct
* @covers Imbo\Auth\AccessControl\Adapter\ArrayAdapter::validateAccessList
* @covers Imbo\Auth\AccessControl\Adapter\ArrayAdapter::getUsersForResource
*/
public function testReturnsCorrectListOfAllowedUsersForResource() {
$accessControl = new ArrayAdapter([
[
Expand Down Expand Up @@ -49,6 +55,11 @@ public function testReturnsCorrectListOfAllowedUsersForResource() {
);
}

/**
* @covers Imbo\Auth\AccessControl\Adapter\ArrayAdapter::__construct
* @covers Imbo\Auth\AccessControl\Adapter\ArrayAdapter::validateAccessList
* @covers Imbo\Auth\AccessControl\Adapter\ArrayAdapter::getPrivateKey
*/
public function testGetPrivateKey() {
$accessControl = new ArrayAdapter([
[
Expand All @@ -71,8 +82,14 @@ public function testGetPrivateKey() {

$this->assertSame('privateKey1', $accessControl->getPrivateKey('pubKey1'));
$this->assertSame('privateKey2', $accessControl->getPrivateKey('pubKey2'));
$this->assertNull($accessControl->getPrivateKey('pubKey3'));
}

/**
* @covers Imbo\Auth\AccessControl\Adapter\ArrayAdapter::__construct
* @covers Imbo\Auth\AccessControl\Adapter\ArrayAdapter::validateAccessList
* @covers Imbo\Auth\AccessControl\Adapter\ArrayAdapter::hasAccess
*/
public function testCanReadResourcesFromGroups() {
$acl = [
[
Expand Down Expand Up @@ -103,6 +120,11 @@ public function testCanReadResourcesFromGroups() {
$this->assertTrue($ac->hasAccess('pubkey', Resource::USER_GET, 'user1'));
}

/**
* @covers Imbo\Auth\AccessControl\Adapter\ArrayAdapter::__construct
* @covers Imbo\Auth\AccessControl\Adapter\ArrayAdapter::validateAccessList
* @covers Imbo\Auth\AccessControl\Adapter\ArrayAdapter::hasAccess
*/
public function testCanReadResourcesGrantedUsingWildcard() {
$accessControl = new ArrayAdapter([
[
Expand Down Expand Up @@ -143,11 +165,171 @@ public function getAuthConfig() {
/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Public key declared twice in config: pubkey
* @covers Imbo\Auth\AccessControl\Adapter\ArrayAdapter::__construct
* @covers Imbo\Auth\AccessControl\Adapter\ArrayAdapter::validateAccessList
*/
public function testThrowsErrorOnDuplicatePublicKey() {
$accessControl = new ArrayAdapter([
['publicKey' => 'pubkey', 'privateKey' => 'privkey', 'acl' => []],
['publicKey' => 'pubkey', 'privateKey' => 'privkey', 'acl' => []]
]);
}

/**
* Data provider
*
* @return array[]
*/
public function getGroupsData() {
$query = new GroupQuery();
$query->page(2)->limit(2);

return [
'no groups' => [
[],
[],
],
'some groups' => [
['g1' => [], 'g2' => [], 'g3' => []],
['g1' => [], 'g2' => [], 'g3' => []],
],
'groups with query object' => [
['g1' => [], 'g2' => [], 'g3' => [], 'g4' => [], 'g5' => []],
['g3' => [], 'g4' => []],
$query
],
];
}

/**
* @dataProvider getGroupsData
* @covers Imbo\Auth\AccessControl\Adapter\ArrayAdapter::getGroups
*/
public function testCanGetGroups(array $groups, array $result, $query = null) {
$numGroups = count($groups);

$model = $this->getMock('Imbo\Model\Groups');
$model->expects($this->once())->method('setHits')->with($numGroups);

$adapter = new ArrayAdapter([], $groups);
$this->assertSame(array_values($result), array_values($adapter->getGroups($query, $model)));
}

/**
* Data provider
*
* @return array[]
*/
public function getGroups() {
return [
'no groups' => [
[], 'group', false,
],
'group exists' => [
['group' => [], 'othergroup' => []], 'group', true,
],
'group does not exist' => [
['group' => [], 'othergroup' => []], 'somegroup', false,
],
];
}

/**
* @dataProvider getGroups
* @covers Imbo\Auth\AccessControl\Adapter\ArrayAdapter::groupExists
*/
public function testCanCheckIfGroupExists($groups, $group, $exists) {
$adapter = new ArrayAdapter([], $groups);
$this->assertSame($exists, $adapter->groupExists($group));
}

/**
* @covers Imbo\Auth\AccessControl\Adapter\ArrayAdapter::publicKeyExists
*/
public function testPublicKeyExists() {
$adapter = new ArrayAdapter([
[
'publicKey' => 'pubKey1',
'privateKey' => 'privateKey1',
'acl' => [[
'resources' => [Resource::IMAGES_GET],
'users' => ['user1', 'user2'],
]]
],
[
'publicKey' => 'pubKey2',
'privateKey' => 'privateKey2',
'acl' => [[
'resources' => [Resource::IMAGES_GET],
'users' => ['user2', 'user3', '*'],
]]
]
]);

$this->assertTrue($adapter->publicKeyExists('pubKey1'));
$this->assertTrue($adapter->publicKeyExists('pubKey2'));
$this->assertFalse($adapter->publicKeyExists('pubKey3'));
}

/**
* Data provider
*
* @return array[]
*/
public function getAccessRules() {
$acl = [
[
'id' => 1,
'publicKey' => 'pubKey1',
'privateKey' => 'privateKey1',
'acl' => [[
'resources' => [Resource::IMAGES_GET],
'users' => ['user1', 'user2'],
]]
],
[
'id' => 2,
'publicKey' => 'pubKey2',
'privateKey' => 'privateKey2',
'acl' => [[
'resources' => [Resource::IMAGES_GET],
'users' => ['user2', 'user3', '*'],
]]
],
];

return [
'access rule exists' => [
'acl' => $acl,
'publicKey' => 'pubKey1',
'ruleId' => 1,
'rule' => [
'id' => 1,
'resources' => [Resource::IMAGES_GET],
'users' => ['user1', 'user2'],
],
],
'no access rules' => [
'acl' => [],
'publicKey' => 'key',
'ruleId' => 1,
'rule' => null,
],
'access rule that does not exist' => [
'acl' => $acl,
'publicKey' => 'pubKey1',
'ruleId' => 2,
'rule' => null,
],
];
}

/**
* @dataProvider getAccessRules
* @covers Imbo\Auth\AccessControl\Adapter\ArrayAdapter::getAccessRule
*/
public function testGetAccessRule($acl, $publicKey, $ruleId, $rule) {
$adapter = new ArrayAdapter($acl);
$this->assertSame($rule, $adapter->getAccessRule($publicKey, $ruleId));
}
}

0 comments on commit 2997c89

Please sign in to comment.