Passed
Push — master ( 852ed5...a44092 )
by Tim
02:20
created

DiscoPowerTest::setUpBeforeClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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