Skip to content

Commit 0616929

Browse files
net0wellclaude
andcommitted
test(dashboard): critérios de aceite das specs 007 e 009
- Projeto PythonScriptRunner.Dashboard.Tests (TestServer/WebApplicationFactory + Testcontainers), reaproveitando por link a infraestrutura dos testes do worker. - Spec 007 AC-1..AC-25: configuração inválida, matriz endpoint × papel (login fixo e host), CSRF, 412/428, sintaxe 422, upload/download e 413, keyset, 410, tempo real em duas instâncias (≤ 1 s), auditoria, desempenho com ~200 mil execuções (opt-in completo por PSR_PERF_RUNS_PER_DAY), prefixo customizado, snapshot do OpenAPI, segredos, login fixo (sessão, bloqueio, 429, recarga, chaves compartilhadas), filtro, localhost e visão geral. - Spec 009 AC-9..AC-11: DashboardHost com login fixo e com OIDC; psr hash-password. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 14e25fe commit 0616929

14 files changed

Lines changed: 7545 additions & 0 deletions

‎src/PythonScriptRunner.Core/PythonScriptRunner.Core.csproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<InternalsVisibleTo Include="PythonScriptRunner.EntityFrameworkCore.Tests" />
2121
<InternalsVisibleTo Include="PythonScriptRunner.RabbitMQ.Tests" />
2222
<InternalsVisibleTo Include="PythonScriptRunner.Worker.Tests" />
23+
<InternalsVisibleTo Include="PythonScriptRunner.Dashboard.Tests" />
2324
</ItemGroup>
2425

2526
<ItemGroup>

‎src/PythonScriptRunner.RabbitMQ/PythonScriptRunner.RabbitMQ.csproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<ItemGroup>
1313
<InternalsVisibleTo Include="PythonScriptRunner.RabbitMQ.Tests" />
1414
<InternalsVisibleTo Include="PythonScriptRunner.Worker.Tests" />
15+
<InternalsVisibleTo Include="PythonScriptRunner.Dashboard.Tests" />
1516
</ItemGroup>
1617

1718
<ItemGroup>

‎tests/PythonScriptRunner.Dashboard.Tests/AccessTests.cs‎

