From 9474fb7811b9bba9cb4455f483f54122d9c5a167 Mon Sep 17 00:00:00 2001 From: Aaron Po Date: Sat, 31 Jan 2026 13:54:02 -0500 Subject: [PATCH] add tests for login behaviour --- .../API.Core/Properties/launchSettings.json | 23 --------------- src/Core/API/API.Specs/Features/Auth.feature | 15 +++++----- src/Core/API/API.Specs/Steps/ApiSteps.cs | 28 +++++++++++++++---- 3 files changed, 30 insertions(+), 36 deletions(-) delete mode 100644 src/Core/API/API.Core/Properties/launchSettings.json diff --git a/src/Core/API/API.Core/Properties/launchSettings.json b/src/Core/API/API.Core/Properties/launchSettings.json deleted file mode 100644 index 5941a5e..0000000 --- a/src/Core/API/API.Core/Properties/launchSettings.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "$schema": "https://json.schemastore.org/launchsettings.json", - "profiles": { - "http": { - "commandName": "Project", - "dotnetRunMessages": true, - "launchBrowser": false, - "applicationUrl": "http://localhost:5069", - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } - }, - "https": { - "commandName": "Project", - "dotnetRunMessages": true, - "launchBrowser": false, - "applicationUrl": "https://localhost:7002;http://localhost:5069", - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } - } - } -} diff --git a/src/Core/API/API.Specs/Features/Auth.feature b/src/Core/API/API.Specs/Features/Auth.feature index 296733b..c8d9145 100644 --- a/src/Core/API/API.Specs/Features/Auth.feature +++ b/src/Core/API/API.Specs/Features/Auth.feature @@ -1,11 +1,12 @@ Feature: User Login - As a registered user - I want to log in to my account - So that I receive an authentication token to access authenticated routes +As a registered user +I want to log in to my account +So that I receive an authentication token to access authenticated routes + Scenario: Successful login with valid credentials Given the API is running And I have an existing account - And I submit a login request with a valid username and password - Then the system successfully authenticates the user - And returns a valid access token - And the response has HTTP status 200 \ No newline at end of file + And I submit a login request with a valid username and password + Then the response has HTTP status 200 + And the response JSON should have "message" equal "Logged in successfully." + And the response JSON should have a valid access token. \ No newline at end of file diff --git a/src/Core/API/API.Specs/Steps/ApiSteps.cs b/src/Core/API/API.Specs/Steps/ApiSteps.cs index 0bb7726..760dce0 100644 --- a/src/Core/API/API.Specs/Steps/ApiSteps.cs +++ b/src/Core/API/API.Specs/Steps/ApiSteps.cs @@ -14,15 +14,13 @@ public class ApiSteps private (string username, string password) testUser; - - private - [Given("the API is running")] public void GivenTheApiIsRunning() { _client = _factory.CreateClient(); } + [Then("the response status code should be {int}")] public void ThenStatusCodeShouldBe(int expected) { @@ -62,7 +60,7 @@ public class ApiSteps } [Then("the response has HTTP status {int}")] - public void ThenTheResponseHasHttpStatus(int expectedCode) + public void ThenTheResponseHasHttpStatusInt(int expectedCode) { _response.Should().NotBeNull("No response was received from the API"); @@ -76,8 +74,26 @@ public class ApiSteps } [Given("I submit a login request with a valid username and password")] - public void GivenISubmitALoginRequestWithAValidUsernameAndPassword() + public async Task GivenISubmitALoginRequestWithAValidUsernameAndPassword() { - WhenISendAnHttpRequestToWithBody("POST", "/api/v1/account/login"); + await WhenISendAnHttpRequestToWithBody("POST", "/api/v1/account/login", $@" + {{ + ""username"": ""{testUser.username}"", + ""password"": ""{testUser.password}"" + }}"); + } + + [Then("the response JSON should have a valid access token.")] + public async Task ThenTheResponseJsonShouldHaveAValidAccessToken() + { + var dict = await _response!.Content.ReadFromJsonAsync>(); + dict.Should().NotBeNull(); + + dict!.TryGetValue("AccessToken", out var value).Should().BeTrue(); + + var messageStr = value!.ToString(); + + Console.WriteLine(messageStr); + } } \ No newline at end of file