forked from pivotal-cf/om
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_service_test.go
212 lines (185 loc) · 6.93 KB
/
setup_service_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
package api_test
import (
"fmt"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/ghttp"
"github.com/pivotal-cf/om/api"
"net/http"
)
var _ = Describe("Setup", func() {
var (
client *ghttp.Server
service api.Api
)
BeforeEach(func() {
client = ghttp.NewServer()
service = api.New(api.ApiInput{
UnauthedClient: httpClient{
client.URL(),
},
})
})
AfterEach(func() {
client.Close()
})
Describe("Setup", func() {
It("makes a request to setup the OpsManager", func() {
client.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("POST", "/api/v0/setup"),
ghttp.VerifyContentType("application/json"),
ghttp.RespondWith(http.StatusOK, `{
"setup": {
"identity_provider": "some-provider",
"admin_user_name": "some-username",
"admin_password": "some-password",
"admin_password_confirmation": "some-password-confirmation",
"decryption_passphrase": "some-passphrase",
"decryption_passphrase_confirmation":"some-passphrase-confirmation",
"eula_accepted": "true",
"http_proxy": "http://http-proxy.com",
"https_proxy": "http://https-proxy.com",
"no_proxy": "10.10.10.10,11.11.11.11"
}
}`),
),
)
output, err := service.Setup(api.SetupInput{
IdentityProvider: "some-provider",
AdminUserName: "some-username",
AdminPassword: "some-password",
AdminPasswordConfirmation: "some-password-confirmation",
DecryptionPassphrase: "some-passphrase",
DecryptionPassphraseConfirmation: "some-passphrase-confirmation",
EULAAccepted: "true",
HTTPProxyURL: "http://http-proxy.com",
HTTPSProxyURL: "http://https-proxy.com",
NoProxy: "10.10.10.10,11.11.11.11",
})
Expect(err).ToNot(HaveOccurred())
Expect(output).To(Equal(api.SetupOutput{}))
})
When("the client fails to make the request", func() {
It("returns an error", func() {
client.Close()
_, err := service.Setup(api.SetupInput{})
Expect(err).To(MatchError(ContainSubstring("could not make api request to setup endpoint")))
})
})
When("the api returns an unexpected status code", func() {
It("returns an error", func() {
client.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("POST", "/api/v0/setup"),
ghttp.RespondWith(http.StatusInternalServerError, `{"error": "something bad happened"}`),
),
)
_, err := service.Setup(api.SetupInput{})
Expect(err).To(MatchError(ContainSubstring("request failed: unexpected response")))
})
})
})
Describe("EnsureAvailability", func() {
When("the availability endpoint returns an unexpected status code", func() {
It("returns a helpful error", func() {
client.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", "/login/ensure_availability"),
ghttp.RespondWith(http.StatusTeapot, `{"error": "something bad happened"}`),
),
)
_, err := service.EnsureAvailability(api.EnsureAvailabilityInput{})
Expect(err).To(MatchError("Unexpected response code: 418 I'm a teapot"))
})
})
When("the availability endpoint returns an OK status with an unexpected body", func() {
It("returns a helpful error", func() {
client.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", "/login/ensure_availability"),
ghttp.RespondWith(http.StatusOK, "some body"),
),
)
_, err := service.EnsureAvailability(api.EnsureAvailabilityInput{})
Expect(err).To(MatchError("Received OK with an unexpected body: some body"))
})
})
When("the availability endpoint returns a found status with an unexpected location header", func() {
It("returns a helpful error", func() {
client.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", "/login/ensure_availability"),
ghttp.RespondWith(http.StatusFound, "", map[string][]string{"Location": {"https://some-opsman/something/else"}}),
),
)
_, err := service.EnsureAvailability(api.EnsureAvailabilityInput{})
Expect(err).To(MatchError("Unexpected redirect location: /something/else"))
})
})
When("the authentication mechanism has not been setup", func() {
It("makes a request to determine the availability of the OpsManager authentication mechanism", func() {
client.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", "/login/ensure_availability"),
ghttp.RespondWith(http.StatusFound, "", map[string][]string{"Location": {fmt.Sprintf("%s/setup", client.URL())}}),
),
)
output, err := service.EnsureAvailability(api.EnsureAvailabilityInput{})
Expect(err).ToNot(HaveOccurred())
Expect(output).To(Equal(api.EnsureAvailabilityOutput{
Status: api.EnsureAvailabilityStatusUnstarted,
}))
})
})
When("the authentication mechanism is currently being setup", func() {
It("makes a request to determine the availability of the OpsManager authentication mechanism", func() {
client.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", "/login/ensure_availability"),
ghttp.RespondWith(http.StatusOK, "Waiting for authentication system to start..."),
),
)
output, err := service.EnsureAvailability(api.EnsureAvailabilityInput{})
Expect(err).ToNot(HaveOccurred())
Expect(output).To(Equal(api.EnsureAvailabilityOutput{
Status: api.EnsureAvailabilityStatusPending,
}))
})
})
When("the authentication mechanism is completely setup", func() {
It("makes a request to determine the availability of the OpsManager authentication mechanism", func() {
client.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", "/login/ensure_availability"),
ghttp.RespondWith(http.StatusFound, "", map[string][]string{"Location": {"https://some-opsman/auth/cloudfoundry"}}),
),
)
output, err := service.EnsureAvailability(api.EnsureAvailabilityInput{})
Expect(err).ToNot(HaveOccurred())
Expect(output).To(Equal(api.EnsureAvailabilityOutput{
Status: api.EnsureAvailabilityStatusComplete,
}))
})
})
When("the request fails", func() {
It("returns an error", func() {
client.Close()
_, err := service.EnsureAvailability(api.EnsureAvailabilityInput{})
Expect(err).To(MatchError(ContainSubstring("could not make request round trip")))
})
})
When("the location header cannot be parsed", func() {
It("returns an error", func() {
client.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", "/login/ensure_availability"),
ghttp.RespondWith(http.StatusFound, "", map[string][]string{"Location": {"%%%%%%"}}),
),
)
_, err := service.EnsureAvailability(api.EnsureAvailabilityInput{})
Expect(err).To(MatchError(ContainSubstring(`parse "%%%%%%": invalid URL escape "%%%"`)))
})
})
})
})