File: PreProdWarningTest.php

package info (click to toggle)
simplesamlphp 1.19.7-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 42,920 kB
  • sloc: php: 202,044; javascript: 14,867; xml: 2,700; sh: 225; perl: 82; makefile: 70; python: 5
file content (152 lines) | stat: -rw-r--r-- 3,904 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php

namespace SimpleSAML\Test\Module\preprodwarning\Controller;

use PHPUnit\Framework\TestCase;
use SimpleSAML\Auth\State;
use SimpleSAML\Auth\ProcessingChain;
use SimpleSAML\Configuration;
use SimpleSAML\Error;
use SimpleSAML\HTTP\RunnableResponse;
use SimpleSAML\Logger;
use SimpleSAML\Module;
use SimpleSAML\Module\preprodwarning\Controller;
use SimpleSAML\Session;
use SimpleSAML\XHTML\Template;
use Symfony\Component\HttpFoundation\Request;

/**
 * Set of tests for the controllers in the "preprodwarning" module.
 *
 * @package SimpleSAML\Test
 */
class PreProdWarningTest extends TestCase
{
    /** @var \SimpleSAML\Configuration */
    protected $config;

    /** @var \SimpleSAML\Logger */
    protected $logger;

    /** @var \SimpleSAML\Session */
    protected $session;

    /**
     * Set up for each test.
     * @return void
     */
    protected function setUp()
    {
        parent::setUp();

        $this->config = Configuration::loadFromArray(
            [
                'module.enable' => ['preprodwarning' => true],
            ],
            '[ARRAY]',
            'simplesaml'
        );

        $this->session = Session::getSessionFromRequest();

        Configuration::setPreLoadedConfig($this->config, 'config.php');

        $this->logger = new class () extends Logger {
            public static function info($str)
            {
                // do nothing
            }
        };
    }


    /**
     * Test that a valid requests results in a Twig template
     * @return void
     */
    public function testMissingStateIdThrowsException()
    {
        $request = Request::create(
            '/warning',
            'GET'
        );

        $c = new Controller\PreProdWarning($this->config, $this->session);
        $c->setLogger($this->logger);

        $this->expectException(Error\BadRequest::class);
        $this->expectExceptionMessage('Missing required StateId query parameter.');

        $c->main($request);
    }


    /**
     * Test that a valid requests results in a Twig template
     * @return void
     */
    public function testWarning()
    {
        $request = Request::create(
            '/warning',
            'GET',
            ['StateId' => 'someStateId']
        );

        $c = new Controller\PreProdWarning($this->config, $this->session);
        $c->setLogger($this->logger);
        $c->setAuthState(new class () extends State {
            public static function loadState($id, $stage, $allowMissing = false)
            {
                return [];
            }
        });

        $response = $c->main($request);

        // Validate response
        $this->assertInstanceOf(Template::class, $response);
        $this->assertTrue($response->isSuccessful());
    }


    /**
     * @return void
     */
    public function testWarningAccepted()
    {
        $request = Request::create(
            '/warning',
            'POST',
            ['StateId' => 'someStateId', 'yes' => 'yes']
        );

        $c = new Controller\PreProdWarning($this->config, $this->session);
        $c->setLogger($this->logger);
        $c->setAuthState(new class () extends State {
            public static function loadState($id, $stage, $allowMissing = false)
            {
                return [
                    ProcessingChain::FILTERS_INDEX => [],
                    'ReturnURL' => 'https://example.org'
                ];
            }
        });
        $c->setProcessingChain(new class () extends ProcessingChain {
            public function __construct()
            {
                // stub
            }

            public static function resumeProcessing($state): void
            {
            }
        });

        $response = $c->main($request);

        // Validate response
        $this->assertInstanceOf(RunnableResponse::class, $response);
        $this->assertTrue($response->isSuccessful());
    }
}