3.6 KiB
3.6 KiB
QA procedure: get_current_user
Package: test (black-box), extending test/user_test.go (mockUserRepo) and following the router
wiring pattern in test/stale_token_invalidation_http_test.go.
Test infrastructure additions needed first
- No new
mockUserRepomethods are needed:GetUser(id)already exists and is already exercised by other tests (e.g.test/stale_token_invalidation_http_test.go).DELETE /v1/auth/registeralready deletes the row viaCancelRegistrationUseCase, already used intest/cancel_pending_registration_http_test.go.
Setup shared by every case
- Call
validator.InitValidator()once. - Build
getUserUseCase := useCases.NewGetUserUseCase(userRepo). - Build
meHandler := auth.NewMeHandler(getUserUseCase). - Also build
verificationHandler := auth.NewVerificationHandler(useCases.NewVerifyUserUseCase(userRepo, hasher), useCases.NewCancelRegistrationUseCase(userRepo), true)so the "deleted account" scenario can callDELETE /v1/auth/registeron the same router. - Mount on a fresh
chi.NewRouter(), mirroringNewAuthRouter's protected group:with a testr.Route("/v1/auth", func(r chi.Router) { r.Group(func(r chi.Router) { r.Use(jwtauth.Verifier(token)) r.Use(jwtauth.Authenticator(token)) r.Use(middleware.RequireCurrentToken(getUserUseCase)) r.Get("/me", meHandler.Me) r.Delete("/register", verificationHandler.CancelRegistration) }) })token := jwtauth.New("HS256", []byte("test-secret"), nil). No live server. - Mint jwt cookies with
token.Encode(map[string]any{"user_id": uint32(id), "token_version": uint32(v)}), the same convention astest/stale_token_invalidation_http_test.go.
HTTP-level checks
- Seed
mockUserRepowith user id 7, username "alice", email "alice@example.com",IsVerified: true,TokenVersion: 0. Mint a cookie for user 7 withtoken_version: 0.GET /v1/auth/me→ assert status200,Cache-Controlheader equals"no-store", and the decoded JSON body hasid: 7,username: "alice",email: "alice@example.com",is_verified: true. - Seed user id 8, username "bob", email "bob@example.com",
IsVerified: false,TokenVersion: 0. Mint a matching cookie.GET /v1/auth/me→ assert status200(not 403),Cache-Control: no-store, andis_verified: falsein the body. - No
jwtcookie at all against the router from step 6 → assert status401(produced byjwtauth.Authenticatoritself; don't assert an exact body for this case, same rationale asfeatures/account_verification.qa.mdstep 15). - Seed user id 7 with
TokenVersion: 1. Mint a cookie encodingtoken_version: 0(stale).GET /v1/auth/me→ assert status401and body exactlyapperr.UnauthorizedErrorMessage. - Seed user id 7,
IsVerified: false,TokenVersion: 0. Mint a matching cookie.DELETE /v1/auth/registerwith that cookie → assert status200. ThenGET /v1/auth/mewith the same cookie → assert status401and body exactlyapperr.UnauthorizedErrorMessage. - Seed user id 7,
IsVerified: true,TokenVersion: 0, matching cookie. SendGET /v1/auth/me5 times in a row on the same router/repo → assert every response is200and none is429.
Manual verification (not exercised by the Go test)
- The SPA actually calling
GET /v1/auth/meon load/refresh to restore session state and route an unverified user to the pending-verification screen (rather than guessing from stored client state) is out of scope for this slice's automated test and should be checked by hand against a running dev build.