diff --git a/CommBank-Server/Models/Goal.cs b/CommBank-Server/Models/Goal.cs index 77ff1ad5..81f01923 100644 --- a/CommBank-Server/Models/Goal.cs +++ b/CommBank-Server/Models/Goal.cs @@ -11,6 +11,8 @@ public class Goal public string? Name { get; set; } + public string? Icon { get; set; } + public UInt64 TargetAmount { get; set; } = 0; public DateTime TargetDate { get; set; } diff --git a/CommBank.Tests/Fake/FakeCollections.cs b/CommBank.Tests/Fake/FakeCollections.cs index 28452832..1d14e887 100644 --- a/CommBank.Tests/Fake/FakeCollections.cs +++ b/CommBank.Tests/Fake/FakeCollections.cs @@ -1,4 +1,5 @@ using CommBank.Models; +using System.Collections.Generic; namespace CommBank.Tests.Fake { @@ -10,18 +11,17 @@ public class FakeCollections List transactions; List users; - public FakeCollections() { accounts = new() { - new() + new Account { Id = "1", Name = "Tag's GoalSaver" }, - new() + new Account { Id = "2", Name = "Trot's GoalSaver" @@ -29,67 +29,56 @@ public FakeCollections() }; goals = new() - { - new() - { - Id = "1", - Name = "House Down Payment" - }, +{ + new Goal + { + Id = "1", + Name = "House Down Payment", + UserId = "1" + }, - new() - { - Id = "2", - Name = "Tesla Model Y" - }, + new Goal + { + Id = "2", + Name = "Tesla Model Y", + UserId = "1" + }, - new() - { - Id = "3", - Name = "Trip to London" - }, - }; + new Goal + { + Id = "3", + Name = "Trip to London", + UserId = "2" + } +}; tags = new() { - new() - { - Id = "1" - }, - - new() - { - Id = "2" - } + new Tag { Id = "1" }, + new Tag { Id = "2" } }; transactions = new() { - new() - { - Id = "1" - }, - - new() - { - Id = "2" - } + new Transaction { Id = "1" }, + new Transaction { Id = "2" } }; users = new() { - new() + new User { Id = "1", Name = "Tag" }, - new() + new User { Id = "2", Name = "Trot" }, - new() + new User { Id = "3", Name = "Tugg" @@ -97,29 +86,10 @@ public FakeCollections() }; } - public List GetAccounts() - { - return accounts; - } - - public List GetGoals() - { - return goals; - } - - public List GetTags() - { - return tags; - } - - public List GetTransactions() - { - return transactions; - } - - public List GetUsers() - { - return users; - } + public List GetAccounts() => accounts; + public List GetGoals() => goals; + public List GetTags() => tags; + public List GetTransactions() => transactions; + public List GetUsers() => users; } } \ No newline at end of file diff --git a/CommBank.Tests/Fake/FakeGoalsService.cs b/CommBank.Tests/Fake/FakeGoalsService.cs index 643a27e6..1cb11e9d 100644 --- a/CommBank.Tests/Fake/FakeGoalsService.cs +++ b/CommBank.Tests/Fake/FakeGoalsService.cs @@ -18,8 +18,11 @@ public FakeGoalsService(List goals, Goal goal) public async Task> GetAsync() => await Task.FromResult(_goals); - public async Task?> GetForUserAsync(string id) => - await Task.FromResult(_goals); + public async Task?> GetForUserAsync(string id) + { + var filtered = _goals.Where(g => g.UserId == id).ToList(); + return await Task.FromResult(filtered); + } public async Task GetAsync(string id) => await Task.FromResult(_goal); diff --git a/CommBank.Tests/GoalControllerTests.cs b/CommBank.Tests/GoalControllerTests.cs index 8380181f..992d2000 100644 --- a/CommBank.Tests/GoalControllerTests.cs +++ b/CommBank.Tests/GoalControllerTests.cs @@ -1,4 +1,5 @@ -using CommBank.Controllers; +using Xunit; +using CommBank.Controllers; using CommBank.Services; using CommBank.Models; using CommBank.Tests.Fake; @@ -62,13 +63,36 @@ public async void Get() Assert.NotEqual(goals[1], result.Value); } - [Fact] - public async void GetForUser() + [Fact] +public async void GetForUser() +{ + // Arrange + var goals = collections.GetGoals(); + var users = collections.GetUsers(); + + IGoalsService goalsService = new FakeGoalsService(goals, goals[0]); + IUsersService usersService = new FakeUsersService(users, users[0]); + GoalController controller = new(goalsService, usersService); + + var httpContext = new Microsoft.AspNetCore.Http.DefaultHttpContext(); + controller.ControllerContext.HttpContext = httpContext; + + var userId = users[0].Id!; + + // Act + var result = await controller.GetForUser(userId); + + // Assert + Assert.NotNull(result); + + // result is likely List? → safely handle null + var goalsForUser = result ?? new List(); + + Assert.True(goalsForUser.Count > 0); + + foreach (var goal in goalsForUser) { - // Arrange - - // Act - - // Assert + Assert.Equal(userId, goal.UserId); } +} } \ No newline at end of file