- Tests for user & group commands

- Count the admin history to check if commands are logging
This commit is contained in:
nathangray 2018-08-29 16:30:33 -06:00
parent 9681982ff0
commit be6903b074
4 changed files with 415 additions and 1 deletions

View File

@ -94,6 +94,9 @@ class AclCommandTest extends CommandBase {
public function testAddForUserWhenEmpty()
{
// Set up
$log_count = $this->get_log_count();
// Run
$data = array(
'allow' => true,
'account' => $this->account_id,
@ -109,6 +112,7 @@ class AclCommandTest extends CommandBase {
$acl = new Acl($this->account_id);
$this->assertTrue($acl->check($data['location'], Acl::ADD, static::APP));
$this->assertEquals($data['rights'], $acl->get_specific_rights($data['location'], $data['app']));
$this->assertGreaterThan($log_count, $this->get_log_count(), "Command ($command) did not log");
}
/**
@ -120,6 +124,7 @@ class AclCommandTest extends CommandBase {
$acl = new Acl($this->account_id);
$acl->add_repository(static::APP, $GLOBALS['egw_info']['user']['account_id'], $this->account_id, Acl::ADD);
$acl->read_repository();
$log_count = $this->get_log_count();
$data = array(
'allow' => false,
@ -136,6 +141,7 @@ class AclCommandTest extends CommandBase {
$acl->read_repository();
$this->assertFalse($acl->check($data['location'], Acl::ADD, static::APP));
$this->assertEquals(0, $acl->get_specific_rights($data['location'], $data['app']));
$this->assertGreaterThan($log_count, $this->get_log_count(), "Command ($command) did not log");
}
/**
@ -147,6 +153,7 @@ class AclCommandTest extends CommandBase {
$acl = new Acl($this->account_id);
$acl->add_repository(static::APP, $GLOBALS['egw_info']['user']['account_id'], $this->account_id, Acl::READ|Acl::ADD|Acl::EDIT);
$acl->read_repository();
$log_count = $this->get_log_count();
// Run - remove delete
$data = array(
@ -167,6 +174,7 @@ class AclCommandTest extends CommandBase {
$this->assertTrue($acl->check($data['location'], Acl::EDIT, static::APP));
$this->assertTrue($acl->check($data['location'], Acl::DELETE, static::APP));
$this->assertEquals(Acl::READ|Acl::ADD|Acl::EDIT|Acl::DELETE, $acl->get_specific_rights($data['location'], $data['app']));
$this->assertGreaterThan($log_count, $this->get_log_count(), "Command ($command) did not log");
}
/**
@ -178,6 +186,7 @@ class AclCommandTest extends CommandBase {
$acl = new Acl($this->account_id);
$acl->add_repository(static::APP, $GLOBALS['egw_info']['user']['account_id'], $this->account_id, Acl::READ|Acl::ADD|Acl::EDIT|Acl::DELETE);
$acl->read_repository();
$log_count = $this->get_log_count();
// Run - remove delete
$data = array(
@ -198,6 +207,7 @@ class AclCommandTest extends CommandBase {
$this->assertTrue($acl->check($data['location'], Acl::EDIT, static::APP));
$this->assertFalse($acl->check($data['location'], Acl::DELETE, static::APP));
$this->assertEquals(Acl::READ|Acl::ADD|Acl::EDIT, $acl->get_specific_rights($data['location'], $data['app']));
$this->assertGreaterThan($log_count, $this->get_log_count(), "Command ($command) did not log");
}
/**
@ -205,6 +215,7 @@ class AclCommandTest extends CommandBase {
*/
public function testAddForGroupWhenEmpty()
{
$log_count = $this->get_log_count();
// Set up
$data = array(
'allow' => true,
@ -226,6 +237,7 @@ class AclCommandTest extends CommandBase {
$acl = new Acl($this->account_id);
$this->assertTrue($acl->check($data['location'], Acl::ADD, static::APP));
$this->assertEquals($data['rights'], $acl->get_rights($data['location'], $data['app']));
$this->assertGreaterThan($log_count, $this->get_log_count(), "Command ($command) did not log");
}
/**
@ -238,6 +250,7 @@ class AclCommandTest extends CommandBase {
$acl->add_repository(static::APP, $GLOBALS['egw_info']['user']['account_id'], $this->group_id, Acl::ADD);
$acl->read_repository();
$this->assertTrue($acl->check($GLOBALS['egw_info']['user']['account_id'], Acl::ADD, static::APP));
$log_count = $this->get_log_count();
$data = array(
'allow' => false,
@ -261,6 +274,7 @@ class AclCommandTest extends CommandBase {
$this->assertFalse($acl->check($data['location'], Acl::ADD, static::APP));
$this->assertEquals(0, $acl->get_rights($data['location'], $data['app']));
$this->assertGreaterThan($log_count, $this->get_log_count(), "Command ($command) did not log");
}
@ -271,6 +285,8 @@ class AclCommandTest extends CommandBase {
public function testAddForEntry()
{
// Set up
$log_count = $this->get_log_count();
$data = array(
'allow' => true,
'account' => $this->account_id,
@ -286,6 +302,7 @@ class AclCommandTest extends CommandBase {
$acl = new Acl($this->account_id);
$this->assertTrue($acl->check($data['location'], Acl::EDIT, static::APP));
$this->assertEquals($data['rights'], $acl->get_specific_rights($data['location'], $data['app']));
$this->assertGreaterThan($log_count, $this->get_log_count(), "Command ($command) did not log");
}
/**
@ -298,6 +315,7 @@ class AclCommandTest extends CommandBase {
$acl = new Acl($this->account_id);
$acl->add_repository(static::APP, 'A' . $GLOBALS['egw_info']['user']['person_id'], $this->account_id, Acl::ADD);
$acl->read_repository();
$log_count = $this->get_log_count();
$data = array(
'allow' => false,
@ -314,5 +332,6 @@ class AclCommandTest extends CommandBase {
$acl->read_repository();
$this->assertFalse($acl->check($data['location'], Acl::ADD, static::APP));
$this->assertEquals(0, $acl->get_specific_rights($data['location'], $data['app']));
$this->assertGreaterThan($log_count, $this->get_log_count(), "Command ($command) did not log");
}
}

View File

@ -21,5 +21,13 @@ require_once realpath(__DIR__.'/../../api/tests/LoggedInTest.php'); // Applicati
*/
abstract class CommandBase extends EGroupware\Api\LoggedInTest
{
// Nothing here yet
/**
* Get how many entries in the admin command history log so we can see if
* a command made a log entry.
*/
protected function get_log_count()
{
return count(admin_cmd::search(array()));
}
}

View File

@ -0,0 +1,200 @@
<?php
/**
* Tests for ACL command
*
* @link http://www.egroupware.org
* @author Nathan Gray
* @copyright (c) 2018 Nathan Gray
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
*/
// test base providing common stuff
require_once __DIR__.'/CommandBase.php';
use EGroupware\Api;
class GroupCommandTest extends CommandBase {
// User for testing
protected $account_id;
// Define group details once, then modify as needed for tests
protected $group = array(
'account_lid' => 'Group Command Test',
'account_members' => array()
);
public function setUp()
{
// Can't set this until now - value is not available
$this->group['account_members'] = array($GLOBALS['egw_info']['user']['account_id']);
}
public function tearDown()
{
// Delete the accounts we created
if($this->group_id)
{
$GLOBALS['egw']->accounts->delete($this->group_id);
}
if($this->account_id)
{
$GLOBALS['egw']->accounts->delete($this->account_id);
}
parent::tearDown();
}
/**
* Test that adding a group works when we give it what it needs
*/
public function testAddGroup()
{
// Set up
$pre_search = $GLOBALS['egw']->accounts->search(array('type' => 'both'));
$log_count = $this->get_log_count();
// Execute
$command = new admin_cmd_edit_group(false, $this->group);
$command->comment = 'Needed for unit test ' . $this->getName();
$command->run();
$this->group_id = $command->account;
// Check
$post_search = $GLOBALS['egw']->accounts->search(array('type' => 'both'));
$this->assertNotEmpty($this->group_id, 'Did not create test group account');
$this->assertEquals(count($pre_search) + 1, count($post_search), 'Should have one more account than before');
$this->assertArrayHasKey($this->group_id, $post_search);
$this->assertGreaterThan($log_count, $this->get_log_count(), "Command ($command) did not log");
}
/**
* Try to add a new group with the same name as existing group. It should
* throw an exception
*/
public function testGroupAlreadyExists()
{
// Set up
$pre_search = $GLOBALS['egw']->accounts->search(array('type' => 'both'));
$this->expectException(Api\Exception\WrongUserinput::class);
// Execute
$this->account['account_lid'] = 'Default';
$command = new admin_cmd_edit_group(false, $this->account);
$command->comment = 'Needed for unit test ' . $this->getName();
$command->run();
$this->group_id = $command->account;
// Check
$post_search = $GLOBALS['egw']->accounts->search(array('type' => 'both'));
$this->assertEquals(count($pre_search), count($post_search), 'Should have same number of accounts as before');
}
/**
* Try to add a new group without specifying the name. It should throw an
* exception
*/
public function testNameMissing()
{
// Set up
$pre_search = $GLOBALS['egw']->accounts->search(array('type' => 'both'));
$this->expectException(Api\Exception\WrongUserinput::class);
$account = $this->group;
unset($account['account_lid']);
// Execute
$command = new admin_cmd_edit_group(false, $account);
$command->comment = 'Needed for unit test ' . $this->getName();
$command->run();
$this->group_id = $command->account;
// Check
$post_search = $GLOBALS['egw']->accounts->search(array('type' => 'both'));
$this->assertEquals(count($pre_search), count($post_search), 'Should have same number of accounts as before');
}
/**
* Try to add a new group without specifying members. It should throw
* an exception
*/
public function testMembersMissing()
{
// Set up
$pre_search = $GLOBALS['egw']->accounts->search(array('type' => 'both'));
$this->expectException(Api\Exception\WrongUserinput::class);
$account = $this->group;
unset($account['account_members']);
// Execute
$command = new admin_cmd_edit_group(false, $account);
$command->comment = 'Needed for unit test ' . $this->getName();
$command->run();
$this->group_id = $command->account;
// Check
$post_search = $GLOBALS['egw']->accounts->search(array('type' => 'both'));
$this->assertEquals(count($pre_search), count($post_search), 'Should have same number of accounts as before');
}
/**
* Test adding & removing a new member
*/
public function testChangeMembers()
{
// Set up
// Make a new user so it doesn't matter if something goes wrong
$account = array(
'account_lid' => 'test_user',
'account_firstname' => 'TestUser',
'account_lastname' => 'Test',
'account_primary_group' => 'Default',
'account_groups' => array('Default')
);
if(($account_id = $GLOBALS['egw']->accounts->name2id($account['account_lid'])))
{
// Delete if there in case something went wrong
$GLOBALS['egw']->accounts->delete($account_id);
}
$command = new admin_cmd_edit_user(false, $account);
$command->comment = 'Needed for unit test ' . $this->getName();
$command->run();
$this->account_id = $command->account;
$command = new admin_cmd_edit_group(false, $this->group);
$command->comment = 'Needed for unit test ' . $this->getName();
$command->run();
$this->group_id = $command->account;
// Count accounts
$pre_search = $GLOBALS['egw']->accounts->search(array('type' => 'both'));
$log_count = $this->get_log_count();
// Execute
$account = $this->account;
$account['account_members'][] = $this->account_id;
$command = new admin_cmd_edit_group($this->group_id, $account);
$command->comment = 'Needed for unit test ' . $this->getName();
$command->run();
// Check
$post_search = $GLOBALS['egw']->accounts->search(array('type' => 'both'));
$this->assertEquals(count($pre_search), count($post_search), 'Should have same number of accounts as before');
$this->assertGreaterThan($log_count, $this->get_log_count(), "Command ($command) did not log");
// Now remove
$pre_search = $post_search;
$log_count = $this->get_log_count();
$account = $this->account;
$command = new admin_cmd_edit_group($this->group_id, $account);
$command->comment = 'Needed for unit test ' . $this->getName();
$command->run();
// Check
$post_search = $GLOBALS['egw']->accounts->search(array('type' => 'both'));
$this->assertEquals(count($pre_search), count($post_search), 'Should have same number of accounts as before');
$this->assertGreaterThan($log_count, $this->get_log_count(), "Command ($command) did not log");
}
}

View File

@ -0,0 +1,187 @@
<?php
/**
* Tests for ACL command
*
* @link http://www.egroupware.org
* @author Nathan Gray
* @copyright (c) 2018 Nathan Gray
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
*/
// test base providing common stuff
require_once __DIR__.'/CommandBase.php';
use EGroupware\Api;
class UserCommandTest extends CommandBase {
// User for testing
protected $account_id;
// Define account details once, then modify as needed for tests
protected $account = array(
'account_lid' => 'user_test',
'account_firstname' => 'UserCommand',
'account_lastname' => 'Test'
);
public function tearDown()
{
if($this->account_id)
{
$GLOBALS['egw']->accounts->delete($this->account_id);
}
parent::tearDown();
}
/**
* Test that adding a user works when we give it what it needs
*/
public function testAddUser()
{
// Set up
$pre_search = $GLOBALS['egw']->accounts->search(array('type' => 'both'));
$log_count = $this->get_log_count();
// Execute
$command = new admin_cmd_edit_user(false, $this->account);
$command->comment = 'Needed for unit test ' . $this->getName();
$command->run();
$this->account_id = $command->account;
// Check
$post_search = $GLOBALS['egw']->accounts->search(array('type' => 'both'));
$this->assertNotEmpty($this->account_id, 'Did not create test user account');
$this->assertEquals(count($pre_search) + 1, count($post_search), 'Should have one more account than before');
$this->assertArrayHasKey($this->account_id, $post_search);
$this->assertGreaterThan($log_count, $this->get_log_count(), "Command ($command) did not log");
}
/**
* Try to add a new user with the same login as current user. It should
* throw an exception
*/
public function testUserAlreadyExists()
{
// Set up
$pre_search = $GLOBALS['egw']->accounts->search(array('type' => 'both'));
$this->expectException(Api\Exception\WrongUserinput::class);
// Execute
$this->account['account_lid'] = $GLOBALS['egw_info']['user']['account_lid'];
$command = new admin_cmd_edit_user(false, $this->account);
$command->comment = 'Needed for unit test ' . $this->getName();
$command->run();
$this->account_id = $command->account;
// Check
$post_search = $GLOBALS['egw']->accounts->search(array('type' => 'both'));
$this->assertEquals(count($pre_search), count($post_search), 'Should have same number of accounts as before');
}
/**
* Try to add a new user without specifying the login. It should throw an
* exception
*/
public function testLoginMissing()
{
// Set up
$pre_search = $GLOBALS['egw']->accounts->search(array('type' => 'both'));
$this->expectException(Api\Exception\WrongUserinput::class);
$account = $this->account;
unset($account['account_lid']);
// Execute
$command = new admin_cmd_edit_user(false, $account);
$command->comment = 'Needed for unit test ' . $this->getName();
$command->run();
$this->account_id = $command->account;
// Check
$post_search = $GLOBALS['egw']->accounts->search(array('type' => 'both'));
$this->assertEquals(count($pre_search), count($post_search), 'Should have same number of accounts as before');
}
/**
* Try to add a new user without specifying the last name. It should throw
* an exception
*/
public function testLastnameMissing()
{
// Set up
$pre_search = $GLOBALS['egw']->accounts->search(array('type' => 'both'));
$this->expectException(Api\Exception\WrongUserinput::class);
$account = $this->account;
unset($account['account_lastname']);
// Execute
$command = new admin_cmd_edit_user(false, $account);
$command->comment = 'Needed for unit test ' . $this->getName();
$command->run();
$this->account_id = $command->account;
// Check
$post_search = $GLOBALS['egw']->accounts->search(array('type' => 'both'));
$this->assertEquals(count($pre_search), count($post_search), 'Should have same number of accounts as before');
}
/**
* If password is provided, password2 must be provided or an exception is thrown
*/
public function testPasswordOnce()
{
// Set up
$command = new admin_cmd_edit_user(false, $this->account);
$command->comment = 'Setup for unit test ' . $this->getName();
$command->run();
$this->account_id = $command->account;
$pre_search = $GLOBALS['egw']->accounts->search(array('type' => 'both'));
$account = $this->account;
$account['account_passwd'] = 'passw0rd';
$this->expectException(Api\Exception\WrongUserinput::class);
// Execute
$command = new admin_cmd_edit_user(false, $account);
$command->comment = 'Needed for unit test ' . $this->getName();
$command->run();
// Check
$post_search = $GLOBALS['egw']->accounts->search(array('type' => 'both'));
$this->assertEquals(count($pre_search), count($post_search), 'Should have same number of accounts as before');
}
/**
* If password is provided, password2 must be provided and the same or an
* an exception is thrown
*/
public function testPasswordMismatch()
{
// Set up
$command = new admin_cmd_edit_user(false, $this->account);
$command->comment = 'Setup for unit test ' . $this->getName();
$command->run();
$this->account_id = $command->account;
$pre_search = $GLOBALS['egw']->accounts->search(array('type' => 'both'));
$account = $this->account;
$account['account_passwd'] = 'passw0rd';
$account['account_passwd_2'] = 'pAssw0rd';
$this->expectException(Api\Exception\WrongUserinput::class);
// Execute
$command = new admin_cmd_edit_user(false, $account);
$command->comment = 'Needed for unit test ' . $this->getName();
$command->run();
// Check
$post_search = $GLOBALS['egw']->accounts->search(array('type' => 'both'));
$this->assertEquals(count($pre_search), count($post_search), 'Should have same number of accounts as before');
}
}