Include the accumulated workflow scheduling, run history, subtree rerun, operator UI, deployment, data asset, object storage, database browser, and documentation changes currently in the worktree.
417 lines
17 KiB
Go
417 lines
17 KiB
Go
package api
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/store"
|
|
)
|
|
|
|
type externalWriteTokenCreateResponse struct {
|
|
Token map[string]any `json:"token"`
|
|
APIKey string `json:"apiKey"`
|
|
}
|
|
|
|
type externalWriteDocumentResponse struct {
|
|
ConnectionID string `json:"connectionId"`
|
|
Collection string `json:"collection"`
|
|
Operation string `json:"operation"`
|
|
Document map[string]any `json:"document"`
|
|
}
|
|
|
|
func setupExternalBusinessWriteTest(t *testing.T) (*store.MemoryStore, *store.MemoryStore, http.Handler, *domain.MongoConnectionRecord) {
|
|
t.Helper()
|
|
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
connection, err := dataStore.CreateMongoConnection(ctx, domain.CreateMongoConnectionInput{
|
|
Name: "第三方业务库",
|
|
URI: "mongodb://partner-write.example:27017",
|
|
Database: "partner-business",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create mongo connection: %v", err)
|
|
}
|
|
|
|
businessStore := store.NewMemoryStore()
|
|
originalResolver := mongoConnectionStoreResolver
|
|
mongoConnectionStoreResolver = func(_ context.Context, item *domain.MongoConnectionRecord) (databaseBrowserStore, func(), error) {
|
|
if item == nil || item.ID != connection.ID {
|
|
t.Fatalf("unexpected mongo connection: %#v", item)
|
|
}
|
|
return businessStore, func() {}, nil
|
|
}
|
|
t.Cleanup(func() {
|
|
mongoConnectionStoreResolver = originalResolver
|
|
})
|
|
|
|
return dataStore, businessStore, NewHandler(dataStore, nil), connection
|
|
}
|
|
|
|
func createExternalBusinessWriteToken(
|
|
t *testing.T,
|
|
handler http.Handler,
|
|
connectionID string,
|
|
collections []string,
|
|
) (string, string) {
|
|
t.Helper()
|
|
|
|
payload := map[string]any{
|
|
"name": "第三方写入方",
|
|
"description": "测试 token",
|
|
"enabled": true,
|
|
"scopes": []map[string]any{
|
|
{
|
|
"connectionId": connectionID,
|
|
"collections": collections,
|
|
},
|
|
},
|
|
}
|
|
body, err := json.Marshal(payload)
|
|
if err != nil {
|
|
t.Fatalf("marshal token create payload: %v", err)
|
|
}
|
|
|
|
recorder := httptest.NewRecorder()
|
|
request := httptest.NewRequest(http.MethodPost, "/api/external/business-db/tokens", bytes.NewReader(body))
|
|
handler.ServeHTTP(recorder, request)
|
|
if recorder.Code != http.StatusCreated {
|
|
t.Fatalf("create external write token status: %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
|
|
var response externalWriteTokenCreateResponse
|
|
if err := json.Unmarshal(recorder.Body.Bytes(), &response); err != nil {
|
|
t.Fatalf("decode token create response: %v", err)
|
|
}
|
|
if response.APIKey == "" {
|
|
t.Fatalf("expected one-time api key in response: %s", recorder.Body.String())
|
|
}
|
|
tokenID, _ := response.Token["id"].(string)
|
|
if tokenID == "" {
|
|
t.Fatalf("expected token id in response: %s", recorder.Body.String())
|
|
}
|
|
return tokenID, response.APIKey
|
|
}
|
|
|
|
func TestExternalBusinessWriteTokenCRUD(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore, businessStore, handler, connection := setupExternalBusinessWriteTest(t)
|
|
|
|
if _, err := businessStore.CreateDatabaseCollection(ctx, "orders"); err != nil {
|
|
t.Fatalf("create business collection: %v", err)
|
|
}
|
|
|
|
tokenID, apiKey := createExternalBusinessWriteToken(t, handler, connection.ID, []string{"orders"})
|
|
|
|
settings, err := dataStore.GetSettings(ctx)
|
|
if err != nil {
|
|
t.Fatalf("get settings: %v", err)
|
|
}
|
|
serialized, err := json.Marshal(settings)
|
|
if err != nil {
|
|
t.Fatalf("marshal settings: %v", err)
|
|
}
|
|
settingsDocument := map[string]any{}
|
|
if err := json.Unmarshal(serialized, &settingsDocument); err != nil {
|
|
t.Fatalf("decode settings document: %v", err)
|
|
}
|
|
externalConfig, ok := settingsDocument["externalBusinessWrite"].(map[string]any)
|
|
if !ok {
|
|
t.Fatalf("expected externalBusinessWrite in settings, got %#v", settingsDocument)
|
|
}
|
|
tokens, ok := externalConfig["tokens"].([]any)
|
|
if !ok || len(tokens) != 1 {
|
|
t.Fatalf("expected one stored token, got %#v", externalConfig["tokens"])
|
|
}
|
|
storedToken, ok := tokens[0].(map[string]any)
|
|
if !ok {
|
|
t.Fatalf("expected token document, got %#v", tokens[0])
|
|
}
|
|
if storedToken["tokenHash"] == "" {
|
|
t.Fatalf("expected stored token hash, got %#v", storedToken)
|
|
}
|
|
if storedToken["tokenValue"] != apiKey {
|
|
t.Fatalf("expected stored plaintext token for explicit reveal, got %#v", storedToken["tokenValue"])
|
|
}
|
|
if storedToken["apiKey"] != nil {
|
|
t.Fatalf("did not expect plaintext apiKey in stored settings: %#v", storedToken)
|
|
}
|
|
|
|
listRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(listRecorder, httptest.NewRequest(http.MethodGet, "/api/external/business-db/tokens", nil))
|
|
if listRecorder.Code != http.StatusOK {
|
|
t.Fatalf("list external write tokens status: %d body=%s", listRecorder.Code, listRecorder.Body.String())
|
|
}
|
|
if bytes.Contains(listRecorder.Body.Bytes(), []byte(apiKey)) {
|
|
t.Fatalf("list response must not expose plaintext api keys: %s", listRecorder.Body.String())
|
|
}
|
|
if bytes.Contains(listRecorder.Body.Bytes(), []byte("tokenValue")) {
|
|
t.Fatalf("list response must not expose tokenValue field: %s", listRecorder.Body.String())
|
|
}
|
|
|
|
revealRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(revealRecorder, httptest.NewRequest(http.MethodGet, "/api/external/business-db/tokens/"+tokenID+"/reveal", nil))
|
|
if revealRecorder.Code != http.StatusOK {
|
|
t.Fatalf("reveal external write token status: %d body=%s", revealRecorder.Code, revealRecorder.Body.String())
|
|
}
|
|
if !bytes.Contains(revealRecorder.Body.Bytes(), []byte(apiKey)) {
|
|
t.Fatalf("expected reveal response to contain plaintext api key, got %s", revealRecorder.Body.String())
|
|
}
|
|
|
|
rotateRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
rotateRecorder,
|
|
httptest.NewRequest(http.MethodPost, "/api/external/business-db/tokens/"+tokenID+"/rotate", nil),
|
|
)
|
|
if rotateRecorder.Code != http.StatusOK {
|
|
t.Fatalf("rotate external write token status: %d body=%s", rotateRecorder.Code, rotateRecorder.Body.String())
|
|
}
|
|
var rotateResponse externalWriteTokenCreateResponse
|
|
if err := json.Unmarshal(rotateRecorder.Body.Bytes(), &rotateResponse); err != nil {
|
|
t.Fatalf("decode rotate response: %v", err)
|
|
}
|
|
if rotateResponse.APIKey == "" || rotateResponse.APIKey == apiKey {
|
|
t.Fatalf("expected a new api key on rotation: old=%q new=%q", apiKey, rotateResponse.APIKey)
|
|
}
|
|
|
|
revealRotatedRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(revealRotatedRecorder, httptest.NewRequest(http.MethodGet, "/api/external/business-db/tokens/"+tokenID+"/reveal", nil))
|
|
if revealRotatedRecorder.Code != http.StatusOK {
|
|
t.Fatalf("reveal rotated external write token status: %d body=%s", revealRotatedRecorder.Code, revealRotatedRecorder.Body.String())
|
|
}
|
|
if !bytes.Contains(revealRotatedRecorder.Body.Bytes(), []byte(rotateResponse.APIKey)) {
|
|
t.Fatalf("expected reveal response to contain rotated api key, got %s", revealRotatedRecorder.Body.String())
|
|
}
|
|
|
|
createPayload := fmt.Sprintf(`{"connectionId":"%s","collection":"orders","document":{"orderNo":"SO-100","status":"new"}}`, connection.ID)
|
|
|
|
oldKeyRecorder := httptest.NewRecorder()
|
|
oldKeyRequest := httptest.NewRequest(http.MethodPost, "/api/external/business-db/documents", bytes.NewBufferString(createPayload))
|
|
oldKeyRequest.Header.Set("X-API-Key", apiKey)
|
|
handler.ServeHTTP(oldKeyRecorder, oldKeyRequest)
|
|
if oldKeyRecorder.Code != http.StatusUnauthorized {
|
|
t.Fatalf("expected old api key to be invalid after rotation, got status=%d body=%s", oldKeyRecorder.Code, oldKeyRecorder.Body.String())
|
|
}
|
|
|
|
newKeyRecorder := httptest.NewRecorder()
|
|
newKeyRequest := httptest.NewRequest(http.MethodPost, "/api/external/business-db/documents", bytes.NewBufferString(createPayload))
|
|
newKeyRequest.Header.Set("X-API-Key", rotateResponse.APIKey)
|
|
handler.ServeHTTP(newKeyRecorder, newKeyRequest)
|
|
if newKeyRecorder.Code != http.StatusCreated {
|
|
t.Fatalf("expected rotated api key to work, got status=%d body=%s", newKeyRecorder.Code, newKeyRecorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestExternalBusinessWriteAuthAndCollectionScope(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore, businessStore, handler, connection := setupExternalBusinessWriteTest(t)
|
|
|
|
if _, err := businessStore.CreateDatabaseCollection(ctx, "orders"); err != nil {
|
|
t.Fatalf("create orders collection: %v", err)
|
|
}
|
|
if _, err := businessStore.CreateDatabaseCollection(ctx, "customers"); err != nil {
|
|
t.Fatalf("create customers collection: %v", err)
|
|
}
|
|
tokenID, apiKey := createExternalBusinessWriteToken(t, handler, connection.ID, []string{"orders"})
|
|
|
|
createPayload := fmt.Sprintf(`{"connectionId":"%s","collection":"orders","document":{"orderNo":"SO-200","status":"new"}}`, connection.ID)
|
|
|
|
missingKeyRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(missingKeyRecorder, httptest.NewRequest(http.MethodPost, "/api/external/business-db/documents", bytes.NewBufferString(createPayload)))
|
|
if missingKeyRecorder.Code != http.StatusUnauthorized {
|
|
t.Fatalf("expected missing key to be rejected, got status=%d body=%s", missingKeyRecorder.Code, missingKeyRecorder.Body.String())
|
|
}
|
|
|
|
invalidKeyRecorder := httptest.NewRecorder()
|
|
invalidKeyRequest := httptest.NewRequest(http.MethodPost, "/api/external/business-db/documents", bytes.NewBufferString(createPayload))
|
|
invalidKeyRequest.Header.Set("X-API-Key", "invalid-token")
|
|
handler.ServeHTTP(invalidKeyRecorder, invalidKeyRequest)
|
|
if invalidKeyRecorder.Code != http.StatusUnauthorized {
|
|
t.Fatalf("expected invalid key to be rejected, got status=%d body=%s", invalidKeyRecorder.Code, invalidKeyRecorder.Body.String())
|
|
}
|
|
|
|
otherConnection, err := dataStore.CreateMongoConnection(ctx, domain.CreateMongoConnectionInput{
|
|
Name: "未授权业务库",
|
|
URI: "mongodb://other-business.example:27017",
|
|
Database: "other-business",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create second mongo connection: %v", err)
|
|
}
|
|
|
|
unauthorizedConnectionRecorder := httptest.NewRecorder()
|
|
unauthorizedConnectionRequest := httptest.NewRequest(
|
|
http.MethodPost,
|
|
"/api/external/business-db/documents",
|
|
bytes.NewBufferString(fmt.Sprintf(`{"connectionId":"%s","collection":"orders","document":{"orderNo":"SO-201"}}`, otherConnection.ID)),
|
|
)
|
|
unauthorizedConnectionRequest.Header.Set("X-API-Key", apiKey)
|
|
handler.ServeHTTP(unauthorizedConnectionRecorder, unauthorizedConnectionRequest)
|
|
if unauthorizedConnectionRecorder.Code != http.StatusForbidden {
|
|
t.Fatalf("expected unauthorized connection to be rejected, got status=%d body=%s", unauthorizedConnectionRecorder.Code, unauthorizedConnectionRecorder.Body.String())
|
|
}
|
|
|
|
unauthorizedCollectionRecorder := httptest.NewRecorder()
|
|
unauthorizedCollectionRequest := httptest.NewRequest(
|
|
http.MethodPost,
|
|
"/api/external/business-db/documents",
|
|
bytes.NewBufferString(fmt.Sprintf(`{"connectionId":"%s","collection":"customers","document":{"customerNo":"C-1"}}`, connection.ID)),
|
|
)
|
|
unauthorizedCollectionRequest.Header.Set("X-API-Key", apiKey)
|
|
handler.ServeHTTP(unauthorizedCollectionRecorder, unauthorizedCollectionRequest)
|
|
if unauthorizedCollectionRecorder.Code != http.StatusForbidden {
|
|
t.Fatalf("expected unauthorized collection to be rejected, got status=%d body=%s", unauthorizedCollectionRecorder.Code, unauthorizedCollectionRecorder.Body.String())
|
|
}
|
|
|
|
disableRecorder := httptest.NewRecorder()
|
|
disableRequest := httptest.NewRequest(
|
|
http.MethodPut,
|
|
"/api/external/business-db/tokens/"+tokenID,
|
|
bytes.NewBufferString(`{"enabled":false}`),
|
|
)
|
|
handler.ServeHTTP(disableRecorder, disableRequest)
|
|
if disableRecorder.Code != http.StatusOK {
|
|
t.Fatalf("disable token status: %d body=%s", disableRecorder.Code, disableRecorder.Body.String())
|
|
}
|
|
|
|
disabledRecorder := httptest.NewRecorder()
|
|
disabledRequest := httptest.NewRequest(http.MethodPost, "/api/external/business-db/documents", bytes.NewBufferString(createPayload))
|
|
disabledRequest.Header.Set("X-API-Key", apiKey)
|
|
handler.ServeHTTP(disabledRecorder, disabledRequest)
|
|
if disabledRecorder.Code != http.StatusUnauthorized {
|
|
t.Fatalf("expected disabled token to be rejected, got status=%d body=%s", disabledRecorder.Code, disabledRecorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestExternalBusinessWriteCreateAndPatchByID(t *testing.T) {
|
|
ctx := context.Background()
|
|
_, businessStore, handler, connection := setupExternalBusinessWriteTest(t)
|
|
|
|
if _, err := businessStore.CreateDatabaseCollection(ctx, "orders"); err != nil {
|
|
t.Fatalf("create business collection: %v", err)
|
|
}
|
|
_, apiKey := createExternalBusinessWriteToken(t, handler, connection.ID, []string{"orders"})
|
|
|
|
createRecorder := httptest.NewRecorder()
|
|
createRequest := httptest.NewRequest(
|
|
http.MethodPost,
|
|
"/api/external/business-db/documents",
|
|
bytes.NewBufferString(fmt.Sprintf(`{"connectionId":"%s","collection":"orders","document":{"orderNo":"SO-300","status":"new","amount":10}}`, connection.ID)),
|
|
)
|
|
createRequest.Header.Set("X-API-Key", apiKey)
|
|
handler.ServeHTTP(createRecorder, createRequest)
|
|
if createRecorder.Code != http.StatusCreated {
|
|
t.Fatalf("create external document status: %d body=%s", createRecorder.Code, createRecorder.Body.String())
|
|
}
|
|
|
|
var createResponse externalWriteDocumentResponse
|
|
if err := json.Unmarshal(createRecorder.Body.Bytes(), &createResponse); err != nil {
|
|
t.Fatalf("decode create response: %v", err)
|
|
}
|
|
documentID, _ := createResponse.Document["_id"].(string)
|
|
if documentID == "" {
|
|
t.Fatalf("expected created document id, got %#v", createResponse.Document)
|
|
}
|
|
if createResponse.Operation != "created" {
|
|
t.Fatalf("expected create operation, got %#v", createResponse)
|
|
}
|
|
|
|
patchRecorder := httptest.NewRecorder()
|
|
patchRequest := httptest.NewRequest(
|
|
http.MethodPatch,
|
|
"/api/external/business-db/documents/"+documentID,
|
|
bytes.NewBufferString(fmt.Sprintf(`{"connectionId":"%s","collection":"orders","patch":{"status":"done","amount":18}}`, connection.ID)),
|
|
)
|
|
patchRequest.Header.Set("X-API-Key", apiKey)
|
|
handler.ServeHTTP(patchRecorder, patchRequest)
|
|
if patchRecorder.Code != http.StatusOK {
|
|
t.Fatalf("patch external document status: %d body=%s", patchRecorder.Code, patchRecorder.Body.String())
|
|
}
|
|
|
|
var patchResponse externalWriteDocumentResponse
|
|
if err := json.Unmarshal(patchRecorder.Body.Bytes(), &patchResponse); err != nil {
|
|
t.Fatalf("decode patch response: %v", err)
|
|
}
|
|
if patchResponse.Operation != "updated" {
|
|
t.Fatalf("expected update operation, got %#v", patchResponse)
|
|
}
|
|
if patchResponse.Document["_id"] != documentID {
|
|
t.Fatalf("expected _id to stay stable, got %#v", patchResponse.Document["_id"])
|
|
}
|
|
if patchResponse.Document["status"] != "done" {
|
|
t.Fatalf("expected patched status, got %#v", patchResponse.Document)
|
|
}
|
|
}
|
|
|
|
func TestExternalBusinessWriteUpsertByBusinessKey(t *testing.T) {
|
|
ctx := context.Background()
|
|
_, businessStore, handler, connection := setupExternalBusinessWriteTest(t)
|
|
|
|
if _, err := businessStore.CreateDatabaseCollection(ctx, "orders"); err != nil {
|
|
t.Fatalf("create business collection: %v", err)
|
|
}
|
|
_, apiKey := createExternalBusinessWriteToken(t, handler, connection.ID, []string{"orders"})
|
|
|
|
createRecorder := httptest.NewRecorder()
|
|
createRequest := httptest.NewRequest(
|
|
http.MethodPost,
|
|
"/api/external/business-db/upserts",
|
|
bytes.NewBufferString(fmt.Sprintf(`{"connectionId":"%s","collection":"orders","match":{"externalId":"EXT-1"},"patch":{"status":"new","amount":20},"setOnInsert":{"source":"partner-a"}}`, connection.ID)),
|
|
)
|
|
createRequest.Header.Set("X-API-Key", apiKey)
|
|
handler.ServeHTTP(createRecorder, createRequest)
|
|
if createRecorder.Code != http.StatusOK {
|
|
t.Fatalf("create upsert status: %d body=%s", createRecorder.Code, createRecorder.Body.String())
|
|
}
|
|
|
|
var createResponse externalWriteDocumentResponse
|
|
if err := json.Unmarshal(createRecorder.Body.Bytes(), &createResponse); err != nil {
|
|
t.Fatalf("decode create upsert response: %v", err)
|
|
}
|
|
if createResponse.Operation != "created" {
|
|
t.Fatalf("expected created operation from upsert create path, got %#v", createResponse)
|
|
}
|
|
documentID, _ := createResponse.Document["_id"].(string)
|
|
if documentID == "" {
|
|
t.Fatalf("expected upserted document id, got %#v", createResponse.Document)
|
|
}
|
|
if createResponse.Document["source"] != "partner-a" {
|
|
t.Fatalf("expected setOnInsert field, got %#v", createResponse.Document)
|
|
}
|
|
|
|
updateRecorder := httptest.NewRecorder()
|
|
updateRequest := httptest.NewRequest(
|
|
http.MethodPost,
|
|
"/api/external/business-db/upserts",
|
|
bytes.NewBufferString(fmt.Sprintf(`{"connectionId":"%s","collection":"orders","match":{"externalId":"EXT-1"},"patch":{"status":"done","amount":25},"setOnInsert":{"source":"partner-b"}}`, connection.ID)),
|
|
)
|
|
updateRequest.Header.Set("X-API-Key", apiKey)
|
|
handler.ServeHTTP(updateRecorder, updateRequest)
|
|
if updateRecorder.Code != http.StatusOK {
|
|
t.Fatalf("update upsert status: %d body=%s", updateRecorder.Code, updateRecorder.Body.String())
|
|
}
|
|
|
|
var updateResponse externalWriteDocumentResponse
|
|
if err := json.Unmarshal(updateRecorder.Body.Bytes(), &updateResponse); err != nil {
|
|
t.Fatalf("decode update upsert response: %v", err)
|
|
}
|
|
if updateResponse.Operation != "updated" {
|
|
t.Fatalf("expected updated operation from upsert update path, got %#v", updateResponse)
|
|
}
|
|
if updateResponse.Document["_id"] != documentID {
|
|
t.Fatalf("expected same document to be updated, got %#v", updateResponse.Document)
|
|
}
|
|
if updateResponse.Document["status"] != "done" || updateResponse.Document["amount"] != float64(25) {
|
|
t.Fatalf("expected updated fields in upsert response, got %#v", updateResponse.Document)
|
|
}
|
|
if updateResponse.Document["source"] != "partner-a" {
|
|
t.Fatalf("expected setOnInsert fields to be preserved on update, got %#v", updateResponse.Document)
|
|
}
|
|
}
|