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

StateTest.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 StateTest.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 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
     */
39
    protected function setUp(): void
40
    {
41
        parent::setUp();
42
43
        $this->config = Configuration::loadFromArray(
44
            [
45
                'module.enable' => ['webauthn' => true],
46
                'secretsalt' => 'abc123',
47
                'enable.saml20-idp' => true,
48
            ],
49
            '[ARRAY]',
50
            'simplesaml'
51
        );
52
53
        $this->session = Session::getSessionFromRequest();
54
55
        $this->logger = new class () extends Logger {
56
            public static function info($str)
57
            {
58
                // do nothing
59
            }
60
        };
61
    }
62
63
64
    /**
65
     * @dataProvider stateTestsProvider
66
     *
67
     * @param string $method The method to be used for the test
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(
74
        string $method,
75
        string $controllerEndpoint,
76
        string $controllerClass,
77
        string $controllerMethod
78
    ): void {
79
        $_SERVER['REQUEST_URI'] = '/module.php/webauthn/' . $controllerEndpoint;
80
        $request = Request::create(
81
            '/' . $controllerEndpoint,
82
            $method
83
        );
84
85
        $c = new $controllerClass($this->config, $this->session);
86
        $c->setLogger($this->logger);
87
88
        $this->expectException(Error\BadRequest::class);
89
        $this->expectExceptionMessage('Missing required StateId query parameter.');
90
91
        call_user_func([$c, $controllerMethod], $request);
92
    }
93
94
95
    /**
96
     * @dataProvider stateTestsProvider
97
     *
98
     * @param string $method The method to be used for the test
99
     * @param string $controllerEndpoint The name of the endpoint of the controller to test
100
     * @param string $controllerClass The name of the controller class to test
101
     * @psalm-param class-string $controllerClass
102
     * @param string $controllerMethod The name of the controller method to test
103
     */
104
    public function testNoState(
105
        string $method,
106
        string $controllerEndpoint,
107
        string $controllerClass,
108
        string $controllerMethod
109
    ): void {
110
        $_SERVER['REQUEST_URI'] = '/module.php/webauthn/' . $controllerEndpoint;
111
        $request = Request::create(
112
            '/' . $controllerEndpoint . '?StateId=someStateId',
113
            $method,
114
            []
115
        );
116
117
        $c = new $controllerClass($this->config, $this->session);
118
        $c->setLogger($this->logger);
119
120
        $this->expectException(Error\NoState::class);
121
        $this->expectExceptionMessage('NOSTATE');
122
123
        call_user_func([$c, $controllerMethod], $request);
124
    }
125
126
127
    /**
128
     * @return array
129
     */
130
    public function stateTestsProvider(): array
131
    {
132
        return [
133
            ['POST', 'authprocess', Controller\AuthProcess::class, 'main'],
134
            ['POST', 'managetoken', Controller\ManageToken::class, 'main'],
135
            ['POST', 'regprocess', Controller\RegProcess::class, 'main'],
136
            ['POST', 'webauthn', Controller\WebAuthn::class, 'main'],
137
        ];
138
    }
139
}
140