From 6f3f08ed340e59a62a2d0428a5c32f99551ef1ce Mon Sep 17 00:00:00 2001 From: Rory& Date: Tue, 3 Jun 2025 01:01:40 +0200 Subject: Fix performance issues, add fake user bot to test client, more testing --- .../SafeNSound.Sdk/SafeNSoundAuthentication.cs | 17 +++------- testFrontend/SafeNSound.Sdk/SafeNSoundClient.cs | 38 ++++++++++++++++++++++ testFrontend/SafeNSound.Sdk/WrappedHttpClient.cs | 13 +++++++- 3 files changed, 54 insertions(+), 14 deletions(-) (limited to 'testFrontend/SafeNSound.Sdk') diff --git a/testFrontend/SafeNSound.Sdk/SafeNSoundAuthentication.cs b/testFrontend/SafeNSound.Sdk/SafeNSoundAuthentication.cs index 333db6d..d0ed245 100644 --- a/testFrontend/SafeNSound.Sdk/SafeNSoundAuthentication.cs +++ b/testFrontend/SafeNSound.Sdk/SafeNSoundAuthentication.cs @@ -13,13 +13,13 @@ public class SafeNSoundAuthentication(SafeNSoundConfiguration config) { res.EnsureSuccessStatusCode(); } - public async Task Login(AuthDto authDto) { + public async Task Login(AuthDto authDto) { var hc = new WrappedHttpClient() { BaseAddress = new Uri(config.BaseUri) }; var res = await hc.PostAsJsonAsync("/auth/login", authDto); - return (await res.Content.ReadFromJsonAsync())!; + return (await res.Content.ReadFromJsonAsync())!; } public async Task Delete(AuthDto authDto) { @@ -32,16 +32,7 @@ public class SafeNSoundAuthentication(SafeNSoundConfiguration config) { } } -public class RegisterDto { - [JsonPropertyName("username")] - public string Username { get; set; } = string.Empty; - - [JsonPropertyName("password")] - public string Password { get; set; } = string.Empty; - - [JsonPropertyName("email")] - public string Email { get; set; } = string.Empty; - +public class RegisterDto : AuthDto { [JsonPropertyName("type")] public string UserType { get; set; } = string.Empty; } @@ -68,7 +59,7 @@ public class WhoAmI { public required string UserType { get; set; } } -public class SafeNSoundAuthResult : WhoAmI { +public class AuthResult : WhoAmI { [JsonPropertyName("accessToken")] public required string AccessToken { get; set; } } \ No newline at end of file diff --git a/testFrontend/SafeNSound.Sdk/SafeNSoundClient.cs b/testFrontend/SafeNSound.Sdk/SafeNSoundClient.cs index c6f16f2..8291178 100644 --- a/testFrontend/SafeNSound.Sdk/SafeNSoundClient.cs +++ b/testFrontend/SafeNSound.Sdk/SafeNSoundClient.cs @@ -1,4 +1,5 @@ using System.Net.Http.Json; +using System.Text.Json.Nodes; using System.Text.Json.Serialization; namespace SafeNSound.Sdk; @@ -54,10 +55,47 @@ public class SafeNSoundClient(SafeNSoundConfiguration config, string accessToken res.EnsureSuccessStatusCode(); return (await res.Content.ReadFromJsonAsync>())!; } + + public async Task DeleteAccount(AuthDto auth) { + var res = await HttpClient.DeleteAsJsonAsync("/auth/delete", auth); + res.EnsureSuccessStatusCode(); + } + + public async Task> GetAssignedUsers() { + var res = await HttpClient.GetAsync("/monitor/assignedUsers"); + res.EnsureSuccessStatusCode(); + return (await res.Content.ReadFromJsonAsync>())!; + } + + public async Task AddAssignedUser(string targetUserId) { + var res = await HttpClient.PatchAsJsonAsync("/monitor/assignedUsers", new { userId = targetUserId }); + res.EnsureSuccessStatusCode(); + } + + public async Task RemoveAssignedUser(string targetUserId) { + var res = await HttpClient.DeleteAsJsonAsync($"/monitor/assignedUsers", new { userId = targetUserId }); + res.EnsureSuccessStatusCode(); + } + + public async IAsyncEnumerable GetAllUserIdsEnumerable() { + var res = await HttpClient.GetAsync($"/admin/allUserIds"); + res.EnsureSuccessStatusCode(); + await foreach (var item in res.Content.ReadFromJsonAsAsyncEnumerable()) { + yield return item!; + } + } + + public async Task MonitorAllUsers() { + var res = await HttpClient.PostAsync("/admin/monitorAllUsers", null); + res.EnsureSuccessStatusCode(); + } } public class AlarmDto { [JsonPropertyName("reason")] public required string Reason { get; set; } + + [JsonPropertyName("createdAt")] + public DateTime CreatedAt { get; set; } } \ No newline at end of file diff --git a/testFrontend/SafeNSound.Sdk/WrappedHttpClient.cs b/testFrontend/SafeNSound.Sdk/WrappedHttpClient.cs index e4b4500..aa785cd 100644 --- a/testFrontend/SafeNSound.Sdk/WrappedHttpClient.cs +++ b/testFrontend/SafeNSound.Sdk/WrappedHttpClient.cs @@ -157,12 +157,15 @@ public class WrappedHttpClient "Access-Control-Allow-Methods", "Access-Control-Allow-Headers", "Access-Control-Expose-Headers", + "Access-Control-Allow-Credentials", "Cache-Control", "Cross-Origin-Resource-Policy", "X-Content-Security-Policy", "Referrer-Policy", "X-Robots-Tag", - "Content-Security-Policy" + "Content-Security-Policy", + "Keep-Alive", + "ETag" ])); return responseMessage; @@ -338,4 +341,12 @@ public class WrappedHttpClient }; return await SendAsync(request); } + + public async Task PatchAsJsonAsync(string url, T payload) { + var request = new HttpRequestMessage(HttpMethod.Patch, url) + { + Content = new StringContent(JsonSerializer.Serialize(payload), Encoding.UTF8, "application/json") + }; + return await SendAsync(request); + } } \ No newline at end of file -- cgit 1.5.1