-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathuaa_test.go
190 lines (163 loc) · 5.72 KB
/
uaa_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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package uaa_test
import (
"net/http"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/ghttp"
. "github.com/cloudfoundry/bosh-cli/v7/uaa"
)
var _ = Describe("UAA", func() {
var (
uaa UAA
server *ghttp.Server
)
BeforeEach(func() {
uaa, server = BuildServer()
})
AfterEach(func() {
server.Close()
})
Describe("RefreshTokenGrant", func() {
It("returns a new access token that can only be refreshed", func() {
server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("POST", "/oauth/token"),
ghttp.VerifyBody([]byte("grant_type=refresh_token&refresh_token=refresh-token")),
ghttp.VerifyHeader(http.Header{"Content-Type": []string{"application/x-www-form-urlencoded"}}),
ghttp.RespondWith(http.StatusOK, `{
"token_type": "new-bearer",
"access_token": "new-access-token",
"refresh_token": "new-refresh-token"
}`),
),
)
newToken, err := uaa.RefreshTokenGrant("refresh-token")
Expect(err).ToNot(HaveOccurred())
Expect(newToken.Type()).To(Equal("new-bearer"))
Expect(newToken.Value()).To(Equal("new-access-token"))
newRefreshToken, refreshable := newToken.(RefreshableAccessToken)
Expect(refreshable).To(BeTrue())
Expect(newRefreshToken.RefreshValue()).To(Equal("new-refresh-token"))
})
It("returns error if token response is non-200", func() {
server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("POST", "/oauth/token"),
ghttp.RespondWith(http.StatusBadRequest, ``),
),
)
_, err := uaa.RefreshTokenGrant("refresh-token")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("UAA responded with non-successful status code"))
})
It("returns error if token cannot be unmarshalled", func() {
server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("POST", "/oauth/token"),
ghttp.RespondWith(http.StatusOK, ``),
),
)
_, err := uaa.RefreshTokenGrant("refresh-token")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("Unmarshaling UAA response"))
})
})
Describe("ClientCredentialsGrant", func() {
It("obtains client token", func() {
server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("POST", "/oauth/token"),
ghttp.VerifyBody([]byte("grant_type=client_credentials")),
ghttp.VerifyHeader(http.Header{"Content-Type": []string{"application/x-www-form-urlencoded"}}),
ghttp.VerifyBasicAuth("client", "client-secret"),
ghttp.VerifyHeader(http.Header{
"Accept": []string{"application/json"},
}),
ghttp.RespondWith(http.StatusOK, `{
"token_type": "bearer",
"access_token": "access-token"
}`),
),
)
token, err := uaa.ClientCredentialsGrant()
Expect(err).ToNot(HaveOccurred())
Expect(token.Type()).To(Equal("bearer"))
Expect(token.Value()).To(Equal("access-token"))
})
It("returns error if prompts response in non-200", func() {
server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("POST", "/oauth/token"),
ghttp.RespondWith(http.StatusBadRequest, ``),
),
)
_, err := uaa.ClientCredentialsGrant()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("UAA responded with non-successful status code"))
})
It("returns error if prompts cannot be unmarshalled", func() {
server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("POST", "/oauth/token"),
ghttp.RespondWith(http.StatusOK, ``),
),
)
_, err := uaa.ClientCredentialsGrant()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("Unmarshaling UAA response"))
})
})
Describe("OwnerPasswordCredentialsGrant", func() {
It("obtains access token based on prompt answers", func() {
server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("POST", "/oauth/token"),
ghttp.VerifyBody([]byte("grant_type=password&key1=ans1&key2=ans2")),
ghttp.VerifyHeader(http.Header{"Content-Type": []string{"application/x-www-form-urlencoded"}}),
ghttp.VerifyBasicAuth("client", "client-secret"),
ghttp.VerifyHeader(http.Header{
"Accept": []string{"application/json"},
}),
ghttp.RespondWith(http.StatusOK, `{
"token_type": "bearer",
"access_token": "access-token",
"refresh_token": "refresh-token"
}`),
),
)
answers := []PromptAnswer{
{Key: "key1", Value: "ans1"},
{Key: "key2", Value: "ans2"},
}
token, err := uaa.OwnerPasswordCredentialsGrant(answers)
Expect(err).ToNot(HaveOccurred())
Expect(token.Type()).To(Equal("bearer"))
Expect(token.Value()).To(Equal("access-token"))
newRefreshToken, refreshable := token.(RefreshableAccessToken)
Expect(refreshable).To(BeTrue())
Expect(newRefreshToken.RefreshValue()).To(Equal("refresh-token"))
})
It("returns error if prompts response in non-200", func() {
server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("POST", "/oauth/token"),
ghttp.RespondWith(http.StatusBadRequest, ``),
),
)
_, err := uaa.OwnerPasswordCredentialsGrant(nil)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("UAA responded with non-successful status code"))
})
It("returns error if prompts cannot be unmarshalled", func() {
server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("POST", "/oauth/token"),
ghttp.RespondWith(http.StatusOK, ``),
),
)
_, err := uaa.OwnerPasswordCredentialsGrant(nil)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("Unmarshaling UAA response"))
})
})
})