forked from s12v/go-jwks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
83 lines (72 loc) · 1.79 KB
/
client_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
package jwks
import (
"github.com/square/go-jose"
"testing"
"time"
)
func TestJWKSClient_GetKey(t *testing.T) {
keyId := "test-4317493287"
sourceMock := NewDummySource(&jose.JSONWebKeySet{Keys: []jose.JSONWebKey{{
KeyID: keyId,
}}})
cacheMock := NewMockCache()
client := NewClient(sourceMock, cacheMock, time.Minute)
jwk, err := client.GetKey(keyId, "sig")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if jwk.KeyID != keyId {
t.Fatalf("unexpected keyID: %v", jwk.KeyID)
}
}
func TestJWKSClient_GetKeyWithPrefetch(t *testing.T) {
keyId := "test-4317493287"
mockJwk := jose.JSONWebKey{
KeyID: keyId,
Use: "sig",
}
sourceMock := NewDummySource(&jose.JSONWebKeySet{Keys: []jose.JSONWebKey{{
KeyID: keyId,
Use: "enc",
}}})
cacheMock := NewMockCache()
cacheMock.SetWithExpiration(
keyId,
&cacheEntry{
refresh: 0,
jwk: &mockJwk,
},
time.Unix(0, 0),
)
client := NewClient(sourceMock, cacheMock, time.Minute)
key1, err := client.GetKey(keyId, "sig")
time.Sleep(time.Millisecond * 5)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if key1.Use != "sig" {
t.Fatalf("unexpected Use: %v", key1.Use)
}
key2, _ := cacheMock.Get(keyId)
if key2.(*cacheEntry).jwk.Use != "enc" {
t.Fatal("key should be updated in cache")
}
}
func TestNewDefaultClient_InvalidNegativeRefresh(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("expected a panic")
}
}()
sourceMock := NewDummySource(&jose.JSONWebKeySet{})
NewDefaultClient(sourceMock, time.Second, -1)
}
func TestNewDefaultClient_InvalidRefreshBiggerThanTtl(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("expected a panic")
}
}()
sourceMock := NewDummySource(&jose.JSONWebKeySet{})
NewDefaultClient(sourceMock, time.Minute, time.Second)
}