From 0c378a5754daa48cace4496e9b6e62423c666487 Mon Sep 17 00:00:00 2001 From: Shaswat Shankar Date: Fri, 19 Jun 2026 20:17:37 +0530 Subject: [PATCH 1/3] Implement GetForUser goal test and remove secrets file --- .gitignore | 2 +- CommBank-Server/Secrets.json | 5 ----- CommBank.Tests/GoalControllerTests.cs | 31 +++++++++++++++++++++------ 3 files changed, 25 insertions(+), 13 deletions(-) delete mode 100644 CommBank-Server/Secrets.json diff --git a/.gitignore b/.gitignore index 67697151..885c68b3 100644 --- a/.gitignore +++ b/.gitignore @@ -403,5 +403,5 @@ ASALocalRun/ # Local History for Visual Studio .localhistory/ - +Secrets.json diff --git a/CommBank-Server/Secrets.json b/CommBank-Server/Secrets.json deleted file mode 100644 index 0e5bf949..00000000 --- a/CommBank-Server/Secrets.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "ConnectionStrings": { - "CommBank": "{CONNECTION_STRING}" - } -} \ No newline at end of file diff --git a/CommBank.Tests/GoalControllerTests.cs b/CommBank.Tests/GoalControllerTests.cs index 8380181f..b9f3b578 100644 --- a/CommBank.Tests/GoalControllerTests.cs +++ b/CommBank.Tests/GoalControllerTests.cs @@ -62,13 +62,30 @@ 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); + + // Act + var httpContext = new Microsoft.AspNetCore.Http.DefaultHttpContext(); + controller.ControllerContext.HttpContext = httpContext; + + var result = await controller.GetForUser(users[0].Id!); + + // Assert + Assert.NotNull(result); + + foreach (Goal goal in result!) { - // Arrange - - // Act - - // Assert + Assert.IsAssignableFrom(goal); } +} } \ No newline at end of file From 240d4743e2bfca4d5902cb2d06d2ffe387557ac8 Mon Sep 17 00:00:00 2001 From: Shaswat Shankar Date: Fri, 19 Jun 2026 20:41:51 +0530 Subject: [PATCH 2/3] Add Icon field to Goal model --- CommBank-Server/Models/Goal.cs | 2 ++ 1 file changed, 2 insertions(+) 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; } From 9b3e75abca1cfd5af8008dd1c5b45bf59ff4c9cf Mon Sep 17 00:00:00 2001 From: Shaswat Shankar Date: Fri, 26 Jun 2026 12:06:24 +0530 Subject: [PATCH 3/3] Cover GetGoalsForUser route --- .../Controllers/TransactionController.cs | 147 +++++++++++++++--- CommBank-Server/Models/Transaction.cs | 15 ++ CommBank.Tests/GoalControllerTests.cs | 141 +++++++++++++---- CommBank.Tests/TransactionControllerTests.cs | 121 ++++++++++++-- 4 files changed, 355 insertions(+), 69 deletions(-) diff --git a/CommBank-Server/Controllers/TransactionController.cs b/CommBank-Server/Controllers/TransactionController.cs index c8e0293a..c448ff3f 100644 --- a/CommBank-Server/Controllers/TransactionController.cs +++ b/CommBank-Server/Controllers/TransactionController.cs @@ -9,68 +9,175 @@ namespace CommBank.Controllers; public class TransactionController : ControllerBase { private readonly ITransactionsService _transactionsService; + private readonly ITagsService _tagsService; - public TransactionController(ITransactionsService transactionsService) => + + public TransactionController( + ITransactionsService transactionsService, + ITagsService tagsService) + { _transactionsService = transactionsService; + _tagsService = tagsService; + } + + [HttpGet] - public async Task> Get() => - await _transactionsService.GetAsync(); + public async Task> Get() + { + var transactions = await _transactionsService.GetAsync(); + + + foreach(var transaction in transactions) + { + transaction.Tags = new List(); + + + if(transaction.TagIds != null) + { + foreach(var tagId in transaction.TagIds) + { + var tag = await _tagsService.GetAsync(tagId); + + + if(tag != null) + { + transaction.Tags.Add(tag); + } + } + } + } + + + return transactions; + } + + + + [HttpGet("User/{id:length(24)}")] - public async Task?> GetForUser(string id) => - await _transactionsService.GetForUserAsync(id); + public async Task?> GetForUser(string id) + { + var transactions = + await _transactionsService.GetForUserAsync(id); + + + + if(transactions == null) + return null; + + + + foreach(var transaction in transactions) + { + transaction.Tags = new List(); + + + if(transaction.TagIds != null) + { + foreach(var tagId in transaction.TagIds) + { + var tag = await _tagsService.GetAsync(tagId); + + + if(tag != null) + { + transaction.Tags.Add(tag); + } + } + } + } + + + return transactions; + } + + + + [HttpGet("{id:length(24)}")] public async Task> Get(string id) { - var transaction = await _transactionsService.GetAsync(id); + var transaction = + await _transactionsService.GetAsync(id); - if (transaction is null) - { + + if(transaction == null) return NotFound(); - } + return transaction; } + + + + [HttpPost] public async Task Post(Transaction newTransaction) { await _transactionsService.CreateAsync(newTransaction); - return CreatedAtAction(nameof(Get), new { id = newTransaction.Id }, newTransaction); + return CreatedAtAction( + nameof(Get), + new { id = newTransaction.Id }, + newTransaction + ); } + + + + [HttpPut("{id:length(24)}")] - public async Task Update(string id, Transaction updatedTransaction) + public async Task Update( + string id, + Transaction updatedTransaction) { - var transaction = await _transactionsService.GetAsync(id); + var transaction = + await _transactionsService.GetAsync(id); - if (transaction is null) - { + + + if(transaction == null) return NotFound(); - } + + updatedTransaction.Id = transaction.Id; - await _transactionsService.UpdateAsync(id, updatedTransaction); + + await _transactionsService.UpdateAsync( + id, + updatedTransaction + ); + return NoContent(); } + + + + [HttpDelete("{id:length(24)}")] public async Task Delete(string id) { - var transaction = await _transactionsService.GetAsync(id); + var transaction = + await _transactionsService.GetAsync(id); - if (transaction is null) - { + + + if(transaction == null) return NotFound(); - } + + await _transactionsService.RemoveAsync(id); + return NoContent(); } } \ No newline at end of file diff --git a/CommBank-Server/Models/Transaction.cs b/CommBank-Server/Models/Transaction.cs index cd7c521b..abfacdfc 100644 --- a/CommBank-Server/Models/Transaction.cs +++ b/CommBank-Server/Models/Transaction.cs @@ -10,22 +10,37 @@ public class Transaction [BsonRepresentation(BsonType.ObjectId)] public string? Id { get; set; } + [JsonConverter(typeof(JsonStringEnumConverter))] [BsonRepresentation(BsonType.String)] public TransactionType TransactionType { get; set; } + public double Amount { get; set; } = 0.00; + public DateTime DateTime { get; set; } = DateTime.Now; + [BsonRepresentation(BsonType.ObjectId)] public string? GoalId { get; set; } + [BsonRepresentation(BsonType.ObjectId)] public string? UserId { get; set; } + [BsonRepresentation(BsonType.ObjectId)] public string[]? TagIds { get; set; } + + + // NEW FIELD + // This will be filled from backend + [BsonIgnore] + public List Tags { get; set; } = new(); + + + public string? Description { get; set; } } \ No newline at end of file diff --git a/CommBank.Tests/GoalControllerTests.cs b/CommBank.Tests/GoalControllerTests.cs index b9f3b578..09c16c07 100644 --- a/CommBank.Tests/GoalControllerTests.cs +++ b/CommBank.Tests/GoalControllerTests.cs @@ -2,7 +2,6 @@ using CommBank.Services; using CommBank.Models; using CommBank.Tests.Fake; -using Microsoft.AspNetCore.Mvc; namespace CommBank.Tests; @@ -15,77 +14,151 @@ public 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); + + IGoalsService goalsService = + new FakeGoalsService(goals, goals[0]); + + IUsersService usersService = + new FakeUsersService(users, users[0]); + + GoalController controller = + new(goalsService, usersService); + // Act - var httpContext = new Microsoft.AspNetCore.Http.DefaultHttpContext(); + 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) { Assert.IsAssignableFrom(goal); - Assert.Equal(goals[index].Id, goal.Id); - Assert.Equal(goals[index].Name, goal.Name); + + Assert.Equal( + goals[index].Id, + goal.Id + ); + + Assert.Equal( + goals[index].Name, + goal.Name + ); + index++; } } + + [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); + + IGoalsService goalsService = + new FakeGoalsService(goals, goals[0]); + + IUsersService usersService = + new FakeUsersService(users, users[0]); + + GoalController controller = + new(goalsService, usersService); + + // Act - var httpContext = new Microsoft.AspNetCore.Http.DefaultHttpContext(); + var httpContext = + new Microsoft.AspNetCore.Http.DefaultHttpContext(); + controller.ControllerContext.HttpContext = httpContext; - var result = await controller.Get(goals[0].Id!); + + + var result = + await controller.Get(goals[0].Id!); + + // Assert - Assert.IsAssignableFrom(result.Value); - Assert.Equal(goals[0], result.Value); - Assert.NotEqual(goals[1], result.Value); - } + Assert.IsAssignableFrom( + result.Value + ); - [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]); + Assert.Equal( + goals[0], + result.Value + ); + + + Assert.NotEqual( + goals[1], + result.Value + ); + } - GoalController controller = new(goalsService, usersService); - // Act - var httpContext = new Microsoft.AspNetCore.Http.DefaultHttpContext(); - controller.ControllerContext.HttpContext = httpContext; - var result = await controller.GetForUser(users[0].Id!); - // Assert - Assert.NotNull(result); - foreach (Goal goal in result!) + [Fact] + public async void GetForUser() { - Assert.IsAssignableFrom(goal); + // 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 + ); + + // Act + var httpContext = + new Microsoft.AspNetCore.Http.DefaultHttpContext(); + + controller.ControllerContext.HttpContext = + httpContext; + + var result = + await controller.GetForUser( + users[0].Id! + ); + + // Assert + Assert.NotNull(result); + + foreach (Goal goal in result!) + { + Assert.IsAssignableFrom(goal); + } } -} } \ No newline at end of file diff --git a/CommBank.Tests/TransactionControllerTests.cs b/CommBank.Tests/TransactionControllerTests.cs index 26fdec5c..100df647 100644 --- a/CommBank.Tests/TransactionControllerTests.cs +++ b/CommBank.Tests/TransactionControllerTests.cs @@ -14,45 +14,136 @@ public TransactionControllerTests() collections = new(); } + [Fact] public async void GetAll() { // Arrange var transactions = collections.GetTransactions(); - ITransactionsService service = new FakeTransactionsService(transactions, transactions[0]); - TransactionController controller = new(service); + var tags = collections.GetTags(); + + ITransactionsService transactionsService = + new FakeTransactionsService( + transactions, + transactions[0] + ); + + + ITagsService tagsService = + new FakeTagsService( + tags, + tags[0] + ); + + + TransactionController controller = + new( + transactionsService, + tagsService + ); + + // Act - var httpContext = new Microsoft.AspNetCore.Http.DefaultHttpContext(); - controller.ControllerContext.HttpContext = httpContext; - var result = await controller.Get(); + var httpContext = + new Microsoft.AspNetCore.Http.DefaultHttpContext(); + + controller.ControllerContext.HttpContext = + httpContext; + + + var result = + await controller.Get(); + + // Assert var index = 0; + + foreach (Transaction transaction in result) { - Assert.IsAssignableFrom(transaction); - Assert.Equal(transactions[index].Id, transaction.Id); + Assert.IsAssignableFrom( + transaction + ); + + + Assert.Equal( + transactions[index].Id, + transaction.Id + ); + + index++; } } + + + + [Fact] public async void Get() { // Arrange var transactions = collections.GetTransactions(); - ITransactionsService service = new FakeTransactionsService(transactions, transactions[0]); - TransactionController controller = new(service); + var tags = collections.GetTags(); + + + ITransactionsService transactionsService = + new FakeTransactionsService( + transactions, + transactions[0] + ); + + + ITagsService tagsService = + new FakeTagsService( + tags, + tags[0] + ); + + + + TransactionController controller = + new( + transactionsService, + tagsService + ); + + // Act - var httpContext = new Microsoft.AspNetCore.Http.DefaultHttpContext(); - controller.ControllerContext.HttpContext = httpContext; - var result = await controller.Get(transactions[0].Id!); + var httpContext = + new Microsoft.AspNetCore.Http.DefaultHttpContext(); + + + controller.ControllerContext.HttpContext = + httpContext; + + + var result = + await controller.Get( + transactions[0].Id! + ); + + // Assert - Assert.IsAssignableFrom(result.Value); - Assert.Equal(transactions[0], result.Value); - Assert.NotEqual(transactions[1], result.Value); + Assert.IsAssignableFrom( + result.Value + ); + + + Assert.Equal( + transactions[0], + result.Value + ); + + + Assert.NotEqual( + transactions[1], + result.Value + ); } } \ No newline at end of file