mirror of
https://github.com/jcreek/CosmicClash.git
synced 2026-09-13 03:02:02 +00:00
feat: add verified Steam session endpoint
This commit is contained in:
@@ -21,6 +21,16 @@ func (s *sessionBackendSpy) Authenticate(_ context.Context, sessionID, _ string,
|
||||
return domain.Session{SessionID: sessionID, PlayerID: "player-1"}, nil
|
||||
}
|
||||
|
||||
type steamLoginSpy struct{ calls int }
|
||||
|
||||
func (s *steamLoginSpy) Authenticate(_ context.Context, ticket string, _ time.Time) (domain.VerifiedIdentity, error) {
|
||||
s.calls++
|
||||
if ticket != "valid-web-ticket" {
|
||||
return domain.VerifiedIdentity{}, domain.ErrTicketRejected
|
||||
}
|
||||
return domain.VerifiedIdentity{PlayerID: "player-1", SteamID: "steam-1"}, nil
|
||||
}
|
||||
|
||||
func (b *queueBackendSpy) Create(_ context.Context, playerID, ticketID, _ string, spec domain.QueueSpec, now time.Time) (domain.QueueTicket, error) {
|
||||
b.createCalls++
|
||||
return domain.QueueTicket{TicketID: ticketID, PlayerID: playerID, Playlist: spec.Playlist, State: domain.Queued, EnqueuedAt: now, ExpiresAt: now.Add(domain.QueueExpiryWindow)}, nil
|
||||
@@ -294,6 +304,48 @@ func TestQueueAPIUsesInjectedSessionBackend(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSteamSessionAPIRequiresBackendVerificationAndIssuesOpaqueSession(t *testing.T) {
|
||||
now := time.Unix(1000, 0).UTC()
|
||||
sessions := domain.NewSessionStore()
|
||||
provider := &steamLoginSpy{}
|
||||
service := &Service{Sessions: sessions, SteamLogin: provider, Now: func() time.Time { return now }}
|
||||
server := httptest.NewServer(service.Handler())
|
||||
defer server.Close()
|
||||
request := func(body string) *http.Response {
|
||||
req, _ := http.NewRequest(http.MethodPost, server.URL+"/v1/session/steam", strings.NewReader(body))
|
||||
response, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return response
|
||||
}
|
||||
response := request(`{"web_api_ticket":"valid-web-ticket","steam_id":"spoofed"}`)
|
||||
if response.StatusCode != http.StatusBadRequest {
|
||||
t.Fatalf("extra field status = %d", response.StatusCode)
|
||||
}
|
||||
response.Body.Close()
|
||||
response = request(`{"web_api_ticket":"invalid"}`)
|
||||
if response.StatusCode != http.StatusUnauthorized {
|
||||
t.Fatalf("invalid ticket status = %d", response.StatusCode)
|
||||
}
|
||||
response.Body.Close()
|
||||
response = request(`{"web_api_ticket":"valid-web-ticket"}`)
|
||||
if response.StatusCode != http.StatusOK {
|
||||
t.Fatalf("valid ticket status = %d", response.StatusCode)
|
||||
}
|
||||
var result struct {
|
||||
PlayerID string `json:"player_id"`
|
||||
AccessToken string `json:"access_token"`
|
||||
}
|
||||
if err := json.NewDecoder(response.Body).Decode(&result); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
response.Body.Close()
|
||||
if result.PlayerID != "player-1" || !strings.Contains(result.AccessToken, ":") || provider.calls != 2 {
|
||||
t.Fatalf("session result=%+v provider_calls=%d", result, provider.calls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestQueueRecoveryAPIIsAuthenticatedOwnerOnlyAndExpiresStaleTickets(t *testing.T) {
|
||||
now := time.Unix(1000, 0).UTC()
|
||||
sessions := domain.NewSessionStore()
|
||||
|
||||
Reference in New Issue
Block a user