Passed
Push — master ( 3900ea...e2f039 )
by Tim
08:09
created

StateTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 113
rs 10
c 0
b 0
f 0
eloc 37
wmc 5

9 Methods

Rating   Name   Duplication   Size   Complexity  
stateTestsProvider() 0 7 ?
testMissingState() 0 19 ?
setUp() 0 19 ?
A hp$0 ➔ info() 0 2 1
testNoState() 0 20 ?
A hp$0 ➔ testMissingState() 0 19 1
A hp$0 ➔ setUp() 0 19 1
A hp$0 ➔ stateTestsProvider() 0 7 1
A hp$0 ➔ testNoState() 0 20 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\XHTML\Template;
16
use Symfony\Component\HttpFoundation\Request;
17
18
/**
19
 * Set of tests for the controllers in the "webauthn" module.
20
 *
21
 * @package SimpleSAML\Test
22
 */
23
class StateTest extends TestCase
24
{
25
    /** @var \SimpleSAML\Configuration */
26
    protected $config;
27
28
    /** @var \SimpleSAML\Logger */
29
    protected $logger;
30
31
    /** @var \SimpleSAML\Session */
32
    protected $session;
33
34
35
    /**
36
     * Set up for each test.
37
     */
38
    protected function setUp(): void
39
    {
40
        parent::setUp();
41
42
        $this->config = Configuration::loadFromArray(
43
            [
44
                'module.enable' => ['webauthn' => true],
45
                'secretsalt' => 'abc123',
46
                'enable.saml20-idp' => true,
47
            ],
48
            '[ARRAY]',
49
            'simplesaml'
50
        );
51
52
        $this->session = Session::getSessionFromRequest();
53
54
        $this->logger = new class () extends Logger {
55
            public static function info(string $str): void
56
            {
57
                // do nothing
58
            }
59
        };
60
    }
61
62
63
    /**
64
     * @dataProvider stateTestsProvider
65
     *
66
     * @param string $method The method to be used for the test
67
     * @param string $controllerEndpoint The name of the endpoint of the controller to test
68
     * @param string $controllerClass The name of the controller class to test
69
     * @psalm-param class-string $controllerClass
70
     * @param string $controllerMethod The name of the controller method to test
71
     */
72
    public function testMissingState(
73
        string $method,
74
        string $controllerEndpoint,
75
        string $controllerClass,
76
        string $controllerMethod
77
    ): void {
78
        $_SERVER['REQUEST_URI'] = '/module.php/webauthn/' . $controllerEndpoint;
79
        $request = Request::create(
80
            '/' . $controllerEndpoint,
81
            $method
82
        );
83
84
        $c = new $controllerClass($this->config, $this->session);
85
        $c->setLogger($this->logger);
86
87
        $this->expectException(Error\BadRequest::class);
88
        $this->expectExceptionMessage('Missing required StateId query parameter.');
89
90
        call_user_func([$c, $controllerMethod], $request);
91
    }
92
93
94
    /**
95
     * @dataProvider stateTestsProvider
96
     *
97
     * @param string $method The method to be used for the test
98
     * @param string $controllerEndpoint The name of the endpoint of the controller to test
99
     * @param string $controllerClass The name of the controller class to test
100
     * @psalm-param class-string $controllerClass
101
     * @param string $controllerMethod The name of the controller method to test
102
     */
103
    public function testNoState(
104
        string $method,
105
        string $controllerEndpoint,
106
        string $controllerClass,
107
        string $controllerMethod
108
    ): void {
109
        $_SERVER['REQUEST_URI'] = '/module.php/webauthn/' . $controllerEndpoint;
110
        $request = Request::create(
111
            '/' . $controllerEndpoint,
112
            $method,
113
            ['StateId' => 'someStateId']
114
        );
115
116
        $c = new $controllerClass($this->config, $this->session);
117
        $c->setLogger($this->logger);
118
119
        $this->expectException(Error\NoState::class);
120
        $this->expectExceptionMessage('NOSTATE');
121
122
        call_user_func([$c, $controllerMethod], $request);
123
    }
124
125
126
    /**
127
     * @return array
128
     */
129
    public function stateTestsProvider(): array
130
    {
131
        return [
132
            ['POST', 'authprocess', Controller\AuthProcess::class, 'main'],
133
            ['POST', 'managetoken', Controller\ManageToken::class, 'main'],
134
            ['POST', 'regprocess', Controller\RegProcess::class, 'main'],
135
            ['POST', 'webauthn', Controller\WebAuthn::class, 'main'],
136
        ];
137
    }
138
}
139