SecurityTokenTest.php
4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
/**
* @package framework
* @subpackage tests
*/
class SecurityTokenTest extends SapphireTest {
public function testIsEnabled() {
$inst1 = SecurityToken::inst();
$this->assertTrue($inst1->isEnabled());
SecurityToken::disable();
$inst2 = SecurityToken::inst();
$this->assertFalse($inst2->isEnabled());
SecurityToken::enable();
}
public 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'));
}
public function testIsEnabledStatic() {
$this->assertTrue(SecurityToken::is_enabled());
SecurityToken::disable();
$this->assertFalse(SecurityToken::is_enabled());
SecurityToken::enable();
$this->assertTrue(SecurityToken::is_enabled());
}
public function testInst() {
$inst1 = SecurityToken::inst();
$this->assertInstanceOf('SecurityToken', $inst1);
}
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');
$t->setValue('mytoken');
$this->assertFalse($t->check('invalidtoken'), 'Invalid token returns false');
$t->setValue('mytoken');
$this->assertTrue($t->check('mytoken'), 'Valid token returns true');
}
public function testReset() {
$t = new SecurityToken();
$initialValue = $t->getValue();
$t->reset();
$this->assertNotEquals($t->getValue(), $initialValue);
}
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');
$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');
}
public 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'
);
}
public function testUpdateFieldSet() {
$fs = new FieldList();
$t = new SecurityToken();
$t->updateFieldSet($fs);
$f = $fs->dataFieldByName($t->getName());
$this->assertInstanceOf('HiddenField', $f);
$this->assertEquals($f->getName(), $t->getName(), 'Name matches');
$this->assertEquals($f->Value(), $t->getValue(), 'Value matches');
}
public function testUpdateFieldSetDoesntAddTwice() {
$fs = new FieldList();
$t = new SecurityToken();
$t->updateFieldSet($fs); // first
$t->updateFieldSet($fs); // second
$f = $fs->dataFieldByName($t->getName());
$this->assertInstanceOf('HiddenField', $f);
$this->assertEquals(1, $fs->Count());
}
public function testUnnamedTokensCarrySameValue() {
$t1 = new SecurityToken();
$t2 = new SecurityToken();
$this->assertEquals($t1->getName(), $t2->getName());
$this->assertEquals($t1->getValue(), $t2->getValue());
}
public function testNamedTokensCarryDifferentValues() {
$t1 = new SecurityToken('one');
$t2 = new SecurityToken('two');
$this->assertNotEquals($t1->getName(), $t2->getName());
$this->assertNotEquals($t1->getValue(), $t2->getValue());
}
}