87 lines
2.7 KiB
Go
87 lines
2.7 KiB
Go
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 TestCharacterAppearanceOwnershipAndDurability(t *testing.T) {
|
|
ctx := context.Background()
|
|
db, err := store.Open(ctx, "file:"+filepath.ToSlash(filepath.Join(t.TempDir(), "appearance.db")))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer db.Close()
|
|
b, _ := content.Load()
|
|
world := game.New("appearance", 1, time.Now(), b)
|
|
lease, _, err := db.Acquire(ctx, world)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
svc := NewService(world, lease)
|
|
a := content.Appearance{Version: 1, Family: "human", Variant: 3, Seed: 42, Height: 1, Width: 1, Detail: .5, Primary: "#d8a582", Secondary: "#493947", Accent: "#578f8b"}
|
|
view, err := svc.join(ctx, "artist", "Artist", "", &a)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if view.Entity.Appearance == nil || *view.Entity.Appearance != a {
|
|
t.Fatal("new character appearance missing")
|
|
}
|
|
other := a
|
|
other.Primary = "#ffffff"
|
|
if _, err = svc.join(ctx, "artist", "Artist", "", &other); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if *world.ForAccount("artist").View.Entity.Appearance != a {
|
|
t.Fatal("join retry overwrote appearance")
|
|
}
|
|
auth, _ := NewAuth(ctx, db, true, "http://127.0.0.1", "", "", "")
|
|
server := httptest.NewServer(NewHTTP(svc, auth, t.TempDir(), nil).Handler())
|
|
defer server.Close()
|
|
auth.base = server.URL
|
|
token := game.ID() + game.ID()
|
|
if err = db.CreateSession(ctx, hash(token), "artist"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
request := func(value any, cookie, origin string) int {
|
|
t.Helper()
|
|
raw, _ := json.Marshal(value)
|
|
r, _ := http.NewRequest("POST", server.URL+"/api/appearance", strings.NewReader(string(raw)))
|
|
r.Header.Set("Origin", origin)
|
|
if cookie != "" {
|
|
r.AddCookie(&http.Cookie{Name: auth.cookieName(), Value: cookie})
|
|
}
|
|
resp, err := server.Client().Do(r)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer resp.Body.Close()
|
|
return resp.StatusCode
|
|
}
|
|
if request(other, "", server.URL) != 401 || request(other, token, "https://elsewhere.example") != 403 {
|
|
t.Fatal("appearance auth boundary bypassed")
|
|
}
|
|
bad := other
|
|
bad.Family = "creature"
|
|
if request(bad, token, server.URL) != 400 {
|
|
t.Fatal("player could choose creature")
|
|
}
|
|
before := world.ForAccount("artist").View.Stats["movementSpeed"]
|
|
if request(other, token, server.URL) != 200 {
|
|
t.Fatal("appearance save rejected")
|
|
}
|
|
record, err := db.ActiveCharacter(ctx, "artist")
|
|
if err != nil || *record.Character.View.Entity.Appearance != other || record.Character.View.Stats["movementSpeed"] != before {
|
|
t.Fatal("appearance not durable or changed stats", err)
|
|
}
|
|
}
|