~fkooman/vpn-user-portal

ref: 57df62b9a04e9b2391ca809e5ff9d76e524eceb1 vpn-user-portal/tests/VpnPortalModuleTest.php -rw-r--r-- 5.2 KiB
57df62b9François Kooman update CHANGES 1 year, 4 months ago
                                                                                
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php

/*
 * eduVPN - End-user friendly VPN.
 *
 * Copyright: 2016-2019, The Commons Conservancy eduVPN Programme
 * SPDX-License-Identifier: AGPL-3.0+
 */

namespace LC\Portal\Tests;

use DateInterval;
use DateTime;
use LC\Common\Config;
use LC\Common\Http\NullAuthenticationHook;
use LC\Common\Http\Request;
use LC\Common\Http\Service;
use LC\Common\HttpClient\ServerClient;
use LC\Portal\ClientFetcher;
use LC\Portal\Storage;
use LC\Portal\VpnPortalModule;
use PDO;
use PHPUnit\Framework\TestCase;

class VpnPortalModuleTest extends TestCase
{
    /** @var \LC\Common\Http\Service */
    private $service;

    /**
     * @return void
     */
    protected function setUp()
    {
        $schemaDir = \dirname(__DIR__).'/schema';
        $serverClient = new ServerClient(new TestHttpClient(), 'serverClient');
        $storage = new Storage(new PDO('sqlite::memory:'), $schemaDir, new DateInterval('P90D'));
        $storage->init();

        $vpnPortalModule = new VpnPortalModule(
            new Config(['sessionExpiry' => 'P90D']),
            new JsonTpl(),
            $serverClient,
            new TestSession(),
            $storage,
            new ClientFetcher(new Config(['Api' => []])),
            new DateInterval('P90D')
        );
        $vpnPortalModule->setDateTime(new DateTime('2019-01-01'));
        $this->service = new Service();
        $this->service->addModule($vpnPortalModule);
        $this->service->addBeforeHook('auth', new NullAuthenticationHook('foo'));
    }

    /**
     * @return void
     */
    public function testHomeGet()
    {
        $this->assertSame(
            [
                'vpnPortalHome' => [
                    'motdMessage' => [
                        'id' => 1,
                        'message_type' => 'motd',
                        'message_body' => 'Hello World!',
                    ],
                ],
            ],
            $this->makeRequest('GET', '/home')
        );
    }

    /**
     * @return void
     */
    public function testConfigurtionsPost()
    {
        $this->assertSame(
            trim(file_get_contents(sprintf('%s/data/foo_MyConfig.ovpn', __DIR__))),
            $this->makeRequest(
                'POST',
                '/configurations',
                [],
                ['displayName' => 'MyConfig', 'profileId' => 'internet'],
                true
            )->getBody()
        );
    }

    /**
     * @return void
     */
    public function testAccount()
    {
        $this->assertSame(
            [
                'vpnPortalAccount' => [
                    'hasTotpSecret' => false,
                    'userInfo' => [],
                    'userPermissions' => [],
                    'authorizedClients' => [],
                    'twoFactorMethods' => [
                        'totp',
                    ],
                    'userMessages' => [],
                    'userConnectionLogEntries' => [],
                    'idNameMapping' => [
                        'internet' => 'Internet Access',
                    ],
                ],
            ],
            $this->makeRequest('GET', '/account')
        );
    }

    /**
     * @return void
     */
    public function testConfigurations()
    {
        $this->assertSame(
            [
                'vpnPortalConfigurations' => [
                    'expiryDate' => '2019-04-01',
                    'profileList' => [
                        'internet' => [
                            'displayName' => 'Internet Access',
                        ],
                    ],
                    'userCertificateList' => [
                        [
                            'display_name' => 'Foo',
                            'valid_from' => 123456,
                            'valid_to' => 2345567,
                            'client_id' => null,
                        ],
                    ],
                ],
            ],
            $this->makeRequest('GET', '/configurations')
        );
    }

    /**
     * @return void
     */
    public function testDisableConfirm()
    {
        $this->assertSame(
            302,
            $this->makeRequest('POST', '/deleteCertificate', [], ['commonName' => '12345678901234567890123456789012'], true)->getStatusCode()
        );
    }

    /**
     * @param string               $requestMethod
     * @param string               $pathInfo
     * @param array<string,string> $getData
     * @param array<string,string> $postData
     * @param bool                 $returnResponseObj
     *
     * @return \LC\Common\Http\Response|array
     */
    private function makeRequest($requestMethod, $pathInfo, array $getData = [], array $postData = [], $returnResponseObj = false)
    {
        $response = $this->service->run(
            new Request(
                [
                    'SERVER_PORT' => '80',
                    'SERVER_NAME' => 'vpn.example',
                    'REQUEST_METHOD' => $requestMethod,
                    'REQUEST_URI' => $pathInfo,
                    'SCRIPT_NAME' => '/index.php',
                ],
                $getData,
                $postData
            )
        );

        if ($returnResponseObj) {
            return $response;
        }

        $responseBody = $response->getBody();

        return json_decode($responseBody, true);
    }
}