599 lines
25 KiB
Go
599 lines
25 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"] != nil && storedToken["tokenValue"] != "" {
|
|
t.Fatalf("did not expect stored plaintext token, 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.StatusGone {
|
|
t.Fatalf("expected reveal to be unavailable after one-time display, got %d body=%s", revealRecorder.Code, 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.StatusGone {
|
|
t.Fatalf("expected rotated token reveal to be unavailable after one-time display, got %d body=%s", revealRotatedRecorder.Code, 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 TestExternalBusinessWriteRejectsInvalidScopeCollections(t *testing.T) {
|
|
_, _, handler, connection := setupExternalBusinessWriteTest(t)
|
|
|
|
createPayload := map[string]any{
|
|
"name": "bad token",
|
|
"enabled": true,
|
|
"scopes": []map[string]any{
|
|
{
|
|
"connectionId": connection.ID,
|
|
"collections": []string{"system.profile"},
|
|
},
|
|
},
|
|
}
|
|
createBody, err := json.Marshal(createPayload)
|
|
if err != nil {
|
|
t.Fatalf("marshal invalid create payload: %v", err)
|
|
}
|
|
createRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
createRecorder,
|
|
httptest.NewRequest(http.MethodPost, "/api/external/business-db/tokens", bytes.NewReader(createBody)),
|
|
)
|
|
if createRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected invalid scope collection rejected, got status=%d body=%s", createRecorder.Code, createRecorder.Body.String())
|
|
}
|
|
if !bytes.Contains(createRecorder.Body.Bytes(), []byte("scopes[0].collections[0]")) ||
|
|
!bytes.Contains(createRecorder.Body.Bytes(), []byte("reserved system prefix")) {
|
|
t.Fatalf("expected scope collection validation issue, got %s", createRecorder.Body.String())
|
|
}
|
|
|
|
tokenID, _ := createExternalBusinessWriteToken(t, handler, connection.ID, []string{"orders"})
|
|
updatePayload := map[string]any{
|
|
"scopes": []map[string]any{
|
|
{
|
|
"connectionId": connection.ID,
|
|
"collections": []string{"orders/archive"},
|
|
},
|
|
},
|
|
}
|
|
updateBody, err := json.Marshal(updatePayload)
|
|
if err != nil {
|
|
t.Fatalf("marshal invalid update payload: %v", err)
|
|
}
|
|
updateRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
updateRecorder,
|
|
httptest.NewRequest(http.MethodPut, "/api/external/business-db/tokens/"+tokenID, bytes.NewReader(updateBody)),
|
|
)
|
|
if updateRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected invalid updated scope collection rejected, got status=%d body=%s", updateRecorder.Code, updateRecorder.Body.String())
|
|
}
|
|
if !bytes.Contains(updateRecorder.Body.Bytes(), []byte("scopes[0].collections[0]")) ||
|
|
!bytes.Contains(updateRecorder.Body.Bytes(), []byte("path separators")) {
|
|
t.Fatalf("expected updated scope collection validation issue, got %s", updateRecorder.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 TestExternalBusinessWriteRejectsInvalidTargetCollection(t *testing.T) {
|
|
_, _, handler, connection := setupExternalBusinessWriteTest(t)
|
|
_, apiKey := createExternalBusinessWriteToken(t, handler, connection.ID, []string{"orders"})
|
|
|
|
recorder := httptest.NewRecorder()
|
|
request := httptest.NewRequest(
|
|
http.MethodPost,
|
|
"/api/external/business-db/documents",
|
|
bytes.NewBufferString(fmt.Sprintf(`{"connectionId":"%s","collection":"orders/archive","document":{"orderNo":"SO-250"}}`, connection.ID)),
|
|
)
|
|
request.Header.Set("X-API-Key", apiKey)
|
|
handler.ServeHTTP(recorder, request)
|
|
if recorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected invalid target collection rejected, got status=%d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
if !bytes.Contains(recorder.Body.Bytes(), []byte("collection")) ||
|
|
!bytes.Contains(recorder.Body.Bytes(), []byte("path separators")) {
|
|
t.Fatalf("expected target collection validation issue, got %s", recorder.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 TestExternalBusinessWriteRejectsUnsafeDocumentKeys(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-399","profile":{"tier":"silver"}}}`, connection.ID)),
|
|
)
|
|
createRequest.Header.Set("X-API-Key", apiKey)
|
|
handler.ServeHTTP(createRecorder, createRequest)
|
|
if createRecorder.Code != http.StatusCreated {
|
|
t.Fatalf("seed 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 seeded document response: %v", err)
|
|
}
|
|
documentID, _ := createResponse.Document["_id"].(string)
|
|
if documentID == "" {
|
|
t.Fatalf("expected seeded document id, got %#v", createResponse.Document)
|
|
}
|
|
|
|
cases := []struct {
|
|
name string
|
|
method string
|
|
path string
|
|
body string
|
|
bodyContains string
|
|
}{
|
|
{
|
|
name: "create nested object operator",
|
|
method: http.MethodPost,
|
|
path: "/api/external/business-db/documents",
|
|
body: fmt.Sprintf(`{"connectionId":"%s","collection":"orders","document":{"profile":{"$where":"return true"}}}`, connection.ID),
|
|
bodyContains: "document.profile.$where",
|
|
},
|
|
{
|
|
name: "create array item operator",
|
|
method: http.MethodPost,
|
|
path: "/api/external/business-db/documents",
|
|
body: fmt.Sprintf(`{"connectionId":"%s","collection":"orders","document":{"items":[{"$set":{"status":"done"}}]}}`, connection.ID),
|
|
bodyContains: "document.items[0].$set",
|
|
},
|
|
{
|
|
name: "create nested nul key",
|
|
method: http.MethodPost,
|
|
path: "/api/external/business-db/documents",
|
|
body: fmt.Sprintf(`{"connectionId":"%s","collection":"orders","document":{"profile":{"bad\u0000key":"blocked"}}}`, connection.ID),
|
|
bodyContains: "NUL bytes",
|
|
},
|
|
{
|
|
name: "patch nested object operator",
|
|
method: http.MethodPatch,
|
|
path: "/api/external/business-db/documents/" + documentID,
|
|
body: fmt.Sprintf(`{"connectionId":"%s","collection":"orders","patch":{"profile":{"$inc":{"score":1}}}}`, connection.ID),
|
|
bodyContains: "patch.profile.$inc",
|
|
},
|
|
{
|
|
name: "patch nested nul key",
|
|
method: http.MethodPatch,
|
|
path: "/api/external/business-db/documents/" + documentID,
|
|
body: fmt.Sprintf(`{"connectionId":"%s","collection":"orders","patch":{"profile":{"bad\u0000key":"blocked"}}}`, connection.ID),
|
|
bodyContains: "NUL bytes",
|
|
},
|
|
{
|
|
name: "upsert match nested operator",
|
|
method: http.MethodPost,
|
|
path: "/api/external/business-db/upserts",
|
|
body: fmt.Sprintf(`{"connectionId":"%s","collection":"orders","match":{"profile":{"$ne":"blocked"}},"patch":{"status":"done"}}`, connection.ID),
|
|
bodyContains: "match.profile.$ne",
|
|
},
|
|
{
|
|
name: "upsert match nul key",
|
|
method: http.MethodPost,
|
|
path: "/api/external/business-db/upserts",
|
|
body: fmt.Sprintf(`{"connectionId":"%s","collection":"orders","match":{"profile":{"bad\u0000key":"blocked"}},"patch":{"status":"done"}}`, connection.ID),
|
|
bodyContains: "NUL bytes",
|
|
},
|
|
{
|
|
name: "upsert setOnInsert nested operator",
|
|
method: http.MethodPost,
|
|
path: "/api/external/business-db/upserts",
|
|
body: fmt.Sprintf(`{"connectionId":"%s","collection":"orders","match":{"externalId":"EXT-2"},"patch":{"status":"new"},"setOnInsert":{"audit":{"$set":{"source":"partner"}}}}`, connection.ID),
|
|
bodyContains: "setOnInsert.audit.$set",
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
recorder := httptest.NewRecorder()
|
|
request := httptest.NewRequest(tc.method, tc.path, bytes.NewBufferString(tc.body))
|
|
request.Header.Set("X-API-Key", apiKey)
|
|
handler.ServeHTTP(recorder, request)
|
|
if recorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected unsafe payload rejected, got status=%d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
if !bytes.Contains(recorder.Body.Bytes(), []byte(tc.bodyContains)) {
|
|
t.Fatalf("expected validation response to mention %q, body=%s", tc.bodyContains, recorder.Body.String())
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|