1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the U2F Security bundle. |
5
|
|
|
* |
6
|
|
|
* (c) Michael Barbey <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Mbarbey\U2fSecurityBundle\Tests\Event\Authentication; |
13
|
|
|
|
14
|
|
|
use Symfony\Bundle\FrameworkBundle\Tests\TestCase; |
15
|
|
|
use Mbarbey\U2fSecurityBundle\Model\User\U2fUser; |
16
|
|
|
use Mbarbey\U2fSecurityBundle\Event\Authentication\U2fPreAuthenticationEvent; |
17
|
|
|
|
18
|
|
|
class U2fPreAuthenticationEventTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
private $appId; |
21
|
|
|
private $event; |
22
|
|
|
private $user; |
23
|
|
|
private $reason; |
24
|
|
|
|
25
|
|
|
public function testName() |
26
|
|
|
{ |
27
|
|
|
$name = 'u2f.authentication.before'; |
28
|
|
|
|
29
|
|
|
$this->assertEquals(U2fPreAuthenticationEvent::getName(), $name); |
30
|
|
|
|
31
|
|
|
$this->setUpAllowed(); |
32
|
|
|
$this->assertEquals($this->event->getName(), $name); |
33
|
|
|
|
34
|
|
|
$this->setUpDeniedWithReason(); |
35
|
|
|
$this->assertEquals($this->event->getName(), $name); |
36
|
|
|
|
37
|
|
|
$this->setUpDeniedWithoutReason(); |
38
|
|
|
$this->assertEquals($this->event->getName(), $name); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testAppId() |
42
|
|
|
{ |
43
|
|
|
$this->setUpAllowed(); |
44
|
|
|
$this->assertEquals($this->event->getAppId(), $this->appId); |
45
|
|
|
|
46
|
|
|
$this->setUpDeniedWithReason(); |
47
|
|
|
$this->assertEquals($this->event->getAppId(), $this->appId); |
48
|
|
|
|
49
|
|
|
$this->setUpDeniedWithoutReason(); |
50
|
|
|
$this->assertEquals($this->event->getAppId(), $this->appId); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testUser() |
54
|
|
|
{ |
55
|
|
|
$this->setUpAllowed(); |
56
|
|
|
$this->assertEquals($this->event->getUser(), $this->user); |
57
|
|
|
|
58
|
|
|
$this->setUpDeniedWithReason(); |
59
|
|
|
$this->assertEquals($this->event->getUser(), $this->user); |
60
|
|
|
|
61
|
|
|
$this->setUpDeniedWithoutReason(); |
62
|
|
|
$this->assertEquals($this->event->getUser(), $this->user); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testIsAborted() |
66
|
|
|
{ |
67
|
|
|
$this->setUpAllowed(); |
68
|
|
|
$this->assertFalse($this->event->isAborted()); |
69
|
|
|
|
70
|
|
|
$this->setUpDeniedWithReason(); |
71
|
|
|
$this->assertTrue($this->event->isAborted()); |
72
|
|
|
|
73
|
|
|
$this->setUpDeniedWithoutReason(); |
74
|
|
|
$this->assertTrue($this->event->isAborted()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testReason() |
78
|
|
|
{ |
79
|
|
|
$this->setUpAllowed(); |
80
|
|
|
$this->assertNull($this->event->getReason()); |
81
|
|
|
|
82
|
|
|
$this->setUpDeniedWithReason(); |
83
|
|
|
$this->assertEquals($this->event->getReason(), $this->reason); |
84
|
|
|
|
85
|
|
|
$this->setUpDeniedWithoutReason(); |
86
|
|
|
$this->assertNull($this->event->getReason()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
private function setUpAllowed() |
90
|
|
|
{ |
91
|
|
|
$this->user = $this->getMockForAbstractClass(U2fUser::class); |
92
|
|
|
$this->appId = random_bytes(8); |
93
|
|
|
|
94
|
|
|
$this->event = new U2fPreAuthenticationEvent($this->appId, $this->user); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
private function setUpDeniedWithReason() |
98
|
|
|
{ |
99
|
|
|
$this->setUpAllowed(); |
100
|
|
|
|
101
|
|
|
$this->reason = random_bytes(16); |
102
|
|
|
$this->event->abort($this->reason); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
private function setUpDeniedWithoutReason() |
106
|
|
|
{ |
107
|
|
|
$this->setUpAllowed(); |
108
|
|
|
|
109
|
|
|
$this->event->abort(); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|