-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added more tests for the auth adapters
- Loading branch information
1 parent
e13cc76
commit 64ee612
Showing
4 changed files
with
460 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
tests/phpunit/ImboIntegrationTest/Auth/AccessControl/Adapter/AdapterTests.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
<?php | ||
/** | ||
* This file is part of the Imbo package | ||
* | ||
* (c) Christer Edvartsen <cogo@starzinger.net> | ||
* | ||
* For the full copyright and license information, please view the LICENSE file that was | ||
* distributed with this source code. | ||
*/ | ||
|
||
namespace ImboIntegrationTest\Auth\AccessControl\Adapter; | ||
|
||
/** | ||
* @group integration | ||
*/ | ||
abstract class AdapterTests extends \PHPUnit_Framework_TestCase { | ||
/** | ||
* @var Imbo\Auth\AccessControl\Adapter\MutableAdapterInterface | ||
*/ | ||
private $adapter; | ||
|
||
/** | ||
* Get the adapter we want to test | ||
* | ||
* @return Imbo\Auth\AccessControl\Adapter\MutableAdapterInterface | ||
*/ | ||
abstract protected function getAdapter(); | ||
|
||
/** | ||
* Set up | ||
*/ | ||
public function setUp() { | ||
$this->adapter = $this->getAdapter(); | ||
} | ||
|
||
/** | ||
* Tear down | ||
*/ | ||
public function tearDown() { | ||
$this->adapter = null; | ||
} | ||
|
||
public function testReturnsEmptyArrayWhenThereIsNoGroups() { | ||
$model = $this->getMock('Imbo\Model\Groups'); | ||
$model->expects($this->once())->method('setHits')->with(0); | ||
|
||
$this->assertSame([], $this->adapter->getGroups(null, $model)); | ||
} | ||
|
||
public function testCanAddAndFetchGroups() { | ||
$this->assertFalse($this->adapter->getGroup('g1')); | ||
$this->assertFalse($this->adapter->getGroup('g2')); | ||
|
||
$this->adapter->addResourceGroup('g1', ['images.get', 'images.head']); | ||
$this->adapter->addResourceGroup('g2', ['status.get']); | ||
|
||
$this->assertSame(['images.get', 'images.head'], $this->adapter->getGroup('g1')); | ||
$this->assertSame(['status.get'], $this->adapter->getGroup('g2')); | ||
|
||
$model = $this->getMock('Imbo\Model\Groups'); | ||
$model->expects($this->once())->method('setHits')->with(2); | ||
|
||
$groups = $this->adapter->getGroups(null, $model); | ||
|
||
$this->assertArrayHasKey('g1', $groups); | ||
$this->assertArrayHasKey('g2', $groups); | ||
|
||
$this->assertSame(['images.get', 'images.head'], $groups['g1']); | ||
$this->assertSame(['status.get'], $groups['g2']); | ||
} | ||
|
||
public function testCanCheckIfGroupExists() { | ||
$this->assertFalse($this->adapter->groupExists('g1')); | ||
$this->assertFalse($this->adapter->groupExists('g2')); | ||
$this->assertFalse($this->adapter->groupExists('g3')); | ||
|
||
$this->adapter->addResourceGroup('g1'); | ||
$this->adapter->addResourceGroup('g2'); | ||
|
||
$this->assertTrue($this->adapter->groupExists('g1')); | ||
$this->assertTrue($this->adapter->groupExists('g2')); | ||
$this->assertFalse($this->adapter->groupExists('g3')); | ||
} | ||
|
||
public function testCanUpdateResourceGroup() { | ||
$this->adapter->addResourceGroup('g1', ['images.get', 'images.head']); | ||
$this->adapter->addResourceGroup('g2', ['image.get']); | ||
|
||
$this->assertSame(['images.get', 'images.head'], $this->adapter->getGroup('g1')); | ||
$this->assertSame(['image.get'], $this->adapter->getGroup('g2')); | ||
|
||
$this->adapter->updateResourceGroup('g1', ['status.get']); | ||
$this->assertSame(['status.get'], $this->adapter->getGroup('g1')); | ||
$this->assertSame(['image.get'], $this->adapter->getGroup('g2')); // Has not changed | ||
} | ||
|
||
public function testCanRemoveGroup() { | ||
$this->adapter->addResourceGroup('g1', ['images.get', 'images.head']); | ||
$this->assertSame(['images.get', 'images.head'], $this->adapter->getGroup('g1')); | ||
$this->assertTrue($this->adapter->deleteResourceGroup('g1')); | ||
$this->assertSame(false, $this->adapter->getGroup('g1')); | ||
} | ||
|
||
public function testCanManipulateKeys() { | ||
$this->assertNull($this->adapter->getPrivateKey('publicKey')); | ||
$this->assertFalse($this->adapter->publicKeyExists('publicKey')); | ||
$this->assertTrue($this->adapter->addKeyPair('publicKey', 'privateKey')); | ||
$this->assertTrue($this->adapter->publicKeyExists('publicKey')); | ||
$this->assertSame('privateKey', $this->adapter->getPrivateKey('publicKey')); | ||
$this->assertTrue($this->adapter->updatePrivateKey('publicKey', 'newPrivateKey')); | ||
$this->assertSame('newPrivateKey', $this->adapter->getPrivateKey('publicKey')); | ||
$this->assertTrue($this->adapter->deletePublicKey('publicKey')); | ||
$this->assertFalse($this->adapter->publicKeyExists('publicKey')); | ||
$this->assertNull($this->adapter->getPrivateKey('publicKey')); | ||
} | ||
|
||
public function testGetAccessRuleThatDoesNotExist() { | ||
$this->assertNull($this->adapter->getAccessRule('publickey', 'id')); | ||
} | ||
|
||
public function testCanManipulateAccessRules() { | ||
$this->adapter->addKeyPair('public', 'private'); | ||
$this->assertInternalType('string', $ruleId = $this->adapter->addAccessRule('public', ['resources' => ['image.get'], 'users' => ['user']])); | ||
$this->assertSame([ | ||
'id' => $ruleId, | ||
'resources' => ['image.get'], | ||
'users' => ['user'], | ||
], $this->adapter->getAccessRule('public', $ruleId)); | ||
$this->assertTrue($this->adapter->deleteAccessRule('public', $ruleId)); | ||
$this->assertNull($this->adapter->getAccessRule('publickey', $ruleId)); | ||
} | ||
|
||
/** | ||
* @expectedException Imbo\Exception\DatabaseException | ||
* @expectedExceptionCode 500 | ||
* @expectedExceptionMessage Could not delete rule from database | ||
*/ | ||
public function testDeleteAccessRuleWithIdThatDoesNotExist() { | ||
$this->assertFalse($this->adapter->deleteAccessRule('public', 'asdasd')); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
tests/phpunit/ImboIntegrationTest/Auth/AccessControl/Adapter/MongoDBTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
/** | ||
* This file is part of the Imbo package | ||
* | ||
* (c) Christer Edvartsen <cogo@starzinger.net> | ||
* | ||
* For the full copyright and license information, please view the LICENSE file that was | ||
* distributed with this source code. | ||
*/ | ||
|
||
namespace ImboIntegrationTest\Auth\AccessControl\Adapter; | ||
|
||
use Imbo\Auth\AccessControl\Adapter\MongoDB, | ||
MongoClient; | ||
|
||
/** | ||
* @covers Imbo\Auth\AccessControl\Adapter\MongoDB | ||
* @group integration | ||
* @group mongodb | ||
*/ | ||
class MongoDBTest extends AdapterTests { | ||
/** | ||
* Name of the test database | ||
* | ||
* @var string | ||
*/ | ||
protected $databaseName = 'imboIntegrationTestAuth'; | ||
|
||
/** | ||
* @see ImboIntegrationTest\Database\DatabaseTests::getAdapter() | ||
*/ | ||
protected function getAdapter() { | ||
return new MongoDB([ | ||
'databaseName' => $this->databaseName, | ||
]); | ||
} | ||
|
||
/** | ||
* Make sure we have the mongo extension available and drop the test database just in case | ||
*/ | ||
public function setUp() { | ||
if (!class_exists('MongoClient')) { | ||
$this->markTestSkipped('pecl/mongo >= 1.3.0 is required to run this test'); | ||
} | ||
|
||
$client = new MongoClient(); | ||
$client->selectDB($this->databaseName)->drop(); | ||
|
||
parent::setUp(); | ||
} | ||
|
||
/** | ||
* Drop the test database after each test | ||
*/ | ||
public function tearDown() { | ||
if (class_exists('MongoClient')) { | ||
$client = new MongoClient(); | ||
$client->selectDB($this->databaseName)->drop(); | ||
} | ||
|
||
parent::tearDown(); | ||
} | ||
} |
Oops, something went wrong.