|
| 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-<role></c> |
| 112 | + /// and pipeline <c>trilha-<role></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 | +} |
0 commit comments