package transport import ( "aeternalis/server/content" "aeternalis/server/internal/game" "aeternalis/server/internal/store" "context" "encoding/json" "net/http" "net/http/httptest" "path/filepath" "strings" "testing" "time" ) func TestTravelHTTPAuthorizationAndResume(t *testing.T) { ctx := context.Background() db, err := store.Open(ctx, "file:"+filepath.ToSlash(filepath.Join(t.TempDir(), "travel.db"))) if err != nil { t.Fatal(err) } defer db.Close() b, _ := content.Load() source := game.New("source", 1, time.Now(), b) hub := game.New("hub", 2, time.Now(), b) hub.Kind = "hub" hub.Phase = "hub" sl, _, _ := db.Acquire(ctx, source) hl, _, _ := db.Acquire(ctx, hub) c, _ := source.Join("owner", "Traveler") c.View.Inventory["escape_stone"] = 1 _ = source.Connect(c.View.Entity.ID) if err = sl.Save(ctx, source, ""); err != nil { t.Fatal(err) } auth, _ := NewAuth(ctx, db, true, "http://127.0.0.1", "", "", "") cookie := func(account string) *http.Cookie { t.Helper() out := httptest.NewRecorder() r := httptest.NewRequest("POST", auth.base+"/auth/dev", nil) if e := auth.session(out, r, account); e != nil { t.Fatal(e) } return out.Result().Cookies()[0] } owner, intruder := cookie("owner"), cookie("intruder") s := NewService(source, sl) h := NewHTTP(s, auth, t.TempDir(), nil).Handler() target := NewHTTP(NewService(hub, hl), auth, t.TempDir(), nil).Handler() request := func(handler http.Handler, path, origin string, body any, cookie *http.Cookie) *httptest.ResponseRecorder { t.Helper() method := "GET" var raw []byte if body != nil { method = "POST" raw, _ = json.Marshal(body) } r := httptest.NewRequest(method, auth.base+path, strings.NewReader(string(raw))) r.RemoteAddr = "127.0.0.1:4321" if origin != "" { r.Header.Set("Origin", origin) } if cookie != nil { r.AddCookie(cookie) } out := httptest.NewRecorder() handler.ServeHTTP(out, r) return out } req := map[string]string{"requestId": game.ID(), "destination": "hub", "route": "escape"} if r := request(h, "/api/travel/prepare", auth.base, req, nil); r.Code != 401 { t.Fatal("anonymous travel", r.Code) } if r := request(h, "/api/travel/prepare", "https://evil.example", req, owner); r.Code != 403 { t.Fatal("cross-origin travel", r.Code) } if r := request(h, "/api/travel/prepare", auth.base, req, intruder); r.Code != 409 { t.Fatal("other account traveled", r.Code) } if !s.ready.Load() { t.Fatal("ordinary rejection froze world") } r := request(h, "/api/travel/prepare", auth.base, req, owner) if r.Code != 200 { t.Fatal(r.Code, r.Body.String()) } var tr store.Transfer if err = json.Unmarshal(r.Body.Bytes(), &tr); err != nil { t.Fatal(err) } if strings.Contains(r.Body.String(), "escape_stone") || strings.Contains(r.Body.String(), "account") { t.Fatal("private escrow leaked") } if r = request(h, "/api/travel", auth.base, nil, owner); r.Code != 200 || !strings.Contains(r.Body.String(), tr.ID) { t.Fatal("pending recovery failed", r.Body.String()) } if r = request(h, "/api/travel?id="+tr.ID, auth.base, nil, intruder); r.Body.String() != "null\n" { t.Fatal("transfer exposed to another account", r.Body.String()) } accept := map[string]string{"transferId": tr.ID} if r = request(target, "/api/travel/accept", auth.base, accept, intruder); r.Code != 409 { t.Fatal("other account claimed transfer", r.Code) } if r = request(target, "/api/travel/accept", auth.base, accept, owner); r.Code != 200 { t.Fatal("arrival failed", r.Body.String()) } if r = request(target, "/api/travel/accept", auth.base, accept, owner); r.Code != 200 || hub.LivingCount() != 1 { t.Fatal("arrival retry duplicated character", r.Body.String()) } if r = request(h, "/api/join", auth.base, map[string]string{"name": "Replacement", "passage": ""}, owner); r.Code != 409 { t.Fatal("created replacement after travel", r.Code) } if r = request(h, "/api/join", auth.base, map[string]string{"name": "", "passage": c.View.Entity.ID}, owner); r.Code != 409 { t.Fatal("legacy shortcut bypassed travel", r.Code) } }