Skip to content

Commit c1926c3

Browse files
committed
Add first draft tests for Dpop::getWebId().
1 parent d5c490f commit c1926c3

2 files changed

Lines changed: 59 additions & 2 deletions

File tree

src/Utils/DPop.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class DPop {
3535
* @throws \Exception "Missng DPoP token" when the DPoP token is missing, but the Authorisation header in the request specifies it
3636
*/
3737
public function getWebId($request) {
38+
// @FIXME: What happens when HTTP_AUTHORIZATION is not set?
3839
$auth = explode(" ", $request->getServerParams()['HTTP_AUTHORIZATION']);
3940
$jwt = $auth[1] ?? false;
4041

tests/unit/Utils/DPOPTest.php

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Pdsinterop\Solid\Auth\Utils;
44

5+
use Laminas\Diactoros\ServerRequest;
56
use Pdsinterop\Solid\Auth\AbstractTestCase;
67
use Lcobucci\JWT\Validation\RequiredConstraintsViolated;
78

@@ -54,7 +55,7 @@ protected function setUp(): void
5455
]);
5556

5657
$this->url = 'https://www.example.com';
57-
$this->serverRequest = new \Laminas\Diactoros\ServerRequest(array(),array(), $this->url);
58+
$this->serverRequest = new ServerRequest(array(),array(), $this->url);
5859
}
5960

6061
private function getWrongKey()
@@ -179,7 +180,62 @@ public function testValidateDpopWithCorrectToken(): void
179180
$this->assertTrue($result);
180181
}
181182

182-
// getWebId
183+
final public function testGetWebIdWithoutRequest(): void
184+
{
185+
$dpop = new DPop();
186+
187+
$this->expectArgumentCountError(1);
188+
189+
$dpop->getWebId();
190+
}
191+
192+
/**
193+
* @covers ::getWebId
194+
*/
195+
final public function testGetWebIdWithoutHttpAuthorizationHeader(): void
196+
{
197+
198+
$dpop = new DPop();
199+
200+
$request = new ServerRequest(array(),array(), $this->url);
201+
202+
$this->markTestIncomplete('The current result is not testable (Undefined array key "HTTP_AUTHORIZATION")');
203+
204+
$dpop->getWebId($request);
205+
}
206+
207+
/**
208+
* @covers ::getWebId
209+
*/
210+
final public function testGetWebIdWithInvalidJwt(): void
211+
{
212+
$dpop = new DPop();
213+
214+
$this->expectException(\Exception::class);
215+
$this->expectExceptionMessage('Invalid JWT token');
216+
217+
$request = new ServerRequest(array('HTTP_AUTHORIZATION' => 'Invalid JWT'),array(), $this->url);
218+
219+
$dpop->getWebId($request);
220+
}
221+
222+
223+
/**
224+
* @covers ::getWebId
225+
*/
226+
final public function testGetWebId(): void
227+
{
228+
$dpop = new DPop();
229+
230+
$token = $this->sign($this->dpop)['token'];
231+
232+
$request = new ServerRequest(array('HTTP_AUTHORIZATION' => $token),array(), $this->url);
233+
234+
$actual = $dpop->getWebId($request);
235+
$expected = 'public';
236+
237+
$this->assertEquals($expected, $actual);
238+
}
183239

184240
/////////////////////////////// DATAPROVIDERS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
185241

0 commit comments

Comments
 (0)