Passed
Pull Request — master (#31)
by Tim
02:15
created

ManageTokenTest.php$1 ➔ loadState()   A

Complexity

Conditions 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 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
    public function testManageTokenWithSubmitNeverMind(): void
69
    {
70
        $_SERVER['REQUEST_URI'] = '/module.php/webauthn/managetoken';
71
        $request = Request::create(
72
            '/managetoken',
73
            'POST',
74
            ['StateId' => 'someStateId', 'submit' => 'NEVERMIND']
75
        );
76
77
78
        $c = new Controller\ManageToken($this->config, $this->session);
79
        $c->setLogger($this->logger);
80
        $c->setAuthState(new class () extends State {
81
            public static function loadState(string $id, string $stage, bool $allowMissing = false): ?array
82
            {
83
                return [
84
                    'FIDO2AuthSuccessful' => true,
85
                ];
86
            }
87
        });
88
89
        $response = $c->main($request);
90
91
        $this->assertTrue($response->isSuccessful());
92
    }
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