internal/store/sessions_test.go
46 lines · 1190 bytes
1package store
2
3import (
4 "testing"
5 "time"
6)
7
8func TestCountLoginTokensSince(t *testing.T) {
9 s := open(t)
10 if err := s.MigrateUp(); err != nil {
11 t.Fatal(err)
12 }
13 uid, err := s.CreateUser("cmc", true)
14 if err != nil {
15 t.Fatal(err)
16 }
17 for i := 0; i < 3; i++ {
18 _, hash, err := NewToken()
19 if err != nil {
20 t.Fatal(err)
21 }
22 if err := s.CreateLoginToken(uid, hash, time.Minute); err != nil {
23 t.Fatal(err)
24 }
25 }
26
27 n, err := s.CountLoginTokensSince(uid, time.Now().Add(-time.Hour))
28 if err != nil || n != 3 {
29 t.Fatalf("count in the last hour = %d, %v; want 3", n, err)
30 }
31
32 // A window that opens in the future sees none of them, which is what
33 // makes the hourly bound a window rather than a lifetime total.
34 if n, err := s.CountLoginTokensSince(uid, time.Now().Add(time.Hour)); err != nil || n != 0 {
35 t.Fatalf("count in a future window = %d, %v; want 0", n, err)
36 }
37
38 // One account's requests must not spend another account's budget.
39 other, err := s.CreateUser("kim", false)
40 if err != nil {
41 t.Fatal(err)
42 }
43 if n, err := s.CountLoginTokensSince(other, time.Now().Add(-time.Hour)); err != nil || n != 0 {
44 t.Fatalf("other account count = %d, %v; want 0", n, err)
45 }
46}