refactor(server/api/test): withUser helper; migrate 51 context-value sites (A3)
This commit is contained in:
@@ -51,7 +51,7 @@ func doAdminReq(t *testing.T, h *handlers, method, path string, body []byte, use
|
|||||||
if body != nil {
|
if body != nil {
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
}
|
}
|
||||||
req = req.WithContext(context.WithValue(req.Context(), userCtxKeyForTest(), user))
|
req = withUser(req, user)
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
newAdminLidarrRouter(h).ServeHTTP(w, req)
|
newAdminLidarrRouter(h).ServeHTTP(w, req)
|
||||||
return w
|
return w
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ func doAdminQuarantineReq(t *testing.T, h *handlers, method, path string, body [
|
|||||||
if body != nil {
|
if body != nil {
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
}
|
}
|
||||||
req = req.WithContext(context.WithValue(req.Context(), userCtxKeyForTest(), user))
|
req = withUser(req, user)
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
newAdminQuarantineRouter(h).ServeHTTP(w, req)
|
newAdminQuarantineRouter(h).ServeHTTP(w, req)
|
||||||
return w
|
return w
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ func doAdminRequestReq(t *testing.T, h *handlers, method, path string, body []by
|
|||||||
if body != nil {
|
if body != nil {
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
}
|
}
|
||||||
req = req.WithContext(context.WithValue(req.Context(), userCtxKeyForTest(), user))
|
req = withUser(req, user)
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
newAdminRequestsRouter(h).ServeHTTP(w, req)
|
newAdminRequestsRouter(h).ServeHTTP(w, req)
|
||||||
return w
|
return w
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ func newAdminTracksRouter(h *handlers) chi.Router {
|
|||||||
func doAdminTracksReq(t *testing.T, h *handlers, method, path string, user dbq.User) *httptest.ResponseRecorder {
|
func doAdminTracksReq(t *testing.T, h *handlers, method, path string, user dbq.User) *httptest.ResponseRecorder {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
req := httptest.NewRequest(method, path, bytes.NewBuffer(nil))
|
req := httptest.NewRequest(method, path, bytes.NewBuffer(nil))
|
||||||
req = req.WithContext(context.WithValue(req.Context(), userCtxKeyForTest(), user))
|
req = withUser(req, user)
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
newAdminTracksRouter(h).ServeHTTP(w, req)
|
newAdminTracksRouter(h).ServeHTTP(w, req)
|
||||||
return w
|
return w
|
||||||
|
|||||||
@@ -212,7 +212,7 @@ func TestHandleLogout_DeletesSessionAndClearsCookie(t *testing.T) {
|
|||||||
req.AddCookie(&http.Cookie{Name: auth.SessionCookieName, Value: token})
|
req.AddCookie(&http.Cookie{Name: auth.SessionCookieName, Value: token})
|
||||||
// handleLogout runs behind RequireUser in real routing; simulate that by
|
// handleLogout runs behind RequireUser in real routing; simulate that by
|
||||||
// putting the user into context here.
|
// putting the user into context here.
|
||||||
req = req.WithContext(context.WithValue(req.Context(), userCtxKeyForTest(), user))
|
req = withUser(req, user)
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
h.handleLogout(w, req)
|
h.handleLogout(w, req)
|
||||||
|
|
||||||
@@ -256,7 +256,7 @@ func TestHandleLogout_BearerHeaderWithTrailingWhitespaceDeletesSession(t *testin
|
|||||||
req := httptest.NewRequest(http.MethodPost, "/api/auth/logout", nil)
|
req := httptest.NewRequest(http.MethodPost, "/api/auth/logout", nil)
|
||||||
// Trailing whitespace — RequireUser trims this, so logout must too.
|
// Trailing whitespace — RequireUser trims this, so logout must too.
|
||||||
req.Header.Set("Authorization", "Bearer "+token+" ")
|
req.Header.Set("Authorization", "Bearer "+token+" ")
|
||||||
req = req.WithContext(context.WithValue(req.Context(), userCtxKeyForTest(), user))
|
req = withUser(req, user)
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
h.handleLogout(w, req)
|
h.handleLogout(w, req)
|
||||||
|
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ import (
|
|||||||
func callEvents(h *handlers, user dbq.User, body []byte) *httptest.ResponseRecorder {
|
func callEvents(h *handlers, user dbq.User, body []byte) *httptest.ResponseRecorder {
|
||||||
req := httptest.NewRequest(http.MethodPost, "/api/events", bytes.NewReader(body))
|
req := httptest.NewRequest(http.MethodPost, "/api/events", bytes.NewReader(body))
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
req = req.WithContext(context.WithValue(req.Context(), userCtxKeyForTest(), user))
|
req = withUser(req, user)
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
h.handleEvents(w, req)
|
h.handleEvents(w, req)
|
||||||
return w
|
return w
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ import (
|
|||||||
|
|
||||||
func callLike(h *handlers, user dbq.User, method, path string) *httptest.ResponseRecorder {
|
func callLike(h *handlers, user dbq.User, method, path string) *httptest.ResponseRecorder {
|
||||||
req := httptest.NewRequest(method, path, nil)
|
req := httptest.NewRequest(method, path, nil)
|
||||||
req = req.WithContext(context.WithValue(req.Context(), userCtxKeyForTest(), user))
|
req = withUser(req, user)
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
likesRouter(h).ServeHTTP(w, req)
|
likesRouter(h).ServeHTTP(w, req)
|
||||||
return w
|
return w
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ func TestHandleGetMe_ReturnsAuthenticatedUser(t *testing.T) {
|
|||||||
user := seedUser(t, pool, "alice", "hunter2", true)
|
user := seedUser(t, pool, "alice", "hunter2", true)
|
||||||
|
|
||||||
req := httptest.NewRequest(http.MethodGet, "/api/me", nil)
|
req := httptest.NewRequest(http.MethodGet, "/api/me", nil)
|
||||||
req = req.WithContext(context.WithValue(req.Context(), userCtxKeyForTest(), user))
|
req = withUser(req, user)
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
h.handleGetMe(w, req)
|
h.handleGetMe(w, req)
|
||||||
|
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ func doPlaylistsReq(h *handlers, user dbq.User, method, path string, body []byte
|
|||||||
if body != nil {
|
if body != nil {
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
}
|
}
|
||||||
req = req.WithContext(context.WithValue(req.Context(), userCtxKeyForTest(), user))
|
req = withUser(req, user)
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
newPlaylistsRouter(h).ServeHTTP(w, req)
|
newPlaylistsRouter(h).ServeHTTP(w, req)
|
||||||
return w
|
return w
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import (
|
|||||||
|
|
||||||
func callRadio(h *handlers, user interface{}, query string) *httptest.ResponseRecorder {
|
func callRadio(h *handlers, user interface{}, query string) *httptest.ResponseRecorder {
|
||||||
req := httptest.NewRequest(http.MethodGet, "/api/radio?"+query, nil)
|
req := httptest.NewRequest(http.MethodGet, "/api/radio?"+query, nil)
|
||||||
req = req.WithContext(context.WithValue(req.Context(), userCtxKeyForTest(), user))
|
req = withUser(req, user)
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
h.handleRadio(w, req)
|
h.handleRadio(w, req)
|
||||||
return w
|
return w
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ func newRequestsRouter(h *handlers) chi.Router {
|
|||||||
|
|
||||||
// withUser injects a user into the request context the same way RequireUser does.
|
// withUser injects a user into the request context the same way RequireUser does.
|
||||||
func withUser(req *http.Request, user dbq.User) *http.Request {
|
func withUser(req *http.Request, user dbq.User) *http.Request {
|
||||||
return req.WithContext(context.WithValue(req.Context(), userCtxKeyForTest(), user))
|
return withUser(req, user)
|
||||||
}
|
}
|
||||||
|
|
||||||
// doCreateRequest fires POST /api/requests with the given JSON body as the given user.
|
// doCreateRequest fires POST /api/requests with the given JSON body as the given user.
|
||||||
|
|||||||
Reference in New Issue
Block a user