|
| 1 | +namespace NpgsqlRestTests |
| 2 | +{ |
| 3 | + public static partial class Database |
| 4 | + { |
| 5 | + public static void SseScopeTests() |
| 6 | + { |
| 7 | + script.Append(""" |
| 8 | +
|
| 9 | + -- Anonymous-subscribable event stream; per-event filtering comes from publish-side hints. |
| 10 | + create function ssecope_events() |
| 11 | + returns void |
| 12 | + language plpgsql immutable |
| 13 | + as $$ begin perform 1; end $$; |
| 14 | + comment on function ssecope_events() is ' |
| 15 | + HTTP GET |
| 16 | + sse_subscribe |
| 17 | + '; |
| 18 | +
|
| 19 | + -- Publisher: emits the message with an optional scope hint (mirrors the documented |
| 20 | + -- RAISE ... USING HINT = ''authorize <role>'' production pattern). |
| 21 | + create procedure ssecope_publish(_msg text, _hint text) |
| 22 | + language plpgsql |
| 23 | + as $$ |
| 24 | + begin |
| 25 | + if _hint is null then |
| 26 | + raise info '%', _msg; |
| 27 | + else |
| 28 | + raise info '%', _msg using hint = _hint; |
| 29 | + end if; |
| 30 | + end $$; |
| 31 | + comment on procedure ssecope_publish(text, text) is ' |
| 32 | + HTTP POST |
| 33 | + sse_publish |
| 34 | + '; |
| 35 | +
|
| 36 | + create function ssecope_login_a() |
| 37 | + returns table (name_identifier int, name text, role text[]) |
| 38 | + language sql as $$ |
| 39 | + select 9001, 'scope_user_a', array['role_a'] |
| 40 | + $$; |
| 41 | + comment on function ssecope_login_a() is 'login'; |
| 42 | +
|
| 43 | + create function ssecope_login_b() |
| 44 | + returns table (name_identifier int, name text, role text[]) |
| 45 | + language sql as $$ |
| 46 | + select 9002, 'scope_user_b', array['role_b'] |
| 47 | + $$; |
| 48 | + comment on function ssecope_login_b() is 'login'; |
| 49 | + """); |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +namespace NpgsqlRestTests.SseTests |
| 55 | +{ |
| 56 | + using NpgsqlRestTests.Setup; |
| 57 | + |
| 58 | + [Collection("TestFixture")] |
| 59 | + public class SseScopeTests(TestFixture test) |
| 60 | + { |
| 61 | + private const string SubscribeUrl = "/api/ssecope-events/info"; |
| 62 | + private const string PublishUrl = "/api/ssecope-publish"; |
| 63 | + |
| 64 | + private static StringContent Publish(string msg, string? hint) => |
| 65 | + new(hint is null |
| 66 | + ? $"{{\"msg\":\"{msg}\",\"hint\":null}}" |
| 67 | + : $"{{\"msg\":\"{msg}\",\"hint\":\"{hint}\"}}", |
| 68 | + Encoding.UTF8, "application/json"); |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// Per-event scoping via RAISE ... USING HINT: 'authorize role_a' must deliver ONLY to |
| 72 | + /// subscribers whose claims include role_a; bare 'authorize' must deliver only to |
| 73 | + /// authenticated subscribers. Non-delivery is proven by ordering, not by waiting: each |
| 74 | + /// excluded subscriber's FIRST received event must be the later, broader one. |
| 75 | + /// </summary> |
| 76 | + [Fact] |
| 77 | + public async Task Hint_Scoped_Events_Are_Delivered_Only_To_Matching_Subscribers() |
| 78 | + { |
| 79 | + // Three subscribers: A (role_a), B (role_b), Anon (not authenticated). |
| 80 | + using var clientA = test.Application.CreateClient(); |
| 81 | + using var loginA = await clientA.PostAsync("/api/ssecope-login-a/", null); |
| 82 | + loginA.StatusCode.Should().Be(HttpStatusCode.OK); |
| 83 | + |
| 84 | + using var clientB = test.Application.CreateClient(); |
| 85 | + using var loginB = await clientB.PostAsync("/api/ssecope-login-b/", null); |
| 86 | + loginB.StatusCode.Should().Be(HttpStatusCode.OK); |
| 87 | + |
| 88 | + using var clientAnon = test.Application.CreateClient(); |
| 89 | + |
| 90 | + await using var sseA = await SseTestClient.OpenAsync(clientA, SubscribeUrl); |
| 91 | + await sseA.WaitForRegisteredAsync(); |
| 92 | + await using var sseB = await SseTestClient.OpenAsync(clientB, SubscribeUrl); |
| 93 | + await sseB.WaitForRegisteredAsync(); |
| 94 | + await using var sseAnon = await SseTestClient.OpenAsync(clientAnon, SubscribeUrl); |
| 95 | + await sseAnon.WaitForRegisteredAsync(); |
| 96 | + |
| 97 | + // 1. Scoped to role_a only. |
| 98 | + using (var p = await clientA.PostAsync(PublishUrl, Publish("only-role-a", "authorize role_a"))) |
| 99 | + p.StatusCode.Should().BeOneOf(HttpStatusCode.OK, HttpStatusCode.NoContent); |
| 100 | + |
| 101 | + // 2. Scoped to any authenticated subscriber. |
| 102 | + using (var p = await clientA.PostAsync(PublishUrl, Publish("any-authenticated", "authorize"))) |
| 103 | + p.StatusCode.Should().BeOneOf(HttpStatusCode.OK, HttpStatusCode.NoContent); |
| 104 | + |
| 105 | + // 3. Unscoped terminator: delivered to everyone (subscribe endpoint is anonymous, default scope). |
| 106 | + using (var p = await clientA.PostAsync(PublishUrl, Publish("everyone", null))) |
| 107 | + p.StatusCode.Should().BeOneOf(HttpStatusCode.OK, HttpStatusCode.NoContent); |
| 108 | + |
| 109 | + // A (role_a) receives all three, in order. |
| 110 | + (await sseA.ReadDataLineAsync(TimeSpan.FromSeconds(10))).Should().Be("only-role-a"); |
| 111 | + (await sseA.ReadDataLineAsync(TimeSpan.FromSeconds(10))).Should().Be("any-authenticated"); |
| 112 | + (await sseA.ReadDataLineAsync(TimeSpan.FromSeconds(10))).Should().Be("everyone"); |
| 113 | + |
| 114 | + // B (role_b) must NOT get the role_a event: its first event is the authenticated one. |
| 115 | + (await sseB.ReadDataLineAsync(TimeSpan.FromSeconds(10))).Should().Be("any-authenticated", |
| 116 | + "an event hinted 'authorize role_a' must not be delivered to a subscriber without role_a"); |
| 117 | + (await sseB.ReadDataLineAsync(TimeSpan.FromSeconds(10))).Should().Be("everyone"); |
| 118 | + |
| 119 | + // Anonymous must get NEITHER scoped event: its first event is the unscoped one. |
| 120 | + (await sseAnon.ReadDataLineAsync(TimeSpan.FromSeconds(10))).Should().Be("everyone", |
| 121 | + "events hinted 'authorize …' must never be delivered to unauthenticated subscribers"); |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments