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

anonymous//tests/lib/Controller/StateTest.php$0   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 3
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Test\Module\webauthn\Controller;
6
7
use PHPUnit\Framework\TestCase;
8
use SimpleSAML\Auth\State;
9
use SimpleSAML\Configuration;
10
use SimpleSAML\Error;
11
use SimpleSAML\HTTP\RunnableResponse;
12
use SimpleSAML\Logger;
13
use SimpleSAML\Module\webauthn\Controller;
14
use SimpleSAML\Session;
15
use SimpleSAML\Utils;
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 StateTest 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
     * @return void
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(string $str): void
58
            {
59
                // do nothing
60
            }
61
        };
62
    }
63
64
65
    /**
66
     * @dataProvider stateTestsProvider
67
     *
68
     * @param string $controllerEndpoint The name of the endpoint of the controller to test
69
     * @param string $controllerClass The name of the controller class to test
70
     * @psalm-param class-string $controllerClass
71
     * @param string $controllerMethod The name of the controller method to test
72
     */
73
    public function testMissingState(string $controllerEndpoint, string $controllerClass, string $controllerMethod): void
74
    {
75
        $_SERVER['REQUEST_URI'] = '/module.php/webauthn/' . $controllerEndpoint;
76
        $request = Request::create(
77
            '/' . $controllerEndpoint,
78
            'GET'
79
        );
80
81
        $c = new $controllerClass($this->config, $this->session);
82
        $c->setLogger($this->logger);
83
84
        $this->expectException(Error\BadRequest::class);
85
        $this->expectExceptionMessage('Missing required StateId query parameter.');
86
87
        call_user_func([$c, $controllerMethod], $request);
88
    }
89
90
91
    /**
92
     * @dataProvider stateTestsProvider
93
     *
94
     * @param string $controllerEndpoint The name of the endpoint of the controller to test
95
     * @param string $controllerClass The name of the controller class to test
96
     * @psalm-param class-string $controllerClass
97
     * @param string $controllerMethod The name of the controller method to test
98
     */
99
    public function testNoState(string $controllerEndpoint, string $controllerClass, string $controllerMethod): void
100
    {
101
        $_SERVER['REQUEST_URI'] = '/module.php/webauthn/' . $controllerEndpoint;
102
        $request = Request::create(
103
            '/' . $controllerEndpoint,
104
            'GET',
105
            ['StateId' => 'someStateId']
106
        );
107
108
        $c = new $controllerClass($this->config, $this->session);
109
        $c->setLogger($this->logger);
110
111
        $this->expectException(Error\NoState::class);
112
        $this->expectExceptionMessage('NOSTATE');
113
114
        call_user_func([$c, $controllerMethod], $request);
115
    }
116
117
118
    /**
119
     * @return array
120
     */
121
    public function stateTestsProvider(): array
122
    {
123
        return [
124
//            ['authprocess', Controller\AuthProcess::class, 'main'],
125
//            ['managetoken', Controller\ManageToken::class, 'main'],
126
//            ['regprocess', Controller\RegProcess::class, 'main'],
127
            ['webauthn', Controller\WebAuthn::class, 'main'],
128
        ];
129
    }
130
}
131