-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnowledgeBaseIntegrationTests.cs
More file actions
193 lines (163 loc) · 8.17 KB
/
Copy pathKnowledgeBaseIntegrationTests.cs
File metadata and controls
193 lines (163 loc) · 8.17 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
using System.Net;
using System.Net.Http.Json;
using System.Text.Json;
using Identity.Api.Authentication;
using KnowledgeBase.Api;
using KnowledgeBase.Application.Entries; // BP-031 dispatch-coverage marker
using Microsoft.AspNetCore.TestHost;
using BuildingBlocks.Testing.Http;
using static BuildingBlocks.Testing.Http.IntegrationTestHttpHelpers;
namespace Integration.Tests.Modules.KnowledgeBase;
public sealed class KnowledgeBaseIntegrationTests
{
[Xunit.Fact]
public async Task KnowledgeBasePublicEndpointsExposeSeededPublishedEntries()
{
await using var application = await PostgresBackedApiApplication.StartAsync();
var client = application.App.GetTestClient();
client.BaseAddress = new Uri("https://localhost");
var listResponse = await client.GetAsync("/api/v1/knowledge-base/entries");
Xunit.Assert.Equal(HttpStatusCode.OK, listResponse.StatusCode);
using (var listJson = await ReadJsonAsync(listResponse))
{
var entries = listJson.RootElement.GetProperty("entries").EnumerateArray().ToArray();
Xunit.Assert.True(entries.Length >= 2);
Xunit.Assert.Contains(entries, entry => string.Equals(entry.GetProperty("slug").GetString(), "what-is-this-baseline", StringComparison.Ordinal));
}
var detailResponse = await client.GetAsync("/api/v1/knowledge-base/entries/what-is-this-baseline");
Xunit.Assert.Equal(HttpStatusCode.OK, detailResponse.StatusCode);
using var detailJson = await ReadJsonAsync(detailResponse);
Xunit.Assert.Equal("What is this baseline?", detailJson.RootElement.GetProperty("title").GetString());
Xunit.Assert.Equal("published", detailJson.RootElement.GetProperty("status").GetString());
}
[Xunit.Fact]
public async Task KnowledgeBaseOperatorsCanCreateUpdateAndPublishEntries()
{
await using var application = await PostgresBackedApiApplication.StartAsync();
var client = application.App.GetTestClient();
client.BaseAddress = new Uri("https://localhost");
var adminSession = await SignInAsync(client, "admin", "LocalOnly!123");
var createResponse = await SendJsonAsync(
client,
HttpMethod.Post,
"/api/v1/knowledge-base/manage/entries",
new CreateKnowledgeEntryRequest(
Slug: null,
Title: "How do I trust the frontend contract?",
Body: "Generated contracts should be refreshed from the backend OpenAPI snapshot.",
Category: "Contracts",
Featured: false,
SortOrder: 30),
adminSession.Cookies,
adminSession.HeaderName,
adminSession.RequestToken);
Xunit.Assert.Equal(HttpStatusCode.OK, createResponse.StatusCode);
string entryId;
using (var createJson = await ReadJsonAsync(createResponse))
{
entryId = createJson.RootElement.GetProperty("entryId").GetString() ?? string.Empty;
Xunit.Assert.Equal("draft", createJson.RootElement.GetProperty("status").GetString());
Xunit.Assert.Equal("how-do-i-trust-the-frontend-contract", createJson.RootElement.GetProperty("slug").GetString());
Xunit.Assert.Equal(1, createJson.RootElement.GetProperty("version").GetInt32());
}
var publicBeforePublish = await client.GetAsync("/api/v1/knowledge-base/entries");
using (var publicBeforeJson = await ReadJsonAsync(publicBeforePublish))
{
var entries = publicBeforeJson.RootElement.GetProperty("entries").EnumerateArray().ToArray();
Xunit.Assert.DoesNotContain(entries, entry => string.Equals(entry.GetProperty("entryId").GetString(), entryId, StringComparison.Ordinal));
}
var updateResponse = await SendJsonAsync(
client,
HttpMethod.Put,
$"/api/v1/knowledge-base/manage/entries/{entryId}",
new UpdateKnowledgeEntryRequest(
ExpectedVersion: 1,
Slug: null,
Title: "How do I trust the frontend contract?",
Body: "Refresh the generated TypeScript contracts after changing the backend OpenAPI surface.",
Category: "Contracts",
Featured: true,
SortOrder: 30),
adminSession.Cookies,
adminSession.HeaderName,
adminSession.RequestToken);
Xunit.Assert.Equal(HttpStatusCode.OK, updateResponse.StatusCode);
var publishResponse = await SendJsonAsync(
client,
HttpMethod.Put,
$"/api/v1/knowledge-base/manage/entries/{entryId}/publish",
new PublishKnowledgeEntryRequest(ExpectedVersion: 2),
adminSession.Cookies,
adminSession.HeaderName,
adminSession.RequestToken,
idempotencyKey: "knowledge-base-publish-request-1");
Xunit.Assert.Equal(HttpStatusCode.OK, publishResponse.StatusCode);
var publicAfterPublish = await client.GetAsync("/api/v1/knowledge-base/entries");
Xunit.Assert.Equal(HttpStatusCode.OK, publicAfterPublish.StatusCode);
using (var publicAfterJson = await ReadJsonAsync(publicAfterPublish))
{
var entries = publicAfterJson.RootElement.GetProperty("entries").EnumerateArray().ToArray();
Xunit.Assert.Contains(entries, entry => string.Equals(entry.GetProperty("entryId").GetString(), entryId, StringComparison.Ordinal));
}
var detailResponse = await client.GetAsync("/api/v1/knowledge-base/entries/how-do-i-trust-the-frontend-contract");
Xunit.Assert.Equal(HttpStatusCode.OK, detailResponse.StatusCode);
}
[Xunit.Fact]
public async Task KnowledgeBaseMutationsRejectStaleVersionUpdates()
{
await using var application = await PostgresBackedApiApplication.StartAsync();
var client = application.App.GetTestClient();
client.BaseAddress = new Uri("https://localhost");
var adminSession = await SignInAsync(client, "admin", "LocalOnly!123");
var createResponse = await SendJsonAsync(
client,
HttpMethod.Post,
"/api/v1/knowledge-base/manage/entries",
new CreateKnowledgeEntryRequest(
Slug: "stale-version-entry",
Title: "Stale version entry",
Body: "First draft",
Category: "Testing",
Featured: false,
SortOrder: 99),
adminSession.Cookies,
adminSession.HeaderName,
adminSession.RequestToken);
using var createJson = await ReadJsonAsync(createResponse);
var entryId = createJson.RootElement.GetProperty("entryId").GetString() ?? string.Empty;
var firstUpdate = await SendJsonAsync(
client,
HttpMethod.Put,
$"/api/v1/knowledge-base/manage/entries/{entryId}",
new UpdateKnowledgeEntryRequest(
ExpectedVersion: 1,
Slug: "stale-version-entry",
Title: "Stale version entry",
Body: "Second draft",
Category: "Testing",
Featured: false,
SortOrder: 99),
adminSession.Cookies,
adminSession.HeaderName,
adminSession.RequestToken);
Xunit.Assert.Equal(HttpStatusCode.OK, firstUpdate.StatusCode);
var staleUpdate = await SendJsonAsync(
client,
HttpMethod.Put,
$"/api/v1/knowledge-base/manage/entries/{entryId}",
new UpdateKnowledgeEntryRequest(
ExpectedVersion: 1,
Slug: "stale-version-entry",
Title: "Stale version entry",
Body: "Stale write",
Category: "Testing",
Featured: false,
SortOrder: 99),
adminSession.Cookies,
adminSession.HeaderName,
adminSession.RequestToken);
Xunit.Assert.Equal(HttpStatusCode.Conflict, staleUpdate.StatusCode);
using var staleJson = await ReadJsonAsync(staleUpdate);
Xunit.Assert.Equal("knowledge-base.entry_version_conflict", staleJson.RootElement.GetProperty("code").GetString());
}
}