-
Notifications
You must be signed in to change notification settings - Fork 21
/
info_test.go
132 lines (118 loc) · 4.06 KB
/
info_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
package uaa_test
import (
"net/http"
"net/http/httptest"
"testing"
uaa "github.com/cloudfoundry-community/go-uaa"
. "github.com/onsi/gomega"
"github.com/sclevine/spec"
)
const InfoResponseJSON string = `{
"app": {
"version": "4.5.0"
},
"links": {
"uaa": "https://uaa.run.pivotal.io",
"passwd": "https://account.run.pivotal.io/forgot-password",
"login": "https://login.run.pivotal.io",
"register": "https://account.run.pivotal.io/sign-up"
},
"zone_name": "uaa",
"entityID": "login.run.pivotal.io",
"commit_id": "df80f63",
"idpDefinitions": {
"SAML" : "http://localhost:8080/uaa/saml/discovery?returnIDParam=idp&entityID=cloudfoundry-saml-login&idp=SAML&isPassive=true"
},
"prompts": {
"username": [
"text",
"Email"
],
"password": [
"password",
"Password"
]
},
"timestamp": "2017-07-21T22:45:01+0000"
}`
func testInfo(t *testing.T, when spec.G, it spec.S) {
var (
s *httptest.Server
a *uaa.API
h http.Handler
handlerCalls int
)
it.Before(func() {
RegisterTestingT(t)
handlerCalls = 0
s = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
handlerCalls = handlerCalls + 1
h.ServeHTTP(w, req)
}))
a, _ = uaa.New(s.URL, uaa.WithNoAuthentication())
})
it.After(func() {
if s != nil {
s.Close()
}
})
when("the info endpoint responds with valid info", func() {
it.Before(func() {
h = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
Expect(req.Method).To(Equal(http.MethodGet))
Expect(req.URL.Path).To(Equal("/info"))
Expect(req.Header.Get("Accept")).To(Equal("application/json"))
_, err := w.Write([]byte(InfoResponseJSON))
Expect(err).NotTo(HaveOccurred())
})
})
it("calls the /info endpoint", func() {
infoResponse, _ := a.GetInfo()
Expect(handlerCalls).To(Equal(1))
Expect(infoResponse.App.Version).To(Equal("4.5.0"))
Expect(infoResponse.Links.ForgotPassword).To(Equal("https://account.run.pivotal.io/forgot-password"))
Expect(infoResponse.Links.Uaa).To(Equal("https://uaa.run.pivotal.io"))
Expect(infoResponse.Links.Registration).To(Equal("https://account.run.pivotal.io/sign-up"))
Expect(infoResponse.Links.Login).To(Equal("https://login.run.pivotal.io"))
Expect(infoResponse.ZoneName).To(Equal("uaa"))
Expect(infoResponse.EntityID).To(Equal("login.run.pivotal.io"))
Expect(infoResponse.CommitID).To(Equal("df80f63"))
Expect(infoResponse.IdpDefinitions["SAML"]).To(Equal("http://localhost:8080/uaa/saml/discovery?returnIDParam=idp&entityID=cloudfoundry-saml-login&idp=SAML&isPassive=true"))
Expect(infoResponse.Prompts["username"]).To(Equal([]string{"text", "Email"}))
Expect(infoResponse.Prompts["password"]).To(Equal([]string{"password", "Password"}))
Expect(infoResponse.Timestamp).To(Equal("2017-07-21T22:45:01+0000"))
})
})
when("the info endpoint responds with an error", func() {
it.Before(func() {
h = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
Expect(req.Method).To(Equal(http.MethodGet))
Expect(req.URL.Path).To(Equal("/info"))
Expect(req.Header.Get("Accept")).To(Equal("application/json"))
w.WriteHeader(http.StatusInternalServerError)
})
})
it("returns a helpful error", func() {
_, err := a.GetInfo()
Expect(err).NotTo(BeNil())
Expect(err.Error()).To(ContainSubstring("An error occurred while calling"))
})
})
when("the info endpoint responds with unparsable JSON", func() {
it.Before(func() {
h = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
Expect(req.Method).To(Equal(http.MethodGet))
Expect(req.URL.Path).To(Equal("/info"))
Expect(req.Header.Get("Accept")).To(Equal("application/json"))
_, err := w.Write([]byte("{unparsable-json-response}"))
Expect(err).NotTo(HaveOccurred())
})
})
it("returns a helpful error", func() {
_, err := a.GetInfo()
Expect(err).NotTo(BeNil())
Expect(err.Error()).To(ContainSubstring("An unknown error occurred while parsing response from"))
Expect(err.Error()).To(ContainSubstring("Response was {unparsable-json-response}"))
})
})
}