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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Secrets.json
# globs
Makefile.in
*.userprefs
Expand Down
140 changes: 63 additions & 77 deletions CommBank-Server/Controllers/GoalController.cs
Original file line number Diff line number Diff line change
@@ -1,102 +1,88 @@
using Microsoft.AspNetCore.Mvc;
using CommBank.Services;
using Xunit;
using Moq;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;
using CommBank.Controllers;
using CommBank.Models;
using CommBank.Services;

namespace CommBank.Controllers;

[ApiController]
[Route("api/[controller]")]
public class GoalController : ControllerBase
namespace CommBank.Tests
{
private readonly IGoalsService _goalsService;
private readonly IUsersService _usersService;

public GoalController(IGoalsService goalsService, IUsersService usersService)
public class GoalControllerTests
{
_goalsService = goalsService;
_usersService = usersService;
}

[HttpGet]
public async Task<List<Goal>> Get() =>
await _goalsService.GetAsync();
private readonly Mock<IGoalsService> _mockGoalsService;
private readonly Mock<IUsersService> _mockUsersService;
private readonly GoalController _controller;

[HttpGet("{id:length(24)}")]
public async Task<ActionResult<Goal>> Get(string id)
{
var goal = await _goalsService.GetAsync(id);

if (goal is null)
public GoalControllerTests()
{
return NotFound();
}

return goal;
}

[HttpGet("User/{id:length(24)}")]
public async Task<List<Goal>?> GetForUser(string id) =>
await _goalsService.GetForUserAsync(id);
_mockGoalsService = new Mock<IGoalsService>();
_mockUsersService = new Mock<IUsersService>();

[HttpPost]
public async Task<IActionResult> Post(Goal newGoal)
{
await _goalsService.CreateAsync(newGoal);
_controller = new GoalController(
_mockGoalsService.Object,
_mockUsersService.Object
);
}

if (newGoal.Id is not null && newGoal.UserId is not null)
[Fact]
public async Task GetForUser_ReturnsGoals_WhenUserExists()
{
var user = await _usersService.GetAsync(newGoal.UserId);
// Arrange
var userId = "507f1f77bcf86cd799439011";

if (user is not null && user.Id is not null)
var goals = new List<Goal>
{
if (user.GoalIds is not null)
{
user.GoalIds.Add(newGoal.Id);
}
else
{
user.GoalIds = new()
{
newGoal.Id
};
}

await _usersService.UpdateAsync(user.Id, user);
}
}
new Goal { Id = "1", UserId = userId },
new Goal { Id = "2", UserId = userId }
};

return CreatedAtAction(nameof(Get), new { id = newGoal.Id }, newGoal);
}
_mockGoalsService
.Setup(s => s.GetForUserAsync(userId))
.ReturnsAsync(goals);

[HttpPut("{id:length(24)}")]
public async Task<IActionResult> Update(string id, Goal updatedGoal)
{
var goal = await _goalsService.GetAsync(id);
// Act
var result = await _controller.GetForUser(userId);

if (goal is null)
{
return NotFound();
// Assert
Assert.NotNull(result);
Assert.Equal(2, result.Count);
}

updatedGoal.Id = goal.Id;
[Fact]
public async Task GetForUser_ReturnsEmptyList_WhenNoGoals()
{
// Arrange
var userId = "507f1f77bcf86cd799439011";

await _goalsService.UpdateAsync(id, updatedGoal);
_mockGoalsService
.Setup(s => s.GetForUserAsync(userId))
.ReturnsAsync(new List<Goal>());

return NoContent();
}
// Act
var result = await _controller.GetForUser(userId);

[HttpDelete("{id:length(24)}")]
public async Task<IActionResult> Delete(string id)
{
var goal = await _goalsService.GetAsync(id);
// Assert
Assert.NotNull(result);
Assert.Empty(result);
}

if (goal is null)
[Fact]
public async Task GetForUser_CallsServiceOnce()
{
return NotFound();
}
// Arrange
var userId = "507f1f77bcf86cd799439011";

_mockGoalsService
.Setup(s => s.GetForUserAsync(userId))
.ReturnsAsync(new List<Goal>());

await _goalsService.RemoveAsync(id);
// Act
await _controller.GetForUser(userId);

return NoContent();
// Assert
_mockGoalsService.Verify(s => s.GetForUserAsync(userId), Times.Once);
}
}
}
89 changes: 89 additions & 0 deletions CommBank-Server/Controllers/GoalControllerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using Xunit;
using Moq;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;

using CommBank.Controllers;
using CommBank.Models;
using CommBank.Services;

namespace CommBank.Tests
{
public class GoalControllerTests
{
private readonly Mock<IGoalsService> _mockGoalsService;
private readonly Mock<IUsersService> _mockUsersService;
private readonly GoalController _controller;

public GoalControllerTests()
{
_mockGoalsService = new Mock<IGoalsService>();
_mockUsersService = new Mock<IUsersService>();

_controller = new GoalController(
_mockGoalsService.Object,
_mockUsersService.Object
);
}

[Fact]
public async Task GetForUser_ReturnsGoals_WhenDataExists()
{
// Arrange
var userId = "507f1f77bcf86cd799439011";

var goals = new List<Goal>
{
new Goal { Id = "1", UserId = userId },
new Goal { Id = "2", UserId = userId }
};

_mockGoalsService
.Setup(s => s.GetForUserAsync(userId))
.ReturnsAsync(goals);

// Act
var result = await _controller.GetForUser(userId);

// Assert
Assert.NotNull(result);
Assert.Equal(2, result.Count);
}

[Fact]
public async Task GetForUser_ReturnsEmptyList_WhenNoGoals()
{
// Arrange
var userId = "507f1f77bcf86cd799439011";

_mockGoalsService
.Setup(s => s.GetForUserAsync(userId))
.ReturnsAsync(new List<Goal>());

// Act
var result = await _controller.GetForUser(userId);

// Assert
Assert.NotNull(result);
Assert.Empty(result);
}

[Fact]
public async Task GetForUser_ServiceCalledOnce()
{
// Arrange
var userId = "507f1f77bcf86cd799439011";

_mockGoalsService
.Setup(s => s.GetForUserAsync(userId))
.ReturnsAsync(new List<Goal>());

// Act
await _controller.GetForUser(userId);

// Assert
_mockGoalsService.Verify(s => s.GetForUserAsync(userId), Times.Once);
}
}
}
3 changes: 3 additions & 0 deletions CommBank-Server/Models/Goal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ public class Goal

[BsonRepresentation(BsonType.ObjectId)]
public string? UserId { get; set; }

// New field added for task
public string? Icon { get; set; }
}
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://priya:priya%402718@namastenode.7yndxoo.mongodb.net/CommBank?retryWrites=true&w=majority"
}
}