silverstripe-framework/tests/security/SecurityTokenTest.php

154 lines
4.2 KiB
PHP
Raw Normal View History

<?php
/**
* @package framework
* @subpackage tests
*/
class SecurityTokenTest extends SapphireTest {
2014-08-15 08:53:05 +02:00
public function testIsEnabled() {
$inst1 = SecurityToken::inst();
$this->assertTrue($inst1->isEnabled());
2014-08-15 08:53:05 +02:00
SecurityToken::disable();
$inst2 = SecurityToken::inst();
$this->assertFalse($inst2->isEnabled());
2014-08-15 08:53:05 +02:00
SecurityToken::enable();
}
public function testEnableAndDisable() {
$inst = SecurityToken::inst();
$this->assertFalse($inst->check('randomvalue'));
SecurityToken::disable();
$inst = SecurityToken::inst();
$this->assertTrue($inst->check('randomvalue'));
2014-08-15 08:53:05 +02:00
SecurityToken::enable();
$inst = SecurityToken::inst();
$this->assertFalse($inst->check('randomvalue'));
}
2014-08-15 08:53:05 +02:00
public function testIsEnabledStatic() {
$this->assertTrue(SecurityToken::is_enabled());
SecurityToken::disable();
$this->assertFalse(SecurityToken::is_enabled());
2014-08-15 08:53:05 +02:00
SecurityToken::enable();
$this->assertTrue(SecurityToken::is_enabled());
}
public function testInst() {
$inst1 = SecurityToken::inst();
$this->assertInstanceOf('SecurityToken', $inst1);
}
2014-08-15 08:53:05 +02:00
public function testInstReturnsSingleton() {
$inst1 = SecurityToken::inst();
$inst2 = SecurityToken::inst();
$this->assertEquals($inst1, $inst2);
}
public 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');
2014-08-15 08:53:05 +02:00
$t->setValue('mytoken');
$this->assertFalse($t->check('invalidtoken'), 'Invalid token returns false');
2014-08-15 08:53:05 +02:00
$t->setValue('mytoken');
$this->assertTrue($t->check('mytoken'), 'Valid token returns true');
}
public function testReset() {
$t = new SecurityToken();
$initialValue = $t->getValue();
2014-08-15 08:53:05 +02:00
$t->reset();
$this->assertNotEquals($t->getValue(), $initialValue);
}
2014-08-15 08:53:05 +02:00
public 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');
2014-08-15 08:53:05 +02:00
$t->setValue('mytoken');
$r = new SS_HTTPRequest('GET', 'dummy', array($n => 'invalidtoken'));
$this->assertFalse($t->checkRequest($r), 'Invalid token returns false');
2014-08-15 08:53:05 +02:00
$t->setValue('mytoken');
$r = new SS_HTTPRequest('GET', 'dummy', array($n => 'mytoken'));
$this->assertTrue($t->checkRequest($r), 'Valid token returns true');
}
2014-08-15 08:53:05 +02:00
public function testAddToUrl() {
$t = new SecurityToken();
$url = 'http://absolute.tld/action/';
$this->assertEquals(
2014-08-15 08:53:05 +02:00
sprintf('%s?%s=%s', $url, $t->getName(), $t->getValue()),
$t->addToUrl($url),
'Urls without existing GET parameters'
);
2014-08-15 08:53:05 +02:00
$url = 'http://absolute.tld/?getparam=1';
$this->assertEquals(
2014-08-15 08:53:05 +02:00
sprintf('%s&%s=%s', $url, $t->getName(), $t->getValue()),
$t->addToUrl($url),
'Urls with existing GET parameters'
);
}
2014-08-15 08:53:05 +02:00
public function testUpdateFieldSet() {
$fs = new FieldList();
2014-08-15 08:53:05 +02:00
$t = new SecurityToken();
$t->updateFieldSet($fs);
$f = $fs->dataFieldByName($t->getName());
2014-08-15 08:53:05 +02:00
$this->assertInstanceOf('HiddenField', $f);
$this->assertEquals($f->getName(), $t->getName(), 'Name matches');
$this->assertEquals($f->Value(), $t->getValue(), 'Value matches');
}
2014-08-15 08:53:05 +02:00
public function testUpdateFieldSetDoesntAddTwice() {
$fs = new FieldList();
2014-08-15 08:53:05 +02:00
$t = new SecurityToken();
$t->updateFieldSet($fs); // first
$t->updateFieldSet($fs); // second
$f = $fs->dataFieldByName($t->getName());
2014-08-15 08:53:05 +02:00
$this->assertInstanceOf('HiddenField', $f);
$this->assertEquals(1, $fs->Count());
}
2014-08-15 08:53:05 +02:00
public function testUnnamedTokensCarrySameValue() {
$t1 = new SecurityToken();
$t2 = new SecurityToken();
2014-08-15 08:53:05 +02:00
$this->assertEquals($t1->getName(), $t2->getName());
$this->assertEquals($t1->getValue(), $t2->getValue());
}
2014-08-15 08:53:05 +02:00
public function testNamedTokensCarryDifferentValues() {
$t1 = new SecurityToken('one');
$t2 = new SecurityToken('two');
2014-08-15 08:53:05 +02:00
$this->assertNotEquals($t1->getName(), $t2->getName());
$this->assertNotEquals($t1->getValue(), $t2->getValue());
}
}