Skip to content

Commit 0a33933

Browse files
authored
Merge pull request #9 from pdsinterop/test/server
Tests for Server and StorageServer class
2 parents 8dae1e5 + 5ec42b4 commit 0a33933

File tree

5 files changed

+314
-9
lines changed

5 files changed

+314
-9
lines changed

TODO

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@
5050
- [v] WAC-viewer
5151
- [v] CORS for all
5252
- [v] Initialize storage
53+
- [v] Notifications
5354
- [-] API for storage creation
54-
- [-] Notifications
5555

5656
------ Test suites -----
5757
- [v] webid
@@ -72,7 +72,7 @@
7272
- [v] Mailer
7373
- [v] MailTemplateGenerator
7474
- [v] MailTemplates
75-
- [ ] Server
76-
- [ ] StorageServer
75+
- [v] Server
76+
- [v] StorageServer
7777
- [-] SolidNotifications
7878
- [-] SolidPubSub

tests/phpunit/MiddlewareTest.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
<?php
22
namespace Pdsinterop\PhpSolid;
33

4-
use Pdsinterop\PhpSolid\Middleware;
4+
require_once(__DIR__ . "/test-config.php");
55

6-
const PUBSUB_SERVER = "https://localhost:1234";
7-
function header($header) {
8-
MiddleWareTest::$headers[] = $header;
9-
}
6+
use Pdsinterop\PhpSolid\Middleware;
107

118
class MiddlewareTest extends \PHPUnit\Framework\TestCase
129
{

tests/phpunit/ServerTest.php

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

tests/phpunit/StorageServerTest.php

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?php
2+
namespace Pdsinterop\PhpSolid;
3+
4+
require_once(__DIR__ . "/test-config.php");
5+
6+
use Pdsinterop\PhpSolid\StorageServer;
7+
8+
class StorageServerTest extends \PHPUnit\Framework\TestCase
9+
{
10+
public static $headers = [];
11+
public static $createdUser;
12+
13+
protected function setUp(): void
14+
{
15+
$statements = [
16+
'DROP TABLE IF EXISTS allowedClients',
17+
'DROP TABLE IF EXISTS userStorage',
18+
'DROP TABLE IF EXISTS users',
19+
'CREATE TABLE IF NOT EXISTS allowedClients (
20+
userId VARCHAR(255) NOT NULL PRIMARY KEY,
21+
clientId VARCHAR(255) NOT NULL
22+
)',
23+
'CREATE TABLE IF NOT EXISTS userStorage (
24+
userId VARCHAR(255) NOT NULL PRIMARY KEY,
25+
storageUrl VARCHAR(255) NOT NULL
26+
)',
27+
'CREATE TABLE IF NOT EXISTS users (
28+
user_id VARCHAR(255) NOT NULL PRIMARY KEY,
29+
email TEXT NOT NULL,
30+
password TEXT NOT NULL,
31+
data TEXT
32+
)',
33+
];
34+
35+
Db::connect();
36+
try {
37+
// create tables
38+
foreach($statements as $statement){
39+
Db::$pdo->exec($statement);
40+
}
41+
} catch(\PDOException $e) {
42+
echo $e->getMessage();
43+
}
44+
45+
$newUser = [
46+
"password" => "hello123!@#ABC",
47+
"email" => "[email protected]",
48+
"hello" => "world"
49+
];
50+
self::$createdUser = User::createUser($newUser);
51+
$_SERVER['REQUEST_URI'] = "/test/";
52+
$_SERVER['REQUEST_SCHEME'] = "https";
53+
$_SERVER['SERVER_NAME'] = "storage-" . self::$createdUser['userId'] . ".example.com";
54+
}
55+
56+
public function testGetFileSystem() {
57+
$filesystem = StorageServer::getFileSystem();
58+
$this->assertInstanceOf('\League\Flysystem\Filesystem', $filesystem);
59+
}
60+
61+
62+
public function testRespond() {
63+
$response = new MockResponse();
64+
ob_start();
65+
StorageServer::respond($response);
66+
$sentBody = ob_get_contents();
67+
ob_end_clean();
68+
$this->assertTrue(in_array("HTTP/1.1 200", StorageServerTest::$headers));
69+
$this->assertTrue(in_array("Foo:Bar", StorageServerTest::$headers));
70+
$this->assertTrue(in_array("Foo:Blah", StorageServerTest::$headers));
71+
72+
$this->assertEquals($sentBody, "{\"Hello\":\"world\"}");
73+
}
74+
75+
public function testGetOwner() {
76+
$owner = StorageServer::getOwner();
77+
$this->assertEquals(self::$createdUser['webId'], $owner['webId']);
78+
$this->assertEquals(self::$createdUser['email'], $owner['email']);
79+
}
80+
81+
public function testGetOwnerWebId() {
82+
$webId = StorageServer::getOwnerWebId();
83+
$this->assertEquals(self::$createdUser['webId'], $webId);
84+
}
85+
86+
public function testGenerateDefaultAcl() {
87+
$defaultAcl = StorageServer::generateDefaultAcl();
88+
$this->assertTrue(strpos($defaultAcl, self::$createdUser['webId']) > 0);
89+
$this->assertMatchesRegularExpression("/@prefix/", $defaultAcl);
90+
}
91+
92+
public function testGeneratePublicAppendAcl() {
93+
$publicAppendAcl = StorageServer::generatePublicAppendAcl();
94+
$this->assertTrue(strpos($publicAppendAcl, self::$createdUser['webId']) > 0);
95+
$this->assertMatchesRegularExpression("/@prefix/", $publicAppendAcl);
96+
}
97+
98+
public function testGeneratePublicReadAcl() {
99+
$publicReadAcl = StorageServer::generatePublicReadAcl();
100+
$this->assertTrue(strpos($publicReadAcl, self::$createdUser['webId']) > 0);
101+
$this->assertMatchesRegularExpression("/@prefix/", $publicReadAcl);
102+
}
103+
104+
public function testGenerateDefaultPrivateTypeIndex() {
105+
$privateTypeIndex = StorageServer::generateDefaultPrivateTypeIndex();
106+
$this->assertTrue(strpos($privateTypeIndex, "UnlistedDocument") > 0);
107+
$this->assertMatchesRegularExpression("/@prefix/", $privateTypeIndex);
108+
}
109+
110+
public function testGenerateDefaultPublicTypeIndex() {
111+
$publicTypeIndex = StorageServer::generateDefaultPublicTypeIndex();
112+
$this->assertTrue(strpos($publicTypeIndex, "ListedDocument") > 0);
113+
$this->assertMatchesRegularExpression("/@prefix/", $publicTypeIndex);
114+
}
115+
116+
public function testGenerateDefaultPreferences() {
117+
$preferences = StorageServer::generateDefaultPreferences();
118+
$this->assertTrue(strpos($preferences, "ConfigurationFile") > 0);
119+
$this->assertMatchesRegularExpression("/@prefix/", $preferences);
120+
}
121+
122+
/*
123+
Currently untested:
124+
public static function getWebId($rawRequest) {
125+
public static function initializeStorage() {
126+
*/
127+
}
128+

