Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CommBank-Server/Models/Goal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -27,4 +29,4 @@ public class Goal

[BsonRepresentation(BsonType.ObjectId)]
public string? UserId { get; set; }
}
}
89 changes: 88 additions & 1 deletion CommBank.Tests/GoalControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using CommBank.Models;
using CommBank.Tests.Fake;
using Microsoft.AspNetCore.Mvc;
using Xunit;

namespace CommBank.Tests;

Expand Down Expand Up @@ -63,12 +64,98 @@ public async void Get()
}

[Fact]
public async void GetForUser()
public async void GetForUser_ReturnsGoalsWithIcons()
{
// Arrange
var goals = collections.GetGoals();
var users = collections.GetUsers();

// Add icons to test data
goals[0].Icon = "🎯";
goals[1].Icon = "💰";
goals[2].Icon = null; // Test null icon

var user = users[0];

IGoalsService goalsService = new FakeGoalsService(goals, goals[0]);
IUsersService usersService = new FakeUsersService(users, user);

GoalController controller = new(goalsService, usersService);

var httpContext = new Microsoft.AspNetCore.Http.DefaultHttpContext();
controller.ControllerContext.HttpContext = httpContext;

// Act
var result = await controller.GetForUser(user.Id!);

// Assert
var okResult = Assert.IsType<OkObjectResult>(result);
var returnedGoals = Assert.IsAssignableFrom<IEnumerable<Goal>>(okResult.Value);

Assert.NotNull(returnedGoals);
Assert.NotEmpty(returnedGoals);

foreach (var goal in returnedGoals)
{
Assert.Equal(user.Id, goal.UserId);

Assert.True(goal.Icon == null || !string.IsNullOrEmpty(goal.Icon));
}


var goalList = returnedGoals.ToList();
Assert.Contains(goalList, g => g.Icon == "🎯");
Assert.Contains(goalList, g => g.Icon == "💰");
Assert.Contains(goalList, g => g.Icon == null);
}

[Fact]
public async void GetForUser_UserWithNoGoals_ReturnsEmptyList()
{
// Arrange
var goals = new List<Goal>();
var users = collections.GetUsers();

var user = users[1];

IGoalsService goalsService = new FakeGoalsService(goals, null);
IUsersService usersService = new FakeUsersService(users, user);

GoalController controller = new(goalsService, usersService);

var httpContext = new Microsoft.AspNetCore.Http.DefaultHttpContext();
controller.ControllerContext.HttpContext = httpContext;

// Act
var result = await controller.GetForUser(user.Id!);

// Assert
var okResult = Assert.IsType<OkObjectResult>(result);
var returnedGoals = Assert.IsAssignableFrom<IEnumerable<Goal>>(okResult.Value);

Assert.NotNull(returnedGoals);
Assert.Empty(returnedGoals);
}

[Fact]
public async void GetForUser_InvalidUserId_ReturnsNotFound()
{
// Arrange
var goals = collections.GetGoals();
var users = collections.GetUsers();

IGoalsService goalsService = new FakeGoalsService(goals, goals[0]);
IUsersService usersService = new FakeUsersService(users, null); // User not found

GoalController controller = new(goalsService, usersService);

var httpContext = new Microsoft.AspNetCore.Http.DefaultHttpContext();
controller.ControllerContext.HttpContext = httpContext;

// Act
var result = await controller.GetForUser("invalid-user-id");

// Assert
Assert.IsType<NotFoundResult>(result);
}
}