Refactored to make unit tests more robust
This commit is contained in:
parent
833a25daf3
commit
c2152c1c84
3 changed files with 46 additions and 27 deletions
|
@ -124,7 +124,13 @@ class core_userkey_manager implements userkey_manager_interface {
|
|||
|
||||
if ($key->iprestriction) {
|
||||
$remoteaddr = getremoteaddr(null);
|
||||
$whitelist = get_config('auth_userkey', 'ipwhitelist');
|
||||
|
||||
if (isset($this->config->ipwhitelist)) {
|
||||
$whitelist = $this->config->ipwhitelist;
|
||||
} else {
|
||||
$whitelist = false;
|
||||
}
|
||||
|
||||
if (empty($remoteaddr) ) {
|
||||
print_error('noip', 'auth_userkey');
|
||||
} else if (!empty($whitelist)) {
|
||||
|
|
|
@ -843,32 +843,6 @@ class auth_plugin_userkey_testcase extends advanced_testcase {
|
|||
$this->auth->user_login_userkey();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that IP address mismatch is ingored if IP is whitelisted.
|
||||
*
|
||||
* @expectedException moodle_exception
|
||||
* @expectedExceptionMessage Unsupported redirect to http://www.example.com/moodle detected, execution terminated.
|
||||
*/
|
||||
public function test_ipmismatch_exception_notthrown_if_ip_is_whitelisted() {
|
||||
global $DB;
|
||||
|
||||
set_config('ipwhitelist', '10.0.0.0/8;172.16.0.0/12;192.168.0.0/16', 'auth_userkey');
|
||||
|
||||
$key = new stdClass();
|
||||
$key->value = 'IpmismatchKey';
|
||||
$key->script = 'auth/userkey';
|
||||
$key->userid = $this->user->id;
|
||||
$key->instance = $this->user->id;
|
||||
$key->iprestriction = '192.168.1.1';
|
||||
$key->validuntil = time() + 300;
|
||||
$key->timecreated = time();
|
||||
$DB->insert_record('user_private_key', $key);
|
||||
|
||||
$_POST['key'] = 'IpmismatchKey';
|
||||
$_SERVER['HTTP_CLIENT_IP'] = '192.168.1.2';
|
||||
@$this->auth->user_login_userkey();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that IP address mismatch exception gets thrown if incorrect IP and outside whitelist.
|
||||
*
|
||||
|
|
|
@ -149,6 +149,45 @@ class core_userkey_manager_testcase extends advanced_testcase {
|
|||
$this->assertEquals(time() + 60, $actualkey->validuntil);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that IP address mismatch exception gets thrown if incorrect IP and outside whitelist.
|
||||
*
|
||||
* @expectedException moodle_exception
|
||||
* @expectedExceptionMessage Client IP address mismatch
|
||||
*/
|
||||
public function test_exception_if_ip_is_outside_whitelist() {
|
||||
global $DB;
|
||||
|
||||
$this->config->iprestriction = true;
|
||||
$this->config->ipwhitelist = '10.0.0.0/8;172.16.0.0/12;192.168.0.0/16';
|
||||
|
||||
$manager = new core_userkey_manager($this->config);
|
||||
$value = $manager->create_key($this->user->id, '193.168.1.1');
|
||||
|
||||
$_SERVER['HTTP_CLIENT_IP'] = '193.168.1.2';
|
||||
|
||||
$manager->validate_key($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that key is accepted if incorrect IP and within whitelist.
|
||||
*/
|
||||
public function test_create_correct_key_if_ip_is_whitelisted() {
|
||||
global $DB;
|
||||
|
||||
$this->config->iprestriction = true;
|
||||
|
||||
$this->config->ipwhitelist = '10.0.0.0/8;172.16.0.0/12;192.168.0.0/16';
|
||||
|
||||
$manager = new core_userkey_manager($this->config);
|
||||
$value = $manager->create_key($this->user->id, '192.168.1.1');
|
||||
|
||||
$_SERVER['HTTP_CLIENT_IP'] = '192.168.1.2';
|
||||
|
||||
$key = $manager->validate_key($value);
|
||||
$this->assertEquals($this->user->id, $key->userid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that key gets created correctly if config option iprestriction is set to false and we set allowedips.
|
||||
*/
|
||||
|
|
Loading…
Reference in a new issue