Skip to content

Commit 31591e8

Browse files
committed
add Server tests
1 parent 3291e7e commit 31591e8

File tree

2 files changed

+171
-1
lines changed

2 files changed

+171
-1
lines changed

TODO

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
- [v] Mailer
7373
- [v] MailTemplateGenerator
7474
- [v] MailTemplates
75-
- [ ] Server
75+
- [v] Server
7676
- [ ] StorageServer
7777
- [-] SolidNotifications
7878
- [-] SolidPubSub

tests/phpunit/ServerTest.php

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
<?php
2+
namespace Pdsinterop\PhpSolid;
3+
4+
use Pdsinterop\PhpSolid\Server;
5+
6+
const PUBSUB_SERVER = "https://localhost:1234";
7+
const KEYDIR = "php://memory/";
8+
const BASEURL = "https://example.com";
9+
const DBPATH = ":memory:";
10+
11+
function header($header) {
12+
ServerTest::$headers[] = $header;
13+
}
14+
15+
function file_get_contents($file) {
16+
if(!isset(ServerTest::$keys)) {
17+
ServerTest::$keys = Server::generateKeySet();
18+
}
19+
if (preg_match("/encryption/", $file)) {
20+
return ServerTest::$keys['encryptionKey'];
21+
}
22+
if (preg_match("/public/", $file)) {
23+
return ServerTest::$keys['publicKey'];
24+
}
25+
if (preg_match("/private/", $file)) {
26+
return ServerTest::$keys['privateKey'];
27+
}
28+
}
29+
30+
class MockBody {
31+
public function rewind() {
32+
return true;
33+
}
34+
public function getContents() {
35+
return json_encode(["Hello" => "world"]);
36+
}
37+
}
38+
39+
class MockResponse {
40+
public function getStatusCode() {
41+
return 200;
42+
}
43+
public function getBody() {
44+
return new MockBody();
45+
}
46+
public function getHeaders() {
47+
return [
48+
"Foo" => ["Bar", "Blah"]
49+
];
50+
}
51+
}
52+
53+
class ServerTest extends \PHPUnit\Framework\TestCase
54+
{
55+
public static $headers = [];
56+
public static $keys;
57+
58+
protected function setUp(): void
59+
{
60+
$statements = [
61+
'DROP TABLE IF EXISTS clients',
62+
'CREATE TABLE clients (
63+
clientId VARCHAR(255) NOT NULL PRIMARY KEY,
64+
origin TEXT NOT NULL,
65+
clientData TEXT NOT NULL
66+
)'
67+
];
68+
69+
Db::connect();
70+
try {
71+
// create tables
72+
foreach($statements as $statement){
73+
Db::$pdo->exec($statement);
74+
}
75+
} catch(\PDOException $e) {
76+
echo $e->getMessage();
77+
}
78+
79+
ClientRegistration::saveClientRegistration([
80+
"client_id" => "1234",
81+
"origin" => "https://example.com",
82+
"redirect_uris" => ["https://example.com"],
83+
"client_name" => "Client name"
84+
]);
85+
}
86+
87+
public function testGenerateKeySet() {
88+
$keys = Server::generateKeySet();
89+
$this->assertTrue(isset($keys['encryptionKey']));
90+
$this->assertTrue(isset($keys['publicKey']));
91+
$this->assertTrue(isset($keys['privateKey']));
92+
$this->assertMatchesRegularExpression("/BEGIN PUBLIC KEY/", $keys['publicKey']);
93+
$this->assertMatchesRegularExpression("/BEGIN PRIVATE KEY/", $keys['privateKey']);
94+
}
95+
96+
97+
public function testGetAuthServer() {
98+
$authServer = Server::getAuthServer();
99+
$this->assertInstanceOf('\Pdsinterop\Solid\Auth\Server', $authServer);
100+
}
101+
102+
public function testGetAuthServerConfig() {
103+
$authServerConfig = Server::getAuthServerConfig();
104+
$this->assertInstanceOf('\Pdsinterop\Solid\Auth\Config', $authServerConfig);
105+
}
106+
107+
public function testGetConfigClient() {
108+
$configClient = Server::getConfigClient();
109+
$this->assertInstanceOf('\Pdsinterop\Solid\Auth\Config\Client', $configClient);
110+
}
111+
112+
public function testGetConfigClientWithGetId() {
113+
$_GET['client_id'] = '1234';
114+
$configClient = Server::getConfigClient();
115+
$this->assertInstanceOf('\Pdsinterop\Solid\Auth\Config\Client', $configClient);
116+
}
117+
118+
public function testGetConfigClientWithPostd() {
119+
$_POST['client_id'] = '1234';
120+
$configClient = Server::getConfigClient();
121+
$this->assertInstanceOf('\Pdsinterop\Solid\Auth\Config\Client', $configClient);
122+
}
123+
public function testGetDpop() {
124+
$dpop = Server::getDpop();
125+
$this->assertInstanceOf('\Pdsinterop\Solid\Auth\Utils\Dpop', $dpop);
126+
}
127+
public function testGetBearer() {
128+
$bearer = Server::getBearer();
129+
$this->assertInstanceOf('\Pdsinterop\Solid\Auth\Utils\Bearer', $bearer);
130+
}
131+
public function testGetEndpoints() {
132+
$endpoints = Server::getEndpoints();
133+
$this->assertEquals($endpoints["issuer"], "https://example.com");
134+
$this->assertEquals($endpoints["jwks_uri"], "https://example.com/jwks/");
135+
$this->assertEquals($endpoints["check_session_iframe"], "https://example.com/session/");
136+
$this->assertEquals($endpoints["end_session_endpoint"], "https://example.com/logout/");
137+
$this->assertEquals($endpoints["authorization_endpoint"], "https://example.com/authorize/");
138+
$this->assertEquals($endpoints["token_endpoint"], "https://example.com/token/");
139+
$this->assertEquals($endpoints["userinfo_endpoint"], "https://example.com/userinfo/");
140+
$this->assertEquals($endpoints["registration_endpoint"], "https://example.com/register/");
141+
}
142+
143+
public function testGetKeys() {
144+
$keys = Server::getKeys();
145+
$this->assertTrue(isset($keys['encryptionKey']));
146+
$this->assertTrue(isset($keys['publicKey']));
147+
$this->assertTrue(isset($keys['privateKey']));
148+
$this->assertMatchesRegularExpression("/BEGIN PUBLIC KEY/", $keys['publicKey']);
149+
$this->assertMatchesRegularExpression("/BEGIN PRIVATE KEY/", $keys['privateKey']);
150+
}
151+
152+
public function testGetTokenGenerator() {
153+
$tokenGenerator = Server::getTokenGenerator();
154+
$this->assertInstanceOf('\Pdsinterop\Solid\Auth\TokenGenerator', $tokenGenerator);
155+
}
156+
157+
public function testRespond() {
158+
$response = new MockResponse();
159+
ob_start();
160+
Server::respond($response);
161+
$sentBody = ob_get_contents();
162+
ob_end_clean();
163+
$this->assertTrue(in_array("HTTP/1.1 200", ServerTest::$headers));
164+
$this->assertTrue(in_array("Foo:Bar", ServerTest::$headers));
165+
$this->assertTrue(in_array("Foo:Blah", ServerTest::$headers));
166+
167+
$this->assertEquals($sentBody, "{\n \"Hello\": \"world\"\n}");
168+
}
169+
}
170+

0 commit comments

Comments
 (0)