|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace OpCacheGUI\Unit\Security; |
|
4
|
|
|
|
|
5
|
|
|
use OpCacheGUI\Security\CsrfToken; |
|
6
|
|
|
|
|
7
|
|
|
class CsrfTokenTest extends \PHPUnit_Framework_TestCase |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @covers OpCacheGUI\Security\CsrfToken::__construct |
|
11
|
|
|
* @covers OpCacheGUI\Security\CsrfToken::get |
|
12
|
|
|
*/ |
|
13
|
|
|
public function testGetWhenAlreadyStored() |
|
14
|
|
|
{ |
|
15
|
|
|
$storage = $this->getMock('\\OpCacheGUI\\Storage\\KeyValuePair'); |
|
16
|
|
|
$storage->method('isKeyValid')->willReturn(true); |
|
17
|
|
|
$storage->method('get')->will($this->returnArgument(0)); |
|
18
|
|
|
|
|
19
|
|
|
$token = new CsrfToken($storage); |
|
20
|
|
|
|
|
21
|
|
|
$this->assertSame('csrfToken', $token->get()); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @covers OpCacheGUI\Security\CsrfToken::__construct |
|
26
|
|
|
* @covers OpCacheGUI\Security\CsrfToken::get |
|
27
|
|
|
* @covers OpCacheGUI\Security\CsrfToken::generate |
|
28
|
|
|
*/ |
|
29
|
|
|
public function testGetWhenNotStored() |
|
30
|
|
|
{ |
|
31
|
|
|
$storage = $this->getMock('\\OpCacheGUI\\Storage\\KeyValuePair'); |
|
32
|
|
|
$storage->method('isKeyValid')->willReturn(false); |
|
33
|
|
|
$storage->method('get')->will($this->returnArgument(0)); |
|
34
|
|
|
|
|
35
|
|
|
$generator = $this->getMock('\\OpCacheGUI\\Security\\Generator'); |
|
36
|
|
|
$generator->method('generate')->willReturn('12345678901234567890123456789012345678901234567890123456'); |
|
37
|
|
|
|
|
38
|
|
|
$token = new CsrfToken($storage); |
|
39
|
|
|
|
|
40
|
|
|
$this->assertSame('csrfToken', $token->get()); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @covers OpCacheGUI\Security\CsrfToken::__construct |
|
45
|
|
|
* @covers OpCacheGUI\Security\CsrfToken::validate |
|
46
|
|
|
* @covers OpCacheGUI\Security\CsrfToken::get |
|
47
|
|
|
*/ |
|
48
|
|
|
public function testValidateValid() |
|
49
|
|
|
{ |
|
50
|
|
|
$storage = $this->getMock('\\OpCacheGUI\\Storage\\KeyValuePair'); |
|
51
|
|
|
$storage->method('isKeyValid')->willReturn(true); |
|
52
|
|
|
$storage->method('get')->will($this->returnArgument(0)); |
|
53
|
|
|
|
|
54
|
|
|
$token = new CsrfToken($storage); |
|
55
|
|
|
|
|
56
|
|
|
$this->assertTrue($token->validate('csrfToken')); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @covers OpCacheGUI\Security\CsrfToken::__construct |
|
61
|
|
|
* @covers OpCacheGUI\Security\CsrfToken::validate |
|
62
|
|
|
* @covers OpCacheGUI\Security\CsrfToken::get |
|
63
|
|
|
*/ |
|
64
|
|
|
public function testValidateNotValid() |
|
65
|
|
|
{ |
|
66
|
|
|
$storage = $this->getMock('\\OpCacheGUI\\Storage\\KeyValuePair'); |
|
67
|
|
|
$storage->method('isKeyValid')->willReturn(true); |
|
68
|
|
|
$storage->method('get')->will($this->returnArgument(0)); |
|
69
|
|
|
|
|
70
|
|
|
$token = new CsrfToken($storage); |
|
71
|
|
|
|
|
72
|
|
|
$this->assertFalse($token->validate('notvalid')); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|