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