🧪 test: add JWT edge case scenarios with validation endpoint
- Add expired JWT token scenario - Add wrong secret JWT token scenario - Add malformed JWT token scenario - Implement /api/v1/auth/validate endpoint - Add JWT parsing and validation to BDD steps Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
@@ -3,6 +3,7 @@ package api
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"dance-lessons-coach/pkg/user"
|
||||
@@ -34,6 +35,7 @@ func (h *AuthHandler) RegisterRoutes(router chi.Router) {
|
||||
router.Post("/register", h.handleRegister)
|
||||
router.Post("/password-reset/request", h.handlePasswordResetRequest)
|
||||
router.Post("/password-reset/complete", h.handlePasswordResetComplete)
|
||||
router.Post("/validate", h.handleValidateToken)
|
||||
}
|
||||
|
||||
// writeValidationError writes a structured validation error response
|
||||
@@ -302,3 +304,56 @@ func (h *AuthHandler) handlePasswordResetComplete(w http.ResponseWriter, r *http
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(map[string]string{"message": "Password reset completed successfully"})
|
||||
}
|
||||
|
||||
// TokenValidationRequest represents a JWT token validation request
|
||||
// This is used for testing JWT validation with different token scenarios
|
||||
type TokenValidationRequest struct {
|
||||
Token string `json:"token" validate:"required"`
|
||||
}
|
||||
|
||||
// handleValidateToken godoc
|
||||
//
|
||||
// @Summary Validate JWT token
|
||||
// @Description Validate a JWT token and return user information if valid
|
||||
// @Tags API/v1/User
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param request body TokenValidationRequest true "Token validation request"
|
||||
// @Success 200 {object} map[string]interface{} "Token is valid with user info"
|
||||
// @Failure 400 {object} map[string]string "Invalid request"
|
||||
// @Failure 401 {object} map[string]string "Invalid token"
|
||||
// @Router /v1/auth/validate [post]
|
||||
func (h *AuthHandler) handleValidateToken(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
var req TokenValidationRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
http.Error(w, `{"error":"invalid_request","message":"Invalid JSON request body"}`, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Validate request using validator
|
||||
if h.validator != nil {
|
||||
if err := h.validator.Validate(req); err != nil {
|
||||
h.writeValidationError(w, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Validate the JWT token
|
||||
user, err := h.authService.ValidateJWT(ctx, req.Token)
|
||||
if err != nil {
|
||||
log.Trace().Ctx(ctx).Err(err).Msg("JWT validation failed in validate endpoint")
|
||||
http.Error(w, fmt.Sprintf(`{"error":"invalid_token","message":"%s"}`, err.Error()), http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
// Return success with user info
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"valid": true,
|
||||
"user_id": user.ID,
|
||||
"message": "Token is valid",
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user