Passed
Push — master ( 3900ea...e2f039 )
by Tim
08:09
created

testManageTokenWithoutAuthenticationThrowsException()

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
c 0
b 0
f 0
eloc 14
nc 1
nop 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A ManageTokenTest.php$3 ➔ loadState() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Test\Module\webauthn\Controller;
6
7
use Exception;
8
use PHPUnit\Framework\TestCase;
9
use SimpleSAML\Auth\State;
10
use SimpleSAML\Configuration;
11
use SimpleSAML\Error;
12
use SimpleSAML\HTTP\RunnableResponse;
13
use SimpleSAML\Logger;
14
use SimpleSAML\Module\webauthn\Controller;
15
use SimpleSAML\Session;
16
use SimpleSAML\XHTML\Template;
17
use Symfony\Component\HttpFoundation\Request;
18
19
/**
20
 * Set of tests for the controllers in the "webauthn" module.
21
 *
22
 * @package SimpleSAML\Test
23
 */
24
class ManageTokenTest extends TestCase
25
{
26
    /** @var \SimpleSAML\Configuration */
27
    protected $config;
28
29
    /** @var \SimpleSAML\Logger */
30
    protected $logger;
31
32
    /** @var \SimpleSAML\Session */
33
    protected $session;
34
35
36
    /**
37
     * Set up for each test.
38
     */
39
    protected function setUp(): void
40
    {
41
        parent::setUp();
42
43
        $this->config = Configuration::loadFromArray(
44
            [
45
                'module.enable' => ['webauthn' => true],
46
                'secretsalt' => 'abc123',
47
                'enable.saml20-idp' => true,
48
            ],
49
            '[ARRAY]',
50
            'simplesaml'
51
        );
52
53
        $this->session = Session::getSessionFromRequest();
54
55
        $this->logger = new class () extends Logger {
56
            public static function info(string $str): void
57
            {
58
                // do nothing
59
            }
60
        };
61
    }
62
63
64
    /**
65
     */
66
    public function testManageTokenWithSubmitNeverMind(): void
67
    {
68
        $_SERVER['REQUEST_URI'] = '/module.php/webauthn/managetoken';
69
        $request = Request::create(
70
            '/managetoken',
71
            'POST',
72
            ['StateId' => 'someStateId', 'submit' => 'NEVERMIND']
73
        );
74
75
76
        $c = new Controller\ManageToken($this->config, $this->session);
77
        $c->setLogger($this->logger);
78
        $c->setAuthState(new class () extends State {
79
            public static function loadState(string $id, string $stage, bool $allowMissing = false): ?array
80
            {
81
                return [
82
                    'FIDO2AuthSuccessful' => true,
83
                ];
84
            }
85
        });
86
87
        $response = $c->main($request);
88
89
        $this->assertTrue($response->isSuccessful());
90
    }
91
92
93
    /**
94
    public function testManageTokenWithSubmitDelete(): void
95
    {
96
        $_SERVER['REQUEST_URI'] = '/module.php/webauthn/managetoken';
97
        $_SERVER['REQUEST_METHOD'] = 'POST';
98
        $request = Request::create(
99
            '/managetoken',
100
            'POST',
101
            ['StateId' => 'someStateId', 'submit' => 'DELETE']
102
        );
103
104
105
        $c = new Controller\ManageToken($this->config, $this->session);
106
        $c->setLogger($this->logger);
107
        $c->setAuthState(new class () extends State {
108
            public static function loadState(string $id, string $stage, bool $allowMissing = false): ?array
109
            {
110
                return [
111
                    'FIDO2AuthSuccessful' => true,
112
                ];
113
            }
114
        });
115
116
        $response = $c->main($request);
117
118
        $this->assertTrue($response->isSuccessful());
119
    }
120
     */
121
122
123
    /**
124
     */
125
    public function testManageTokenWithoutSubmitThrowsException(): void
126
    {
127
        $_SERVER['REQUEST_URI'] = '/module.php/webauthn/managetoken';
128
        $_SERVER['REQUEST_METHOD'] = 'POST';
129
        $request = Request::create(
130
            '/managetoken',
131
            'POST',
132
            ['StateId' => 'someStateId', 'submit' => 'submit']
133
        );
134
135
136
        $c = new Controller\ManageToken($this->config, $this->session);
137
        $c->setLogger($this->logger);
138
        $c->setAuthState(new class () extends State {
139
            public static function loadState(string $id, string $stage, bool $allowMissing = false): ?array
140
            {
141
                return [
142
                    'FIDO2AuthSuccessful' => true,
143
                ];
144
            }
145
        });
146
147
        $this->expectException(Exception::class);
148
        $this->expectExceptionMessage('Unknown submit button state.');
149
150
        $c->main($request);
151
    }
152
153
154
    /**
155
     */
156
    public function testManageTokenWithoutAuthenticationThrowsException(): void
157
    {
158
        $_SERVER['REQUEST_URI'] = '/module.php/webauthn/managetoken';
159
        $_SERVER['REQUEST_METHOD'] = 'POST';
160
        $request = Request::create(
161
            '/managetoken',
162
            'POST',
163
            ['StateId' => 'someStateId', 'submit' => 'submit']
164
        );
165
166
167
        $c = new Controller\ManageToken($this->config, $this->session);
168
        $c->setLogger($this->logger);
169
        $c->setAuthState(new class () extends State {
170
            public static function loadState(string $id, string $stage, bool $allowMissing = false): ?array
171
            {
172
                return [
173
                    'FIDO2AuthSuccessful' => false,
174
                ];
175
            }
176
        });
177
178
        $this->expectException(Exception::class);
179
        $this->expectExceptionMessage('Attempt to access the token management page unauthenticated.');
180
181
        $c->main($request);
182
    }
183
}
184