DiscoPowerTest::testDiscoPowerHasDiscoParams()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 0
dl 0
loc 20
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Test\Module\discopower\Controller;
6
7
use PHPUnit\Framework\Attributes\CoversClass;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\Attributes\CoversClass was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use SimpleSAML\Configuration;
9
use SimpleSAML\Error;
10
use SimpleSAML\Module\discopower\Controller;
11
use SimpleSAML\Session;
12
use SimpleSAML\TestUtils\ClearStateTestCase;
13
use Symfony\Component\HttpFoundation\{Request, StreamedResponse};
14
15
/**
16
 * Set of tests for the controllers in the "discopower" module.
17
 */
18
#[CoversClass(Controller\DiscoPower::class)]
19
final class DiscoPowerTest extends ClearStateTestCase
20
{
21
    /** @var \SimpleSAML\Configuration */
22
    private static Configuration $discoconfig;
23
24
25
    /**
26
     * Set up for each test.
27
     */
28
    protected function setUp(): void
29
    {
30
        parent::setUp();
31
32
        $config = Configuration::loadFromArray(
33
            [
34
                'module.enable' => ['discopower' => true],
35
                'trusted.url.domains' => ['example.com'],
36
            ],
37
            '[ARRAY]',
38
            'simplesaml',
39
        );
40
41
        Configuration::setPreLoadedConfig($config, 'config.php');
42
43
        self::$discoconfig = Configuration::loadFromArray(
44
            [
45
                'defaulttab' => 0,
46
                'trusted.url.domains' => ['example.com'],
47
            ],
48
            '[ARRAY]',
49
            'simplesaml',
50
        );
51
    }
52
53
    public function testDiscoPowerNoDiscoParams(): void
54
    {
55
        $request = Request::create(
56
            '/disco.php',
57
            'GET',
58
        );
59
60
        $c = new Controller\DiscoPower();
61
62
        $this->expectException(Error\Error::class);
63
        $this->expectExceptionMessage("DISCOPARAMS");
64
        $c->main($request);
65
    }
66
67
    public function testDiscoPowerHasDiscoParams(): void
68
    {
69
        Configuration::setPreLoadedConfig(self::$discoconfig, 'module_discopower.php');
70
71
        $request = Request::create(
72
            '/disco.php',
73
            'GET',
74
        );
75
        $_GET = [
76
            'entityID' => 'https://example.com/sp',
77
            'return' => 'https://example.com/acs',
78
            'returnIDParam' => 'idpentityid',
79
        ];
80
        $_SERVER['REQUEST_URI'] = '/disco.php';
81
82
        $c = new Controller\DiscoPower();
83
84
        $r = $c->main($request);
85
        $this->assertInstanceOf(StreamedResponse::class, $r);
86
        $this->assertTrue($r->isSuccessful());
87
    }
88
89
    public function testDiscoPowerReturnUrlDisallowed(): void
90
    {
91
        Configuration::setPreLoadedConfig(self::$discoconfig, 'module_discopower.php');
92
93
        $request = Request::create(
94
            '/disco.php',
95
            'GET',
96
        );
97
        $_GET = [
98
            'entityID' => 'https://example.com/sp',
99
            'return' => 'https://attacker.example.org/acs',
100
            'returnIDParam' => 'idpentityid',
101
        ];
102
        $_SERVER['REQUEST_URI'] = '/disco.php';
103
104
        $c = new Controller\DiscoPower();
105
106
        // All exceptions in this stage are flattened into DISCOPARAMS
107
        $this->expectException(Error\Error::class);
108
        $this->expectExceptionMessage("DISCOPARAMS");
109
        $c->main($request);
110
    }
111
112
    public function testTablistJson(): void
113
    {
114
        $session = Session::getSessionFromRequest();
115
        $session->setData('discopower:tabList', 'faventry', 'http://example.org/idp');
116
        $session->setData('discopower:tabList', 'tabs', ['Frankrijk', 'Nederland', 'Duitsland']);
117
        $session->setData('discopower:tabList', 'defaulttab', 'Nederland');
118
119
        $request = Request::create(
120
            '/tablist',
121
            'GET',
122
        );
123
124
        $c = new Controller\DiscoPower();
125
126
        $r = $c->tablist($request);
127
        $this->assertTrue($r->isSuccessful());
128
        $this->assertEquals('application/json', $r->headers->get('Content-Type'));
129
        $this->assertEquals(
130
            '{"faventry":"http:\/\/example.org\/idp","default":"Nederland","tabs":["Frankrijk","Nederland","Duitsland"]}',
131
            $r->getContent(),
132
        );
133
134
        $request = Request::create(
135
            '/tablist',
136
            'GET',
137
            ['callback' => 'aapnoot'],
138
        );
139
140
        $c = new Controller\DiscoPower();
141
142
        $r = $c->tablist($request);
143
        $this->assertTrue($r->isSuccessful());
144
        $this->assertEquals('text/javascript', $r->headers->get('Content-Type'));
145
        $this->assertEquals(
146
            '/**/aapnoot({"faventry":"http:\/\/example.org\/idp","default":"Nederland","tabs":["Frankrijk","Nederland","Duitsland"]});',
147
            $r->getContent(),
148
        );
149
    }
150
151
    public function testTablistJsonNoSession(): void
152
    {
153
        $request = Request::create(
154
            '/tablist',
155
            'GET',
156
        );
157
158
        $c = new Controller\DiscoPower();
159
160
        $this->expectException(Error\Exception::class);
161
        $this->expectExceptionMessage("Could not get tab list from session");
162
        $c->tablist($request);
163
    }
164
165
    public function testTablistJsonUnsafeCallback(): void
166
    {
167
        $session = Session::getSessionFromRequest();
168
        $session->setData('discopower:tabList', 'faventry', 'http://example.org/idp');
169
        $session->setData('discopower:tabList', 'tabs', ['Frankrijk', 'Nederland', 'Duitsland']);
170
        $session->setData('discopower:tabList', 'defaulttab', 'Nederland');
171
172
        $request = Request::create(
173
            '/tablist',
174
            'GET',
175
            ['callback' => 'alert("hallo")'],
176
        );
177
178
        $c = new Controller\DiscoPower();
179
180
        $this->expectException(Error\Exception::class);
181
        $this->expectExceptionMessage("Unsafe JSONP callback");
182
        $c->tablist($request);
183
    }
184
}
185