GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( ad44e8...fee13a )
by François
03:34
created

SystemMessagesTest::testDeleteSystemMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
/**
3
 *  Copyright (C) 2016 SURFnet.
4
 *
5
 *  This program is free software: you can redistribute it and/or modify
6
 *  it under the terms of the GNU Affero General Public License as
7
 *  published by the Free Software Foundation, either version 3 of the
8
 *  License, or (at your option) any later version.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU Affero General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU Affero General Public License
16
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace SURFnet\VPN\Server\Api;
20
21
use DateTime;
22
use PDO;
23
use PHPUnit_Framework_TestCase;
24
use SURFnet\VPN\Common\Http\BasicAuthenticationHook;
25
use SURFnet\VPN\Common\Http\Request;
26
use SURFnet\VPN\Common\Http\Service;
27
use SURFnet\VPN\Server\Storage;
28
29
class SystemMessagesTest extends PHPUnit_Framework_TestCase
30
{
31
    /** @var \SURFnet\VPN\Common\Http\Service */
32
    private $service;
33
34
    public function setUp()
35
    {
36
        $storage = new Storage(
37
            new PDO('sqlite::memory:')
38
        );
39
        $storage->init();
40
        $storage->addSystemMessage('motd', 'Hello World!', new DateTime('2016-01-01 06:00:00'));
41
42
        $this->service = new Service();
43
        $this->service->addModule(
44
            new SystemMessagesModule(
45
                $storage,
46
                new DateTime('2016-01-01 08:00:00')
47
            )
48
        );
49
50
        $bearerAuthentication = new BasicAuthenticationHook(
51
            [
52
                'vpn-admin-portal' => 'aabbcc',
53
            ]
54
        );
55
56
        $this->service->addBeforeHook('auth', $bearerAuthentication);
57
    }
58
59
    public function testGetSystemMessages()
60
    {
61
        $this->assertSame(
62
            [
63
                [
64
                    'id' => '1',
65
                    'message' => 'Hello World!',
66
                    'date_time' => '2016-01-01 06:00:00',
67
                ],
68
            ],
69
            $this->makeRequest(
70
                ['vpn-admin-portal', 'aabbcc'],
71
                'GET',
72
                'system_messages',
73
                ['message_type' => 'motd'],
74
                []
75
            )
76
        );
77
    }
78
79
    public function testAddSystemMessage()
80
    {
81
        $this->assertTrue(
82
            $this->makeRequest(
83
                ['vpn-admin-portal', 'aabbcc'],
84
                'POST',
85
                'add_system_message',
86
                [],
87
                ['message_type' => 'motd', 'message_body' => 'foo']
88
            )
89
        );
90
        $this->assertSame(
91
            [
92
                [
93
                    'id' => '1',
94
                    'message' => 'Hello World!',
95
                    'date_time' => '2016-01-01 06:00:00',
96
                ],
97
                [
98
                    'id' => '2',
99
                    'message' => 'foo',
100
                    'date_time' => '2016-01-01 08:00:00',
101
                ],
102
            ],
103
            $this->makeRequest(
104
                ['vpn-admin-portal', 'aabbcc'],
105
                'GET',
106
                'system_messages',
107
                ['message_type' => 'motd'],
108
                []
109
            )
110
        );
111
    }
112
113
    public function testDeleteSystemMessage()
114
    {
115
        $this->assertTrue(
116
            $this->makeRequest(
117
                ['vpn-admin-portal', 'aabbcc'],
118
                'POST',
119
                'delete_system_message',
120
                [],
121
                ['message_id' => 1]
122
            )
123
        );
124
    }
125
126
    private function makeRequest(array $basicAuth, $requestMethod, $pathInfo, array $getData = [], array $postData = [])
127
    {
128
        $response = $this->service->run(
129
            new Request(
130
                [
131
                    'SERVER_PORT' => 80,
132
                    'SERVER_NAME' => 'vpn.example',
133
                    'REQUEST_METHOD' => $requestMethod,
134
                    'PATH_INFO' => sprintf('/%s', $pathInfo),
135
                    'REQUEST_URI' => sprintf('/%s', $pathInfo),
136
                    'PHP_AUTH_USER' => $basicAuth[0],
137
                    'PHP_AUTH_PW' => $basicAuth[1],
138
                ],
139
                $getData,
140
                $postData
141
            )
142
        );
143
144
        $responseArray = json_decode($response->getBody(), true)[$pathInfo];
145
        if ($responseArray['ok']) {
146
            if (array_key_exists('data', $responseArray)) {
147
                return $responseArray['data'];
148
            }
149
150
            return true;
151
        }
152
153
        // in case of errors...
154
        return $responseArray;
155
    }
156
}
157