-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathProgram.cs
More file actions
385 lines (354 loc) · 15.8 KB
/
Copy pathProgram.cs
File metadata and controls
385 lines (354 loc) · 15.8 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
using System.Security.Claims;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.RateLimiting;
using Microsoft.Extensions.DependencyInjection;
using NpgsqlRest.Auth;
using NpgsqlRest.CrudSource;
using NpgsqlRest.TsClient;
using NpgsqlRest.HttpFiles;
using NpgsqlRest.OpenAPI;
using NpgsqlRest.UploadHandlers;
using NpgsqlRest.TableFormatHandlers;
using NpgsqlRestClient;
namespace NpgsqlRestTests.Setup;
public class Program
{
/// <summary>
/// Data protector for encrypt/decrypt tests
/// </summary>
public static IDataProtector? DataProtector { get; private set; }
/// <summary>
/// Output path for TsClient generated files (used by tests)
/// </summary>
public static string TsClientOutputPath { get; } = Path.Combine(Path.GetTempPath(), "NpgsqlRestTests", "TsClient");
/// <summary>
/// Output path for TsClient generated JavaScript files with SkipTypes=true (used by tests)
/// </summary>
public static string TsClientJsOutputPath { get; } = Path.Combine(Path.GetTempPath(), "NpgsqlRestTests", "TsClientJs");
/// <summary>
/// Output path for HttpFiles generated files (used by tests)
/// </summary>
public static string HttpFilesOutputPath { get; } = Path.Combine(Path.GetTempPath(), "NpgsqlRestTests", "HttpFiles");
/// <summary>
/// Output path for OpenApi generated files (used by tests)
/// </summary>
public static string OpenApiOutputPath { get; } = Path.Combine(Path.GetTempPath(), "NpgsqlRestTests", "OpenApi");
static async Task ValidateAsync(ParameterValidationValues p)
{
if (p.Routine.Name == "case_jsonpath_param" && p.Parameter.Value?.ToString() == "XXX")
{
p.Context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
await p.Context.Response.WriteAsync($"Parameter {p.Parameter.ActualName} is not valid.");
}
if (string.Equals(p.Parameter.ParameterName, "_user_id", StringComparison.Ordinal))
{
if (p.Context.User.Identity?.IsAuthenticated is true)
{
p.Parameter.Value = p.Context.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value;
}
else
{
p.Parameter.Value = DBNull.Value;
}
}
if (string.Equals(p.Parameter.ParameterName, "_user_roles", StringComparison.Ordinal))
{
if (p.Context.User.Identity?.IsAuthenticated is true)
{
p.Parameter.Value = p.Context.User.Claims.Where(c => c.Type == ClaimTypes.Role).Select(c => c.Value).ToArray();
}
else
{
p.Parameter.Value = DBNull.Value;
}
}
}
public static void Main()
{
var uploadHandlerOptions = new UploadHandlerOptions();
var files = Directory.GetFiles(uploadHandlerOptions.FileSystemPath, "*.csv");
foreach (var file in files)
{
try
{
File.Delete(file);
}
catch (Exception ex)
{
Console.WriteLine($"Error deleting file {file}: {ex.Message}");
}
}
var connectionString = Database.Create();
// disable SQL rewriting to ensure that NpgsqlRest works with this option on.
AppContext.SetSwitch("Npgsql.EnableSqlRewriting", false);
var builder = WebApplication.CreateBuilder([]);
builder.Services.AddRateLimiter(options =>
{
options.RejectionStatusCode = StatusCodes.Status429TooManyRequests;
options.OnRejected = async (context, cancellationToken) =>
{
await context.HttpContext.Response.WriteAsync("Rate limit exceeded. Please try again later.", cancellationToken);
};
options.AddFixedWindowLimiter("max 2 per second", config =>
{
config.PermitLimit = 2;
config.Window = TimeSpan.FromSeconds(1);
config.AutoReplenishment = true;
});
});
builder
.Services
.AddDataProtection()
.SetApplicationName("npgsqlrest-tests");
builder
.Services
.AddAuthentication()
//.AddBearerToken();
.AddCookie();
builder.Services.AddProblemDetails(options =>
{
options.CustomizeProblemDetails = ctx =>
{
// Remove the type field completely
ctx.ProblemDetails.Type = null;
// Remove the traceId extension
ctx.ProblemDetails.Extensions.Remove("traceId");
};
});
var app = builder.Build();
app.UseRateLimiter();
var dataProtectionProvider = app.Services.GetRequiredService<IDataProtectionProvider>();
var dataProtector = dataProtectionProvider.CreateProtector("npgsqlrest-tests");
DataProtector = dataProtector;
var authOptions = new NpgsqlRestAuthenticationOptions
{
DefaultUserIdClaimType = "name_identifier",
DefaultNameClaimType = "name",
DefaultRoleClaimType = "role",
ContextKeyClaimsMapping = new()
{
{ "request.user_id", "name_identifier" },
{ "request.user_name", "name" },
{ "request.user_roles" , "role" },
},
ParameterNameClaimsMapping = new()
{
{ "_user_id", "name_identifier" },
{ "_user_name", "name" },
{ "_user_roles", "role" }
},
};
app.MapGet("/login", () => Results.SignIn(new ClaimsPrincipal(new ClaimsIdentity(
claims: new[]
{
new Claim(authOptions.DefaultUserIdClaimType, "user123"),
new Claim(authOptions.DefaultNameClaimType, "user"),
new Claim(authOptions.DefaultRoleClaimType, "role1"),
new Claim(authOptions.DefaultRoleClaimType, "role2"),
new Claim(authOptions.DefaultRoleClaimType, "role3"),
},
authenticationType: CookieAuthenticationDefaults.AuthenticationScheme))));
var errorHandlingOptions = new ErrorHandlingOptions();
errorHandlingOptions.ErrorCodePolicies.Add("test_err_policy", new Dictionary<string, ErrorCodeMappingOptions>()
{
["22012"] = new() { StatusCode = 409, Title = "Conflict - Custom Policy" }
});
var uploadOptions = new NpgsqlRestUploadOptions()
{
UseDefaultUploadMetadataParameter = true,
DefaultUploadMetadataParameterName = "_default_upload_metadata",
UseDefaultUploadMetadataContextKey = true,
};
uploadOptions.UploadHandlers = uploadOptions.CreateUploadHandlers();
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
uploadOptions.UploadHandlers?.Add("excel", strategy => new ExcelUploadHandler(uploadOptions, strategy));
app.UseNpgsqlRest(new(connectionString)
{
//NameSimilarTo = "get_conn1_connection_name_p",
//SchemaSimilarTo = "custom_param_schema",
IncludeSchemas = ["public", "custom_param_schema", "my_schema", "custom_table_param_schema", "tsclient_test", "polp_schema"],
// Exclude cp_* / cpx_* (owned by CacheProfilesTestFixture), rlpt_* (owned by
// RateLimiterPartitionTestFixture), and apt_* (owned by AuthSchemeTestFixture). All
// reference fixture-specific configuration that is not present here (cache profiles,
// rate-limiter policies named "rlpt-...", auth profiles), so picking them up in the
// default fixture would fail at startup.
NameNotSimilarTo = "(cp[_x]|rlpt[_]|ast[_]|cab[_]|cabx[_]|sset[_])%",
// TsClient configuration for testing - uses tsclient_module annotations for per-function files
EndpointCreateHandlers = [
new TsClient(new TsClientOptions
{
FilePath = Path.Combine(TsClientOutputPath, "{0}.ts"),
FileOverwrite = true,
BySchema = true, // Required for tsclient_module to generate separate files
IncludeHost = false,
CreateSeparateTypeFile = false,
CommentHeader = CommentHeader.Simple,
HeaderLines = [], // No auto-generated timestamp for consistent test assertions
// Only process tsclient_test schema to avoid issues with custom sources that have null definitions
SkipSchemas = ["public", "custom_param_schema", "my_schema", "custom_table_param_schema", ""],
IncludeStatusCode = false
}),
// TsClient configuration for SkipTypes testing - generates pure JavaScript
new TsClient(new TsClientOptions
{
FilePath = Path.Combine(TsClientJsOutputPath, "{0}.js"),
FileOverwrite = true,
BySchema = true,
IncludeHost = false,
CreateSeparateTypeFile = false,
CommentHeader = CommentHeader.Simple,
HeaderLines = [],
SkipSchemas = ["public", "custom_param_schema", "my_schema", "custom_table_param_schema", ""],
IncludeStatusCode = false,
SkipTypes = true
}),
// HttpFiles configuration for testing path parameters
new HttpFile(new HttpFileOptions
{
Option = HttpFileOption.File,
NamePattern = Path.Combine(HttpFilesOutputPath, "{0}"),
FileOverwrite = true,
CommentHeader = CommentHeader.None
}),
// OpenApi configuration for testing path parameters
new OpenApi(new OpenApiOptions
{
FileName = Path.Combine(OpenApiOutputPath, "openapi.json"),
FileOverwrite = true,
DocumentTitle = "NpgsqlRest Test API",
AddCurrentServer = false
}),
],
CommentsMode = CommentsMode.ParseAll,
ValidateParametersAsync = ValidateAsync,
ConnectionStrings = new Dictionary<string, string>()
{
{ "conn1", Database.CreateAdditional("conn1") },
{ "conn2", Database.CreateAdditional("conn2") },
{ "polp_conn", Database.CreatePolpConnection() }
},
CommandCallbackAsync = async (endpoint, command, context) =>
{
if (string.Equals(endpoint.Routine.Name , "get_csv_data"))
{
context.Response.ContentType = "text/csv";
await using var reader = await command.ExecuteReaderAsync();
while (await reader.ReadAsync())
{
var line = $"{reader[0]},{reader[1]},{reader.GetDateTime(2):s},{reader.GetBoolean(3).ToString().ToLowerInvariant()}\n";
await context.Response.WriteAsync(line);
}
}
},
EndpointSourcesCreated = sources =>
{
//sources.Clear();
sources.Add(new CrudSource());
sources.Add(new TestSource());
},
CustomRequestHeaders = new()
{
{ "custom-header1", "custom-header1-value" }
},
AuthenticationOptions = new()
{
DefaultUserIdClaimType = "name_identifier",
DefaultNameClaimType = "name",
DefaultRoleClaimType = "role",
ContextKeyClaimsMapping = new()
{
{ "request.user_id", "name_identifier" },
{ "request.user_name", "name" },
{ "request.user_roles" , "role" },
},
ParameterNameClaimsMapping = new()
{
{ "_user_id", "name_identifier" },
{ "_user_name", "name" },
{ "_user_roles", "role" }
},
PasswordVerificationFailedCommand = "call failed_login($1,$2,$3)",
PasswordVerificationSucceededCommand = "call succeeded_login($1,$2,$3)",
ClaimsJsonContextKey = "request.user_claims",
BasicAuth = new BasicAuthOptions()
{
SslRequirement = SslRequirement.Ignore
},
DefaultDataProtector = dataProtector
},
UploadOptions = uploadOptions,
ErrorHandlingOptions = errorHandlingOptions,
HttpClientOptions = new()
{
Enabled = true
},
CacheOptions = new()
{
InvalidateCacheSuffix = "invalidate"
},
ProxyOptions = new()
{
Enabled = true,
Host = "http://localhost:50954" // ProxyWireMockFixture.Port
},
ValidationOptions = new()
{
Rules = new()
{
// NotNull - checks for null/DBNull values
["not_null"] = new ValidationRule
{
Type = ValidationType.NotNull,
Message = "Parameter '{0}' cannot be null",
StatusCode = 400
},
// NotEmpty - checks for empty string (null values pass)
["not_empty"] = new ValidationRule
{
Type = ValidationType.NotEmpty,
Message = "Parameter '{0}' cannot be empty",
StatusCode = 400
},
// Required - combines NotNull and NotEmpty
["required"] = new ValidationRule
{
Type = ValidationType.Required,
Message = "Parameter '{0}' is required",
StatusCode = 400
},
["email"] = new ValidationRule
{
Type = ValidationType.Regex,
Pattern = @"^[^@\s]+@[^@\s]+\.[^@\s]+$",
Message = "Parameter '{0}' must be a valid email address",
StatusCode = 400
},
// Custom rule for testing custom messages
["product_code"] = new ValidationRule
{
Type = ValidationType.Regex,
Pattern = @"^[A-Z]{3}-\d{4}$",
Message = "Product code must be in format XXX-0000 (3 uppercase letters, dash, 4 digits)",
StatusCode = 400
},
// Rule for testing message format placeholders: {0}=original, {1}=converted, {2}=rule name
["format_test"] = new ValidationRule
{
Type = ValidationType.NotNull,
Message = "Original: {0}, Converted: {1}, Rule: {2}",
StatusCode = 400
}
}
},
TableFormatHandlers = new()
{
["html"] = new HtmlTableFormatHandler(),
["excel"] = new ExcelTableFormatHandler()
}
});
app.Run();
}
}