package handler import ( "context" "encoding/json" "net/http" "net/http/httptest" "strings" "testing" "github.com/multica-ai/multica/server/internal/auth" "github.com/multica-ai/multica/server/internal/testutil" ) func TestQoderConnectionSecrets(t *testing.T) { if testHandler == nil { t.Skip("database not available") } withVCSBox(t) t.Cleanup(func() { _, _ = testPool.Exec(context.Background(), `DELETE FROM daemon_token WHERE id IN (SELECT token_id FROM cloud_agent_connection WHERE provider='qoder_cloud' AND workspace_id=$1)`, testWorkspaceID) _, _ = testPool.Exec(context.Background(), `DELETE FROM cloud_agent_connection WHERE provider='qoder_cloud' AND workspace_id=$1`, testWorkspaceID) }) body := map[string]any{"name": "Managed QCA", "base_url": "https://api.qoder.com/api/v1/cloud", "environment_id": "env_test", "qoder_token": "qca-test-secret", "github_tokens": map[string]string{"https://github.com/owner/repo": "github-test-secret"}} var view qoderSettingsView testutil.Call(t, testHandler.SaveQoderConnection, vcsHandlerRequest("PUT", "/qoder", body, "")).Want(200).JSON(&view) if !view.Configured || !view.HasToken || len(view.Repositories) != 1 { t.Fatalf("unexpected view: %+v", view) } var encrypted string dbfx.QueryRow(t, `SELECT config_encrypted FROM cloud_agent_connection WHERE provider='qoder_cloud' AND workspace_id=$1`, testWorkspaceID).Scan(&encrypted) raw, _ := json.Marshal(view) for _, secret := range []string{"qca-test-secret", "github-test-secret"} { if strings.Contains(encrypted, secret) || strings.Contains(string(raw), secret) { t.Fatal("plaintext credential exposed") } } body["qoder_token"] = "" body["github_tokens"] = map[string]string{"https://github.com/owner/repo": ""} testutil.Call(t, testHandler.SaveQoderConnection, vcsHandlerRequest("PUT", "/qoder", body, "")).Want(200) cfg, _, _, _, err := testHandler.readQoderSettings(context.Background(), testWorkspaceID) if err != nil || cfg.QoderToken != "qca-test-secret" || cfg.GitHubTokens["https://github.com/owner/repo"] != "github-test-secret" { t.Fatal("blank field did not retain credentials") } if !strings.HasPrefix(cfg.MulticaToken, "mdt_") { t.Fatal("managed bridge must use a daemon credential") } var credentialWorkspace, daemonID string var expiring bool dbfx.QueryRow(t, `SELECT workspace_id::text,daemon_id,expires_at>now() AND expires_at