-
-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathSessionAuthenticatorTest.php
More file actions
213 lines (172 loc) · 7 KB
/
Copy pathSessionAuthenticatorTest.php
File metadata and controls
213 lines (172 loc) · 7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
declare(strict_types=1);
namespace Tempest\Auth\Tests;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Tempest\Auth\Authentication\Authenticatable;
use Tempest\Auth\Authentication\AuthenticatableResolver;
use Tempest\Auth\Authentication\SessionAuthenticator;
use Tempest\Auth\Authentication\SessionAuthenticatorReset;
use Tempest\DateTime\DateTime;
use Tempest\Http\Session\Session;
use Tempest\Http\Session\SessionId;
use Tempest\Http\Session\SessionManager;
final class SessionAuthenticatorTest extends TestCase
{
#[Test]
public function current_memoizes_the_resolved_authenticatable_for_the_current_session_identity(): void
{
$authenticatable = new MemoizedAuthenticatable(id: 1);
$resolver = new CountingAuthenticatableResolver($authenticatable);
$session = $this->createSession();
$session->set(SessionAuthenticator::AUTHENTICATABLE_KEY, 1);
$session->set(SessionAuthenticator::AUTHENTICATABLE_CLASS, MemoizedAuthenticatable::class);
$authenticator = new SessionAuthenticator(
sessionManager: new TestingSessionManager(),
session: $session,
authenticatableResolver: $resolver,
);
$this->assertSame($authenticatable, $authenticator->current());
$this->assertSame($authenticatable, $authenticator->current());
$this->assertSame(1, $resolver->resolveCalls);
}
#[Test]
public function current_memoizes_a_missing_authenticatable_for_the_current_session_identity(): void
{
$resolver = new CountingAuthenticatableResolver();
$session = $this->createSession();
$session->set(SessionAuthenticator::AUTHENTICATABLE_KEY, 1);
$session->set(SessionAuthenticator::AUTHENTICATABLE_CLASS, MemoizedAuthenticatable::class);
$authenticator = new SessionAuthenticator(
sessionManager: new TestingSessionManager(),
session: $session,
authenticatableResolver: $resolver,
);
$this->assertNull($authenticator->current());
$this->assertNull($authenticator->current());
$this->assertSame(1, $resolver->resolveCalls);
}
#[Test]
public function current_re_resolves_when_the_session_identity_changes(): void
{
$resolver = new CountingAuthenticatableResolver(
new MemoizedAuthenticatable(id: 1),
new MemoizedAuthenticatable(id: 2),
);
$session = $this->createSession();
$session->set(SessionAuthenticator::AUTHENTICATABLE_KEY, 1);
$session->set(SessionAuthenticator::AUTHENTICATABLE_CLASS, MemoizedAuthenticatable::class);
$authenticator = new SessionAuthenticator(
sessionManager: new TestingSessionManager(),
session: $session,
authenticatableResolver: $resolver,
);
$current = $authenticator->current();
$this->assertInstanceOf(MemoizedAuthenticatable::class, $current);
$this->assertSame(1, $current->id);
$session->set(SessionAuthenticator::AUTHENTICATABLE_KEY, 2);
$current = $authenticator->current();
$this->assertInstanceOf(MemoizedAuthenticatable::class, $current);
$this->assertSame(2, $current->id);
$this->assertSame(2, $resolver->resolveCalls);
}
#[Test]
public function reset_clears_the_cached_current_authenticatable(): void
{
$authenticatable = new MemoizedAuthenticatable(id: 1);
$resolver = new CountingAuthenticatableResolver($authenticatable);
$session = $this->createSession();
$session->set(SessionAuthenticator::AUTHENTICATABLE_KEY, 1);
$session->set(SessionAuthenticator::AUTHENTICATABLE_CLASS, MemoizedAuthenticatable::class);
$authenticator = new SessionAuthenticator(
sessionManager: new TestingSessionManager(),
session: $session,
authenticatableResolver: $resolver,
);
$this->assertSame($authenticatable, $authenticator->current());
new SessionAuthenticatorReset($authenticator)->reset();
$this->assertSame($authenticatable, $authenticator->current());
$this->assertSame(2, $resolver->resolveCalls);
}
#[Test]
public function authenticate_replaces_a_cached_current_authenticatable(): void
{
$resolver = new CountingAuthenticatableResolver(
new MemoizedAuthenticatable(id: 1),
new MemoizedAuthenticatable(id: 2),
);
$session = $this->createSession();
$session->set(SessionAuthenticator::AUTHENTICATABLE_KEY, 1);
$session->set(SessionAuthenticator::AUTHENTICATABLE_CLASS, MemoizedAuthenticatable::class);
$authenticator = new SessionAuthenticator(
sessionManager: new TestingSessionManager(),
session: $session,
authenticatableResolver: $resolver,
);
$current = $authenticator->current();
$this->assertInstanceOf(MemoizedAuthenticatable::class, $current);
$this->assertSame(1, $current->id);
$authenticator->authenticate(new MemoizedAuthenticatable(id: 2));
$current = $authenticator->current();
$this->assertInstanceOf(MemoizedAuthenticatable::class, $current);
$this->assertSame(2, $current->id);
}
private function createSession(): Session
{
$now = DateTime::now();
return new Session(
id: new SessionId('test-session'),
createdAt: $now,
lastActiveAt: $now,
);
}
}
final readonly class MemoizedAuthenticatable implements Authenticatable
{
public function __construct(
public int $id,
) {}
}
final class CountingAuthenticatableResolver implements AuthenticatableResolver
{
public int $resolveCalls = 0;
/** @var array<int|string, MemoizedAuthenticatable> */
private array $authenticatables = [];
public function __construct(MemoizedAuthenticatable ...$authenticatables)
{
foreach ($authenticatables as $authenticatable) {
$this->authenticatables[$authenticatable->id] = $authenticatable;
}
}
public function resolve(int|string $id, string $class): ?Authenticatable
{
$this->resolveCalls++;
if ($class !== MemoizedAuthenticatable::class) {
return null;
}
return $this->authenticatables[$id] ?? null;
}
public function resolveId(Authenticatable $authenticatable): int
{
return $authenticatable instanceof MemoizedAuthenticatable ? $authenticatable->id : 0;
}
}
final class TestingSessionManager implements SessionManager
{
public int $savedSessions = 0;
public function getOrCreate(SessionId $id): Session
{
$now = DateTime::now();
return new Session($id, $now, $now);
}
public function save(Session $session): void
{
$this->savedSessions++;
}
public function delete(Session $session): void {}
public function isValid(Session $session): bool
{
return true;
}
public function deleteExpiredSessions(): void {}
}