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

StateTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 5
eloc 37
c 2
b 1
f 0
dl 0
loc 106
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
setUp() 0 19 ?
A hp$0 ➔ info() 0 2 1
A hp$0 ➔ setUp() 0 19 1
stateTestsProvider() 0 7 ?
testMissingState() 0 15 ?
testNoState() 0 16 ?
A hp$0 ➔ testNoState() 0 16 1
A hp$0 ➔ testMissingState() 0 15 1
A hp$0 ➔ stateTestsProvider() 0 7 1
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 $method The method to be used for the test
69
     * @param string $controllerEndpoint The name of the endpoint of the controller to test
70
     * @param string $controllerClass The name of the controller class to test
71
     * @psalm-param class-string $controllerClass
72
     * @param string $controllerMethod The name of the controller method to test
73
     */
74
    public function testMissingState(string $method, string $controllerEndpoint, string $controllerClass, string $controllerMethod): void
75
    {
76
        $_SERVER['REQUEST_URI'] = '/module.php/webauthn/' . $controllerEndpoint;
77
        $request = Request::create(
78
            '/' . $controllerEndpoint,
79
            $method
80
        );
81
82
        $c = new $controllerClass($this->config, $this->session);
83
        $c->setLogger($this->logger);
84
85
        $this->expectException(Error\BadRequest::class);
86
        $this->expectExceptionMessage('Missing required StateId query parameter.');
87
88
        call_user_func([$c, $controllerMethod], $request);
89
    }
90
91
92
    /**
93
     * @dataProvider stateTestsProvider
94
     *
95
     * @param string $method The method to be used for the test
96
     * @param string $controllerEndpoint The name of the endpoint of the controller to test
97
     * @param string $controllerClass The name of the controller class to test
98
     * @psalm-param class-string $controllerClass
99
     * @param string $controllerMethod The name of the controller method to test
100
     */
101
    public function testNoState(string $method, string $controllerEndpoint, string $controllerClass, string $controllerMethod): void
102
    {
103
        $_SERVER['REQUEST_URI'] = '/module.php/webauthn/' . $controllerEndpoint;
104
        $request = Request::create(
105
            '/' . $controllerEndpoint,
106
            $method,
107
            ['StateId' => 'someStateId']
108
        );
109
110
        $c = new $controllerClass($this->config, $this->session);
111
        $c->setLogger($this->logger);
112
113
        $this->expectException(Error\NoState::class);
114
        $this->expectExceptionMessage('NOSTATE');
115
116
        call_user_func([$c, $controllerMethod], $request);
117
    }
118
119
120
    /**
121
     * @return array
122
     */
123
    public function stateTestsProvider(): array
124
    {
125
        return [
126
            ['POST', 'authprocess', Controller\AuthProcess::class, 'main'],
127
            ['POST', 'managetoken', Controller\ManageToken::class, 'main'],
128
            ['POST', 'regprocess', Controller\RegProcess::class, 'main'],
129
            ['POST', 'webauthn', Controller\WebAuthn::class, 'main'],
130
        ];
131
    }
132
}
133