diff --git a/TODO b/TODO index a623fd7..b86f4d5 100644 --- a/TODO +++ b/TODO @@ -50,8 +50,8 @@ - [v] WAC-viewer - [v] CORS for all - [v] Initialize storage +- [v] Notifications - [-] API for storage creation -- [-] Notifications ------ Test suites ----- - [v] webid @@ -72,7 +72,7 @@ - [v] Mailer - [v] MailTemplateGenerator - [v] MailTemplates -- [ ] Server -- [ ] StorageServer +- [v] Server +- [v] StorageServer - [-] SolidNotifications - [-] SolidPubSub diff --git a/tests/phpunit/MiddlewareTest.php b/tests/phpunit/MiddlewareTest.php index 08cf7ec..ca741c6 100644 --- a/tests/phpunit/MiddlewareTest.php +++ b/tests/phpunit/MiddlewareTest.php @@ -1,12 +1,9 @@ exec($statement); + } + } catch(\PDOException $e) { + echo $e->getMessage(); + } + + ClientRegistration::saveClientRegistration([ + "client_id" => "1234", + "origin" => "https://example.com", + "redirect_uris" => ["https://example.com"], + "client_name" => "Client name" + ]); + } + + public function testGenerateKeySet() { + $keys = Server::generateKeySet(); + $this->assertTrue(isset($keys['encryptionKey'])); + $this->assertTrue(isset($keys['publicKey'])); + $this->assertTrue(isset($keys['privateKey'])); + $this->assertMatchesRegularExpression("/BEGIN PUBLIC KEY/", $keys['publicKey']); + $this->assertMatchesRegularExpression("/BEGIN PRIVATE KEY/", $keys['privateKey']); + } + + + public function testGetAuthServer() { + $authServer = Server::getAuthServer(); + $this->assertInstanceOf('\Pdsinterop\Solid\Auth\Server', $authServer); + } + + public function testGetAuthServerConfig() { + $authServerConfig = Server::getAuthServerConfig(); + $this->assertInstanceOf('\Pdsinterop\Solid\Auth\Config', $authServerConfig); + } + + public function testGetConfigClient() { + $configClient = Server::getConfigClient(); + $this->assertInstanceOf('\Pdsinterop\Solid\Auth\Config\Client', $configClient); + } + + public function testGetConfigClientWithGetId() { + $_GET['client_id'] = '1234'; + $configClient = Server::getConfigClient(); + $this->assertInstanceOf('\Pdsinterop\Solid\Auth\Config\Client', $configClient); + } + + public function testGetConfigClientWithPostd() { + $_POST['client_id'] = '1234'; + $configClient = Server::getConfigClient(); + $this->assertInstanceOf('\Pdsinterop\Solid\Auth\Config\Client', $configClient); + } + public function testGetDpop() { + $dpop = Server::getDpop(); + $this->assertInstanceOf('\Pdsinterop\Solid\Auth\Utils\Dpop', $dpop); + } + public function testGetBearer() { + $bearer = Server::getBearer(); + $this->assertInstanceOf('\Pdsinterop\Solid\Auth\Utils\Bearer', $bearer); + } + public function testGetEndpoints() { + $endpoints = Server::getEndpoints(); + $this->assertEquals($endpoints["issuer"], "https://solid.example.com"); + $this->assertEquals($endpoints["jwks_uri"], "https://solid.example.com/jwks/"); + $this->assertEquals($endpoints["check_session_iframe"], "https://solid.example.com/session/"); + $this->assertEquals($endpoints["end_session_endpoint"], "https://solid.example.com/logout/"); + $this->assertEquals($endpoints["authorization_endpoint"], "https://solid.example.com/authorize/"); + $this->assertEquals($endpoints["token_endpoint"], "https://solid.example.com/token/"); + $this->assertEquals($endpoints["userinfo_endpoint"], "https://solid.example.com/userinfo/"); + $this->assertEquals($endpoints["registration_endpoint"], "https://solid.example.com/register/"); + } + + public function testGetKeys() { + $keys = Server::getKeys(); + $this->assertTrue(isset($keys['encryptionKey'])); + $this->assertTrue(isset($keys['publicKey'])); + $this->assertTrue(isset($keys['privateKey'])); + $this->assertMatchesRegularExpression("/BEGIN PUBLIC KEY/", $keys['publicKey']); + $this->assertMatchesRegularExpression("/BEGIN PRIVATE KEY/", $keys['privateKey']); + } + + public function testGetTokenGenerator() { + $tokenGenerator = Server::getTokenGenerator(); + $this->assertInstanceOf('\Pdsinterop\Solid\Auth\TokenGenerator', $tokenGenerator); + } + + public function testRespond() { + $response = new MockResponse(); + ob_start(); + Server::respond($response); + $sentBody = ob_get_contents(); + ob_end_clean(); + $this->assertTrue(in_array("HTTP/1.1 200", ServerTest::$headers)); + $this->assertTrue(in_array("Foo:Bar", ServerTest::$headers)); + $this->assertTrue(in_array("Foo:Blah", ServerTest::$headers)); + + $this->assertEquals($sentBody, "{\n \"Hello\": \"world\"\n}"); + } + } + diff --git a/tests/phpunit/StorageServerTest.php b/tests/phpunit/StorageServerTest.php new file mode 100644 index 0000000..0de276f --- /dev/null +++ b/tests/phpunit/StorageServerTest.php @@ -0,0 +1,128 @@ +exec($statement); + } + } catch(\PDOException $e) { + echo $e->getMessage(); + } + + $newUser = [ + "password" => "hello123!@#ABC", + "email" => "alice@example.com", + "hello" => "world" + ]; + self::$createdUser = User::createUser($newUser); + $_SERVER['REQUEST_URI'] = "/test/"; + $_SERVER['REQUEST_SCHEME'] = "https"; + $_SERVER['SERVER_NAME'] = "storage-" . self::$createdUser['userId'] . ".example.com"; + } + + public function testGetFileSystem() { + $filesystem = StorageServer::getFileSystem(); + $this->assertInstanceOf('\League\Flysystem\Filesystem', $filesystem); + } + + + public function testRespond() { + $response = new MockResponse(); + ob_start(); + StorageServer::respond($response); + $sentBody = ob_get_contents(); + ob_end_clean(); + $this->assertTrue(in_array("HTTP/1.1 200", StorageServerTest::$headers)); + $this->assertTrue(in_array("Foo:Bar", StorageServerTest::$headers)); + $this->assertTrue(in_array("Foo:Blah", StorageServerTest::$headers)); + + $this->assertEquals($sentBody, "{\"Hello\":\"world\"}"); + } + + public function testGetOwner() { + $owner = StorageServer::getOwner(); + $this->assertEquals(self::$createdUser['webId'], $owner['webId']); + $this->assertEquals(self::$createdUser['email'], $owner['email']); + } + + public function testGetOwnerWebId() { + $webId = StorageServer::getOwnerWebId(); + $this->assertEquals(self::$createdUser['webId'], $webId); + } + + public function testGenerateDefaultAcl() { + $defaultAcl = StorageServer::generateDefaultAcl(); + $this->assertTrue(strpos($defaultAcl, self::$createdUser['webId']) > 0); + $this->assertMatchesRegularExpression("/@prefix/", $defaultAcl); + } + + public function testGeneratePublicAppendAcl() { + $publicAppendAcl = StorageServer::generatePublicAppendAcl(); + $this->assertTrue(strpos($publicAppendAcl, self::$createdUser['webId']) > 0); + $this->assertMatchesRegularExpression("/@prefix/", $publicAppendAcl); + } + + public function testGeneratePublicReadAcl() { + $publicReadAcl = StorageServer::generatePublicReadAcl(); + $this->assertTrue(strpos($publicReadAcl, self::$createdUser['webId']) > 0); + $this->assertMatchesRegularExpression("/@prefix/", $publicReadAcl); + } + + public function testGenerateDefaultPrivateTypeIndex() { + $privateTypeIndex = StorageServer::generateDefaultPrivateTypeIndex(); + $this->assertTrue(strpos($privateTypeIndex, "UnlistedDocument") > 0); + $this->assertMatchesRegularExpression("/@prefix/", $privateTypeIndex); + } + + public function testGenerateDefaultPublicTypeIndex() { + $publicTypeIndex = StorageServer::generateDefaultPublicTypeIndex(); + $this->assertTrue(strpos($publicTypeIndex, "ListedDocument") > 0); + $this->assertMatchesRegularExpression("/@prefix/", $publicTypeIndex); + } + + public function testGenerateDefaultPreferences() { + $preferences = StorageServer::generateDefaultPreferences(); + $this->assertTrue(strpos($preferences, "ConfigurationFile") > 0); + $this->assertMatchesRegularExpression("/@prefix/", $preferences); + } + + /* + Currently untested: + public static function getWebId($rawRequest) { + public static function initializeStorage() { + */ + } + diff --git a/tests/phpunit/test-config.php b/tests/phpunit/test-config.php index e634ab1..f91f9ac 100644 --- a/tests/phpunit/test-config.php +++ b/tests/phpunit/test-config.php @@ -6,4 +6,59 @@ const BANNED_PASSWORDS = []; const MINIMUM_PASSWORD_ENTROPY = 10; const BASEDOMAIN = "solid.example.com"; -const BASEURL = "https://solid.example.com"; \ No newline at end of file +const BASEURL = "https://solid.example.com"; +const PUBSUB_SERVER = "https://localhost:1234"; +const KEYDIR = "php://memory/"; +const STORAGEBASE = "."; + +function header($header) { + if (class_exists('Pdsinterop\PhpSolid\MiddleWareTest')) { + MiddleWareTest::$headers[] = $header; + } + if (class_exists('Pdsinterop\PhpSolid\ServerTest')) { + ServerTest::$headers[] = $header; + } + if (class_exists('Pdsinterop\PhpSolid\StorageServerTest')) { + StorageServerTest::$headers[] = $header; + } +} + +function file_get_contents($file) { + if (class_exists('Pdsinterop\PhpSolid\ServerTest')) { + if(!isset(ServerTest::$keys)) { + ServerTest::$keys = Server::generateKeySet(); + } + if (preg_match("/encryption/", $file)) { + return ServerTest::$keys['encryptionKey']; + } + if (preg_match("/public/", $file)) { + return ServerTest::$keys['publicKey']; + } + if (preg_match("/private/", $file)) { + return ServerTest::$keys['privateKey']; + } + } +} + +class MockBody { + public function rewind() { + return true; + } + public function getContents() { + return json_encode(["Hello" => "world"]); + } +} + +class MockResponse { + public function getStatusCode() { + return 200; + } + public function getBody() { + return new MockBody(); + } + public function getHeaders() { + return [ + "Foo" => ["Bar", "Blah"] + ]; + } +}