refac: split the auth handler into registration and login, account verification, and password reset handlers, each with its own dependencies
This commit is contained in:
1 parent
3282b0580c
commit
9b47685002
18 files changed
+497
-452
No files matched your search
@@ -17,18 +17,11 @@ import (
|
||||
"github.com/go-chi/jwtauth/v5"
|
||||
)
|
||||
|
||||
func newAccountVerificationRouter(userRepo *mockUserRepo, mailRepo *mockMailRepo, token *jwtauth.JWTAuth) *chi.Mux {
|
||||
func newAccountVerificationRouter(userRepo *mockUserRepo, token *jwtauth.JWTAuth) *chi.Mux {
|
||||
hasher := &stubHasher{}
|
||||
authHandler := auth.NewHandler(
|
||||
token,
|
||||
useCases.NewGetUserUseCase(userRepo),
|
||||
useCases.NewListUsersUseCase(userRepo),
|
||||
useCases.NewCreateUserUseCase(userRepo, hasher, mailRepo, "https://cms.example.com"),
|
||||
useCases.NewAuthenticateUserUseCase(userRepo, hasher),
|
||||
verificationHandler := auth.NewVerificationHandler(
|
||||
useCases.NewVerifyUserUseCase(userRepo, hasher),
|
||||
useCases.NewCancelRegistrationUseCase(userRepo),
|
||||
useCases.NewRequestPasswordResetUseCase(userRepo, hasher, mailRepo, "https://cms.example.com"),
|
||||
useCases.NewResetPasswordUseCase(userRepo, hasher),
|
||||
true,
|
||||
)
|
||||
|
||||
@@ -38,7 +31,7 @@ func newAccountVerificationRouter(userRepo *mockUserRepo, mailRepo *mockMailRepo
|
||||
r.Group(func(r chi.Router) {
|
||||
r.Use(jwtauth.Verifier(token))
|
||||
r.Use(jwtauth.Authenticator(token))
|
||||
r.Post("/verify", authHandler.Verify)
|
||||
r.Post("/verify", verificationHandler.Verify)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -76,7 +69,7 @@ func TestAccountVerificationHttp(t *testing.T) {
|
||||
|
||||
t.Run("A logged-in user verifies their account with the correct emailed code", func(t *testing.T) {
|
||||
repo := newAccountVerificationTestUserRepo()
|
||||
router := newAccountVerificationRouter(repo, newMockMailRepo(), token)
|
||||
router := newAccountVerificationRouter(repo, token)
|
||||
|
||||
rec := sendVerifyRequest(router, token, map[string]any{"user_id": uint32(7)}, "abc123")
|
||||
|
||||
@@ -101,7 +94,7 @@ func TestAccountVerificationHttp(t *testing.T) {
|
||||
|
||||
t.Run("A wrong code is refused and nothing changes", func(t *testing.T) {
|
||||
repo := newAccountVerificationTestUserRepo()
|
||||
router := newAccountVerificationRouter(repo, newMockMailRepo(), token)
|
||||
router := newAccountVerificationRouter(repo, token)
|
||||
|
||||
rec := sendVerifyRequest(router, token, map[string]any{"user_id": uint32(7)}, "wrong-code")
|
||||
|
||||
@@ -122,7 +115,7 @@ func TestAccountVerificationHttp(t *testing.T) {
|
||||
expired := repo.users[7]
|
||||
expired.VerificationExpiration = time.Now().Add(-time.Hour)
|
||||
repo.users[7] = expired
|
||||
router := newAccountVerificationRouter(repo, newMockMailRepo(), token)
|
||||
router := newAccountVerificationRouter(repo, token)
|
||||
|
||||
rec := sendVerifyRequest(router, token, map[string]any{"user_id": uint32(7)}, "abc123")
|
||||
|
||||
@@ -140,7 +133,7 @@ func TestAccountVerificationHttp(t *testing.T) {
|
||||
|
||||
t.Run("The code is cleared once used, so it cannot be replayed", func(t *testing.T) {
|
||||
repo := newAccountVerificationTestUserRepo()
|
||||
router := newAccountVerificationRouter(repo, newMockMailRepo(), token)
|
||||
router := newAccountVerificationRouter(repo, token)
|
||||
|
||||
first := sendVerifyRequest(router, token, map[string]any{"user_id": uint32(7)}, "abc123")
|
||||
if first.Code != http.StatusOK {
|
||||
@@ -169,7 +162,7 @@ func TestAccountVerificationHttp(t *testing.T) {
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
repo := newAccountVerificationTestUserRepo()
|
||||
router := newAccountVerificationRouter(repo, newMockMailRepo(), token)
|
||||
router := newAccountVerificationRouter(repo, token)
|
||||
|
||||
rec := sendVerifyRequest(router, token, tc.claims, "abc123")
|
||||
|
||||
@@ -190,7 +183,7 @@ func TestAccountVerificationHttp(t *testing.T) {
|
||||
t.Run("A repository failure unrelated to the code returns 500", func(t *testing.T) {
|
||||
repo := newAccountVerificationTestUserRepo()
|
||||
repo.fail.failNext("ClearVerificationCode")
|
||||
router := newAccountVerificationRouter(repo, newMockMailRepo(), token)
|
||||
router := newAccountVerificationRouter(repo, token)
|
||||
|
||||
rec := sendVerifyRequest(router, token, map[string]any{"user_id": uint32(7)}, "abc123")
|
||||
|
||||
@@ -204,7 +197,7 @@ func TestAccountVerificationHttp(t *testing.T) {
|
||||
|
||||
t.Run("A malformed request body returns 400", func(t *testing.T) {
|
||||
repo := newAccountVerificationTestUserRepo()
|
||||
router := newAccountVerificationRouter(repo, newMockMailRepo(), token)
|
||||
router := newAccountVerificationRouter(repo, token)
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/v1/auth/verify", bytes.NewReader([]byte("not-json")))
|
||||
_, tokenString, _ := token.Encode(map[string]any{"user_id": uint32(7)})
|
||||
@@ -222,7 +215,7 @@ func TestAccountVerificationHttp(t *testing.T) {
|
||||
|
||||
t.Run("A missing code fails validation and returns 400", func(t *testing.T) {
|
||||
repo := newAccountVerificationTestUserRepo()
|
||||
router := newAccountVerificationRouter(repo, newMockMailRepo(), token)
|
||||
router := newAccountVerificationRouter(repo, token)
|
||||
|
||||
rec := sendVerifyRequest(router, token, map[string]any{"user_id": uint32(7)}, "")
|
||||
|
||||
@@ -233,7 +226,7 @@ func TestAccountVerificationHttp(t *testing.T) {
|
||||
|
||||
t.Run("A request with no jwt cookie is answered 401 by jwtauth itself", func(t *testing.T) {
|
||||
repo := newAccountVerificationTestUserRepo()
|
||||
router := newAccountVerificationRouter(repo, newMockMailRepo(), token)
|
||||
router := newAccountVerificationRouter(repo, token)
|
||||
|
||||
rec := sendVerifyRequest(router, token, nil, "abc123")
|
||||
|
||||
|
||||
Reference in new issue
Block a user