-
Notifications
You must be signed in to change notification settings - Fork 19
/
blocklist_test.go
87 lines (70 loc) · 2.35 KB
/
blocklist_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package jwt
import (
"context"
"testing"
"time"
)
func TestBlocklist(t *testing.T) {
b := NewBlocklist(0)
b.GetKey = defaultGetKey
key := "jti:1"
c := Map{"username": "kataras", "age": 27}
sc := Claims{Expiry: Clock().Add(2 * time.Minute).Unix(), ID: key}
token, err := Sign(testAlg, testSecret, Merge(c, sc))
if err != nil {
t.Fatal(err)
}
b.InvalidateToken(token, sc)
if has, _ := b.Has(key); !has {
t.Fatalf("expected token to be in the list")
}
if count, _ := b.Count(); count != 1 {
t.Fatalf("expected list to contain a single token entry but got: %d", count)
}
if err = b.ValidateToken(token, Claims{ID: key}, nil); err != ErrBlocked {
t.Fatalf("expected error: ErrBlock but got: %v", err)
}
if err = b.ValidateToken(token, Claims{ID: key}, ErrExpired); err != ErrExpired {
t.Fatalf("expected error: ErrExpired as it respects the previous one but got: %v", err)
}
if has, _ := b.Has(key); has {
t.Fatalf("expected token to be removed as the validate token's error was ErrExpired")
}
b.InvalidateToken(token, sc)
if removed := b.GC(); removed != 0 {
t.Fatalf("expected nothing to be removed because the expiration is before current time but got: %d", removed)
}
b.Del(key)
if count, _ := b.Count(); count != 0 {
t.Fatalf("expected count to be zero but got: %d", count)
}
if err = b.ValidateToken(token, Claims{}, nil); err != nil {
t.Fatalf("expected no error as this token is now not blocked")
}
b.InvalidateToken([]byte{}, Claims{Expiry: 1})
if got, _ := b.Count(); got != 0 {
t.Fatalf("expected zero entries as the token was empty but got: %d", got)
}
if has, _ := b.Has(""); has {
t.Fatalf("expected Has to always return false as the given token was empty")
}
// Test GC expired.
b.InvalidateToken([]byte("expired one"), Claims{Expiry: 1})
if got, _ := b.Count(); got != 1 {
t.Fatalf("expected upsert not append")
}
if removed := b.GC(); removed != 1 {
t.Fatalf("expected one token to be removed as it's expired")
}
// test automatic gc
ctx, cancel := context.WithCancel(context.Background())
b = NewBlocklistContext(ctx, 500*time.Millisecond)
for i := 0; i < 10; i++ {
b.InvalidateToken(MustGenerateRandom(92), Claims{Expiry: Clock().Add(time.Second).Unix()})
}
time.Sleep(2 * time.Second)
cancel()
if got, _ := b.Count(); got != 0 {
t.Fatalf("expected all entries to be removed but: %d", got)
}
}