Skip to content

Implement user-specific goal filtering and add unit tests#148

Open
Saniya1976 wants to merge 2 commits intofencer-so:mainfrom
Saniya1976:main
Open

Implement user-specific goal filtering and add unit tests#148
Saniya1976 wants to merge 2 commits intofencer-so:mainfrom
Saniya1976:main

Conversation

@Saniya1976
Copy link
Copy Markdown

Summary

This PR improves test coverage and ensures accurate data handling for user-specific goals by implementing proper filtering logic and validating it through unit tests.

Key Changes
Test Improvements
Added unit tests for the GetForUser endpoint in GoalController.
Ensured that users can only access their own goals.
Verified correct behavior of goal retrieval based on UserId.
Mock & Service Enhancements
Updated FakeCollections to better simulate realistic data scenarios.
Improved FakeGoalsService to support filtering goals by UserId.
Applied LINQ-based filtering to mimic actual service behavior.
Outcome
Increased test coverage for goal-related functionality.
Ensured data isolation between users.
Improved reliability and correctness of the goal retrieval logic.

Copilot AI review requested due to automatic review settings March 29, 2026 06:03
@gitguardian
Copy link
Copy Markdown

gitguardian bot commented Mar 29, 2026

⚠️ GitGuardian has uncovered 1 secret following the scan of your pull request.

Please consider investigating the findings and remediating the incidents. Failure to do so may lead to compromising the associated services or software components.

Since your pull request originates from a forked repository, GitGuardian is not able to associate the secrets uncovered with secret incidents on your GitGuardian dashboard.
Skipping this check run and merging your pull request will create secret incidents on your GitGuardian dashboard.

🔎 Detected hardcoded secret in your pull request
GitGuardian id GitGuardian status Secret Commit Filename
- - MongoDB Credentials 86a350d CommBank-Server/Secrets.json View secret
🛠 Guidelines to remediate hardcoded secrets
  1. Understand the implications of revoking this secret by investigating where it is used in your code.
  2. Replace and store your secret safely. Learn here the best practices.
  3. Revoke and rotate this secret.
  4. If possible, rewrite git history. Rewriting git history is not a trivial act. You might completely break other contributing developers' workflow and you risk accidentally deleting legitimate data.

To avoid such incidents in the future consider


🦉 GitGuardian detects secrets in your source code to help developers and security teams secure the modern development process. You are seeing this because you or someone else with access to this repository has authorized GitGuardian to scan your pull request.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds user-specific goal filtering behavior to the test fakes and validates it with a new unit test; it also introduces server-side operational/config changes (DB seeding, connection string config updates) and upgrades the server target framework.

Changes:

  • Add a GetForUser unit test and update test fakes to filter goals by UserId.
  • Extend fake goal data to include UserId for realistic filtering scenarios.
  • Add MongoDB seeding on startup, introduce/modify connection string configuration files, add Goal.Icon, and upgrade the server project to .NET 8.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
CommBank.Tests/GoalControllerTests.cs Adds assertions verifying GetForUser returns only goals owned by the requested user.
CommBank.Tests/Fake/FakeGoalsService.cs Implements LINQ filtering in GetForUserAsync to mimic production behavior.
CommBank.Tests/Fake/FakeCollections.cs Adds UserId fields to seeded fake goals.
CommBank-Server/appsettings.json Adds a MongoDB connection string.
CommBank-Server/appsettings.Development.json Adds connection strings (including MongoDB).
CommBank-Server/Services/DbSeeder.cs Introduces a MongoDB database seeder with sample data creation.
CommBank-Server/Secrets.json Updates the MongoDB connection string value.
CommBank-Server/Program.cs Calls the DB seeder during startup.
CommBank-Server/Models/Goal.cs Adds an Icon field to the goal model.
CommBank-Server/CommBank.csproj Upgrades server target framework to net8.0 and bumps MongoDB driver.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
Copy link

Copilot AI Mar 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The server project now targets net8.0, but CommBank.Tests targets net6.0 and has a ProjectReference to this project. A net6.0 test project can't reference a net8.0 project, so the solution build/test will fail unless you also upgrade the test project's TargetFramework (or multi-target the server / move shared code to a net6/netstandard library).

Suggested change
<TargetFramework>net8.0</TargetFramework>
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>

Copilot uses AI. Check for mistakes.
Comment on lines +27 to +36
// USER
var user = new User
{
Name = "Saniya",
Email = "saniya@test.com",
Password = BCrypt.Net.BCrypt.HashPassword("password123"),
AccountIds = new List<string>(),
GoalIds = new List<string>(),
TransactionIds = new List<string>()
};
Copy link

