|
| 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 | + |
0 commit comments