tests/phpunit/test-config.php

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,59 @@
66
const BANNED_PASSWORDS = [];
77
const MINIMUM_PASSWORD_ENTROPY = 10;
88
const BASEDOMAIN = "solid.example.com";
9-
const BASEURL = "https://solid.example.com";
9+
const BASEURL = "https://solid.example.com";
10+
const PUBSUB_SERVER = "https://localhost:1234";
11+
const KEYDIR = "php://memory/";
12+
const STORAGEBASE = ".";
13+
14+
function header($header) {
15+
if (class_exists('Pdsinterop\PhpSolid\MiddleWareTest')) {
16+
MiddleWareTest::$headers[] = $header;
17+
}
18+
if (class_exists('Pdsinterop\PhpSolid\ServerTest')) {
19+
ServerTest::$headers[] = $header;
20+
}
21+
if (class_exists('Pdsinterop\PhpSolid\StorageServerTest')) {
22+
StorageServerTest::$headers[] = $header;
23+
}
24+
}
25+
26+
function file_get_contents($file) {
27+
if (class_exists('Pdsinterop\PhpSolid\ServerTest')) {
28+
if(!isset(ServerTest::$keys)) {
29+
ServerTest::$keys = Server::generateKeySet();
30+
}
31+
if (preg_match("/encryption/", $file)) {
32+
return ServerTest::$keys['encryptionKey'];
33+
}
34+
if (preg_match("/public/", $file)) {
35+
return ServerTest::$keys['publicKey'];
36+
}
37+
if (preg_match("/private/", $file)) {
38+
return ServerTest::$keys['privateKey'];
39+
}
40+
}
41+
}
42+
43+
class MockBody {
44+
public function rewind() {
45+
return true;
46+
}
47+
public function getContents() {
48+
return json_encode(["Hello" => "world"]);
49+
}
50+
}
51+
52+
class MockResponse {
53+
public function getStatusCode() {
54+
return 200;
55+
}
56+
public function getBody() {
57+
return new MockBody();
58+
}
59+
public function getHeaders() {
60+
return [
61+
"Foo" => ["Bar", "Blah"]
62+
];
63+
}
64+
}

0 commit comments

Comments
 (0)