forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_command_test.go
210 lines (175 loc) · 5.98 KB
/
api_command_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
package v2_test
import (
"errors"
"code.cloudfoundry.org/cli/api/cloudcontrollerv2"
"code.cloudfoundry.org/cli/commands/commandsfakes"
"code.cloudfoundry.org/cli/commands/ui"
. "code.cloudfoundry.org/cli/commands/v2"
"code.cloudfoundry.org/cli/commands/v2/v2fakes"
"code.cloudfoundry.org/cli/utils/config"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gbytes"
)
var _ = Describe("API Command", func() {
var (
cmd ApiCommand
fakeUI ui.UI
fakeActor *v2fakes.FakeAPIConfigActor
fakeConfig *commandsfakes.FakeConfig
)
BeforeEach(func() {
out := NewBuffer()
fakeUI = ui.NewTestUI(out, out)
fakeActor = new(v2fakes.FakeAPIConfigActor)
fakeConfig = new(commandsfakes.FakeConfig)
cmd = ApiCommand{
UI: fakeUI,
Actor: fakeActor,
Config: fakeConfig,
}
})
Context("when the API URL is not provided", func() {
var err error
JustBeforeEach(func() {
err = cmd.Execute([]string{})
})
Context("when the API is not set", func() {
It("displays a tip", func() {
Expect(err).ToNot(HaveOccurred())
Expect(fakeUI.Out).To(Say("No api endpoint set. Use 'cf api' to set an endpoint"))
})
})
Context("when the API is set", func() {
BeforeEach(func() {
fakeConfig.TargetReturns("some-api-target")
fakeConfig.APIVersionReturns("some-version")
fakeConfig.TargetedOrganizationReturns(config.Organization{
Name: "some-org",
})
fakeConfig.TargetedSpaceReturns(config.Space{
Name: "some-space",
})
fakeConfig.CurrentUserReturns(config.User{
Name: "admin",
}, nil)
})
It("outputs the standard target information", func() {
Expect(err).ToNot(HaveOccurred())
Expect(fakeUI.Out).To(Say("API endpoint:\\s+some-api-target"))
Expect(fakeUI.Out).To(Say("API version:\\s+some-version"))
Expect(fakeUI.Out).To(Say("User:\\s+admin"))
Expect(fakeUI.Out).To(Say("Org:\\s+some-org"))
Expect(fakeUI.Out).To(Say("Space:\\s+some-space"))
})
})
Context("when passed a --unset", func() {
BeforeEach(func() {
cmd.Unset = true
})
It("clears the target", func() {
Expect(err).ToNot(HaveOccurred())
Expect(fakeUI.Out).To(Say("Unsetting api endpoint..."))
Expect(fakeUI.Out).To(Say("OK"))
Expect(fakeActor.ClearTargetCallCount()).To(Equal(1))
})
})
})
Context("when a valid api endpoint is specified", func() {
Context("when the API has SSL", func() {
Context("with no protocol", func() {
var (
CCAPI string
err error
)
BeforeEach(func() {
CCAPI = "api.foo.com"
cmd.OptionalArgs.URL = CCAPI
fakeConfig.TargetReturns("some-api-target")
fakeConfig.APIVersionReturns("some-version")
})
JustBeforeEach(func() {
err = cmd.Execute([]string{})
})
Context("when the url has verified SSL", func() {
It("sets the target", func() {
Expect(err).ToNot(HaveOccurred())
Expect(fakeActor.SetTargetCallCount()).To(Equal(1))
url, skipSSLValidation := fakeActor.SetTargetArgsForCall(0)
Expect(url).To(Equal("https://" + CCAPI))
Expect(skipSSLValidation).To(BeFalse())
Expect(fakeUI.Out).To(Say("Setting api endpoint to %s...", CCAPI))
Expect(fakeUI.Out).To(Say("OK"))
Expect(fakeUI.Out).To(Say("API endpoint:\\s+some-api-target"))
Expect(fakeUI.Out).To(Say("API version:\\s+some-version"))
Expect(fakeUI.Out).To(Say("User:"))
Expect(fakeUI.Out).To(Say("Org:"))
Expect(fakeUI.Out).To(Say("Space:"))
})
})
Context("when the url has unverified SSL", func() {
Context("when --skip-ssl-validation is passed", func() {
BeforeEach(func() {
cmd.SkipSSLValidation = true
})
It("sets the target", func() {
Expect(err).ToNot(HaveOccurred())
Expect(fakeActor.SetTargetCallCount()).To(Equal(1))
url, skipSSLValidation := fakeActor.SetTargetArgsForCall(0)
Expect(url).To(Equal("https://" + CCAPI))
Expect(skipSSLValidation).To(BeTrue())
Expect(fakeUI.Out).To(Say("Setting api endpoint to %s...", CCAPI))
Expect(fakeUI.Out).To(Say("OK"))
Expect(fakeUI.Out).To(Say("API endpoint:\\s+some-api-target"))
Expect(fakeUI.Out).To(Say("API version:\\s+some-version"))
Expect(fakeUI.Out).To(Say("User:"))
Expect(fakeUI.Out).To(Say("Org:"))
Expect(fakeUI.Out).To(Say("Space:"))
})
})
Context("when no additional flags are passed", func() {
BeforeEach(func() {
fakeActor.SetTargetReturns(nil, cloudcontrollerv2.UnverifiedServerError{})
})
It("returns an error with a --skip-ssl-validation tip", func() {
Expect(err).To(MatchError(ui.InvalidSSLCertError{API: CCAPI}))
Expect(fakeUI.Out).ToNot(Say("API endpoint:\\s+some-api-target"))
})
})
})
})
Context("when the API does not have SSL", func() {
var CCAPI string
BeforeEach(func() {
CCAPI = "http://api.foo.com"
cmd.OptionalArgs.URL = CCAPI
})
It("sets the target with a warning", func() {
err := cmd.Execute([]string{})
Expect(err).ToNot(HaveOccurred())
Expect(fakeActor.SetTargetCallCount()).To(Equal(1))
url, skipSSLValidation := fakeActor.SetTargetArgsForCall(0)
Expect(url).To(Equal(CCAPI))
Expect(skipSSLValidation).To(BeFalse())
Expect(fakeUI.Out).To(Say("Setting api endpoint to %s...", CCAPI))
Expect(fakeUI.Out).To(Say("Warning: Insecure http API endpoint detected: secure https API endpoints are recommended"))
Expect(fakeUI.Out).To(Say("OK"))
})
})
})
Context("when URL host does not exist", func() {
var CCAPI string
var expectedError error
BeforeEach(func() {
CCAPI = "i.do.not.exist.com"
cmd.OptionalArgs.URL = CCAPI
expectedError = cloudcontrollerv2.RequestError(errors.New("I am an error"))
fakeActor.SetTargetReturns(nil, expectedError)
})
It("sets the target with a warning", func() {
err := cmd.Execute([]string{})
Expect(err).To(MatchError(ui.APIRequestError{Err: expectedError}))
})
})
})
})