Implement unit tests for Sztab Basic Auth flow (SZ-19)
rk@tigase.net opened 4 days ago

Although the application starts up correctly with the basic-auth Spring profile enabled and the admin user configured in application-basic-auth.yml, unit and integration tests fail to authenticate when accessing secured endpoints using the expected credentials (admin@sztab.local / admin123).

These tests return HTTP 401 Unauthorized, indicating that the Basic Auth setup is not being honored in the test context.

Steps to Reproduce 1. Run any test that accesses secured endpoints (e.g., /api/users) using @SpringBootTest or @WebMvcTest with profile basic-auth 2. Provide credentials matching those defined in: • application-basic-auth.yml • Initialized by DataBootstrapper during app startup 3. Observe the test failure due to 401 Unauthorized

  • rk@tigase.net commented 4 days ago

    Estimated Time to Resolve

    TaskDescriptionTime Estimate
    DiagnoseInvestigate why admin user is not available in test context (e.g., missing profile, missing bean, or test config issue)1–2 hours
    FixImplement solution (e.g., include application-basic-auth.yml, wire DataBootstrapper, or mock user via UserDetailsService)1–2 hours
    TestsAdd/restore test class (e.g., SecurityConfigBasicAuthTest) and verify secured endpoints return 200/401 as expected1 hour
    Cleanup & CommitAnnotate test profile usage, push changes, and update README if needed0.5 hour

    Total: 3.5 to 5.5 hours (1 day buffer)

  • rk@tigase.net changed state to 'In Progress' 4 days ago
    Previous Value Current Value
    Open
    In Progress
  • rk@tigase.net commented 2 days ago
    • Merged bugfix/sz19-ut-for-basic-auth into wolnosc.
    • Replaced the old SecurityConfig with BasicAuthSecurityConfig and OidcSecurityConfig.
    • Fixed unit test setup for both Basic and OIDC auth flows.
    • Confirmed clean build, all tests passing.
    • Deleting the branch now.
  • rk@tigase.net changed state to 'Closed' 2 days ago
    Previous Value Current Value
    In Progress
    Closed
  • rk@tigase.net commented 2 days ago

    Issue: SZ-19 - Fix broken unit tests for Basic Auth
    Estimate: 5h 30m
    Actual: 8h 15m

    Notes:
    Initial estimate did not account for several complexities:

    • Refactoring SecurityConfig into two separate configurations: BasicAuthSecurityConfig and OidcSecurityConfig
    • Rewriting the test setup for both modes to avoid classpath leaks and test failures
    • Fixing controller tests to run cleanly with security context
    • Resolving merge conflict in docker-compose.yml
    • Ensuring clean Maven builds and successful execution of all tests before merge

    Tests now pass cleanly and are merged to wolnosc.

  • rk@tigase.net commented 2 days ago

    Root Cause and Complication

    The original SecurityConfig was designed as a single unified Spring Security configuration that attempted to support both Basic Auth and OIDC modes via Spring Profiles. However, during unit testing with @WebMvcTest, this led to unstable behavior and test failures — especially due to improper context isolation and undesired loading of BasicAuthTestConfig in unrelated test classes.

    Resolution Approach

    To ensure clean separation of security contexts and avoid cross-contamination:

    • The unified SecurityConfig was split into two dedicated configurations:
      • BasicAuthSecurityConfig.java (activated via basic-auth profile)
      • OidcSecurityConfig.java (activated via oidc profile)
    • Corresponding test classes were rewritten to explicitly import only the relevant configuration using @Import(...) or @ContextConfiguration(...).
    • Mocking behavior such as JwtDecoder was localized to OidcSecurityConfigTest.
    • The split config approach prevents accidental loading of unused beans and allows @WebMvcTest to construct minimal, stable contexts.

    This restructuring resolves test pollution and sets up a clean foundation for future enhancements to authentication mechanisms.

issue 1 of 1
Type
Bug
Priority
Normal
Assignee
Version
1.0
Sprints
n/a
Customer
n/a
Issue Votes (0)
Watchers (3)
Reference
SZ-19
Please wait...
Page is in error, reload to recover