Lines changed: 374 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
using System.Net;
2+
using System.Text;
3+
using PythonScriptRunner.Dashboard.Tests.Infrastructure;
4+
5+
namespace PythonScriptRunner.Dashboard.Tests;
6+
7+
/// <summary>Spec 007, AC-4: every endpoint × role gives 2xx (or a business answer) or 403, in the fixed login and host modes.</summary>
8+
public sealed class AuthorizationMatrixTests(PostgresFixture postgres, BrokerFixture broker, PythonFixture python)
9+
{
10+
private static readonly string Unknown = Guid.CreateVersion7().ToString();
11+
12+
/// <summary>Method, path, body, lowest role (<c>null</c>: any identified user) and whether an allowed call must be 2xx.</summary>
13+
private static readonly (string Method, string Path, string? Body, DashboardRole? Minimum, bool Ok)[] Matrix =
14+
[
15+
("GET", "/me", null, null, true),
16+
("GET", "/stages", null, DashboardRole.Reader, true),
17+
("GET", "/stages/eco-{role}", null, DashboardRole.Reader, true),
18+
("GET", "/stages/eco-{role}/versions/1", null, DashboardRole.Reader, true),
19+
("GET", "/stages/eco-{role}/versions/1/export", null, DashboardRole.Reader, true),
20+
("GET", "/pipelines", null, DashboardRole.Reader, true),
21+
("GET", "/pipelines/trilha-{role}", null, DashboardRole.Reader, true),
22+
("GET", "/pipelines/trilha-{role}/versions/1", null, DashboardRole.Reader, true),
23+
("GET", "/pipelines/trilha-{role}/versions/1/export", null, DashboardRole.Reader, true),
24+
("GET", "/overview", null, DashboardRole.Reader, true),
25+
("GET", "/runs", null, DashboardRole.Reader, true),
26+
($"GET", $"/runs/{Unknown}", null, DashboardRole.Reader, false),
27+
("GET", $"/runs/{Unknown}/attempts/{Unknown}/logs/stdout", null, DashboardRole.Reader, false),
28+
("GET", $"/artifacts/{Unknown}", null, DashboardRole.Reader, false),
29+
("GET", "/schedules", null, DashboardRole.Reader, true),
30+
("POST", "/schedules/preview", """{"cron":"0 8 * * *","timeZone":"America/Sao_Paulo","count":3}""", DashboardRole.Reader, true),
31+
("GET", "/meta/field-types", null, DashboardRole.Reader, true),
32+
("POST", "/stages/eco-{role}/runs", """{"inputs":{"texto":"oi"}}""", DashboardRole.Executor, true),
33+
("POST", "/pipelines/trilha-{role}/runs", """{"inputs":{"texto":"oi"}}""", DashboardRole.Executor, true),
34+
("POST", $"/runs/{Unknown}/cancel", null, DashboardRole.Executor, false),
35+
("POST", $"/runs/{Unknown}/reprocess", null, DashboardRole.Executor, false),
36+
("POST", "/stages", """{"key":"nova-{role}","name":"Nova"}""", DashboardRole.Author, true),
37+
("PATCH", "/stages/eco-{role}", """{"name":"Eco"}""", DashboardRole.Author, false),
38+
("GET", "/stages/eco-{role}/draft", null, DashboardRole.Author, false),
39+
("POST", "/stages/eco-{role}/draft", """{"fromVersion":1}""", DashboardRole.Author, false),
40+
("PUT", "/stages/eco-{role}/draft", """{"script":"x","pythonVersion":"3.12"}""", DashboardRole.Author, false),
41+
("POST", "/stages/eco-{role}/draft/validate", null, DashboardRole.Author, false),
42+
("POST", "/stages/eco-{role}/draft/test", """{"inputs":{"texto":"oi"}}""", DashboardRole.Author, false),
43+
("POST", "/stages/eco-{role}/draft/publish", null, DashboardRole.Author, false),
44+
("DELETE", "/stages/eco-{role}/draft", null, DashboardRole.Author, false),
45+
("POST", "/pipelines", """{"key":"nova-{role}","name":"Nova"}""", DashboardRole.Author, true),
46+
("POST", "/pipelines/validate", """{"steps":[]}""", DashboardRole.Author, true),
47+
("GET", "/pipelines/trilha-{role}/draft", null, DashboardRole.Author, false),
48+
("POST", "/pipelines/trilha-{role}/draft", """{"fromVersion":1}""", DashboardRole.Author, false),
49+
("PUT", "/pipelines/trilha-{role}/draft", """{"steps":[]}""", DashboardRole.Author, false),
50+
("POST", "/pipelines/trilha-{role}/draft/test", """{"inputs":{"texto":"oi"}}""", DashboardRole.Author, false),
51+
("POST", "/pipelines/trilha-{role}/draft/publish", null, DashboardRole.Author, false),
52+
("DELETE", "/pipelines/trilha-{role}/draft", null, DashboardRole.Author, false),
53+
("PATCH", "/pipelines/trilha-{role}/settings", """{"maxConcurrentRuns":2}""", DashboardRole.Author, false),
54+
("POST", "/pipelines/trilha-{role}/schedules", """{"name":"diário {role}","cron":"0 8 * * *","inputs":{"texto":"oi"}}""", DashboardRole.Author, true),
55+
("PUT", $"/schedules/{Unknown}", """{"name":"x","cron":"0 8 * * *"}""", DashboardRole.Author, false),
56+
("POST", $"/schedules/{Unknown}/enable", null, DashboardRole.Author, false),
57+
("POST", $"/schedules/{Unknown}/disable", null, DashboardRole.Author, false),
58+
("DELETE", $"/schedules/{Unknown}", null, DashboardRole.Author, false),
59+
("POST", "/import", null, DashboardRole.Author, false),
60+
("GET", "/audit", null, DashboardRole.Admin, true),
61+
("GET", "/admin/secrets", null, DashboardRole.Admin, true),
62+
("GET", "/admin/settings", null, DashboardRole.Admin, true),
63+
("PATCH", "/pipelines/trilha-{role}/retention", """{"artifactRetentionDays":3,"runRetentionDays":30}""", DashboardRole.Admin, false),
64+
("POST", "/stages/eco-{role}/versions/1/archive", null, DashboardRole.Author, false),
65+
("POST", "/pipelines/trilha-{role}/versions/1/archive", null, DashboardRole.Author, false),
66+
("DELETE", "/pipelines/trilha-{role}", null, DashboardRole.Admin, false),
67+
("DELETE", "/stages/eco-{role}", null, DashboardRole.Admin, false),
68+
];
69+
70+
private static CancellationToken Ct => TestContext.Current.CancellationToken;
71+
72+
[Fact]
73+
public async Task Spec007_AC04_RoleMatrix_FixedLogin()
74+
{
75+
await using var env = await PsrEnvironment.CreateAsync(postgres, broker, python);
76+
await using var app = await DashboardApp.StartAsync(env);
77+
await SeedAsync(app);
78+
foreach (var (name, role) in TestUsers.All)
79+
{
80+
using var client = await app.LoginAsync(name);
81+
await CheckAsync(client, role);
82+
}
83+
}
84+
85+
[Fact]
86+
public async Task Spec007_AC04_RoleMatrix_HostAuthentication()
87+
{
88+
await using var env = await PsrEnvironment.CreateAsync(postgres, broker, python);
89+
await using var app = await DashboardApp.StartAsync(env, s => HeaderAuthenticationHandler.Register(s));
90+
await SeedAsync(app);
91+
92+
// Authenticated by the host without any dashboard role: identified, but every action is forbidden.
93+
using (var nobody = app.CreateClient())
94+
{
95+
nobody.DefaultRequestHeaders.Add(HeaderAuthenticationHandler.Header, "sub-0|Sem Papel|outra-coisa");
96+
var me = await (await nobody.GetAsync("/psr/api/me", Ct)).JsonAsync();
97+
me["subject"]!.GetValue<string>().ShouldBe("sub-0");
98+
me["role"].ShouldBeNull();
99+
(await nobody.GetAsync("/psr/api/stages", Ct)).StatusCode.ShouldBe(HttpStatusCode.Forbidden);
100+
}
101+
102+
foreach (var (_, role) in TestUsers.All)
103+
{
104+
using var client = app.CreateClient();
105+
client.DefaultRequestHeaders.Add(HeaderAuthenticationHandler.Header, $"sub-{role}|Pessoa {role}|{HeaderAuthenticationHandler.RoleClaim(role)}");
106+
await CheckAsync(client, role);
107+
}
108+
}
109+
110+
/// <summary>
111+
/// Per role (the destructive calls of one role must not change what the next one sees): a published stage <c>eco-&lt;role&gt;</c>
112+
/// and pipeline <c>trilha-&lt;role&gt;</c>, each with a draft of version 2.
113+
/// </summary>
114+
private static async Task SeedAsync(DashboardApp app)
115+
{
116+
foreach (var (_, role) in TestUsers.All)
117+
{
118+
var suffix = role.ToString().ToLowerInvariant();
119+
await app.PublishStageAsync($"eco-{suffix}", Scripts.Echo, [Scripts.Text("texto")], [Scripts.Text("texto")]);
120+
var pipeline = await app.Admin.CreatePipelineAsync(new NewCatalogItem($"trilha-{suffix}", "Trilha"), PsrEnvironment.Author, Ct);
121+
var saved = await app.Admin.SavePipelineDraftAsync($"trilha-{suffix}", pipeline.Version,
122+
Scripts.SingleStep($"eco-{suffix}", 1, [Scripts.Text("texto")]), pipeline.RowVersion, PsrEnvironment.Author, Ct);
123+
await app.Admin.PublishPipelineAsync($"trilha-{suffix}", saved.Version, saved.RowVersion, PsrEnvironment.Author, Ct);
124+
await app.Admin.CreateStageDraftFromAsync($"eco-{suffix}", 1, PsrEnvironment.Author, Ct);
125+
await app.Admin.CreatePipelineDraftFromAsync($"trilha-{suffix}", 1, PsrEnvironment.Author, Ct);
126+
}
127+
}
128+
129+
private static async Task CheckAsync(HttpClient client, DashboardRole role)
130+
{
131+
var failures = new List<string>();
132+
foreach (var (method, path, body, minimum, ok) in Matrix)
133+
{
134+
var suffix = role.ToString().ToLowerInvariant();
135+
using var request = new HttpRequestMessage(new HttpMethod(method), "/psr/api" + path.Replace("{role}", suffix, StringComparison.Ordinal));
136+
if (body is not null)
137+
{
138+
request.Content = new StringContent(body.Replace("{role}", suffix, StringComparison.Ordinal), Encoding.UTF8,
139+
"application/json");
140+
}
141+
142+
if (method is "PUT" or "PATCH" or "DELETE" || path.EndsWith("/publish", StringComparison.Ordinal))
143+
{
144+
request.Headers.IfMatch.Add(new System.Net.Http.Headers.EntityTagHeaderValue("\"1\""));
145+
}
146+
147+
using var response = await client.SendAsync(request, Ct);
148+
var status = (int)response.StatusCode;
149+
var allowed = minimum is null || role >= minimum;
150+
var expectation = allowed ? (ok ? "2xx" : "not 401/403") : "403";
151+
var met = allowed ? (ok ? status is >= 200 and < 300 : status is not (401 or 403)) : status == 403;
152+
if (!met)
153+
{
154+
failures.Add($"{role} {method} {path}: {status} (expected {expectation}) {await response.Content.ReadAsStringAsync(Ct)}");
155+
}
156+
}
157+
158+
failures.ShouldBeEmpty();
159+
}
160+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
using System.Net;
2+
using System.Net.Http.Json;
3+
using System.Text.Json.Nodes;
4+
using PythonScriptRunner.Dashboard.Tests.Infrastructure;
5+
6+
namespace PythonScriptRunner.Dashboard.Tests;
7+
8+
/// <summary>Spec 007: catalog editing through the API (drafts, concurrency, publication, secrets).</summary>
9+
public sealed class CatalogApiTests(PostgresFixture postgres, BrokerFixture broker, PythonFixture python)
10+
{
11+
private static CancellationToken Ct => TestContext.Current.CancellationToken;
12+
13+
[Fact]
14+
public async Task Spec007_AC06_TwoSavesWithTheSameIfMatch_TheSecondGets412WithTheCurrentDraft()
15+
{
16+
await using var env = await PsrEnvironment.CreateAsync(postgres, broker, python);
17+
await using var app = await DashboardApp.StartAsync(env);
18+
using var author = await app.LoginAsync("autor");
19+
(await author.PostAsJsonAsync("/psr/api/stages", new { key = "conflito", name = "Conflito" }, Ct)).StatusCode.ShouldBe(HttpStatusCode.Created);
20+
var read = await author.GetAsync("/psr/api/stages/conflito/draft", Ct);
21+
var etag = read.ETag();
22+
var data = (await read.JsonAsync())["data"]!.AsObject();
23+
24+
data["script"] = Scripts.Echo;
25+
var first = await author.SendJsonAsync(HttpMethod.Put, "/psr/api/stages/conflito/draft", data, etag);
26+
data["script"] = Scripts.Sleep;
27+
var second = await author.SendJsonAsync(HttpMethod.Put, "/psr/api/stages/conflito/draft", data, etag);
28+
var withoutIfMatch = await author.SendJsonAsync(HttpMethod.Put, "/psr/api/stages/conflito/draft", data);
29+
30+
first.StatusCode.ShouldBe(HttpStatusCode.OK);
31+
first.ETag().ShouldNotBe(etag);
32+
second.StatusCode.ShouldBe(HttpStatusCode.PreconditionFailed);
33+
var problem = await second.JsonAsync();
34+
problem["code"]!.GetValue<string>().ShouldBe("CONCURRENCY_CONFLICT");
35+
problem["current"]!["data"]!["script"]!.GetValue<string>().ShouldBe(Scripts.Echo);
36+
withoutIfMatch.StatusCode.ShouldBe((HttpStatusCode)428);
37+
(await withoutIfMatch.JsonAsync())["code"]!.GetValue<string>().ShouldBe("IF_MATCH_REQUIRED");
38+
}
39+
40+
[Fact]
41+
public async Task Spec007_AC07_AC12_PublishWithSyntaxError_Gets422WithLineAndColumn_PublicationIsAudited()
42+
{
43+
await using var env = await PsrEnvironment.CreateAsync(postgres, broker, python);
44+
await using var app = await DashboardApp.StartAsync(env);
45+
using var author = await app.LoginAsync("autor");
46+
await author.PostAsJsonAsync("/psr/api/stages", new { key = "quebrado", name = "Quebrado" }, Ct);
47+
var draft = await author.GetAsync("/psr/api/stages/quebrado/draft", Ct);
48+
var data = (await draft.JsonAsync())["data"]!.AsObject();
49+
data["script"] = "from psrunner import stage\n\n\n@stage\ndef run(inputs, ctx):\n return {\"x\": (1 +}\n";
50+
var saved = await author.SendJsonAsync(HttpMethod.Put, "/psr/api/stages/quebrado/draft", data, draft.ETag());
51+
52+
var publish = await author.SendJsonAsync(HttpMethod.Post, "/psr/api/stages/quebrado/draft/publish", ifMatch: saved.ETag());
53+
54+
publish.StatusCode.ShouldBe(HttpStatusCode.UnprocessableEntity);
55+
var problem = await publish.JsonAsync();
56+
problem["code"]!.GetValue<string>().ShouldBe("SCRIPT_SYNTAX_ERROR");
57+
problem["line"]!.GetValue<int>().ShouldBe(6);
58+
problem["column"]!.GetValue<int>().ShouldBeGreaterThan(0);
59+
(await app.Admin.GetStageDraftAsync("quebrado", Ct)).ShouldNotBeNull("the draft is kept");
60+
61+
data["script"] = Scripts.Echo;
62+
data["inputs"] = new JsonArray(new JsonObject { ["name"] = "texto", ["type"] = "string" });
63+
data["outputs"] = new JsonArray(new JsonObject { ["name"] = "texto", ["type"] = "string" });
64+
var fixedDraft = await author.SendJsonAsync(HttpMethod.Put, "/psr/api/stages/quebrado/draft", data, saved.ETag());
65+
fixedDraft.StatusCode.ShouldBe(HttpStatusCode.OK);
66+
var published = await author.SendJsonAsync(HttpMethod.Post, "/psr/api/stages/quebrado/draft/publish", ifMatch: fixedDraft.ETag());
67+
published.StatusCode.ShouldBe(HttpStatusCode.OK);
68+
(await published.JsonAsync())["status"]!.GetValue<string>().ShouldBe("Published");
69+
70+
(await env.ScalarAsync<string>("SELECT actor FROM psr.audit_events WHERE action = 'StagePublished' AND entity_key = 'quebrado@1'"))
71+
.ShouldBe("login:autor");
72+
}
73+
74+
[Fact]
75+
public async Task Spec007_AC16_Secrets_ShowStatusButNeverValues()
76+
{
77+
const string value = "valor-super-secreto-9f3a";
78+
await using var env = await PsrEnvironment.CreateAsync(postgres, broker, python);
79+
await using var app = await DashboardApp.StartAsync(env, s => s.Configuration["PythonScriptRunner:Secrets:SMTP_SENHA"] = value);
80+
await app.PublishStageAsync("envia", Scripts.Echo, [Scripts.Text("texto")], [Scripts.Text("texto")], ["SMTP_SENHA", "API_TOKEN"]);
81+
await app.PublishStageAsync("outra", Scripts.Echo, [Scripts.Text("texto")], [Scripts.Text("texto")], ["SMTP_SENHA"]);
82+
using var admin = await app.LoginAsync("admin");
83+
84+
var response = await admin.GetAsync("/psr/api/admin/secrets", Ct);
85+
var settings = await admin.GetStringAsync("/psr/api/admin/settings", Ct);
86+
87+
response.StatusCode.ShouldBe(HttpStatusCode.OK);
88+
var text = await response.Content.ReadAsStringAsync(Ct);
89+
text.ShouldNotContain(value);
90+
settings.ShouldNotContain(value);
91+
settings.ShouldNotContain(TestUsers.Hash);
92+
var secrets = JsonNode.Parse(text)!.AsArray();
93+
secrets.Select(s => (s!["name"]!.GetValue<string>(), s["resolved"]!.GetValue<bool>()))
94+
.ShouldBe([("API_TOKEN", false), ("SMTP_SENHA", true)]);
95+
secrets[1]!["declaredBy"]!.AsArray().Select(d => d!.GetValue<string>()).ShouldBe(["envia@1", "outra@1"]);
96+
}
97+
98+
[Fact]
99+
public async Task FieldTypes_DescribeTheTwelveTypesAndTheCompatibility()
100+
{
101+
await using var env = await PsrEnvironment.CreateAsync(postgres, broker, python);
102+
await using var app = await DashboardApp.StartAsync(env);
103+
using var reader = await app.LoginAsync("leitor");
104+
105+
var types = await (await reader.GetAsync("/psr/api/meta/field-types", Ct)).JsonAsync();
106+
107+
types["types"]!.AsArray().Count.ShouldBe(12);
108+
types["compatibility"]!["integer"]!.AsArray().Select(t => t!.GetValue<string>()).ShouldBe(["number", "integer", "decimal", "json"], ignoreOrder: true);
109+
types["compatibility"]!["file"]!.AsArray().Select(t => t!.GetValue<string>()).ShouldBe(["file"]);
110+
}
111+
}

0 commit comments

Comments
 (0)