Completed
Push — master ( bab30e...5d9c47 )
by Stefan
18s queued 14s
created

php$3 ➔ testManageTokenWithoutAuthenticationThrowsException()   A

Complexity

Conditions 1

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 9.504
c 0
b 0
f 0
cc 1

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