Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

configuring h2 tests with production auth interceptor to safely enable refactor #138

Closed
wants to merge 2 commits into from

Conversation

cbb330
Copy link
Collaborator

@cbb330 cbb330 commented Jul 17, 2024

Summary

problem: I cannot refactor the interceptor here (safely): #133 because it doesn't have test coverage.

why?
we have two types of methods to enable authentication in unittests:

  • /mock/* - these use a custom mock MVC object with a hard coded dummytoken interceptor, which has different business logic than production for example, it does not make a differentiation in routes. dummytokeninterceptor is applied to ALL routes indiscriminatly, while the production interceptor has this filter:
            registry
                .addInterceptor((HandlerInterceptor) cons.get().newInstance())
                .addPathPatterns("/**")
                .excludePathPatterns("/actuator/**", "/**/api-docs/**", "/**/swagger-ui/**");

in addition, I will be refactoring the interceptor in a different PR. this PR requires proper testing to prevent regressions in the logic.

  • /h2/* - these don't enable interceptors at all and instead rely on SecurityContextHolder to pass around auth context

So to fix, we need a unittest framework that allows us to:

  1. enable an auth interceptor
  2. use the existing production auth interceptor

Changes

  • Client-facing API Changes
  • Internal API Changes
  • Bug Fixes
  • New Features
  • Performance Improvements
  • Code Style
  • Refactoring
  • Documentation
  • Tests
  1. enable token interceptor in /h2/* tests by adding the AuthorizationPropertiesInitializer source and editting that source
  2. customize the default MVC mock object that is given by @AutoConfigureMockMvc such that all API requests include headers
  3. finally, add unittest for covering prod auth interceptor by querying the swagger endpoint as an example

Testing Done

  • Manually Tested on local docker setup. Please include commands ran, and their output.
  • Added new tests for the changes made.
  • Updated existing tests to reflect the changes made.
  • No tests added or updated. Please explain why. If unsure, please feel free to ask for help.
  • Some other form of testing like staging or soak time in production. Please explain.

since this only changed config for H2 unittests, all h2 unittests are passing including the new unittest i added.

proving that the production auth interceptor configuration results in desired outcome from unittests:

  • without auth on swagger, test succeeds with httpstatus.isOk():
image
  • with auth enabled on swagger, test fails because httpstatus.isOk() returns 401:
image

Additional Information

  • Breaking Changes
  • Deprecations
  • Large PR broken into smaller PRs, and PR plan linked in the description.

@cbb330 cbb330 mentioned this pull request Jul 17, 2024
21 tasks
@cbb330 cbb330 closed this Jul 30, 2024
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.

None yet

1 participant