Files
LinkPort/internal/api/gateway_account_targets_test.go
T

79 lines
4.1 KiB
Go

package api
import (
"encoding/json"
"linkport/internal/model"
"testing"
)
func TestGatewayAccountsReferenceDifferentTargetsAndSwitchIndividually(t *testing.T) {
f := newFixture(t)
f.login()
first, second := f.create("mysql").Config, f.create("mysql").Config
for _, p := range []model.Proxy{first, second} {
requireStatus(t, f.request("DELETE", "/api/v1/proxies/"+p.ID, nil, true), 200)
}
accounts := []model.GatewayAccount{
{ID: "a", Username: "alice", Password: "alice-fixture", TargetID: first.TargetID, TargetAccountID: first.TargetAccountID},
{ID: "b", Username: "bob", Password: "bob-fixture", TargetID: second.TargetID, TargetAccountID: second.TargetAccountID},
}
w := f.request("POST", "/api/v1/proxies", map[string]any{"name": "account routes", "type": "mysql", "listenHost": "127.0.0.1", "listenPort": 24831, "gatewayAccounts": accounts}, true)
requireStatus(t, w, 201)
view := readView(t, w)
path := "/api/v1/proxies/" + view.Config.ID
if view.Config.TargetID != "" || view.Config.GatewayAccounts[1].TargetID != second.TargetID {
t.Fatal("account-only bindings lost")
}
requireStatus(t, f.request("DELETE", "/api/v1/targets/"+first.TargetID, nil, true), 409)
requireStatus(t, f.request("PUT", "/api/v1/targets/"+first.TargetID, map[string]any{"accounts": []model.TargetAccount{{ID: "replacement", Name: "replacement", Username: "fixture", Password: "fixture"}}}, true), 409)
accounts[0].TargetAccountID = "missing"
requireStatus(t, f.request("PUT", path, map[string]any{"gatewayAccounts": accounts}, true), 400)
accounts[0].TargetAccountID = first.TargetAccountID
accounts[0].TargetID = "missing"
requireStatus(t, f.request("PUT", path, map[string]any{"gatewayAccounts": accounts}, true), 400)
w = f.request("GET", "/api/v1/targets/"+second.TargetID, nil, false)
requireStatus(t, w, 200)
var target struct {
GatewayIDs []string `json:"gatewayIds"`
}
if err := json.Unmarshal(w.Body.Bytes(), &target); err != nil || len(target.GatewayIDs) != 1 || target.GatewayIDs[0] != view.Config.ID {
t.Fatal("missing reverse reference", err)
}
requireStatus(t, f.request("POST", path+"/start", map[string]any{}, true), 200)
defer f.request("POST", path+"/stop", map[string]any{}, true)
requireStatus(t, f.request("POST", path+"/target", map[string]any{"gatewayAccountId": "unknown", "targetId": second.TargetID, "targetAccountId": second.TargetAccountID}, true), 400)
w = f.request("POST", path+"/target", map[string]any{"gatewayAccountId": "a", "targetId": second.TargetID, "targetAccountId": second.TargetAccountID}, true)
requireStatus(t, w, 200)
got, err := f.store.Get(view.Config.ID)
if err != nil || got.TargetID != "" || got.GatewayAccounts[0].TargetID != second.TargetID || got.GatewayAccounts[1].TargetID != second.TargetID || got.GatewayAccounts[0].Password != "alice-fixture" || got.GatewayAccounts[1].Password != "bob-fixture" {
t.Fatal("individual switch lost mapping or credentials", err)
}
requireStatus(t, f.request("DELETE", "/api/v1/targets/"+second.TargetID, nil, true), 409)
}
func TestGatewayAccountConnectionsCanBeBuiltIncrementally(t *testing.T) {
f := newFixture(t)
f.login()
target := f.create("mysql").Config
w := f.request("POST", "/api/v1/proxies", map[string]any{
"name": "incremental account routes", "type": "mysql", "listenHost": "127.0.0.1", "listenPort": 24832,
"gatewayAccounts": []model.GatewayAccount{{ID: "a", Username: "alice", Password: "fixture-a"}, {ID: "b", Username: "bob", Password: "fixture-b"}},
}, true)
requireStatus(t, w, 201)
path := "/api/v1/proxies/" + readView(t, w).Config.ID
for i, id := range []string{"a", "b"} {
w = f.request("POST", path+"/target", map[string]any{"gatewayAccountId": id, "targetId": target.TargetID, "targetAccountId": target.TargetAccountID}, true)
requireStatus(t, w, 200)
if i == 0 {
if readView(t, w).Config.GatewayAccounts[1].TargetID != "" {
t.Fatal("other account changed")
}
if f.request("POST", path+"/start", map[string]any{}, true).Code < 400 {
t.Fatal("incomplete mapping started")
}
}
}
requireStatus(t, f.request("POST", path+"/start", map[string]any{}, true), 200)
defer f.request("POST", path+"/stop", map[string]any{}, true)
}