Passed
Push — master ( 6523ae...53286a )
by Stefan
07:26
created

ManageTokenTest.php$0 ➔ setUp()   A

Complexity

Conditions 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
rs 9.6333
cc 1

1 Method

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