forked from cloudfoundry/go-uaa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generated_mfaprovider_test.go
280 lines (251 loc) · 10.1 KB
/
generated_mfaprovider_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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Code generated by go-uaa/generator; DO NOT EDIT.
package uaa_test
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"testing"
uaa "github.com/cloudfoundry-community/go-uaa"
. "github.com/onsi/gomega"
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"
)
func TestMFAProvider(t *testing.T) {
spec.Run(t, "", testMFAProvider, spec.Report(report.Terminal{}))
}
func testMFAProvider(t *testing.T, when spec.G, it spec.S) {
var (
s *httptest.Server
handler http.Handler
called int
a *uaa.API
)
it.Before(func() {
RegisterTestingT(t)
called = 0
s = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
called = called + 1
Expect(handler).NotTo(BeNil())
handler.ServeHTTP(w, req)
}))
c := &http.Client{Transport: http.DefaultTransport}
u, _ := url.Parse(s.URL)
a = &uaa.API{
TargetURL: u,
AuthenticatedClient: c,
UnauthenticatedClient: c,
}
})
it.After(func() {
if s != nil {
s.Close()
}
})
when("GetMFAProvider()", func() {
when("the mfaprovider is returned from the server", func() {
it.Before(func() {
handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
Expect(req.Header.Get("Accept")).To(Equal("application/json"))
Expect(req.URL.Path).To(Equal(uaa.MFAProvidersEndpoint + "/00000000-0000-0000-0000-000000000001"))
w.WriteHeader(http.StatusOK)
w.Write([]byte(mfaproviderResponse))
})
})
it("gets the mfaprovider from the UAA by ID", func() {
mfaprovider, err := a.GetMFAProvider("00000000-0000-0000-0000-000000000001")
Expect(err).NotTo(HaveOccurred())
Expect(mfaprovider.ID).To(Equal("00000000-0000-0000-0000-000000000001"))
})
})
when("the server errors", func() {
it.Before(func() {
handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
Expect(req.Header.Get("Accept")).To(Equal("application/json"))
Expect(req.URL.Path).To(Equal(uaa.MFAProvidersEndpoint + "/00000000-0000-0000-0000-000000000001"))
w.WriteHeader(http.StatusInternalServerError)
})
})
it("returns helpful error", func() {
mfaprovider, err := a.GetMFAProvider("00000000-0000-0000-0000-000000000001")
Expect(err).To(HaveOccurred())
Expect(mfaprovider).To(BeNil())
Expect(err.Error()).To(ContainSubstring("An unknown error occurred while calling"))
})
})
when("the server returns unparsable mfaproviders", func() {
it.Before(func() {
handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
Expect(req.Header.Get("Accept")).To(Equal("application/json"))
Expect(req.URL.Path).To(Equal(uaa.MFAProvidersEndpoint + "/00000000-0000-0000-0000-000000000001"))
w.WriteHeader(http.StatusOK)
w.Write([]byte("{unparsable-json-response}"))
})
})
it("returns helpful error", func() {
mfaprovider, err := a.GetMFAProvider("00000000-0000-0000-0000-000000000001")
Expect(err).To(HaveOccurred())
Expect(mfaprovider).To(BeNil())
Expect(err.Error()).To(ContainSubstring("An unknown error occurred while parsing response from"))
Expect(err.Error()).To(ContainSubstring("Response was {unparsable-json-response}"))
})
})
})
when("CreateMFAProvider()", func() {
it("performs a POST with the mfaprovider data and returns the created mfaprovider", func() {
handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
Expect(req.Header.Get("Accept")).To(Equal("application/json"))
Expect(req.Header.Get("Content-Type")).To(Equal("application/json"))
Expect(req.Method).To(Equal(http.MethodPost))
Expect(req.URL.Path).To(Equal(uaa.MFAProvidersEndpoint))
defer req.Body.Close()
body, _ := ioutil.ReadAll(req.Body)
Expect(body).To(MatchJSON(testMFAProviderJSON))
w.WriteHeader(http.StatusCreated)
w.Write([]byte(mfaproviderResponse))
})
created, err := a.CreateMFAProvider(testMFAProviderValue)
Expect(called).To(Equal(1))
Expect(err).NotTo(HaveOccurred())
Expect(created).NotTo(BeNil())
})
it("returns error when response cannot be parsed", func() {
handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
Expect(req.Method).To(Equal(http.MethodPost))
Expect(req.URL.Path).To(Equal(uaa.MFAProvidersEndpoint))
w.WriteHeader(http.StatusOK)
w.Write([]byte("{unparseable}"))
})
created, err := a.CreateMFAProvider(testMFAProviderValue)
Expect(err).To(HaveOccurred())
Expect(created).To(BeNil())
})
it("returns error when response is not 200 OK", func() {
handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
Expect(req.Method).To(Equal(http.MethodPost))
Expect(req.URL.Path).To(Equal(uaa.MFAProvidersEndpoint))
w.WriteHeader(http.StatusBadRequest)
})
created, err := a.CreateMFAProvider(testMFAProviderValue)
Expect(err).To(HaveOccurred())
Expect(created).To(BeNil())
})
})
when("UpdateMFAProvider()", func() {
it("performs a PUT with the mfaprovider data and returns the updated mfaprovider", func() {
handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
Expect(req.Header.Get("Accept")).To(Equal("application/json"))
Expect(req.Header.Get("Content-Type")).To(Equal("application/json"))
Expect(req.Method).To(Equal(http.MethodPut))
Expect(req.URL.Path).To(Equal(uaa.MFAProvidersEndpoint + "/00000000-0000-0000-0000-000000000001"))
defer req.Body.Close()
body, _ := ioutil.ReadAll(req.Body)
Expect(body).To(MatchJSON(testMFAProviderJSON))
w.WriteHeader(http.StatusOK)
w.Write([]byte(mfaproviderResponse))
})
updated, err := a.UpdateMFAProvider(testMFAProviderValue)
Expect(called).To(Equal(1))
Expect(err).NotTo(HaveOccurred())
Expect(updated).NotTo(BeNil())
})
it("returns error when response cannot be parsed", func() {
handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
Expect(req.Method).To(Equal(http.MethodPut))
Expect(req.URL.Path).To(Equal(uaa.MFAProvidersEndpoint + "/00000000-0000-0000-0000-000000000001"))
w.WriteHeader(http.StatusOK)
w.Write([]byte("{unparseable}"))
})
updated, err := a.UpdateMFAProvider(testMFAProviderValue)
Expect(err).To(HaveOccurred())
Expect(updated).To(BeNil())
})
it("returns error when response is not 200 OK", func() {
handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
Expect(req.Method).To(Equal(http.MethodPut))
Expect(req.URL.Path).To(Equal(uaa.MFAProvidersEndpoint + "/00000000-0000-0000-0000-000000000001"))
w.WriteHeader(http.StatusBadRequest)
})
updated, err := a.UpdateMFAProvider(testMFAProviderValue)
Expect(err).To(HaveOccurred())
Expect(updated).To(BeNil())
})
})
when("DeleteMFAProvider()", func() {
it("errors when the mfaproviderID is empty", func() {
deleted, err := a.DeleteMFAProvider("")
Expect(called).To(Equal(0))
Expect(err).To(HaveOccurred())
Expect(deleted).To(BeNil())
})
it("performs a DELETE for the mfaprovider", func() {
handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
Expect(req.Header.Get("Accept")).To(Equal("application/json"))
Expect(req.Method).To(Equal(http.MethodDelete))
Expect(req.URL.Path).To(Equal(uaa.MFAProvidersEndpoint + "/00000000-0000-0000-0000-000000000001"))
w.WriteHeader(http.StatusOK)
w.Write([]byte(mfaproviderResponse))
})
deleted, err := a.DeleteMFAProvider("00000000-0000-0000-0000-000000000001")
Expect(called).To(Equal(1))
Expect(err).NotTo(HaveOccurred())
Expect(deleted).NotTo(BeNil())
})
it("returns error when response cannot be parsed", func() {
handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
Expect(req.Method).To(Equal(http.MethodDelete))
Expect(req.URL.Path).To(Equal(uaa.MFAProvidersEndpoint + "/00000000-0000-0000-0000-000000000001"))
w.WriteHeader(http.StatusOK)
w.Write([]byte("{unparseable}"))
})
deleted, err := a.DeleteMFAProvider("00000000-0000-0000-0000-000000000001")
Expect(err).To(HaveOccurred())
Expect(deleted).To(BeNil())
})
it("returns error when response is not 200 OK", func() {
handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
Expect(req.Method).To(Equal(http.MethodDelete))
Expect(req.URL.Path).To(Equal(uaa.MFAProvidersEndpoint + "/00000000-0000-0000-0000-000000000001"))
w.WriteHeader(http.StatusBadRequest)
})
deleted, err := a.DeleteMFAProvider("00000000-0000-0000-0000-000000000001")
Expect(err).To(HaveOccurred())
Expect(deleted).To(BeNil())
})
})
when("ListMFAProviders()", func() {
it("can accept a filter query to limit results", func() {
handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
Expect(req.Header.Get("Accept")).To(Equal("application/json"))
Expect(req.URL.Path).To(Equal(uaa.MFAProvidersEndpoint))
w.WriteHeader(http.StatusOK)
w.Write([]byte(mfaproviderListResponse))
})
mfaproviderList, err := a.ListMFAProviders()
Expect(err).NotTo(HaveOccurred())
Expect(mfaproviderList[0].ID).To(Equal("00000000-0000-0000-0000-000000000001"))
Expect(mfaproviderList[1].ID).To(Equal("00000000-0000-0000-0000-000000000002"))
})
it("returns an error when the endpoint doesn't respond", func() {
handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
Expect(req.Header.Get("Accept")).To(Equal("application/json"))
Expect(req.URL.Path).To(Equal(uaa.MFAProvidersEndpoint))
w.WriteHeader(http.StatusInternalServerError)
})
mfaproviderList, err := a.ListMFAProviders()
Expect(err).To(HaveOccurred())
Expect(mfaproviderList).To(BeNil())
})
it("returns an error when response is unparseable", func() {
handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
Expect(req.Header.Get("Accept")).To(Equal("application/json"))
Expect(req.URL.Path).To(Equal(uaa.MFAProvidersEndpoint))
w.WriteHeader(http.StatusOK)
w.Write([]byte("{unparsable}"))
})
mfaproviderList, err := a.ListMFAProviders()
Expect(err).To(HaveOccurred())
Expect(mfaproviderList).To(BeNil())
})
})
}