Skip to content

Commit 1bc88f4

Browse files
committed
Change DPOPTest to be cleaner
- Rename tests to mention subject under test - Add separator comments between different method types - Move helper funtion to end of class
1 parent d903504 commit 1bc88f4

1 file changed

Lines changed: 37 additions & 33 deletions

File tree

tests/unit/Utils/DPOPTest.php

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,40 +12,12 @@
1212
*/
1313
class DPOPTest extends TestCase
1414
{
15+
////////////////////////////////// FIXTURES \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
1516

1617
private $dpop;
1718
private $url;
1819
private $serverRequest;
1920

20-
protected function sign($dpop, $privateKey = null)
21-
{
22-
$keyPath = __DIR__ . '/../../fixtures/keys';
23-
if (!$privateKey) {
24-
$privateKey = file_get_contents($keyPath . '/private.key');
25-
}
26-
27-
$signature = '';
28-
$success = \openssl_sign(
29-
Base64Url::encode(json_encode($dpop['header'])).'.'.
30-
Base64Url::encode(json_encode($dpop['payload'])),
31-
$signature,
32-
$privateKey,
33-
OPENSSL_ALGO_SHA256
34-
);
35-
36-
if (!$success) {
37-
throw new \Exception('unable to sign dpop');
38-
}
39-
$token = Base64Url::encode(json_encode($dpop['header'])).'.'.
40-
Base64Url::encode(json_encode($dpop['payload'])).'.'.
41-
Base64Url::encode($signature);
42-
43-
return array_merge($dpop, [
44-
'signature' => $signature,
45-
'token' => $token,
46-
]);
47-
}
48-
4921
protected function setUp(): void
5022
{
5123
$keyPath = __DIR__ . '/../../fixtures/keys';
@@ -93,10 +65,12 @@ private function getWrongKey()
9365
return $keyInfo;
9466
}
9567

68+
/////////////////////////////////// TESTS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
69+
9670
/**
9771
* @covers ::validateDpop
9872
*/
99-
public function testWrongTyp(): void
73+
public function testValidateDpopWithWrongTyp(): void
10074
{
10175
$this->dpop['header']['typ'] = 'jwt';
10276
$token = $this->sign($this->dpop);
@@ -111,7 +85,7 @@ public function testWrongTyp(): void
11185
/**
11286
* @covers ::validateDpop
11387
*/
114-
public function testAlgNone(): void
88+
public function testValidateDpopWithAlgNone(): void
11589
{
11690
$this->dpop['header']['alg'] = 'none';
11791
$token = $this->sign($this->dpop);
@@ -126,7 +100,7 @@ public function testAlgNone(): void
126100
/**
127101
* @covers ::validateDpop
128102
*/
129-
public function testWrongKey(): void
103+
public function testValidateDpopWithWrongKey(): void
130104
{
131105
$theWrongKey = $this->getWrongKey();
132106
$this->dpop['header']['jwk'] = [
@@ -148,7 +122,7 @@ public function testWrongKey(): void
148122
/**
149123
* @covers ::validateDpop
150124
*/
151-
public function testCorrectToken(): void
125+
public function testValidateDpopWithCorrectToken(): void
152126
{
153127
$token = $this->sign($this->dpop);
154128

@@ -157,4 +131,34 @@ public function testCorrectToken(): void
157131
$this->assertTrue($result);
158132
}
159133

134+
///////////////////////////// HELPER FUNCTIONS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\
135+
136+
protected function sign($dpop, $privateKey = null)
137+
{
138+
$keyPath = __DIR__ . '/../../fixtures/keys';
139+
if (!$privateKey) {
140+
$privateKey = file_get_contents($keyPath . '/private.key');
141+
}
142+
143+
$signature = '';
144+
$success = \openssl_sign(
145+
Base64Url::encode(json_encode($dpop['header'])).'.'.
146+
Base64Url::encode(json_encode($dpop['payload'])),
147+
$signature,
148+
$privateKey,
149+
OPENSSL_ALGO_SHA256
150+
);
151+
152+
if (!$success) {
153+
throw new \Exception('unable to sign dpop');
154+
}
155+
$token = Base64Url::encode(json_encode($dpop['header'])).'.'.
156+
Base64Url::encode(json_encode($dpop['payload'])).'.'.
157+
Base64Url::encode($signature);
158+
159+
return array_merge($dpop, [
160+
'signature' => $signature,
161+
'token' => $token,
162+
]);
163+
}
160164
}

0 commit comments

Comments
 (0)