MCPcopy Create free account
hub / github.com/bytebase/bytebase / TestMCPAuthMiddlewareValidToken

Function TestMCPAuthMiddlewareValidToken

backend/api/mcp/server_test.go:111–137  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

109}
110
111func TestMCPAuthMiddlewareValidToken(t *testing.T) {
112 secret := "test-secret-key"
113 profile := &config.Profile{Mode: common.ReleaseModeDev}
114
115 e := echo.New()
116 req := httptest.NewRequest(http.MethodPost, "/mcp", strings.NewReader(`{}`))
117 req.Header.Set("Content-Type", "application/json")
118 req.Header.Set("Authorization", "Bearer "+generateValidToken(t, secret))
119 rec := httptest.NewRecorder()
120 c := e.NewContext(req, rec)
121
122 // Create server with auth - note: we pass nil store since we're testing middleware only
123 // A full integration test would require a real store
124 s, err := NewServer(nil, profile, secret)
125 require.NoError(t, err)
126 handler := s.authMiddleware(func(c *echo.Context) error {
127 // Verify access token is set in request context
128 ctx := c.Request().Context()
129 token := getAccessToken(ctx)
130 require.NotEmpty(t, token)
131 return c.String(http.StatusOK, "success")
132 })
133
134 err = handler(c)
135 require.NoError(t, err)
136 require.Equal(t, http.StatusOK, rec.Code)
137}
138
139// TestMCPProxiedPublicHostNotRejected is the BYT-9693 regression. Behind a
140// same-host reverse proxy (proxy_pass http://127.0.0.1:8080), the connection

Callers

nothing calls this directly

Calls 8

authMiddlewareMethod · 0.95
generateValidTokenFunction · 0.85
getAccessTokenFunction · 0.85
NewServerFunction · 0.70
StringMethod · 0.65
EqualMethod · 0.65
handlerFunction · 0.50
SetMethod · 0.45

Tested by

no test coverage detected