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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com>
// SPDX-License-Identifier: AGPL-3.0-or-later WITH AdditionRef-Progmasoft-Patent-Grant-1.1
using System.Net;
using System.Net.Http.Json;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging.Abstractions;
using Progmasoft.Progmaweb.Api.Accounts;
namespace Progmasoft.Progmaweb.Api.Tests;
public sealed class AccountStoreSelectionTests
{
private const string Database = "Host=/var/run/postgresql;Database=progmaweb;Username=progmaweb";
private static IConfiguration Configuration(string? store, string? connectionString) =>
new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string?>
{
[AccountStoreRegistration.StoreKey] = store,
["ConnectionStrings:Accounts"] = connectionString
})
.Build();
[Theory]
[InlineData(null, Database, false, AccountStoreKind.PostgreSql)]
[InlineData(null, Database, true, AccountStoreKind.PostgreSql)]
[InlineData("PostgreSQL", Database, true, AccountStoreKind.PostgreSql)]
[InlineData("postgresql", Database, false, AccountStoreKind.PostgreSql)]
[InlineData(null, null, false, AccountStoreKind.Memory)]
[InlineData("", " ", false, AccountStoreKind.Memory)]
[InlineData("Memory", null, false, AccountStoreKind.Memory)]
[InlineData("memory", null, true, AccountStoreKind.Memory)]
internal void TheConfigurationSelectsTheStore(
string? store,
string? connectionString,
bool isProduction,
AccountStoreKind expected)
{
Assert.Equal(expected, AccountStoreRegistration.Select(Configuration(store, connectionString), isProduction));
}
[Theory]
// Production never falls back to memory on its own.
[InlineData(null, null, true, "No account database is configured")]
[InlineData("", "", true, "No account database is configured")]
[InlineData("PostgreSQL", null, false, "is not set")]
[InlineData("Memory", Database, false, "Remove one of them")]
[InlineData("Sqlite", null, false, "must be PostgreSQL or Memory")]
public void AConfigurationThatCannotBeMeantIsRefused(
string? store,
string? connectionString,
bool isProduction,
string expectedMessage)
{
InvalidOperationException refused = Assert.Throws<InvalidOperationException>(
() => AccountStoreRegistration.Select(Configuration(store, connectionString), isProduction));
Assert.Contains(expectedMessage, refused.Message, StringComparison.Ordinal);
// A message about the configuration never repeats the connection string.
Assert.DoesNotContain("Host=", refused.Message, StringComparison.Ordinal);
}
[Fact]
public void AProductionServiceWithoutADatabaseDoesNotStart()
{
using AccountApiFactory factory = new(environment: "Production");
InvalidOperationException refused = Assert.Throws<InvalidOperationException>(() => factory.CreateAccountClient());
Assert.Contains("No account database is configured", refused.Message, StringComparison.Ordinal);
}
[Fact]
public void AProductionServiceWithoutOAuthKeysDoesNotStart()
{
using AccountApiFactory factory = new(environment: "Production", accountStore: "Memory", oauthKeys: false);
InvalidOperationException refused = Assert.Throws<InvalidOperationException>(() => factory.CreateAccountClient());
Assert.Contains("must name the token key files in production", refused.Message, StringComparison.Ordinal);
}
[Fact]
public async Task AProductionServiceThatAsksForMemoryStartsAndKeepsAccountsInMemory()
{
await using AccountApiFactory factory = new(environment: "Production", accountStore: "Memory");
using HttpClient client = factory.CreateAccountClient();
HttpResponseMessage health = await client.GetAsync("/health", TestContext.Current.CancellationToken);
Assert.Equal(HttpStatusCode.OK, health.StatusCode);
Assert.IsType<InMemoryAccountStore>(factory.Services.GetRequiredService<IAccountStore>());
}
[Fact]
public async Task TheTestServiceKeepsAccountsInMemoryWithoutBeingAsked()
{
await using AccountApiFactory factory = new();
using HttpClient client = factory.CreateAccountClient();
await client.GetAsync("/health", TestContext.Current.CancellationToken);
Assert.IsType<InMemoryAccountStore>(factory.Services.GetRequiredService<IAccountStore>());
}
}
public sealed class ExpiredSessionCleanupTests
{
private sealed class FixedClock(DateTimeOffset now) : TimeProvider
{
public override DateTimeOffset GetUtcNow() => now;
}
private sealed class FailingStore : IAccountStore
{
public int Attempts { get; private set; }
public ValueTask<int> RemoveExpiredSessionsAsync(DateTimeOffset now, CancellationToken cancellationToken)
{
Attempts++;
throw new IOException("The database does not answer.");
}
public ValueTask<CreateAccountResult> CreateAsync(AccountRecord account, CancellationToken cancellationToken) =>
throw new NotSupportedException();
public ValueTask<AccountRecord?> FindByEmailAsync(string normalizedEmail, CancellationToken cancellationToken) =>
throw new NotSupportedException();
public ValueTask<AccountRecord?> FindByGoogleSubjectAsync(string googleSubject, CancellationToken cancellationToken) =>
throw new NotSupportedException();
public ValueTask<AccountRecord?> FindByIdAsync(Guid id, CancellationToken cancellationToken) =>
throw new NotSupportedException();
public ValueTask<AccountRecord?> FindByAccountNameAsync(
string normalizedAccountName,
CancellationToken cancellationToken) =>
throw new NotSupportedException();
public ValueTask StoreSessionAsync(SessionRecord session, CancellationToken cancellationToken) =>
throw new NotSupportedException();
public ValueTask<SessionRecord?> FindSessionAsync(byte[] tokenDigest, CancellationToken cancellationToken) =>
throw new NotSupportedException();
public ValueTask RevokeSessionAsync(byte[] tokenDigest, CancellationToken cancellationToken) =>
throw new NotSupportedException();
}
[Fact]
public async Task ARemovalTakesWhatHasExpiredByTheClock()
{
DateTimeOffset now = new(2026, 10, 6, 12, 0, 0, TimeSpan.Zero);
InMemoryAccountStore store = new();
AccountRecord account = new(Guid.NewGuid(), "Owner", "OWNER", "o@example.com", "O@EXAMPLE.COM", null, "hash", now);
await store.CreateAsync(account, TestContext.Current.CancellationToken);
byte[] expired = [.. Enumerable.Repeat((byte)1, 32)];
byte[] valid = [.. Enumerable.Repeat((byte)2, 32)];
await store.StoreSessionAsync(new SessionRecord(expired, account.Id, now.AddHours(-13), now.AddHours(-1)), TestContext.Current.CancellationToken);
await store.StoreSessionAsync(new SessionRecord(valid, account.Id, now, now.AddHours(12)), TestContext.Current.CancellationToken);
using ExpiredSessionCleanup cleanup = new(store, new FixedClock(now), NullLogger<ExpiredSessionCleanup>.Instance);
await cleanup.RemoveOnceAsync(TestContext.Current.CancellationToken);
Assert.Null(await store.FindSessionAsync(expired, TestContext.Current.CancellationToken));
Assert.NotNull(await store.FindSessionAsync(valid, TestContext.Current.CancellationToken));
}
[Fact]
public async Task ARemovalThatFailsDoesNotStopTheService()
{
FailingStore store = new();
using ExpiredSessionCleanup cleanup = new(store, TimeProvider.System, NullLogger<ExpiredSessionCleanup>.Instance);
await cleanup.RemoveOnceAsync(TestContext.Current.CancellationToken);
await cleanup.RemoveOnceAsync(TestContext.Current.CancellationToken);
Assert.Equal(2, store.Attempts);
}
[Fact]
public async Task ACancelledRemovalIsNotSwallowed()
{
InMemoryAccountStore store = new();
using ExpiredSessionCleanup cleanup = new(store, TimeProvider.System, NullLogger<ExpiredSessionCleanup>.Instance);
using CancellationTokenSource cancelled = new();
await cancelled.CancelAsync();
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => cleanup.RemoveOnceAsync(cancelled.Token));
}
}
public sealed class DurableAccountApiTests
{
private static async Task<string> SessionCookieAsync(HttpResponseMessage response)
{
Assert.True(response.IsSuccessStatusCode, await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken));
string header = Assert.Single(
response.Headers.GetValues("Set-Cookie"),
value => value.StartsWith("__Host-ProgmasoftSession=", StringComparison.Ordinal));
return header[..header.IndexOf(';', StringComparison.Ordinal)];
}
private static HttpRequestMessage Me(string cookie)
{
HttpRequestMessage request = new(HttpMethod.Get, "/api/v1/accounts/me");
request.Headers.Add("Cookie", cookie);
return request;
}
[Fact]
public async Task AnAccountAndItsSessionSurviveARestartOfTheService()
{
await using PostgresTestDatabase database = await PostgresTestDatabase.CreateAsync();
string cookie;
await using (AccountApiFactory beforeRestart = new(accountDatabase: database.ConnectionString))
{
using HttpClient client = beforeRestart.CreateAccountClient(handleCookies: false);
HttpResponseMessage registration = await client.PostAsJsonAsync("/api/v1/accounts/register", new
{
accountName = "Survivor",
email = "survivor@example.com",
password = "a-long-development-password"
}, TestContext.Current.CancellationToken);
Assert.Equal(HttpStatusCode.Created, registration.StatusCode);
cookie = await SessionCookieAsync(registration);
Assert.IsType<PostgresAccountStore>(beforeRestart.Services.GetRequiredService<IAccountStore>());
}
// The first service is gone with everything it held in memory. The second starts on the same database.
await using AccountApiFactory afterRestart = new(accountDatabase: database.ConnectionString);
using HttpClient restarted = afterRestart.CreateAccountClient(handleCookies: false);
HttpResponseMessage me = await restarted.SendAsync(Me(cookie), TestContext.Current.CancellationToken);
Assert.Equal(HttpStatusCode.OK, me.StatusCode);
AccountResponse account = Assert.IsType<AccountResponse>(
await me.Content.ReadFromJsonAsync<AccountResponse>(TestContext.Current.CancellationToken));
Assert.Equal("Survivor", account.AccountName);
Assert.Equal("survivor@example.com", account.Email);
HttpResponseMessage login = await restarted.PostAsJsonAsync("/api/v1/accounts/login", new
{
email = "survivor@example.com",
password = "a-long-development-password"
}, TestContext.Current.CancellationToken);
Assert.Equal(HttpStatusCode.OK, login.StatusCode);
HttpResponseMessage again = await restarted.PostAsJsonAsync("/api/v1/accounts/register", new
{
accountName = "survivor",
email = "other@example.com",
password = "a-long-development-password"
}, TestContext.Current.CancellationToken);
Assert.Equal(HttpStatusCode.BadRequest, again.StatusCode);
// Signing out removes the session from the database, not only from this process.
HttpRequestMessage logout = new(HttpMethod.Post, "/api/v1/accounts/logout");
logout.Headers.Add("Cookie", cookie);
await restarted.SendAsync(logout, TestContext.Current.CancellationToken);
HttpResponseMessage afterLogout = await restarted.SendAsync(Me(cookie), TestContext.Current.CancellationToken);
Assert.Equal(HttpStatusCode.Unauthorized, afterLogout.StatusCode);
}
[Fact]
public async Task TheHealthEndpointAnswersWhileTheDatabaseDoes()
{
await using PostgresTestDatabase database = await PostgresTestDatabase.CreateAsync();
await using AccountApiFactory factory = new(accountDatabase: database.ConnectionString, environment: "Production");
using HttpClient client = factory.CreateAccountClient();
HttpResponseMessage health = await client.GetAsync("/health", TestContext.Current.CancellationToken);
Assert.Equal(HttpStatusCode.OK, health.StatusCode);
}
[Fact]
public void AServiceWhoseDatabaseCannotBeReachedDoesNotStart()
{
// Nothing listens on this port of the loopback address; the short timeout keeps the test quick.
using AccountApiFactory factory = new(
accountDatabase: "Host=127.0.0.1;Port=1;Database=absent;Username=nobody;Timeout=2;Command Timeout=2");
Assert.ThrowsAny<Exception>(() => factory.CreateAccountClient());
}
}
|