ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
ProofKeyRefreshTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\WOPI\Discovery;
22
23use PHPUnit\Framework\TestCase;
24
25final class ProofKeyRefreshTest extends TestCase
26{
28 private array $stored = [];
29 private int $now = 1_000_000;
30 private int $fetches = 0;
32
33 protected function setUp(): void
34 {
35 $this->discovered = new ProofKeys(new ProofKey('bmV3', 'AQAB'));
36 }
37
38 private function refresh(bool $failing = false): ProofKeyRefresh
39 {
40 $settings = $this->createStub(\ilSetting::class);
41 $settings->method('get')->willReturnCallback(
42 fn(string $key, ?string $default = null): ?string => $this->stored[$key] ?? $default
43 );
44 $settings->method('set')->willReturnCallback(function (string $key, string $value): void {
45 $this->stored[$key] = $value;
46 });
47 $settings->method('delete')->willReturnCallback(function (string $key): void {
48 unset($this->stored[$key]);
49 });
50
51 return new ProofKeyRefresh(
54 function () use ($failing): ?ProofKeys {
55 $this->fetches++;
56 if ($failing) {
57 throw new \RuntimeException('discovery not reachable');
58 }
59 return $this->discovered;
60 },
61 fn(): int => $this->now
62 );
63 }
64
66 {
67 $keys = ($this->refresh())();
68
69 $this->assertSame('bmV3', $keys->current->modulus);
70 $this->assertSame('bmV3', $this->stored['wopi_proof_modulus']);
71 }
72
74 {
75 $refresh = $this->refresh();
76
77 $this->assertNotNull($refresh());
78 $this->now += ProofKeyRefresh::MIN_INTERVAL - 1;
79 $this->assertNull($refresh());
80 $this->now += 1;
81 $this->assertNotNull($refresh());
82 $this->assertSame(2, $this->fetches);
83 }
84
85 public function testFailedFetchKeepsTheStoredKeys(): void
86 {
87 $this->stored['wopi_proof_modulus'] = 'b2xk';
88 $this->stored['wopi_proof_exponent'] = 'AQAB';
89
90 $this->assertNull(($this->refresh(true))());
91 $this->assertSame('b2xk', $this->stored['wopi_proof_modulus']);
92 }
93
95 {
96 $this->stored['wopi_proof_modulus'] = 'b2xk';
97 $this->stored['wopi_proof_exponent'] = 'AQAB';
98 $this->discovered = null;
99
100 $this->assertNull(($this->refresh())());
101 $this->assertSame('b2xk', $this->stored['wopi_proof_modulus']);
102 }
103}
$settings
Definition: index.php:44
Fetches the proof keys from the discovery again when a request could not be verified with the stored ...
A public RSA key of a WOPI client, as published in its discovery with base64 encoded modulus and expo...
Definition: ProofKey.php:30
The current and the previous proof key of a WOPI client.
Definition: ProofKeys.php:30