summary refs log tree commit diff
path: root/testFrontend/SafeNSound.Sdk
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2025-06-03 01:01:40 +0200
committerRory& <root@rory.gay>2025-06-03 01:01:40 +0200
commit6f3f08ed340e59a62a2d0428a5c32f99551ef1ce (patch)
treeff77390b1d3ea61414c14c94ac1fa2a05030879b /testFrontend/SafeNSound.Sdk
parentMore alarm testing (diff)
downloadnodejs-final-assignment-6f3f08ed340e59a62a2d0428a5c32f99551ef1ce.tar.xz
Fix performance issues, add fake user bot to test client, more testing
Diffstat (limited to 'testFrontend/SafeNSound.Sdk')
-rw-r--r--testFrontend/SafeNSound.Sdk/SafeNSoundAuthentication.cs17
-rw-r--r--testFrontend/SafeNSound.Sdk/SafeNSoundClient.cs38
-rw-r--r--testFrontend/SafeNSound.Sdk/WrappedHttpClient.cs13
3 files changed, 54 insertions, 14 deletions
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<SafeNSoundAuthResult> Login(AuthDto authDto) { + public async Task<AuthResult> 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<SafeNSoundAuthResult>())!; + return (await res.Content.ReadFromJsonAsync<AuthResult>())!; } 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<Dictionary<string, AlarmDto>>())!; } + + public async Task DeleteAccount(AuthDto auth) { + var res = await HttpClient.DeleteAsJsonAsync("/auth/delete", auth); + res.EnsureSuccessStatusCode(); + } + + public async Task<List<string>> GetAssignedUsers() { + var res = await HttpClient.GetAsync("/monitor/assignedUsers"); + res.EnsureSuccessStatusCode(); + return (await res.Content.ReadFromJsonAsync<List<string>>())!; + } + + 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<string> GetAllUserIdsEnumerable() { + var res = await HttpClient.GetAsync($"/admin/allUserIds"); + res.EnsureSuccessStatusCode(); + await foreach (var item in res.Content.ReadFromJsonAsAsyncEnumerable<string>()) { + 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<HttpResponseMessage> PatchAsJsonAsync<T>(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