Copilot AI Mar 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DbSeeder creates a default user with a known plaintext password (even though hashed before storing). If seeding runs against a fresh non-dev database, this effectively creates a backdoor account. Consider restricting seeding to non-production environments and avoid creating fixed credentials (or require the password/user to be supplied securely via configuration).

Copilot uses AI. Check for mistakes.
Comment on lines +52 to +60
// Seed DB
try
{
await DbSeeder.SeedAsync(mongoDatabase);
}
catch (Exception ex)
{
Console.WriteLine($"[DB ERROR] Failed to seed database: {ex.Message}");
}
Copy link

Copilot AI Mar 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Database seeding is executed unconditionally on app startup. Even with the "already seeded" check, this adds startup I/O and can unintentionally seed production environments. Gate seeding behind app.Environment.IsDevelopment() and/or a dedicated configuration flag, and log failures via the configured logger instead of Console.WriteLine.

Copilot uses AI. Check for mistakes.
Comment on lines 65 to 67
[Fact]
public async void GetForUser()
{
Copy link

Copilot AI Mar 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test method is declared as async void. In xUnit, async void tests can lead to unobserved exceptions and flaky/incorrect pass results; change the signature to async Task so failures are properly awaited and reported.

Copilot uses AI. Check for mistakes.
Comment on lines +52 to +55
// Seed DB
try
{
await DbSeeder.SeedAsync(mongoDatabase);
Copy link

Copilot AI Mar 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR description focuses on user-specific goal filtering and unit tests, but this PR also adds a production-impacting DB seeder, changes runtime configuration (connection strings), and upgrades the server to net8.0. Please either update the PR description/scope or split these changes into separate PRs to reduce review and deployment risk.

Copilot uses AI. Check for mistakes.
}
},
"ConnectionStrings": {
"CommBank": "mongodb+srv://saniyakumari1976_db_user:k8XP8tHt3D15ebdi@common-bank.xlevoxq.mongodb.net/?appName=common-bank"
Copy link

Copilot AI Mar 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hard-coded MongoDB connection string (including credentials) is committed to appsettings.json. This is a security risk and will leak secrets to anyone with repo access; replace with a placeholder and load the real value from a secure secret store (environment variables / user secrets / CI secret), and rotate/revoke the exposed credentials.

Suggested change
"CommBank": "mongodb+srv://saniyakumari1976_db_user:k8XP8tHt3D15ebdi@common-bank.xlevoxq.mongodb.net/?appName=common-bank"
"CommBank": "YourMongoDbConnectionString"

Copilot uses AI. Check for mistakes.
},
"ConnectionStrings": {
"DefaultConnection": "Server=localhost;Database=CommBank;Trusted_Connection=True;MultipleActiveResultSets=true;TrustServerCertificate=True",
"CommBank": "mongodb+srv://saniyakumari1976_db_user:k8XP8tHt3D15ebdi@common-bank.xlevoxq.mongodb.net/?appName=common-bank"
Copy link

Copilot AI Mar 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hard-coded MongoDB connection string (including credentials) is committed to appsettings.Development.json. Even development configs should not contain real credentials in source control; use a placeholder and rely on local user-secrets/environment variables, and rotate/revoke the exposed credentials.

Suggested change
"CommBank": "mongodb+srv://saniyakumari1976_db_user:k8XP8tHt3D15ebdi@common-bank.xlevoxq.mongodb.net/?appName=common-bank"
"CommBank": "Use configuration provider (User Secrets/Environment) for CommBank MongoDB connection string"

Copilot uses AI. Check for mistakes.
{
"ConnectionStrings": {
"CommBank": "{CONNECTION_STRING}"
"CommBank": "mongodb+srv://saniyakumari1976_db_user:k8XP8tHt3D15ebdi@common-bank.xlevoxq.mongodb.net/?appName=common-bank"
Copy link

Copilot AI Mar 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secrets.json now contains an actual MongoDB connection string with credentials. This is a critical secret leak (and the file is referenced at startup). Remove the credential from the repo, ensure Secrets.json is excluded via .gitignore (or replaced with a template), and rotate/revoke the exposed credentials.

Suggested change
"CommBank": "mongodb+srv://saniyakumari1976_db_user:k8XP8tHt3D15ebdi@common-bank.xlevoxq.mongodb.net/?appName=common-bank"
"CommBank": "YOUR_MONGODB_CONNECTION_STRING"

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants