|
7 | 7 | "net/http/httptest" |
8 | 8 | "strings" |
9 | 9 | "testing" |
| 10 | + |
| 11 | + "github.com/gastownhall/wasteland/internal/api" |
10 | 12 | ) |
11 | 13 |
|
12 | 14 | const testSecret = "test-session-secret" |
@@ -83,6 +85,7 @@ func setupHostedTestServer(t *testing.T) (*SessionStore, *httptest.Server) { |
83 | 85 | mux := http.NewServeMux() |
84 | 86 | mux.HandleFunc("POST /api/auth/connect", server.handleConnect) |
85 | 87 | mux.HandleFunc("GET /api/auth/status", server.handleAuthStatus) |
| 88 | + mux.HandleFunc("GET /api/bootstrap", server.handleBootstrap) |
86 | 89 | mux.HandleFunc("POST /api/auth/logout", server.handleLogout) |
87 | 90 | mux.HandleFunc("POST /api/auth/connect-session", server.handleConnectSession) |
88 | 91 | mux.HandleFunc("POST /api/auth/join", server.handleJoin) |
@@ -148,6 +151,7 @@ func setupMultiWastelandTestServer(t *testing.T) (*SessionStore, *httptest.Serve |
148 | 151 | }) |
149 | 152 |
|
150 | 153 | mux := http.NewServeMux() |
| 154 | + mux.HandleFunc("GET /api/bootstrap", server.handleBootstrap) |
151 | 155 | mux.Handle("/", server.AuthMiddleware(inner)) |
152 | 156 |
|
153 | 157 | ts := httptest.NewServer(mux) |
@@ -486,6 +490,72 @@ func TestHandleAuthStatus_Authenticated(t *testing.T) { |
486 | 490 | } |
487 | 491 | } |
488 | 492 |
|
| 493 | +func TestHandleBootstrap_Authenticated_RemembersAndReturnsActiveUpstream(t *testing.T) { |
| 494 | + sessions, ts := setupMultiWastelandTestServer(t) |
| 495 | + |
| 496 | + sessionID, _ := sessions.Create("conn-1") |
| 497 | + req, _ := http.NewRequest("GET", ts.URL+"/api/bootstrap", nil) |
| 498 | + req.Header.Set("X-Wasteland", "gastownhall/gascity") |
| 499 | + req.AddCookie(&http.Cookie{ |
| 500 | + Name: cookieName, |
| 501 | + Value: SignSessionCookie(sessionID, "conn-1", testSecret), |
| 502 | + }) |
| 503 | + |
| 504 | + resp, err := http.DefaultClient.Do(req) |
| 505 | + if err != nil { |
| 506 | + t.Fatal(err) |
| 507 | + } |
| 508 | + defer resp.Body.Close() //nolint:errcheck // test cleanup |
| 509 | + |
| 510 | + if got := resp.Header.Get("Cache-Control"); got != "no-store" { |
| 511 | + t.Fatalf("Cache-Control = %q, want %q", got, "no-store") |
| 512 | + } |
| 513 | + |
| 514 | + var boot api.BootstrapResponse |
| 515 | + if err := json.NewDecoder(resp.Body).Decode(&boot); err != nil { |
| 516 | + t.Fatalf("decode bootstrap: %v", err) |
| 517 | + } |
| 518 | + if !boot.Authenticated || !boot.Connected { |
| 519 | + t.Fatalf("expected authenticated connected bootstrap, got %+v", boot) |
| 520 | + } |
| 521 | + if boot.ActiveUpstream != "gastownhall/gascity" { |
| 522 | + t.Fatalf("active_upstream = %q, want %q", boot.ActiveUpstream, "gastownhall/gascity") |
| 523 | + } |
| 524 | + if boot.Mode != "pr" { |
| 525 | + t.Fatalf("mode = %q, want %q", boot.Mode, "pr") |
| 526 | + } |
| 527 | + if got := sessions.ActiveUpstream(sessionID); got != "gastownhall/gascity" { |
| 528 | + t.Fatalf("remembered upstream = %q, want %q", got, "gastownhall/gascity") |
| 529 | + } |
| 530 | +} |
| 531 | + |
| 532 | +func TestHandleBootstrap_UsesRememberedUpstreamWhenHeaderMissing(t *testing.T) { |
| 533 | + sessions, ts := setupMultiWastelandTestServer(t) |
| 534 | + |
| 535 | + sessionID, _ := sessions.Create("conn-1") |
| 536 | + sessions.RememberActiveUpstream(sessionID, "gastownhall/gascity") |
| 537 | + |
| 538 | + req, _ := http.NewRequest("GET", ts.URL+"/api/bootstrap", nil) |
| 539 | + req.AddCookie(&http.Cookie{ |
| 540 | + Name: cookieName, |
| 541 | + Value: SignSessionCookie(sessionID, "conn-1", testSecret), |
| 542 | + }) |
| 543 | + |
| 544 | + resp, err := http.DefaultClient.Do(req) |
| 545 | + if err != nil { |
| 546 | + t.Fatal(err) |
| 547 | + } |
| 548 | + defer resp.Body.Close() //nolint:errcheck // test cleanup |
| 549 | + |
| 550 | + var boot api.BootstrapResponse |
| 551 | + if err := json.NewDecoder(resp.Body).Decode(&boot); err != nil { |
| 552 | + t.Fatalf("decode bootstrap: %v", err) |
| 553 | + } |
| 554 | + if boot.ActiveUpstream != "gastownhall/gascity" { |
| 555 | + t.Fatalf("active_upstream = %q, want %q", boot.ActiveUpstream, "gastownhall/gascity") |
| 556 | + } |
| 557 | +} |
| 558 | + |
489 | 559 | func TestHandleLogout(t *testing.T) { |
490 | 560 | sessions, ts := setupHostedTestServer(t) |
491 | 561 |
|
|
0 commit comments