feat: implement patch test management and validation improvements
This commit is contained in:
@@ -102,7 +102,7 @@ func CreatePatchTest(w http.ResponseWriter, r *http.Request) {
|
||||
func UpdatePatchTest(w http.ResponseWriter, r *http.Request) {
|
||||
id := chi.URLParam(r, "id")
|
||||
if id == "" || !validators.IsValidID(id) {
|
||||
http.Error(w, "Patch test not found", http.StatusNotFound)
|
||||
http.Error(w, "Patch test not found (ID: " + id + ")", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"crussell/db"
|
||||
@@ -37,6 +38,7 @@ func makeUserRequest(handler http.Handler, method, path string, body interface{}
|
||||
}
|
||||
|
||||
// makeRequestWithContext creates a request with specific user context
|
||||
|
||||
func makeRequestWithContext(handler http.Handler, method, path string, body interface{}, userID, role string) *httptest.ResponseRecorder {
|
||||
var req *http.Request
|
||||
if body != nil {
|
||||
@@ -47,18 +49,13 @@ func makeRequestWithContext(handler http.Handler, method, path string, body inte
|
||||
req = httptest.NewRequest(method, path, nil)
|
||||
}
|
||||
|
||||
// Set up chi routing context (required for chi.URLParam to work)
|
||||
rctx := chi.NewRouteContext()
|
||||
// Parse the path to extract ID parameters for chi
|
||||
// chi routes like /api/admin/users/{id} need {id} in route context
|
||||
if method == "GET" || method == "PUT" || method == "POST" || method == "DELETE" || method == "PATCH" {
|
||||
// Extract path params from URL for chi
|
||||
if id, paramName := extractIDFromPath(path); id != "" {
|
||||
rctx.URLParams.Add(paramName, id)
|
||||
}
|
||||
}
|
||||
|
||||
// Set up context with user ID and role (simulating middleware)
|
||||
ctx := context.WithValue(req.Context(), chi.RouteCtxKey, rctx)
|
||||
ctx = context.WithValue(ctx, mw.UserIDKey, userID)
|
||||
ctx = context.WithValue(ctx, mw.UserRoleKey, role)
|
||||
@@ -69,10 +66,7 @@ func makeRequestWithContext(handler http.Handler, method, path string, body inte
|
||||
return w
|
||||
}
|
||||
|
||||
// extractIDFromPath extracts the ID from URL paths like /api/admin/users/{id} or /api/admin/bookings/{id}/progress
|
||||
// It returns only the ID segment, not any nested path parts
|
||||
func extractIDFromPath(path string) (string, string) {
|
||||
// Define patterns with their param names: (prefix, paramName)
|
||||
patterns := []struct {
|
||||
prefix string
|
||||
paramName string
|
||||
@@ -81,16 +75,16 @@ func extractIDFromPath(path string) (string, string) {
|
||||
{"/api/admin/users/", "id"},
|
||||
{"/api/admin/bookings/", "id"},
|
||||
{"/api/admin/services/", "id"},
|
||||
{"/api/admin/patch-tests/", "id"},
|
||||
{"/api/bookings/", "id"},
|
||||
{"/api/services/eligible-for/", "userId"},
|
||||
{"/api/services/", "id"},
|
||||
}
|
||||
|
||||
for _, p := range patterns {
|
||||
if idx := findLastSegment(path, p.prefix); idx >= 0 {
|
||||
// Extract only the ID segment (up to the next / or end of path)
|
||||
suffix := path[idx:]
|
||||
if slashIdx := findSlash(suffix); slashIdx >= 0 {
|
||||
if strings.HasPrefix(path, p.prefix) {
|
||||
suffix := path[len(p.prefix):]
|
||||
if slashIdx := strings.Index(suffix, "/"); slashIdx >= 0 {
|
||||
return suffix[:slashIdx], p.paramName
|
||||
}
|
||||
return suffix, p.paramName
|
||||
@@ -99,25 +93,6 @@ func extractIDFromPath(path string) (string, string) {
|
||||
return "", ""
|
||||
}
|
||||
|
||||
// findSlash finds the position of the first / in the string
|
||||
func findSlash(s string) int {
|
||||
for i := 0; i < len(s); i++ {
|
||||
if s[i] == '/' {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
func findLastSegment(path, prefix string) int {
|
||||
for i := len(path) - 1; i >= len(prefix); i-- {
|
||||
if len(path) > i && path[i-len(prefix):i] == prefix {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
func parseResponseBody(w *httptest.ResponseRecorder, dest interface{}) error {
|
||||
return json.Unmarshal(w.Body.Bytes(), dest)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user