2010-12-05 09:41:38 +01:00
|
|
|
<?php
|
2010-12-05 09:22:57 +01:00
|
|
|
/**
|
|
|
|
* @package sapphire
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
class SecurityTokenTest extends SapphireTest {
|
|
|
|
|
|
|
|
function testIsEnabled() {
|
|
|
|
$inst1 = SecurityToken::inst();
|
|
|
|
$this->assertTrue($inst1->isEnabled());
|
|
|
|
|
|
|
|
SecurityToken::disable();
|
|
|
|
$inst2 = SecurityToken::inst();
|
|
|
|
$this->assertFalse($inst2->isEnabled());
|
|
|
|
|
|
|
|
SecurityToken::enable();
|
|
|
|
}
|
|
|
|
|
|
|
|
function testEnableAndDisable() {
|
|
|
|
$inst = SecurityToken::inst();
|
|
|
|
$this->assertFalse($inst->check('randomvalue'));
|
|
|
|
|
|
|
|
SecurityToken::disable();
|
|
|
|
$inst = SecurityToken::inst();
|
|
|
|
$this->assertTrue($inst->check('randomvalue'));
|
|
|
|
|
|
|
|
SecurityToken::enable();
|
|
|
|
$inst = SecurityToken::inst();
|
|
|
|
$this->assertFalse($inst->check('randomvalue'));
|
|
|
|
}
|
|
|
|
|
|
|
|
function testIsEnabledStatic() {
|
|
|
|
$this->assertTrue(SecurityToken::is_enabled());
|
|
|
|
|
|
|
|
SecurityToken::disable();
|
|
|
|
$this->assertFalse(SecurityToken::is_enabled());
|
|
|
|
|
|
|
|
SecurityToken::enable();
|
|
|
|
$this->assertTrue(SecurityToken::is_enabled());
|
|
|
|
}
|
|
|
|
|
|
|
|
function testInst() {
|
|
|
|
$inst1 = SecurityToken::inst();
|
2011-03-11 03:06:09 +01:00
|
|
|
$this->assertType('SecurityToken', $inst1);
|
2010-12-05 09:22:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function testInstReturnsSingleton() {
|
|
|
|
$inst1 = SecurityToken::inst();
|
|
|
|
$inst2 = SecurityToken::inst();
|
|
|
|
$this->assertEquals($inst1, $inst2);
|
|
|
|
}
|
|
|
|
|
|
|
|
function testCheck() {
|
|
|
|
$t = new SecurityToken();
|
|
|
|
|
|
|
|
$t->setValue(null);
|
|
|
|
$this->assertFalse($t->check('invalidtoken'), 'Any token is invalid if no token is stored');
|
|
|
|
|
|
|
|
$t->setValue(null);
|
|
|
|
$this->assertFalse($t->check(null), 'NULL token is invalid if no token is stored');
|
|
|
|
|
|
|
|
$t->setValue('mytoken');
|
|
|
|
$this->assertFalse($t->check('invalidtoken'), 'Invalid token returns false');
|
|
|
|
|
|
|
|
$t->setValue('mytoken');
|
|
|
|
$this->assertTrue($t->check('mytoken'), 'Valid token returns true');
|
|
|
|
}
|
|
|
|
|
|
|
|
function testCheckRequest() {
|
|
|
|
$t = new SecurityToken();
|
|
|
|
$n = $t->getName();
|
|
|
|
|
|
|
|
$t->setValue(null);
|
|
|
|
$r = new SS_HTTPRequest('GET', 'dummy', array($n => 'invalidtoken'));
|
|
|
|
$this->assertFalse($t->checkRequest($r), 'Any token is invalid if no token is stored');
|
|
|
|
|
|
|
|
$t->setValue(null);
|
|
|
|
$r = new SS_HTTPRequest('GET', 'dummy', array($n => null));
|
|
|
|
$this->assertFalse($t->checkRequest($r), 'NULL token is invalid if no token is stored');
|
|
|
|
|
|
|
|
$t->setValue('mytoken');
|
|
|
|
$r = new SS_HTTPRequest('GET', 'dummy', array($n => 'invalidtoken'));
|
|
|
|
$this->assertFalse($t->checkRequest($r), 'Invalid token returns false');
|
|
|
|
|
|
|
|
$t->setValue('mytoken');
|
|
|
|
$r = new SS_HTTPRequest('GET', 'dummy', array($n => 'mytoken'));
|
|
|
|
$this->assertTrue($t->checkRequest($r), 'Valid token returns true');
|
|
|
|
}
|
|
|
|
|
|
|
|
function testAddToUrl() {
|
|
|
|
$t = new SecurityToken();
|
|
|
|
|
|
|
|
$url = 'http://absolute.tld/action/';
|
|
|
|
$this->assertEquals(
|
|
|
|
sprintf('%s?%s=%s', $url, $t->getName(), $t->getValue()),
|
|
|
|
$t->addToUrl($url),
|
|
|
|
'Urls without existing GET parameters'
|
|
|
|
);
|
|
|
|
|
|
|
|
$url = 'http://absolute.tld/?getparam=1';
|
|
|
|
$this->assertEquals(
|
|
|
|
sprintf('%s&%s=%s', $url, $t->getName(), $t->getValue()),
|
|
|
|
$t->addToUrl($url),
|
|
|
|
'Urls with existing GET parameters'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function testUpdateFieldSet() {
|
2011-05-11 09:51:54 +02:00
|
|
|
$fs = new FieldList();
|
2010-12-05 09:22:57 +01:00
|
|
|
$t = new SecurityToken();
|
|
|
|
$t->updateFieldSet($fs);
|
|
|
|
$f = $fs->dataFieldByName($t->getName());
|
|
|
|
|
2011-03-11 03:06:09 +01:00
|
|
|
$this->assertType('HiddenField', $f);
|
2011-10-29 06:01:52 +02:00
|
|
|
$this->assertEquals($f->getName(), $t->getName(), 'Name matches');
|
2010-12-05 09:22:57 +01:00
|
|
|
$this->assertEquals($f->Value(), $t->getValue(), 'Value matches');
|
|
|
|
}
|
|
|
|
|
|
|
|
function testUpdateFieldSetDoesntAddTwice() {
|
2011-05-11 09:51:54 +02:00
|
|
|
$fs = new FieldList();
|
2010-12-05 09:22:57 +01:00
|
|
|
$t = new SecurityToken();
|
|
|
|
$t->updateFieldSet($fs); // first
|
|
|
|
$t->updateFieldSet($fs); // second
|
|
|
|
$f = $fs->dataFieldByName($t->getName());
|
|
|
|
|
2011-03-11 03:06:09 +01:00
|
|
|
$this->assertType('HiddenField', $f);
|
2010-12-05 09:22:57 +01:00
|
|
|
$this->assertEquals(1, $fs->Count());
|
|
|
|
}
|
|
|
|
|
|
|
|
function testUnnamedTokensCarrySameValue() {
|
|
|
|
$t1 = new SecurityToken();
|
|
|
|
$t2 = new SecurityToken();
|
|
|
|
|
|
|
|
$this->assertEquals($t1->getName(), $t2->getName());
|
|
|
|
$this->assertEquals($t1->getValue(), $t2->getValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
function testNamedTokensCarryDifferentValues() {
|
|
|
|
$t1 = new SecurityToken('one');
|
|
|
|
$t2 = new SecurityToken('two');
|
|
|
|
|
|
|
|
$this->assertNotEquals($t1->getName(), $t2->getName());
|
|
|
|
$this->assertNotEquals($t1->getValue(), $t2->getValue());
|
|
|
|
}
|
|
|
|
}
|