|
| 1 | +using NpgsqlRestTests.Setup; |
| 2 | + |
| 3 | +namespace NpgsqlRestTests.SqlFileSourceTests; |
| 4 | + |
| 5 | +public static partial class SqlFiles |
| 6 | +{ |
| 7 | + public static void VirtualParamTests() |
| 8 | + { |
| 9 | + // Virtual param used for claim mapping — not in SQL, auto-filled from claims |
| 10 | + File.WriteAllText(Path.Combine(Dir, "virtual_claim.sql"), """ |
| 11 | + -- @authorize |
| 12 | + -- @user_parameters |
| 13 | + -- @define_param _user_id |
| 14 | + select 'ok' as result; |
| 15 | + """); |
| 16 | + |
| 17 | + // Virtual param with type specified |
| 18 | + File.WriteAllText(Path.Combine(Dir, "virtual_typed.sql"), """ |
| 19 | + -- @define_param extra_info text |
| 20 | + -- @param $1 id |
| 21 | + select id, name from sql_describe_test where id = $1; |
| 22 | + """); |
| 23 | + |
| 24 | + // Multiple virtual params |
| 25 | + File.WriteAllText(Path.Combine(Dir, "virtual_multiple.sql"), """ |
| 26 | + -- @define_param tracking_id text |
| 27 | + -- @define_param source text |
| 28 | + -- @param $1 id |
| 29 | + select id, name from sql_describe_test where id = $1; |
| 30 | + """); |
| 31 | + |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +[Collection("SqlFileSourceFixture")] |
| 36 | +public class VirtualParamTests(SqlFileSourceTestFixture test) |
| 37 | +{ |
| 38 | + [Fact] |
| 39 | + public async Task VirtualClaimParam_EndpointWorks_WithoutPassingParam() |
| 40 | + { |
| 41 | + // _user_id is virtual — not in SQL, but claim mapping fills it |
| 42 | + using var client = test.CreateClient(); |
| 43 | + await client.GetAsync("/login"); |
| 44 | + using var response = await client.GetAsync("/api/virtual-claim"); |
| 45 | + response.StatusCode.Should().Be(HttpStatusCode.OK, |
| 46 | + $"Virtual claim param should not block endpoint. Response: {await response.Content.ReadAsStringAsync()}"); |
| 47 | + } |
| 48 | + |
| 49 | + [Fact] |
| 50 | + public async Task VirtualTypedParam_EndpointWorks_WithRealParam() |
| 51 | + { |
| 52 | + // Real $1 param works, virtual extra_info is ignored in SQL |
| 53 | + using var response = await test.Client.GetAsync("/api/virtual-typed?id=1&extra_info=test"); |
| 54 | + response.StatusCode.Should().Be(HttpStatusCode.OK); |
| 55 | + var content = await response.Content.ReadAsStringAsync(); |
| 56 | + content.Should().Contain("test1"); |
| 57 | + } |
| 58 | + |
| 59 | + [Fact] |
| 60 | + public async Task VirtualMultipleParams_EndpointWorks() |
| 61 | + { |
| 62 | + using var response = await test.Client.GetAsync("/api/virtual-multiple?id=1&tracking_id=abc&source=web"); |
| 63 | + response.StatusCode.Should().Be(HttpStatusCode.OK); |
| 64 | + var content = await response.Content.ReadAsStringAsync(); |
| 65 | + content.Should().Contain("test1"); |
| 66 | + } |
| 67 | + |
| 68 | +} |
0 commit comments