- 迁移 043 一次到位:runtime_instance/environment_binding 并入 browser_env(账号即环境)、 credential_reference 并入 social_account、work_source/metric_plan 并入 creator_work、 sync 租约并入 creator_collection_checkpoint(kind='sync')、audit_event 瘦列、 creator_settings 去转写列、TRUNCATE 账号域(竞品采集数据无损保留) - 统一迁移 runner:environment.Open 串起 001 账号基座 + hub 链 + 038 creator 版本回填 (v+1000) + creator 1017-1042 + 043;account/creator Open 不再迁移 - 三域 store SQL 全量适配:CreateBoundEnv 就绪门禁、SetRuntimeCleanupPending fence 语义(未知代 MissingRuntimeID 允许登记并释放活跃实例)、metric_plan 列并入 work、 checkpoint sync 租约、deletion 链路重写 - api 适配:envView 去 runtime_instance_id、audit filter 去任务列、browser_env 单表查询 - 测试:迁移 043 形态探针、迁移不可变映射新路径、legacy 迁移重放子测试重写、 fence/出口生命周期/单来源 upsert 语义覆盖;CREATORHUB_POSTGRES_TEST_URL 全绿, environment 70%/creator 67.5% 覆盖率,tsc 通过
115 lines
5.1 KiB
Go
115 lines
5.1 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
hub "git.ipao.vip/rogee/creator-hub/internal/environment"
|
|
douyin "git.ipao.vip/rogee/creator-hub/internal/platform/douyin"
|
|
)
|
|
|
|
const testDouyinIdentityURL = "https://www.douyin.com/aweme/v1/web/user/profile/self/?aid=6383&device_platform=webapp"
|
|
|
|
func TestDouyinGatewayBrowserFencesAccountGeneration(t *testing.T) {
|
|
requests := 0
|
|
server := httptest.NewServer(http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
|
|
requests++
|
|
if request.Header.Get("Authorization") != "Bearer gateway-token-1" {
|
|
t.Fatalf("missing gateway authorization")
|
|
}
|
|
var body map[string]any
|
|
if json.NewDecoder(request.Body).Decode(&body) != nil || body["binding_version"] != float64(2) ||
|
|
body["runtime_id"] != "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" || body["network_id"] != "native-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" || body["network_exit_id"] != "exit-a" {
|
|
t.Fatalf("generation fence missing: %#v", body)
|
|
}
|
|
switch request.URL.Path {
|
|
case "/v1/browsers/account-a/douyin/get":
|
|
if body["url"] != testDouyinIdentityURL {
|
|
t.Fatalf("unexpected URL: %#v", body)
|
|
}
|
|
_ = json.NewEncoder(response).Encode(map[string]any{"status": 412, "body": `{"captcha":true}`, "challenge": "captcha"})
|
|
default:
|
|
response.WriteHeader(http.StatusNotFound)
|
|
}
|
|
}))
|
|
defer server.Close()
|
|
browser := douyinGatewayBrowser{gateway: hub.Gateway{Endpoint: server.URL, Token: "gateway-token-1"}, environment: readyDouyinEnvironment()}
|
|
result, err := browser.Get(context.Background(), testDouyinIdentityURL)
|
|
if err != nil || result.Status != 412 || result.Challenge != douyin.ChallengeCaptcha || requests != 1 {
|
|
t.Fatalf("unexpected result: %#v requests=%d err=%v", result, requests, err)
|
|
}
|
|
}
|
|
|
|
func TestDouyinGatewayBrowserCapturesLoginScreen(t *testing.T) {
|
|
encoded := strings.Repeat("cG5n", 300_000)
|
|
server := httptest.NewServer(http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
|
|
if request.URL.Path != "/v1/browsers/account-a/douyin/login-qr" {
|
|
t.Fatalf("unexpected path: %s", request.URL.Path)
|
|
}
|
|
if request.Header.Get("Authorization") != "Bearer gateway-token-1" {
|
|
t.Fatalf("missing gateway authorization")
|
|
}
|
|
_ = json.NewEncoder(response).Encode(map[string]any{
|
|
"content_type": "image/png",
|
|
"body_base64": encoded,
|
|
"qr_detected": true,
|
|
})
|
|
}))
|
|
defer server.Close()
|
|
browser := douyinGatewayBrowser{gateway: hub.Gateway{Endpoint: server.URL, Token: "gateway-token-1"}, environment: readyDouyinEnvironment()}
|
|
result, err := browser.LoginQR(context.Background())
|
|
if err != nil || result.ContentType != "image/png" || result.BodyBase64 != encoded || !result.QRDetected {
|
|
t.Fatalf("unexpected login screen: %#v err=%v", result, err)
|
|
}
|
|
}
|
|
|
|
func TestDouyinGatewayBrowserRejectsInvalidLoginScreen(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(response http.ResponseWriter, _ *http.Request) {
|
|
_ = json.NewEncoder(response).Encode(map[string]any{
|
|
"content_type": "image/png",
|
|
"body_base64": "not-base64",
|
|
})
|
|
}))
|
|
defer server.Close()
|
|
browser := douyinGatewayBrowser{gateway: hub.Gateway{Endpoint: server.URL, Token: "gateway-token-1"}, environment: readyDouyinEnvironment()}
|
|
if _, err := browser.LoginQR(context.Background()); err == nil {
|
|
t.Fatal("invalid login screen was accepted")
|
|
}
|
|
}
|
|
|
|
func TestDouyinGatewayBrowserFailsClosedWithoutReadyBinding(t *testing.T) {
|
|
requests := 0
|
|
server := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) { requests++ }))
|
|
defer server.Close()
|
|
environment := readyDouyinEnvironment()
|
|
environment.Exit.HealthStatus = "unhealthy"
|
|
browser := douyinGatewayBrowser{gateway: hub.Gateway{Endpoint: server.URL, Token: "gateway-token-1"}, environment: environment}
|
|
if _, err := browser.Get(context.Background(), testDouyinIdentityURL); err == nil || requests != 0 {
|
|
t.Fatalf("unready binding reached gateway: requests=%d err=%v", requests, err)
|
|
}
|
|
}
|
|
|
|
func TestDouyinGatewayBrowserDoesNotEchoCredentialOnFailure(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
|
|
response.WriteHeader(http.StatusBadGateway)
|
|
_, _ = response.Write([]byte(`{"error":"private-session"}`))
|
|
}))
|
|
defer server.Close()
|
|
browser := douyinGatewayBrowser{gateway: hub.Gateway{Endpoint: server.URL, Token: "gateway-token-1"}, environment: readyDouyinEnvironment()}
|
|
_, err := browser.Get(context.Background(), testDouyinIdentityURL)
|
|
if err == nil || strings.Contains(err.Error(), "private-session") {
|
|
t.Fatalf("gateway failure leaked credential: %v", err)
|
|
}
|
|
}
|
|
|
|
func readyDouyinEnvironment() hub.EnvironmentContext {
|
|
return hub.EnvironmentContext{Env: hub.Env{Alias: "account-a", Gateway: "gateway-a"}, AccountID: "account-a",
|
|
AccountStatus: "active", AuthorizationStatus: "authorized", BindingVersion: 2,
|
|
Exit: hub.NetworkExit{ID: "exit-a", HealthStatus: "healthy"},
|
|
RuntimeID: "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", RuntimeNetworkID: "native-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}
|
|
}
|