Chore: Move ReqContext to contexthandler service (#62102)
* Chore: Move ReqContext to contexthandler service * Rename package to contextmodel * Generate ngalert files * Remove unused imports
This commit is contained in:
@@ -10,13 +10,13 @@ import (
|
||||
"github.com/google/uuid"
|
||||
"github.com/grafana/grafana/pkg/api/routing"
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/contexthandler/ctxkey"
|
||||
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
"github.com/grafana/grafana/pkg/web"
|
||||
)
|
||||
|
||||
var requests = map[string]*models.ReqContext{}
|
||||
var requests = map[string]*contextmodel.ReqContext{}
|
||||
|
||||
type Server struct {
|
||||
t testing.TB
|
||||
@@ -30,7 +30,7 @@ func NewServer(t testing.TB, routeRegister routing.RouteRegister) *Server {
|
||||
t.Helper()
|
||||
|
||||
m := web.New()
|
||||
initCtx := &models.ReqContext{}
|
||||
initCtx := &contextmodel.ReqContext{}
|
||||
m.Use(func(c *web.Context) {
|
||||
initCtx.Context = c
|
||||
initCtx.Logger = log.New("api-test")
|
||||
@@ -104,20 +104,20 @@ func requestIdentifierFromRequest(req *http.Request) string {
|
||||
return req.Header.Get("X-GRAFANA-WEB-TEST-ID")
|
||||
}
|
||||
|
||||
func RequestWithWebContext(req *http.Request, c *models.ReqContext) *http.Request {
|
||||
func RequestWithWebContext(req *http.Request, c *contextmodel.ReqContext) *http.Request {
|
||||
reqID := requestIdentifierFromRequest(req)
|
||||
requests[reqID] = c
|
||||
return req
|
||||
}
|
||||
|
||||
func RequestWithSignedInUser(req *http.Request, user *user.SignedInUser) *http.Request {
|
||||
return RequestWithWebContext(req, &models.ReqContext{
|
||||
return RequestWithWebContext(req, &contextmodel.ReqContext{
|
||||
SignedInUser: user,
|
||||
IsSignedIn: true,
|
||||
})
|
||||
}
|
||||
|
||||
func requestContextFromRequest(req *http.Request) *models.ReqContext {
|
||||
func requestContextFromRequest(req *http.Request) *contextmodel.ReqContext {
|
||||
reqID := requestIdentifierFromRequest(req)
|
||||
val, exists := requests[reqID]
|
||||
if !exists {
|
||||
@@ -130,7 +130,7 @@ func requestContextFromRequest(req *http.Request) *models.ReqContext {
|
||||
func requestContextMiddleware() web.Middleware {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
c := ctxkey.Get(r.Context()).(*models.ReqContext)
|
||||
c := ctxkey.Get(r.Context()).(*contextmodel.ReqContext)
|
||||
|
||||
ctx := requestContextFromRequest(r)
|
||||
if ctx != nil {
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
|
||||
"github.com/grafana/grafana/pkg/api/response"
|
||||
"github.com/grafana/grafana/pkg/api/routing"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
@@ -17,7 +17,7 @@ import (
|
||||
func TestServer(t *testing.T) {
|
||||
routeRegister := routing.NewRouteRegister()
|
||||
var actualRequest *http.Request
|
||||
routeRegister.Post("/api", routing.Wrap(func(c *models.ReqContext) response.Response {
|
||||
routeRegister.Post("/api", routing.Wrap(func(c *contextmodel.ReqContext) response.Response {
|
||||
actualRequest = c.Req
|
||||
return response.JSON(http.StatusOK, c.SignedInUser)
|
||||
}))
|
||||
@@ -68,7 +68,7 @@ func verifyRequest(t *testing.T, s *Server, req *http.Request, expectedBody stri
|
||||
|
||||
require.NotEmpty(t, requestIdentifierFromRequest(req))
|
||||
|
||||
req = RequestWithWebContext(req, &models.ReqContext{
|
||||
req = RequestWithWebContext(req, &contextmodel.ReqContext{
|
||||
IsSignedIn: true,
|
||||
})
|
||||
require.NotNil(t, req)
|
||||
@@ -79,7 +79,7 @@ func verifyRequest(t *testing.T, s *Server, req *http.Request, expectedBody stri
|
||||
|
||||
func TestServerClient(t *testing.T) {
|
||||
routeRegister := routing.NewRouteRegister()
|
||||
routeRegister.Get("/test", routing.Wrap(func(c *models.ReqContext) response.Response {
|
||||
routeRegister.Get("/test", routing.Wrap(func(c *contextmodel.ReqContext) response.Response {
|
||||
return response.JSON(http.StatusOK, c.SignedInUser)
|
||||
}))
|
||||
|
||||
@@ -87,7 +87,7 @@ func TestServerClient(t *testing.T) {
|
||||
|
||||
t.Run("Making a request with user 1 should return user 1 as signed in user", func(t *testing.T) {
|
||||
req := s.NewRequest(http.MethodGet, "/test", nil)
|
||||
req = RequestWithWebContext(req, &models.ReqContext{
|
||||
req = RequestWithWebContext(req, &contextmodel.ReqContext{
|
||||
SignedInUser: &user.SignedInUser{
|
||||
UserID: 1,
|
||||
},
|
||||
@@ -109,7 +109,7 @@ func TestServerClient(t *testing.T) {
|
||||
|
||||
t.Run("Making a request with user 2 should return user 2 as signed in user", func(t *testing.T) {
|
||||
req := s.NewRequest(http.MethodGet, "/test", nil)
|
||||
req = RequestWithWebContext(req, &models.ReqContext{
|
||||
req = RequestWithWebContext(req, &contextmodel.ReqContext{
|
||||
SignedInUser: &user.SignedInUser{
|
||||
UserID: 2,
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user