diff --git a/CommBank-Server/CommBank.csproj b/CommBank-Server/CommBank.csproj index 983cc88..488323a 100644 --- a/CommBank-Server/CommBank.csproj +++ b/CommBank-Server/CommBank.csproj @@ -1,4 +1,4 @@ - + net6.0 @@ -6,6 +6,7 @@ enable CommBank_Server CommBank-Server + 23aef517-52c0-4041-a693-aca36ab5053e @@ -13,7 +14,7 @@ - + diff --git a/CommBank-Server/Models/Goal.cs b/CommBank-Server/Models/Goal.cs index 77ff1ad..22dc081 100644 --- a/CommBank-Server/Models/Goal.cs +++ b/CommBank-Server/Models/Goal.cs @@ -1,4 +1,4 @@ -using MongoDB.Bson; +using MongoDB.Bson; using MongoDB.Bson.Serialization.Attributes; namespace CommBank.Models; diff --git a/CommBank-Server/Program.cs b/CommBank-Server/Program.cs index a88e560..6faf6af 100644 --- a/CommBank-Server/Program.cs +++ b/CommBank-Server/Program.cs @@ -11,8 +11,17 @@ builder.Configuration.SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("Secrets.json"); -var mongoClient = new MongoClient(builder.Configuration.GetConnectionString("CommBank")); -var mongoDatabase = mongoClient.GetDatabase("CommBank"); +// Get Configuration Settings +var dbSettings = builder.Configuration.GetSection("DatabaseSettings"); +var connectionString = dbSettings.GetValue("ConnectionString"); +var databaseName = dbSettings.GetValue("DatabaseName"); + +// Create MongoDB Client and Database Objects +var mongoClient = new MongoClient(connectionString); +var mongoDatabase = mongoClient.GetDatabase(databaseName); + +// Add Services to the container. +builder.Services.AddSingleton(mongoDatabase); IAccountsService accountsService = new AccountsService(mongoDatabase); IAuthService authService = new AuthService(mongoDatabase); @@ -43,7 +52,7 @@ app.UseSwaggerUI(); } -app.UseHttpsRedirection(); +//app.UseHttpsRedirection(); app.UseAuthorization(); diff --git a/CommBank-Server/appsettings.json b/CommBank-Server/appsettings.json index af0538f..e9a2647 100644 --- a/CommBank-Server/appsettings.json +++ b/CommBank-Server/appsettings.json @@ -1,4 +1,9 @@ -{ +{ + "DatabaseSettings": { + "ConnectionString": "mongodb+srv://ronaldfarnandis0y_db_user:ouztRVfN3VBg8ngU@cluster0.janb71m.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0", + "DatabaseName": "test", + "CollectionName": "Goals" + }, "Logging": { "LogLevel": { "Default": "Information", @@ -6,5 +11,4 @@ } }, "AllowedHosts": "*" -} - +} \ No newline at end of file diff --git a/CommBank.Tests/GoalControllerTests.cs b/CommBank.Tests/GoalControllerTests.cs index 8380181..3027ced 100644 --- a/CommBank.Tests/GoalControllerTests.cs +++ b/CommBank.Tests/GoalControllerTests.cs @@ -66,9 +66,28 @@ 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); + + // Define which user we are testing for + var targetUser = users[0]; + // Find all the goals that belong to our target user from the source data + var expectedGoals = goals.Where(g => g.UserId == targetUser.Id).ToList(); + // Act - + // Call the controller method to get goals for our target user + var result = await controller.GetForUser(targetUser.Id!); + // Assert + // Convert the result to a list for easier assertions + var actualGoals = result.ToList(); + + // Check if the number of returned goals is what we expected + Assert.Equal(expectedGoals.Count, actualGoals.Count); + // Check that every goal in the returned list actually belongs to the target user + Assert.All(actualGoals, goal => Assert.Equal(targetUser.Id, goal.UserId)); } } \ No newline at end of file