From 197f7d362be4a947b1951ed560223527f8c16449 Mon Sep 17 00:00:00 2001 From: Rory& Date: Mon, 2 Jun 2025 19:38:34 +0200 Subject: More alarm testing --- .gitignore | 5 ++-- src/api/middlewares/authMiddleware.js | 4 +++- src/api/routes/alarmRoutes.js | 7 ++---- testFrontend/SafeNSound.FakeUser/Program.cs | 2 ++ .../SafeNSound.FakeUser/SafeNSound.FakeUser.csproj | 10 ++++++++ testFrontend/SafeNSound.Frontend/Pages/Alarm.razor | 27 ++++++++++++++++++++-- testFrontend/SafeNSound.Sdk/SafeNSoundClient.cs | 8 ++++++- 7 files changed, 52 insertions(+), 11 deletions(-) create mode 100644 testFrontend/SafeNSound.FakeUser/Program.cs create mode 100644 testFrontend/SafeNSound.FakeUser/SafeNSound.FakeUser.csproj diff --git a/.gitignore b/.gitignore index b62bf52..7cf901f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules/ -result/ -lcov.info \ No newline at end of file +result +lcov.info +.env diff --git a/src/api/middlewares/authMiddleware.js b/src/api/middlewares/authMiddleware.js index d67c567..b91449f 100644 --- a/src/api/middlewares/authMiddleware.js +++ b/src/api/middlewares/authMiddleware.js @@ -36,11 +36,13 @@ export async function useAuthentication(req, res, next) { req.user = await getUserById(auth.sub); logAuth('User data:', req.user); + req.device = req.user.devices.find(device => device.id === auth.deviceId); + next(); } export async function requireAuth(req, res, next) { - if (!req.auth) { + if (!req.auth || !req.user || !req.device) { logAuth('Unauthorized request to', req.path); res.status(401).send( new SafeNSoundError({ diff --git a/src/api/routes/alarmRoutes.js b/src/api/routes/alarmRoutes.js index 939ca97..23b79c1 100644 --- a/src/api/routes/alarmRoutes.js +++ b/src/api/routes/alarmRoutes.js @@ -46,14 +46,11 @@ export const alarmListRoute = { description: 'Get a list of all alarms for monitored users', async method(req, res) { console.log(req.user.monitoredUsers); - const alarms = []; + const alarms = {}; for (const userId of req.user.monitoredUsers) { const user = await getUserById(userId); if (user.alarm) { - alarms.push({ - user: userId, - alarm: user.alarm - }); + alarms[userId] = user.alarm; } } res.send(alarms); diff --git a/testFrontend/SafeNSound.FakeUser/Program.cs b/testFrontend/SafeNSound.FakeUser/Program.cs new file mode 100644 index 0000000..3751555 --- /dev/null +++ b/testFrontend/SafeNSound.FakeUser/Program.cs @@ -0,0 +1,2 @@ +// See https://aka.ms/new-console-template for more information +Console.WriteLine("Hello, World!"); diff --git a/testFrontend/SafeNSound.FakeUser/SafeNSound.FakeUser.csproj b/testFrontend/SafeNSound.FakeUser/SafeNSound.FakeUser.csproj new file mode 100644 index 0000000..fd4bd08 --- /dev/null +++ b/testFrontend/SafeNSound.FakeUser/SafeNSound.FakeUser.csproj @@ -0,0 +1,10 @@ + + + + Exe + net9.0 + enable + enable + + + diff --git a/testFrontend/SafeNSound.Frontend/Pages/Alarm.razor b/testFrontend/SafeNSound.Frontend/Pages/Alarm.razor index 71d18f2..59b8e4c 100644 --- a/testFrontend/SafeNSound.Frontend/Pages/Alarm.razor +++ b/testFrontend/SafeNSound.Frontend/Pages/Alarm.razor @@ -1,7 +1,11 @@ @page "/Alarm"

Alarm

-Raise alarm + +Raise +Get +Delete +Get all monitored

@if (Exception != null) { @@ -25,7 +29,8 @@ @code { private Exception? Exception { get; set; } private object? Result { get; set; } - + private string UserId { get; set; } = "@me"; + protected override async Task OnInitializedAsync() { if (App.Client is null) { NavigationManager.NavigateTo("/Auth"); @@ -34,9 +39,27 @@ } private async Task RaiseAlarm() { + Result = null; await App.Client!.SetAlarm(new() { Reason = "fall" }); + StateHasChanged(); + } + + private async Task GetAlarm() { + Result = null; + Result = await App.Client!.GetAlarm(UserId); + StateHasChanged(); + } + + private async Task ClearAlarm() { + Result = null; + await App.Client!.DeleteAlarm(UserId); + StateHasChanged(); + } + + private async Task GetAllAlarms() { + Result = await App.Client.GetAllAlarms(); } } \ No newline at end of file diff --git a/testFrontend/SafeNSound.Sdk/SafeNSoundClient.cs b/testFrontend/SafeNSound.Sdk/SafeNSoundClient.cs index 444e313..c6f16f2 100644 --- a/testFrontend/SafeNSound.Sdk/SafeNSoundClient.cs +++ b/testFrontend/SafeNSound.Sdk/SafeNSoundClient.cs @@ -29,7 +29,7 @@ public class SafeNSoundClient(SafeNSoundConfiguration config, string accessToken return (await res.Content.ReadFromJsonAsync())!; } - public async Task SetAlarm(AlarmDto alarm) { + public async Task SetAlarm(AlarmDto alarm, string userId = "@me") { var res = await HttpClient.PutAsJsonAsync("/alarm/@me", alarm); res.EnsureSuccessStatusCode(); } @@ -48,6 +48,12 @@ public class SafeNSoundClient(SafeNSoundConfiguration config, string accessToken #endregion + + public async Task> GetAllAlarms() { + var res = await HttpClient.GetAsync("/alarms"); + res.EnsureSuccessStatusCode(); + return (await res.Content.ReadFromJsonAsync>())!; + } } -- cgit 1.5.1