add tests for login behaviour

This commit is contained in:
Aaron Po
2026-01-31 13:54:02 -05:00
parent 77bb1f6733
commit 9474fb7811
3 changed files with 30 additions and 36 deletions

View File

@@ -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"
}
}
}
}

View File

@@ -1,11 +1,12 @@
Feature: User Login Feature: User Login
As a registered user As a registered user
I want to log in to my account I want to log in to my account
So that I receive an authentication token to access authenticated routes So that I receive an authentication token to access authenticated routes
Scenario: Successful login with valid credentials Scenario: Successful login with valid credentials
Given the API is running Given the API is running
And I have an existing account And I have an existing account
And I submit a login request with a valid username and password And I submit a login request with a valid username and password
Then the system successfully authenticates the user Then the response has HTTP status 200
And returns a valid access token And the response JSON should have "message" equal "Logged in successfully."
And the response has HTTP status 200 And the response JSON should have a valid access token.

View File

@@ -14,15 +14,13 @@ public class ApiSteps
private (string username, string password) testUser; private (string username, string password) testUser;
private
[Given("the API is running")] [Given("the API is running")]
public void GivenTheApiIsRunning() public void GivenTheApiIsRunning()
{ {
_client = _factory.CreateClient(); _client = _factory.CreateClient();
} }
[Then("the response status code should be {int}")] [Then("the response status code should be {int}")]
public void ThenStatusCodeShouldBe(int expected) public void ThenStatusCodeShouldBe(int expected)
{ {
@@ -62,7 +60,7 @@ public class ApiSteps
} }
[Then("the response has HTTP status {int}")] [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"); _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")] [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<Dictionary<string, object>>();
dict.Should().NotBeNull();
dict!.TryGetValue("AccessToken", out var value).Should().BeTrue();
var messageStr = value!.ToString();
Console.WriteLine(messageStr);
} }
} }