sonda/internal/auth/auth_test.go
tgiachi cce689b77d fix(auth): reuse the account, quiet aborted requests, more tests
- requireAdmin reuses the account requireAuth already read in the same request
  instead of reading it again.
- A request the client aborted while the account was being read (a player
  seeking) ends with 499 and no error in the log instead of a 500.
- A comment on what a clock stepped back does to the created-after check.
- Tests: the streaming route with the token of a deleted user, the token's
  issue time round trip, the aborted request, the single lookup for admins.
2026-11-11 19:29:39 +01:00

124 lines
3.1 KiB
Go

package auth
import (
"strings"
"testing"
"time"
"github.com/golang-jwt/jwt/v5"
)
// ---- password hashing ----
func TestHashAndVerify(t *testing.T) {
h, err := HashPassword("correct horse")
if err != nil {
t.Fatal(err)
}
if !strings.HasPrefix(h, "$argon2id$") {
t.Fatalf("hash = %q, want argon2id PHC string", h)
}
if !VerifyPassword(h, "correct horse") {
t.Error("correct password rejected")
}
if VerifyPassword(h, "wrong") {
t.Error("wrong password accepted")
}
}
func TestHashIsSalted(t *testing.T) {
a, _ := HashPassword("same")
b, _ := HashPassword("same")
if a == b {
t.Error("two hashes of the same password are identical")
}
}
func TestVerifyRejectsGarbage(t *testing.T) {
for _, h := range []string{"", "plaintext", "$argon2id$v=19$m=1,t=1,p=1$bad", "$bcrypt$x$y"} {
if VerifyPassword(h, "x") {
t.Errorf("garbage hash %q accepted", h)
}
}
}
// ---- tokens ----
const testSecret = "0123456789abcdef0123456789abcdef"
func TestTokenRoundTrip(t *testing.T) {
iss := NewTokenIssuer(testSecret, time.Hour)
tok, exp, err := iss.Issue(User{ID: 7, Username: "bob"})
if err != nil {
t.Fatal(err)
}
if time.Until(exp) <= 0 {
t.Error("expiry is not in the future")
}
claims, err := iss.Parse(tok)
if err != nil {
t.Fatal(err)
}
if claims.UserID != 7 || claims.Username != "bob" {
t.Errorf("claims = %+v", claims)
}
}
func TestTokenCarriesTheMomentItWasIssued(t *testing.T) {
iss := NewTokenIssuer(testSecret, time.Hour)
at := time.Date(2026, 5, 6, 7, 8, 9, 500_000_000, time.UTC)
iss.now = func() time.Time { return at }
tok, _, err := iss.Issue(User{ID: 7, Username: "bob"})
if err != nil {
t.Fatal(err)
}
claims, err := iss.Parse(tok)
if err != nil {
t.Fatal(err)
}
if want := at.Truncate(time.Second); !claims.IssuedAt.Equal(want) {
t.Errorf("issued at = %v, want %v (whole seconds)", claims.IssuedAt, want)
}
}
func TestTokenExpired(t *testing.T) {
iss := NewTokenIssuer(testSecret, time.Hour)
iss.now = func() time.Time { return time.Now().Add(-2 * time.Hour) }
tok, _, err := iss.Issue(User{ID: 1, Username: "a"})
if err != nil {
t.Fatal(err)
}
iss.now = time.Now
if _, err := iss.Parse(tok); err == nil {
t.Fatal("expired token accepted")
}
}
func TestTokenWrongSecret(t *testing.T) {
tok, _, _ := NewTokenIssuer(testSecret, time.Hour).Issue(User{ID: 1, Username: "a"})
other := NewTokenIssuer("ffffffffffffffffffffffffffffffff", time.Hour)
if _, err := other.Parse(tok); err == nil {
t.Fatal("token signed with another secret accepted")
}
}
func TestTokenRejectsAlgNone(t *testing.T) {
claims := jwt.RegisteredClaims{
Subject: "1",
Issuer: tokenIssuerName,
ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Hour)),
}
tok, err := jwt.NewWithClaims(jwt.SigningMethodNone, claims).SignedString(jwt.UnsafeAllowNoneSignatureType)
if err != nil {
t.Fatal(err)
}
if _, err := NewTokenIssuer(testSecret, time.Hour).Parse(tok); err == nil {
t.Fatal("alg=none token accepted")
}
}
func TestTokenGarbage(t *testing.T) {
if _, err := NewTokenIssuer(testSecret, time.Hour).Parse("not.a.jwt"); err == nil {
t.Fatal("garbage token accepted")
}
}