chore: 初始化仓库,导入项目基线代码
Academy v0.8.3 完整代码基线:Go 后端(internal/cmd/cli/mcp-server/docreader)、 Vue 前端(frontend)、数据库迁移(migrations)、部署编排(docker/helm/deploy)与文档。 今日开发的培训推送管理改动随后单独提交。
This commit is contained in:
commit
7ec21f35b0
5767 files changed
+1436788
No files matched your search
@@ -0,0 +1,103 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"academy/internal/types"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// storedMCPServices answers reads with a fixed stored service.
|
||||
type storedMCPServices struct {
|
||||
updateCapturingMCPServices
|
||||
stored *types.MCPService
|
||||
}
|
||||
|
||||
func (s *storedMCPServices) GetMCPServiceByID(context.Context, uint64, string) (*types.MCPService, error) {
|
||||
return s.stored, nil
|
||||
}
|
||||
|
||||
const storedMCPURL = "https://mcp.example.com/sse"
|
||||
|
||||
func storedMCPService(auth types.MCPAuthType) *storedMCPServices {
|
||||
url := storedMCPURL
|
||||
return &storedMCPServices{stored: &types.MCPService{
|
||||
ID: "svc-1", TransportType: types.MCPTransportSSE, URL: &url,
|
||||
AuthConfig: &types.MCPAuthConfig{AuthType: auth, APIKey: "stored-key"},
|
||||
}}
|
||||
}
|
||||
|
||||
func putMCPServiceAs(t *testing.T, assistant bool, svc *storedMCPServices, body string) []string {
|
||||
t.Helper()
|
||||
gin.SetMode(gin.TestMode)
|
||||
h := &MCPServiceHandler{mcpServiceService: svc}
|
||||
c, _ := gin.CreateTestContext(httptest.NewRecorder())
|
||||
ctx := context.Background()
|
||||
if assistant {
|
||||
ctx = types.WithAuditAgentAction(ctx, types.AuditAgentAction{SessionID: "s1", Operation: "mcp_service.update"})
|
||||
}
|
||||
c.Request = httptest.NewRequestWithContext(ctx, http.MethodPut, "/mcp-services/svc-1", strings.NewReader(body))
|
||||
c.Request.Header.Set("Content-Type", "application/json")
|
||||
c.Params = gin.Params{{Key: "id", Value: "svc-1"}}
|
||||
c.Set(types.TenantIDContextKey.String(), uint64(7))
|
||||
h.UpdateMCPService(c)
|
||||
return c.Errors.Errors()
|
||||
}
|
||||
|
||||
func guardMove(auth types.MCPAuthType, update types.MCPService) (map[string]bool, error) {
|
||||
svc := storedMCPService(auth)
|
||||
fields := map[string]bool{}
|
||||
update.ID = "svc-1"
|
||||
err := (&MCPServiceHandler{mcpServiceService: svc}).guardAssistantEndpointMove(
|
||||
context.Background(), 7, &update, fields)
|
||||
return fields, err
|
||||
}
|
||||
|
||||
func TestAssistantMovingAnMCPServiceDropsItsSecrets(t *testing.T) {
|
||||
moved := "https://other.example.com/sse"
|
||||
fields, err := guardMove(types.MCPAuthAPIKey, types.MCPService{URL: &moved})
|
||||
require.NoError(t, err)
|
||||
assert.True(t, fields[types.MCPUpdateDropSecrets])
|
||||
|
||||
fields, err = guardMove(types.MCPAuthBearer, types.MCPService{TransportType: types.MCPTransportHTTPStreamable})
|
||||
require.NoError(t, err)
|
||||
assert.True(t, fields[types.MCPUpdateDropSecrets])
|
||||
|
||||
svc := storedMCPService(types.MCPAuthAPIKey)
|
||||
require.Empty(t, putMCPServiceAs(t, true, svc, `{"transport_type":"http-streamable"}`))
|
||||
assert.True(t, svc.fields[types.MCPUpdateDropSecrets], "the handler applies the guard")
|
||||
}
|
||||
|
||||
func TestAssistantKeepsSecretsWhenTheAddressStays(t *testing.T) {
|
||||
same := storedMCPURL
|
||||
fields, err := guardMove(types.MCPAuthAPIKey, types.MCPService{URL: &same, TransportType: types.MCPTransportSSE})
|
||||
require.NoError(t, err)
|
||||
assert.False(t, fields[types.MCPUpdateDropSecrets])
|
||||
|
||||
svc := storedMCPService(types.MCPAuthAPIKey)
|
||||
require.Empty(t, putMCPServiceAs(t, true, svc, `{"name":"Docs","enabled":false}`))
|
||||
assert.False(t, svc.fields[types.MCPUpdateDropSecrets])
|
||||
}
|
||||
|
||||
func TestAssistantCannotMoveAnOAuthMCPService(t *testing.T) {
|
||||
moved := "https://other.example.com/sse"
|
||||
_, err := guardMove(types.MCPAuthOAuth, types.MCPService{URL: &moved})
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "OAuth")
|
||||
|
||||
svc := storedMCPService(types.MCPAuthOAuth)
|
||||
require.NotEmpty(t, putMCPServiceAs(t, true, svc, `{"transport_type":"http-streamable"}`))
|
||||
assert.Nil(t, svc.service, "nothing was updated")
|
||||
}
|
||||
|
||||
func TestUserMovingAnMCPServiceKeepsItsSecrets(t *testing.T) {
|
||||
svc := storedMCPService(types.MCPAuthOAuth)
|
||||
require.Empty(t, putMCPServiceAs(t, false, svc, `{"transport_type":"http-streamable"}`))
|
||||
assert.False(t, svc.fields[types.MCPUpdateDropSecrets])
|
||||
}
|
||||
Reference in new issue
Block a user