mirror of
https://github.com/muety/wakapi.git
synced 2023-08-10 21:12:56 +03:00
refactor: replace gorilla mux with chi
This commit is contained in:
@@ -2,7 +2,7 @@ package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/go-chi/chi/v5"
|
||||
conf "github.com/muety/wakapi/config"
|
||||
"github.com/muety/wakapi/middlewares"
|
||||
"github.com/muety/wakapi/models"
|
||||
@@ -20,24 +20,23 @@ func CheckEffectiveUser(w http.ResponseWriter, r *http.Request, userService serv
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var vars = mux.Vars(r)
|
||||
|
||||
if vars["user"] == "" {
|
||||
vars["user"] = fallback
|
||||
userParam := chi.URLParam(r, "user")
|
||||
if userParam == "" {
|
||||
userParam = fallback
|
||||
}
|
||||
|
||||
authorizedUser := middlewares.GetPrincipal(r)
|
||||
if authorizedUser == nil {
|
||||
return respondError(http.StatusUnauthorized, conf.ErrUnauthorized)
|
||||
} else if vars["user"] == "current" {
|
||||
} else if userParam == "current" {
|
||||
return authorizedUser, nil
|
||||
}
|
||||
|
||||
if authorizedUser.ID != vars["user"] && !authorizedUser.IsAdmin {
|
||||
if authorizedUser.ID != userParam && !authorizedUser.IsAdmin {
|
||||
return respondError(http.StatusUnauthorized, conf.ErrUnauthorized)
|
||||
}
|
||||
|
||||
requestedUser, err := userService.GetUserById(vars["user"])
|
||||
requestedUser, err := userService.GetUserById(userParam)
|
||||
if err != nil {
|
||||
return respondError(http.StatusNotFound, "user not found")
|
||||
}
|
||||
|
||||
@@ -2,14 +2,14 @@ package utils
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/muety/wakapi/middlewares"
|
||||
"github.com/muety/wakapi/mocks"
|
||||
"github.com/muety/wakapi/models"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -70,8 +70,8 @@ func mockUserAwareRequest(requestedUser, authorizedUser string) (*http.Request,
|
||||
testPrincipal.SetPrincipal(&testUser)
|
||||
}
|
||||
|
||||
r := httptest.NewRequest("GET", fmt.Sprintf("http://localhost:3000/api/%s/data", requestedUser), nil)
|
||||
r = mux.SetURLVars(r, map[string]string{"user": requestedUser})
|
||||
r := httptest.NewRequest("GET", "http://localhost:3000/api/{user}/data", nil)
|
||||
r = withUrlParam(r, "user", requestedUser)
|
||||
r = r.WithContext(context.WithValue(r.Context(), "principal", &testPrincipal))
|
||||
|
||||
userServiceMock := new(mocks.UserServiceMock)
|
||||
@@ -81,3 +81,12 @@ func mockUserAwareRequest(requestedUser, authorizedUser string) (*http.Request,
|
||||
|
||||
return r, httptest.NewRecorder(), userServiceMock
|
||||
}
|
||||
|
||||
func withUrlParam(r *http.Request, key, value string) *http.Request {
|
||||
r.URL.RawPath = strings.Replace(r.URL.RawPath, "{"+key+"}", value, 1)
|
||||
r.URL.Path = strings.Replace(r.URL.Path, "{"+key+"}", value, 1)
|
||||
rctx := chi.NewRouteContext()
|
||||
rctx.URLParams.Add(key, value)
|
||||
r = r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, rctx))
|
||||
return r
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user