...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package auth
16
17 import (
18 "context"
19 "testing"
20
21 "go.uber.org/zap"
22 )
23
24
25
26 func TestSimpleTokenDisabled(t *testing.T) {
27 initialState := newTokenProviderSimple(zap.NewExample(), dummyIndexWaiter, simpleTokenTTLDefault)
28
29 explicitlyDisabled := newTokenProviderSimple(zap.NewExample(), dummyIndexWaiter, simpleTokenTTLDefault)
30 explicitlyDisabled.enable()
31 explicitlyDisabled.disable()
32
33 for _, tp := range []*tokenSimple{initialState, explicitlyDisabled} {
34 ctx := context.WithValue(context.WithValue(context.TODO(), AuthenticateParamIndex{}, uint64(1)), AuthenticateParamSimpleTokenPrefix{}, "dummy")
35 token, err := tp.assign(ctx, "user1", 0)
36 if err != nil {
37 t.Fatal(err)
38 }
39 authInfo, ok := tp.info(ctx, token, 0)
40 if ok {
41 t.Errorf("expected (true, \"user1\") got (%t, %s)", ok, authInfo.Username)
42 }
43
44 tp.invalidateUser("user1")
45 }
46 }
47
48
49
50 func TestSimpleTokenAssign(t *testing.T) {
51 tp := newTokenProviderSimple(zap.NewExample(), dummyIndexWaiter, simpleTokenTTLDefault)
52 tp.enable()
53 defer tp.disable()
54 ctx := context.WithValue(context.WithValue(context.TODO(), AuthenticateParamIndex{}, uint64(1)), AuthenticateParamSimpleTokenPrefix{}, "dummy")
55 token, err := tp.assign(ctx, "user1", 0)
56 if err != nil {
57 t.Fatal(err)
58 }
59 authInfo, ok := tp.info(ctx, token, 0)
60 if !ok || authInfo.Username != "user1" {
61 t.Errorf("expected (true, \"token2\") got (%t, %s)", ok, authInfo.Username)
62 }
63
64 tp.invalidateUser("user1")
65
66 _, ok = tp.info(context.TODO(), token, 0)
67 if ok {
68 t.Errorf("expected ok == false after user is invalidated")
69 }
70 }
71
View as plain text