puerta/internal/user/ttl_test.go
Roberto Hidalgo 038e09b202 lol, 64bit integer overflows are fun to debug
specially when running a multi-arch cluster and HA with poor observability. a `time.Duration` was being casted to an `int` (because I clearly have no clue of what I'm doing). All is well on 64-bit processors, but 32-bit processors choke on the very long micro-second precision int, overflow, and turn `7d`, around `604800` seconds into `-114753536` seconds, which the browser happily (and correctly) turns into a cookie sent to /dev/null. lol.
2023-02-11 12:01:16 -06:00

70 lines
1.3 KiB
Go

package user_test
import (
"fmt"
"testing"
"git.rob.mx/nidito/puerta/internal/user"
)
func TestParse(t *testing.T) {
ttl := user.TTL{}
err := ttl.Scan("")
if err != nil {
t.Fatalf("Failed scanning empty string: %s", err)
}
err = ttl.Scan("7d")
if err != nil {
t.Fatalf("Failed scanning 7d: %s", err)
}
if ttl.Seconds() != 604800 {
t.Fatalf("parsed bad seconds %d", ttl.Seconds())
}
// conn := sqlite.ConnectionURL{
// Database: "test.db",
// Options: map[string]string{
// "_journal": "WAL",
// "_busy_timeout": "5000",
// },
// }
// _db, err := sqlite.Open(conn)
// if err != nil {
// t.Fatal(err)
// }
// user := &user.User{}
// if err := _db.Get(user, db.Cond{"handle": "test"}); err != nil {
// t.Fatalf("could not get user: %s", err)
// }
// t.Fatalf("user ttl (%v): %d, from now: %s", user.TTL, user.TTL.Seconds(), user.TTL.FromNow())
}
func TestMarshalDB(t *testing.T) {
ttl := user.TTL{}
err := ttl.Scan("7d")
if err != nil {
t.Fatalf("Failed scanning 7d: %s", err)
}
if ttl.Seconds() != 60*60*24*7 {
t.Fatalf("parsed bad seconds %d", ttl.Seconds())
}
data, err := ttl.MarshalDB()
if err != nil {
t.Fatalf("could not marshal ttl %s", err)
}
expected := `"7d"`
if fmt.Sprintf("%s", data) != expected {
t.Fatalf("encoded data mismatch. expected %s, got %s", expected, data)
}
}