diff --git tests/VersioncontrolAccountAuthPlugin.test tests/VersioncontrolAccountAuthPlugin.test index 690a19a..51ec12a 100644 --- tests/VersioncontrolAccountAuthPlugin.test +++ tests/VersioncontrolAccountAuthPlugin.test @@ -1,22 +1,24 @@ t('Versioncontrol account plugin testing'), + 'name' => t('Versioncontrol account authentication plugin testing'), 'description' => t("Test the base 'account' auth plugin's CRUD and authorization logic."), 'group' => t('Version Control'), ); @@ -39,37 +41,132 @@ class VersioncontrolAccountAuthPlugin extends VersioncontrolTestCase { ), ); - foreach ($this->backends as $type => $backend) { - $this->repos[$type] = $this->versioncontrolCreateRepository($type, $repo_data); + foreach ($this->backends as $backend_machine_name => $backend) { + $repo = $this->versioncontrolCreateRepository($backend_machine_name, $repo_data); + $this->repos[$backend_machine_name] = $repo; + // add some more dummy data related to each repo + $label_default_data = array('repo_id' => $repo->repo_id); + $this->branches[$repo->repo_id][0] = $this->versioncontrolCreateBranch($backend_machine_name, $label_default_data); + $this->branches[$repo->repo_id][1] = $this->versioncontrolCreateBranch($backend_machine_name, $label_default_data); + $this->branches[$repo->repo_id][2] = $this->versioncontrolCreateBranch($backend_machine_name, $label_default_data); + $this->branches[$repo->repo_id][3] = $this->versioncontrolCreateBranch($backend_machine_name, $label_default_data); + $this->tags[$repo->repo_id][0] = $this->versioncontrolCreateTag($backend_machine_name, $label_default_data); + $this->tags[$repo->repo_id][1] = $this->versioncontrolCreateTag($backend_machine_name, $label_default_data); + $this->tags[$repo->repo_id][2] = $this->versioncontrolCreateTag($backend_machine_name, $label_default_data); + $this->tags[$repo->repo_id][3] = $this->versioncontrolCreateTag($backend_machine_name, $label_default_data); } } + /** + * Helper to get user data. + */ + protected function getMappedAccountsAuthPluginUserData($repo_id) { + return array( + 'access' => VersioncontrolAuthHandlerMappedAccounts::GRANT, + 'branch_create' => VersioncontrolAuthHandlerMappedAccounts::DENY, + 'branch_update' => VersioncontrolAuthHandlerMappedAccounts::GRANT, + 'branch_delete' => VersioncontrolAuthHandlerMappedAccounts::GRANT, + 'tag_create' => VersioncontrolAuthHandlerMappedAccounts::DENY, + 'tag_update' => VersioncontrolAuthHandlerMappedAccounts::GRANT, + 'tag_delete' => VersioncontrolAuthHandlerMappedAccounts::GRANT, + 'per-label-auth' => array( + $this->branches[$repo_id][0]->label_id => array( + 'update' => VersioncontrolAuthHandlerMappedAccounts::GRANT, + 'delete' => VersioncontrolAuthHandlerMappedAccounts::GRANT, + ), + $this->branches[$repo_id][1]->label_id => array( + 'update' => VersioncontrolAuthHandlerMappedAccounts::GRANT, + 'delete' => VersioncontrolAuthHandlerMappedAccounts::DENY, + ), + $this->branches[$repo_id][2]->label_id => array( + 'update' => VersioncontrolAuthHandlerMappedAccounts::DENY, + 'delete' => VersioncontrolAuthHandlerMappedAccounts::GRANT, + ), + $this->branches[$repo_id][3]->label_id => array( + 'update' => VersioncontrolAuthHandlerMappedAccounts::DENY, + 'delete' => VersioncontrolAuthHandlerMappedAccounts::DENY, + ), + $this->tags[$repo_id][0]->label_id => array( + 'update' => VersioncontrolAuthHandlerMappedAccounts::GRANT, + 'delete' => VersioncontrolAuthHandlerMappedAccounts::GRANT, + ), + $this->tags[$repo_id][1]->label_id => array( + 'update' => VersioncontrolAuthHandlerMappedAccounts::GRANT, + 'delete' => VersioncontrolAuthHandlerMappedAccounts::DENY, + ), + $this->tags[$repo_id][2]->label_id => array( + 'update' => VersioncontrolAuthHandlerMappedAccounts::DENY, + 'delete' => VersioncontrolAuthHandlerMappedAccounts::GRANT, + ), + $this->tags[$repo_id][3]->label_id => array( + 'update' => VersioncontrolAuthHandlerMappedAccounts::DENY, + 'delete' => VersioncontrolAuthHandlerMappedAccounts::DENY, + ), + ), + ); + } + public function testMappedAccountsAuthPluginCrud() { - $repo = reset($this->repos); - // Manually instantiate the plugin for the create portion - $class_name = ctools_plugin_load_class('versioncontrol', 'vcs_auth', 'account', 'handler'); - $authplug = new $class_name(); - $authplug->setRepository($repo); + foreach ($this->repos as $repo) { + // Manually instantiate the plugin for the create portion + $class_name = ctools_plugin_load_class('versioncontrol', 'vcs_auth', 'account', 'handler'); + $authplug = new $class_name(); + $authplug->setRepository($repo); - $super_user = $this->drupalCreateUser(); - $super_user_data = array( - 'access' => VersioncontrolAuthHandlerMappedAccounts::ALL, - 'branch_create' => VersioncontrolAuthHandlerMappedAccounts::GRANT, - 'branch_update' => VersioncontrolAuthHandlerMappedAccounts::GRANT, - 'branch_delete' => VersioncontrolAuthHandlerMappedAccounts::GRANT, - 'tag_create' => VersioncontrolAuthHandlerMappedAccounts::GRANT, - 'tag_update' => VersioncontrolAuthHandlerMappedAccounts::GRANT, - 'tag_delete' => VersioncontrolAuthHandlerMappedAccounts::GRANT, - 'per-label-auth' => array(), - ); + // Add one auth account row + $super_user = $this->drupalCreateUser(); + $super_user_data = $this->getMappedAccountsAuthPluginUserData($repo->repo_id); + $authplug->setUserData($super_user->uid, $super_user_data); + $authplug->save(); - $authplug->setUserData($super_user->uid, $super_user_data); + // Now load the plugin using the provided method and retrieve the user data, + // ensure it's what we sent in + $db_authplug = $repo->getAuthHandler(); + $db_data = $db_authplug->getUserData($super_user->uid); + $at_input = array_diff($super_user_data, $db_data); + $this->assertTrue(empty($at_input), 'Authentication account database data is included on provided data'); + // add uid for the comparison(db info autoreturns it, but we set it at setUserData() not in the array passed) + $super_user_data['uid'] = $super_user->uid; + $at_db = array_diff($db_data, $super_user_data); + $this->assertTrue(empty($at_db), 'Authentication account provided data is included on database data'); - // Now load the plugin using the provided method and retrieve the user data, - // ensure it's what we sent in - $authplug = $repo->getAuthHandler(); - $data = $authplug->getUserData($super_user->uid); - + // Now try test logic + $authplug = $repo->getAuthHandler(); + $branches = $repo->loadBranches(array( + $this->branches[$repo->repo_id][0]->label_id, + $this->branches[$repo->repo_id][1]->label_id, + $this->branches[$repo->repo_id][2]->label_id, + $this->branches[$repo->repo_id][3]->label_id, + )); + $tags = $repo->loadTags(array( + $this->tags[$repo->repo_id][0]->label_id, + $this->tags[$repo->repo_id][1]->label_id, + $this->tags[$repo->repo_id][2]->label_id, + $this->tags[$repo->repo_id][3]->label_id, + )); + // global + $this->assertTrue($authplug->authAccess($super_user->uid), 'global auth validate correctly'); + // branches + $this->assertFalse($authplug->authBranchCreate($super_user->uid), 'branch create validate correctly'); + $this->assertFalse($authplug->authTagCreate($super_user->uid), 'tag create validate correctly'); + $this->assertTrue($authplug->authBranchUpdate($super_user->uid, $this->branches[$repo->repo_id][0]), 'granular branch update validate correctly'); + $this->assertTrue($authplug->authBranchDelete($super_user->uid, $this->branches[$repo->repo_id][0]), 'granular branch delete validate correctly'); + $this->assertTrue($authplug->authBranchUpdate($super_user->uid, $this->branches[$repo->repo_id][1]), 'granular branch update validate correctly'); + $this->assertFalse($authplug->authBranchDelete($super_user->uid, $this->branches[$repo->repo_id][1]), 'granular branch delete validate correctly'); + $this->assertFalse($authplug->authBranchUpdate($super_user->uid, $this->branches[$repo->repo_id][2]), 'granular branch update validate correctly'); + $this->assertTrue($authplug->authBranchDelete($super_user->uid, $this->branches[$repo->repo_id][2]), 'granular branch delete validate correctly'); + $this->assertFalse($authplug->authBranchUpdate($super_user->uid, $this->branches[$repo->repo_id][3]), 'granular branch update validate correctly'); + $this->assertFalse($authplug->authBranchDelete($super_user->uid, $this->branches[$repo->repo_id][3]), 'granular branch delete validate correctly'); + // tags + $this->assertTrue($authplug->authTagUpdate($super_user->uid, $this->tags[$repo->repo_id][0]), 'granular tag update validate correctly'); + $this->assertTrue($authplug->authTagDelete($super_user->uid, $this->tags[$repo->repo_id][0]), 'granular tag delete validate correctly'); + $this->assertTrue($authplug->authTagUpdate($super_user->uid, $this->tags[$repo->repo_id][1]), 'granular tag update validate correctly'); + $this->assertFalse($authplug->authTagDelete($super_user->uid, $this->tags[$repo->repo_id][1]) + $this->assertFalse($authplug->authTagUpdate($super_user->uid, $this->tags[$repo->repo_id][2]), 'granular tag update validate correctly'); + $this->assertTrue($authplug->authTagDelete($super_user->uid, $this->tags[$repo->repo_id][2]), 'granular tag delete validate correctly'); + $this->assertFalse($authplug->authTagUpdate($super_user->uid, $this->tags[$repo->repo_id][3]), 'granular tag update validate correctly'); + $this->assertFalse($authplug->authTagDelete($super_user->uid, $this->tags[$repo->repo_id][3]), 'granular tag delete validate correctly'); + } } public function testAuthLogic() { @@ -81,5 +178,5 @@ class VersioncontrolAccountAuthPlugin extends VersioncontrolTestCase { public function doAuthLogicTest(VersioncontrolRepository $repo) { $authplug = $repo->getAuthHandler(); } - + } diff --git tests/VersioncontrolTestCase.test tests/VersioncontrolTestCase.test index 3861fcc..7ee9d22 100644 --- tests/VersioncontrolTestCase.test +++ tests/VersioncontrolTestCase.test @@ -129,13 +129,14 @@ abstract class VersioncontrolTestCase extends DrupalWebTestCase { * anything that actually tries to interact with the underlying repo will * fail. * - * @param string $backend + * @param string $backend_name + * @param array $data */ - public function versioncontrolCreateRepository($backend = 'test', $data = array()) { + public function versioncontrolCreateRepository($backend_name = 'test', $data = array()) { static $i = 0; $default_data = array( 'name' => 'test_repo_' . ++$i, - 'vcs' => $backend, + 'vcs' => $backend_name, 'root' => '/fake/path/to/repo', 'update_method' => 0, 'updated' => 0, @@ -153,10 +154,36 @@ abstract class VersioncontrolTestCase extends DrupalWebTestCase { $data['plugins'][$plugin_slot] = $default_plugin; } } - $backend = $this->backends[$backend]; + $backend = $this->backends[$backend_name]; $repo = $backend->buildEntity('repo', $data); $repo->insert(); return $repo; } + + public function versioncontrolCreateLabel($type, $backend_name = 'test', $data = array()) { + $default_data = array( + 'name' => $this->randomName(32), + ); + $data += $default_data; + + $backend = $this->backends[$backend_name]; + if (!isset($data['repo_id'])) { + $repo = $this->versioncontrolCreateRepository($backend_name); + $data['repo_id'] = $repo->repo_id; + } + $label = $backend->buildEntity($type, $data); + $label->insert(); + + return $label; + } + + public function versioncontrolCreateBranch($backend_name = 'test', $data = array()) { + return $this->versioncontrolCreateLabel('branch', $backend_name, $data); + } + + public function versioncontrolCreateTag($backend_name = 'test', $data = array()) { + return $this->versioncontrolCreateLabel('tag', $backend_name, $data); + } + }