aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


server/browser_account_info_test.go (2232B)


      1 package main
      2 
      3 import (
      4 	"encoding/json"
      5 	"net/http"
      6 	"net/http/httptest"
      7 	"testing"
      8 	"time"
      9 )
     10 
     11 func TestBrowserAccountInfoDoesNotWaitForSimulation(t *testing.T) {
     12 	world := newTestServer(t)
     13 	cookie := accountVisitor(t, world)
     14 	name := world.characters[1].name
     15 	startTestService(t, world)
     16 	defer holdTestZone(t, world, 1)()
     17 	request := httptest.NewRequest("GET", "/browser-account", nil)
     18 	request.AddCookie(cookie)
     19 	response := httptest.NewRecorder()
     20 	done := make(chan struct{})
     21 
     22 	// A busy simulation must not block a read of committed account information.
     23 	go func() {
     24 		world.httpHandler().ServeHTTP(response, request)
     25 		close(done)
     26 	}()
     27 	t.Cleanup(func() { <-done })
     28 	select {
     29 	case <-done:
     30 	case <-time.After(time.Second):
     31 		t.Fatal("account information waited for the busy zone")
     32 	}
     33 	var account map[string]any
     34 	if err := json.Unmarshal(response.Body.Bytes(), &account); err != nil {
     35 		t.Fatal(err)
     36 	}
     37 	if response.Code != http.StatusOK || account["name"] != name || account["visitor"] != true || account["adopted"] != false {
     38 		t.Fatal("incorrect committed account information", response.Code, account)
     39 	}
     40 }
     41 
     42 func TestBrowserAccountInfoRejectsRevokedCookie(t *testing.T) {
     43 	world := newTestServer(t)
     44 	cookie := accountVisitor(t, world)
     45 	claimed := accountRequest(world, cookie, "claim", "Alice", "secret")
     46 	if claimed.Code != http.StatusNoContent {
     47 		t.Fatal(claimed.Body.String())
     48 	}
     49 	request := httptest.NewRequest("GET", "/browser-account", nil)
     50 	request.AddCookie(cookie)
     51 	response := httptest.NewRecorder()
     52 
     53 	world.httpHandler().ServeHTTP(response, request)
     54 
     55 	if response.Code != http.StatusUnauthorized {
     56 		t.Fatal("revoked cookie could read account details", response.Code)
     57 	}
     58 	request = httptest.NewRequest("GET", "/browser-account", nil)
     59 	request.AddCookie(claimed.Result().Cookies()[0])
     60 	response = httptest.NewRecorder()
     61 	world.httpHandler().ServeHTTP(response, request)
     62 	var account map[string]any
     63 	if err := json.Unmarshal(response.Body.Bytes(), &account); err != nil {
     64 		t.Fatal(err)
     65 	}
     66 	if response.Code != http.StatusOK || account["name"] != "Alice" || account["adopted"] != true || account["visitor"] != false {
     67 		t.Fatal("new credential did not return committed account state", response.Code, account)
     68 	}
     69 }