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
2 changes: 1 addition & 1 deletion CommBank-Server/CommBank.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>CommBank_Server</RootNamespace>
Expand Down
2 changes: 2 additions & 0 deletions 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 Down
2 changes: 1 addition & 1 deletion CommBank-Server/Secrets.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"ConnectionStrings": {
"CommBank": "{CONNECTION_STRING}"
"CommBank": "mongodb+srv://luckybhoir2230_db_user:Lucky9345@cluster0.sgrd1ox.mongodb.net/CommBank?retryWrites=true&w=majority&appName=Cluster0"
}
}
9 changes: 6 additions & 3 deletions CommBank-Server/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}

"AllowedHosts": "*",
"ConnectionStrings": {
"CommBank": "mongodb+srv://luckybhoir2230_db_user:Lucky9345@cluster0.sgrd1ox.mongodb.net/CommBank?retryWrites=true&w=majority&appName=Cluster0"
},
"DatabaseName": "CommBank"
}
113 changes: 52 additions & 61 deletions CommBank.Tests/GoalControllerTests.cs
Original file line number Diff line number Diff line change
@@ -1,74 +1,65 @@
using CommBank.Controllers;
using CommBank.Services;
using CommBank.Models;
using CommBank.Tests.Fake;
using Xunit;
using Moq;
using Microsoft.AspNetCore.Mvc;
using CommBank.Controllers;
using CommBank.Models;
using CommBank.Services;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace CommBank.Tests;

public class GoalControllerTests
namespace CommBank.Tests
{
private readonly FakeCollections collections;

public GoalControllerTests()
public class GoalControllerTests
{
collections = new();
}

[Fact]
public async void GetAll()
{
// 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);
private readonly Mock<IGoalsService> _mockGoalsService;
private readonly GoalController _controller;

// Act
var httpContext = new Microsoft.AspNetCore.Http.DefaultHttpContext();
controller.ControllerContext.HttpContext = httpContext;
var result = await controller.Get();

// Assert
var index = 0;
foreach (Goal goal in result)
public GoalControllerTests()
{
Assert.IsAssignableFrom<Goal>(goal);
Assert.Equal(goals[index].Id, goal.Id);
Assert.Equal(goals[index].Name, goal.Name);
index++;
// We "Mock" the service so we don't need a real database to run tests
_mockGoalsService = new Mock<IGoalsService>();
_controller = new GoalController(_mockGoalsService.Object);
}
}

[Fact]
public async void Get()
{
// 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);
[Fact]
public async Task GetGoalsForUser_ReturnsOkResult_WithListOfGoals()
{
// 1. ARRANGE
var userId = "test-user-123";
var fakeGoals = new List<Goal>
{
new Goal { Id = "1", Name = "Holiday", Icon = "✈️", UserId = userId },
new Goal { Id = "2", Name = "Savings", Icon = "💰", UserId = userId }
};

// Act
var httpContext = new Microsoft.AspNetCore.Http.DefaultHttpContext();
controller.ControllerContext.HttpContext = httpContext;
var result = await controller.Get(goals[0].Id!);
_mockGoalsService.Setup(service => service.GetGoalsForUser(userId))
.ReturnsAsync(fakeGoals);

// Assert
Assert.IsAssignableFrom<Goal>(result.Value);
Assert.Equal(goals[0], result.Value);
Assert.NotEqual(goals[1], result.Value);
}
// 2. ACT
var result = await _controller.GetGoalsForUser(userId);

[Fact]
public async void GetForUser()
{
// Arrange

// Act

// Assert
// 3. ASSERT
var okResult = Assert.IsType<OkObjectResult>(result);
var returnedGoals = Assert.IsType<List<Goal>>(okResult.Value);
Assert.Equal(2, returnedGoals.Count);
Assert.Equal("✈️", returnedGoals[0].Icon); // Verifying our new Icon field!
}

[Fact]
public async Task GetGoalsForUser_ReturnsEmptyList_WhenNoGoalsExist()
{
// ARRANGE
var userId = "empty-user";
_mockGoalsService.Setup(service => service.GetGoalsForUser(userId))
.ReturnsAsync(new List<Goal>());

// ACT
var result = await _controller.GetGoalsForUser(userId);

// ASSERT
var okResult = Assert.IsType<OkObjectResult>(result);
var returnedGoals = Assert.IsType<List<Goal>>(okResult.Value);
Assert.Empty(returnedGoals);
}
}
}