51 lines
1.2 KiB
Go

package main
import (
"math"
"testing"
)
func TestTicketSingleUse(t *testing.T) {
store := newTicketStore()
value, err := store.issue("player-1", "Dave")
if err != nil {
t.Fatal(err)
}
if item, ok := store.consume(value); !ok || item.Subject != "player-1" {
t.Fatalf("first consume failed: %#v %v", item, ok)
}
if _, ok := store.consume(value); ok {
t.Fatal("ticket must be one-time")
}
}
func TestTransformSanitizers(t *testing.T) {
if got := clampFloat(-5, 0, 3600); got != 0 {
t.Fatalf("clamp low=%v", got)
}
if got := clampFloat(4000, 0, 3600); got != 3600 {
t.Fatalf("clamp high=%v", got)
}
if got := clampFloat(math.NaN(), 0, 3600); got != 0 {
t.Fatalf("nan=%v", got)
}
if got := normalizeAngle(-90); got != 270 {
t.Fatalf("angle=%v", got)
}
if got := clampInt(900, 0, 500); got != 500 {
t.Fatalf("hull=%v", got)
}
}
func TestAudienceMatch(t *testing.T) {
if !audienceMatches(claims{AuthorizedParty: "airship-web"}, "airship-web") {
t.Fatal("azp should match")
}
if !audienceMatches(claims{Audience: []interface{}{"account", "airship-web"}}, "airship-web") {
t.Fatal("aud should match")
}
if audienceMatches(claims{AuthorizedParty: "other"}, "airship-web") {
t.Fatal("wrong client must not match")
}
}