Skip to content

feat: hooks round 5 (Option 2) - add before-user-created hook #2034

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

Merged
merged 13 commits into from
Jun 2, 2025

Conversation

cstockton
Copy link
Contributor

@cstockton cstockton commented May 23, 2025

Hooks Round 5 - Option 2

This PR contains Option 1 for implementing the before-user-created hook. See #2032 for option 1.

Summary

This commit explores one possible implementation of this hook by:

  • Adding a triggerBeforeUserCreated method to the *API object in internal/api/hooks.go
  • Adding a triggerBeforeUserCreatedExternal method to the *API object in internal/api/hooks.go
  • Updating callers of signupNewUser to first call triggerBeforeUserCreated in:
    • internal/api/anonymous.go
    • internal/api/external.go
    • internal/api/invite.go
    • internal/api/mail.go
    • internal/api/signup.go
  • Updating callers of signupNewUser to first call triggerBeforeUserCreatedExternal in:
    • internal/api/external.go
    • internal/api/samlacs.go
    • internal/api/token_oidc.go
    • internal/api/web3.go
  • Add end to end tests in internal/api/e2e_test.go

This has the benefit of being outside the transaction, but is a bit more complex. I make a best-effort to ensure I only trigger before-user-created when the user doesn't exist, but being outside the transaction there is a small chance for duplicate calls or calls that happen when a user already has been created. The main thought here is that we can document this behavior and the tradeoff is worth the benefits.

Depends on

feat: hooks round 1 - prepare package structure

  • renamed pkg internal/hooks/v0hooks/v0http -> internal/hooks/hookshttp 8a398ab
  • renamed pkg internal/hooks/v0hooks/v0pgfunc -> internal/hooks/hookspgfunc 8a398ab
  • use pkg internal/e2e for test setup in:
    • pkg internal/hooks/hookspgfunc 4d60288
    • pkg internal/hooks/v0hooks 4a7432b

feat: hooks round 2 - remove indirection and simplify error handling

  • update pkg internal/api to:
    • uses internal/hooks/v0hooks.Manager instead of internal/hooks/hooks.Manager aec5995
  • remove pkg internal/hooks/hooks.Manager 062da5d
  • add pkg internal/hooks/hookserrors 7e80afc
  • use pkg internal/hooks/hookserrors in internal/hooks/v0hooks 57744e8
  • update pkg internal/hooks/v0hooks with an Enabled method 16cc4c9

feat: hooks round 3 - begin adding the Before and After user created hooks

  • update pkg internal/conf d5f5436
    • add BeforeUserCreated and AfterUserCreated to HookConfiguration struct
    • add test cases for EmailValidationBlockedMX to restore 100% test coverage
  • update pkg internal/hooks/v0hooks bd37fe2
    • add BeforeUserCreated method to v0hooks.Manager struct
    • add AfterUserCreated method to v0hooks.Manager struct
    • add tests to reach 100% coverage
  • add pkg internal/e2e/e2ehooks 903e623
    • add HookCall to record calls to hooks
    • add Hook struct to hold []*HookCall for a given hook name
    • add HookRecorder to hold one Hook object per hook name
    • add Instance struct to hold the httptest.Server and HookRecorder
    • add AfterUserCreated method to v0hooks.Manager struct
    • add tests to reach 100% coverage in all internal/e2e packages
  • update pkg internal/hooks/v0hooks 829aec6
    • fix struct and json tag to match to match the Metadata type
  • update pkg internal/hooks/v0hooks ca67be0
    • remove BeforeUserCreated and AfterUserCreated methods
    • add Before & After user created hooks in InvokeHook
  • update pkg internal/e2e/e2eapi 46c144e
    • add comments in IOError tests involving http.RoundTripper
    • update calls to t.Fatal to use require

feat: hooks round 4 - update tests to use require package

  • use pkg require for tests in: f2b3600
    • pkg internal/e2e/...
    • pkg internal/hooks/...

This commit explores one possible implementation of this hook by:
- Adding a `triggerBeforeUserCreated` method to the `*API` object in `internal/api/hooks.go`
- Adding a `triggerBeforeUserCreatedExternal` method to the `*API` object in `internal/api/hooks.go`
- Updating callers of `signupNewUser` to first call `triggerBeforeUserCreated` in:
    - internal/api/anonymous.go
    - internal/api/external.go
    - internal/api/invite.go
    - internal/api/mail.go
    - internal/api/signup.go
- Updating callers of `signupNewUser` to first call `triggerBeforeUserCreatedExternal` in:
    - internal/api/external.go
    - internal/api/samlacs.go
    - internal/api/token_oidc.go
    - internal/api/web3.go
- Add end to end tests in `internal/api/e2e_test.go`
@cstockton cstockton requested a review from a team as a code owner May 23, 2025 15:03
@coveralls
Copy link

coveralls commented May 23, 2025

Pull Request Test Coverage Report for Build 15329359133

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 181 of 277 (65.34%) changed or added relevant lines in 16 files are covered.
  • 2 unchanged lines in 2 files lost coverage.
  • Overall coverage increased (+0.1%) to 70.144%

Changes Missing Coverage Covered Lines Changed/Added Lines %
internal/api/anonymous.go 1 3 33.33%
internal/api/external.go 7 9 77.78%
internal/api/signup.go 1 3 33.33%
internal/api/web3.go 5 7 71.43%
internal/hooks/hookspgfunc/hookspgfunc.go 2 4 50.0%
internal/api/token_oidc.go 0 3 0.0%
internal/hooks/v0hooks/v0hooks.go 17 21 80.95%
internal/api/mail.go 19 25 76.0%
internal/api/samlacs.go 0 6 0.0%
internal/e2e/e2ehooks/e2ehooks.go 30 36 83.33%
Files with Coverage Reduction New Missed Lines %
internal/api/invite.go 1 65.08%
internal/api/token_refresh.go 1 68.21%
Totals Coverage Status
Change from base Build 15205934189: 0.1%
Covered Lines: 11284
Relevant Lines: 16087

💛 - Coveralls

@cstockton
Copy link
Contributor Author

Note: I'll improve test coverage if this implementation is approved.

@cstockton cstockton merged commit b53f6b0 into master Jun 2, 2025
5 checks passed
@cstockton cstockton deleted the cs/hooks-pr5-opt2 branch June 2, 2025 20:52
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.

3 participants