diff --git a/CommBank-Server/Models/Goal.cs b/CommBank-Server/Models/Goal.cs index 77ff1ad..20b9b5d 100644 --- a/CommBank-Server/Models/Goal.cs +++ b/CommBank-Server/Models/Goal.cs @@ -27,4 +27,6 @@ public class Goal [BsonRepresentation(BsonType.ObjectId)] public string? UserId { get; set; } + + public string? Icon { get; set; } } \ No newline at end of file diff --git a/CommBank-Server/Program.cs b/CommBank-Server/Program.cs index a88e560..b4f9ebd 100644 --- a/CommBank-Server/Program.cs +++ b/CommBank-Server/Program.cs @@ -1,17 +1,26 @@ using CommBank.Models; using CommBank.Services; using MongoDB.Driver; +using MongoDB.Driver.Core.Configuration; var builder = WebApplication.CreateBuilder(args); builder.Services.AddControllers(); - builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); builder.Configuration.SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("Secrets.json"); -var mongoClient = new MongoClient(builder.Configuration.GetConnectionString("CommBank")); +var connectionString = builder.Configuration.GetConnectionString("CommBank"); + +var mongoSettings = MongoClientSettings.FromConnectionString(connectionString); +mongoSettings.SslSettings = new SslSettings +{ + EnabledSslProtocols = System.Security.Authentication.SslProtocols.Tls12 +}; +mongoSettings.AllowInsecureTls = true; + +var mongoClient = new MongoClient(mongoSettings); var mongoDatabase = mongoClient.GetDatabase("CommBank"); IAccountsService accountsService = new AccountsService(mongoDatabase); @@ -44,10 +53,6 @@ } app.UseHttpsRedirection(); - app.UseAuthorization(); - app.MapControllers(); - app.Run(); - diff --git a/CommBank-Server/Secrets.json b/CommBank-Server/Secrets.json index 0e5bf94..6941a03 100644 --- a/CommBank-Server/Secrets.json +++ b/CommBank-Server/Secrets.json @@ -1,5 +1,5 @@ { "ConnectionStrings": { - "CommBank": "{CONNECTION_STRING}" + "CommBank": "mongodb://localhost:27017" } } \ No newline at end of file diff --git a/CommBank-Server/appsettings.json b/CommBank-Server/appsettings.json index af0538f..19c74bb 100644 --- a/CommBank-Server/appsettings.json +++ b/CommBank-Server/appsettings.json @@ -5,6 +5,9 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "MongoDbSettings": { + "ConnectionString": "mongodb+srv://ajaimurugan2004_db_user:uCdVJsE5ahlqL2Kb@cluster0.dx1b9sy.mongodb.net/?appName=Cluster0", + "DatabaseName": "rserver" + } } - diff --git a/CommBank.Tests/GoalControllerTests.cs b/CommBank.Tests/GoalControllerTests.cs index 8380181..66725cf 100644 --- a/CommBank.Tests/GoalControllerTests.cs +++ b/CommBank.Tests/GoalControllerTests.cs @@ -66,9 +66,25 @@ public async void Get() 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 + 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); + index++; + } } -} \ No newline at end of file +} diff --git a/CommBank.Tests/GoalControllerTests.cs.cs b/CommBank.Tests/GoalControllerTests.cs.cs new file mode 100644 index 0000000..66725cf --- /dev/null +++ b/CommBank.Tests/GoalControllerTests.cs.cs @@ -0,0 +1,90 @@ +using CommBank.Controllers; +using CommBank.Services; +using CommBank.Models; +using CommBank.Tests.Fake; +using Microsoft.AspNetCore.Mvc; + +namespace CommBank.Tests; + +public class GoalControllerTests +{ + private readonly FakeCollections collections; + + 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); + + // 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) + { + Assert.IsAssignableFrom(goal); + 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); + + // Act + var httpContext = new Microsoft.AspNetCore.Http.DefaultHttpContext(); + controller.ControllerContext.HttpContext = httpContext; + 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); + } + + [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 + 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); + index++; + } + } +}