-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathtoken_test.go
129 lines (104 loc) · 3.51 KB
/
token_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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package uaa_test
import (
"errors"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/cloudfoundry/bosh-cli/v7/uaa"
)
var _ = Describe("AccessTokenImpl", func() {
var token AccessToken
BeforeEach(func() {
token = NewAccessToken("token-type", "token-value")
})
Describe("IsValid", func() {
It("is invalid if it has an empty type", func() {
token = NewAccessToken("", "token-value")
Expect(token.IsValid()).To(BeFalse())
})
It("is invalid if it has an empty value", func() {
token = NewAccessToken("token-type", "")
Expect(token.IsValid()).To(BeFalse())
})
It("is valid if it has both type and value", func() {
token = NewAccessToken("token-type", "token-value")
Expect(token.IsValid()).To(BeTrue())
})
})
Describe("Type", func() {
It("returns", func() {
Expect(token.Type()).To(Equal("token-type"))
})
})
Describe("Value", func() {
It("returns", func() {
Expect(token.Value()).To(Equal("token-value"))
})
})
})
var _ = Describe("RefreshableAccessTokenImpl", func() {
var token RefreshableAccessToken
BeforeEach(func() {
token = NewRefreshableAccessToken("token-type", "token-value", "refresh-value")
})
Describe("Type", func() {
It("returns", func() {
Expect(token.Type()).To(Equal("token-type"))
})
})
Describe("Value", func() {
It("returns", func() {
Expect(token.Value()).To(Equal("token-value"))
})
})
Describe("RefreshValue", func() {
It("returns", func() {
Expect(token.RefreshValue()).To(Equal("refresh-value"))
})
})
Describe("IsValid", func() {
It("is invalid if it has an empty type", func() {
token = NewRefreshableAccessToken("", "token-value", "refresh-value")
Expect(token.IsValid()).To(BeFalse())
})
It("is invalid if it has an empty value", func() {
token = NewRefreshableAccessToken("token-type", "", "refresh-value")
Expect(token.IsValid()).To(BeFalse())
})
It("is valid if it has both type and value", func() {
token = NewRefreshableAccessToken("token-type", "token-value", "refresh-value")
Expect(token.IsValid()).To(BeTrue())
})
})
It("panics if refresh value is empty", func() {
Expect(func() { NewRefreshableAccessToken("access-token-type", "access-token", "") }).To(Panic())
})
})
var _ = Describe("NewTokenInfoFromValue", func() {
It("returns parsed token", func() {
info, err := NewTokenInfoFromValue("seg.eyJ1c2VyX25hbWUiOiJhZG1pbiIsInNjb3BlIjpbIm9wZW5pZCIsImJvc2guYWRtaW4iXSwiZXhwIjoxMjN9.seg")
Expect(err).ToNot(HaveOccurred())
Expect(info).To(Equal(TokenInfo{
Username: "admin",
Scopes: []string{"openid", "bosh.admin"},
ExpiredAt: 123,
}))
})
It("returns an error if token doesnt have 3 segments", func() {
_, err := NewTokenInfoFromValue("seg")
Expect(err).To(Equal(errors.New("Expected token value to have 3 segments")))
_, err = NewTokenInfoFromValue("seg.seg")
Expect(err).To(Equal(errors.New("Expected token value to have 3 segments")))
_, err = NewTokenInfoFromValue("seg.seg.seg.seg")
Expect(err).To(Equal(errors.New("Expected token value to have 3 segments")))
})
It("returns an error if token's 2nd segment cannot be decoded", func() {
_, err := NewTokenInfoFromValue("seg.eyJrZXkiOiJ2YWx1ZXoifQ==.seg")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("Decoding token info"))
})
It("returns an error if token's 2nd segment cannot unmarshaled", func() {
_, err := NewTokenInfoFromValue("seg.a2V5.seg")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("Unmarshaling token info"))
})
})