forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusers_test.go
227 lines (199 loc) · 7.13 KB
/
users_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
package api_test
import (
"net/http"
"time"
"code.cloudfoundry.org/cli/cf/api"
"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
"code.cloudfoundry.org/cli/cf/errors"
"code.cloudfoundry.org/cli/cf/models"
"code.cloudfoundry.org/cli/cf/net"
"code.cloudfoundry.org/cli/cf/trace/tracefakes"
"code.cloudfoundry.org/cli/cf/terminal/terminalfakes"
testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/ghttp"
)
var _ = Describe("UserRepository", func() {
var (
client api.UserRepository
config coreconfig.ReadWriter
ccServer *ghttp.Server
uaaServer *ghttp.Server
ccGateway net.Gateway
uaaGateway net.Gateway
)
BeforeEach(func() {
ccServer = ghttp.NewServer()
uaaServer = ghttp.NewServer()
config = testconfig.NewRepositoryWithDefaults()
config.SetAPIEndpoint(ccServer.URL())
config.SetUaaEndpoint(uaaServer.URL())
ccGateway = net.NewCloudControllerGateway(config, time.Now, new(terminalfakes.FakeUI), new(tracefakes.FakePrinter), "")
uaaGateway = net.NewUAAGateway(config, new(terminalfakes.FakeUI), new(tracefakes.FakePrinter), "")
client = api.NewCloudControllerUserRepository(config, uaaGateway, ccGateway)
})
AfterEach(func() {
if ccServer != nil {
ccServer.Close()
}
if uaaServer != nil {
uaaServer.Close()
}
})
Describe("FindByUsername", func() {
Context("when a username has multiple origins", func() {
BeforeEach(func() {
uaaServer.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", "/Users"),
ghttp.RespondWith(http.StatusOK, `{
"resources": [
{ "id": "user-1-guid", "userName": "some-user" },
{ "id": "user-2-guid", "userName": "some-user" }
]}`),
),
)
})
It("returns an error", func() {
_, err := client.FindByUsername("some-user")
Expect(err).To(MatchError("The user exists in multiple origins."))
})
})
})
Describe("ListUsersInOrgForRoleWithNoUAA", func() {
Context("when there are users in the given org with the given role", func() {
BeforeEach(func() {
ccServer.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", "/v2/organizations/org-guid/managers"),
ghttp.VerifyHeader(http.Header{
"accept": []string{"application/json"},
}),
ghttp.RespondWith(http.StatusOK, `{
"resources":[
{"metadata": {"guid": "user-1-guid"}, "entity": {}}
]}`),
),
)
})
It("makes a request to CC", func() {
_, err := client.ListUsersInOrgForRoleWithNoUAA("org-guid", models.RoleOrgManager)
Expect(err).NotTo(HaveOccurred())
Expect(ccServer.ReceivedRequests()).To(HaveLen(1))
})
It("does not make a request to UAA", func() {
_, err := client.ListUsersInOrgForRoleWithNoUAA("org-guid", models.RoleOrgManager)
Expect(err).NotTo(HaveOccurred())
Expect(uaaServer.ReceivedRequests()).To(BeZero())
})
It("returns the users", func() {
users, err := client.ListUsersInOrgForRoleWithNoUAA("org-guid", models.RoleOrgManager)
Expect(err).NotTo(HaveOccurred())
Expect(len(users)).To(Equal(1))
Expect(users[0].GUID).To(Equal("user-1-guid"))
Expect(users[0].Username).To(BeEmpty())
})
})
Context("when there are multiple pages of users in the given org with the given role", func() {
BeforeEach(func() {
ccServer.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", "/v2/organizations/org-guid/managers"),
ghttp.VerifyHeader(http.Header{
"accept": []string{"application/json"},
}),
ghttp.RespondWith(http.StatusOK, `{
"next_url": "/v2/organizations/org-guid/managers?page=2",
"resources":[
{"metadata": {"guid": "user-1-guid"}, "entity": {}}
]}`),
),
ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", "/v2/organizations/org-guid/managers", "page=2"),
ghttp.VerifyHeader(http.Header{
"accept": []string{"application/json"},
}),
ghttp.RespondWith(http.StatusOK, `{
"resources":[
{"metadata": {"guid": "user-2-guid"}, "entity": {"username":"user 2 from cc"}},
{"metadata": {"guid": "user-3-guid"}, "entity": {"username":"user 3 from cc"}}
]}`),
),
)
})
It("makes a request to CC for each page of results", func() {
_, err := client.ListUsersInOrgForRoleWithNoUAA("org-guid", models.RoleOrgManager)
Expect(err).NotTo(HaveOccurred())
Expect(ccServer.ReceivedRequests()).To(HaveLen(2))
})
It("does not make a request to UAA", func() {
_, err := client.ListUsersInOrgForRoleWithNoUAA("org-guid", models.RoleOrgManager)
Expect(err).NotTo(HaveOccurred())
Expect(uaaServer.ReceivedRequests()).To(BeZero())
})
It("returns all paginated users", func() {
users, err := client.ListUsersInOrgForRoleWithNoUAA("org-guid", models.RoleOrgManager)
Expect(err).NotTo(HaveOccurred())
Expect(len(users)).To(Equal(3))
Expect(users[0].GUID).To(Equal("user-1-guid"))
Expect(users[0].Username).To(BeEmpty())
Expect(users[1].GUID).To(Equal("user-2-guid"))
Expect(users[1].Username).To(Equal("user 2 from cc"))
Expect(users[2].GUID).To(Equal("user-3-guid"))
Expect(users[2].Username).To(Equal("user 3 from cc"))
})
})
Context("when there are no users in the given org with the given role", func() {
BeforeEach(func() {
ccServer.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", "/v2/organizations/org-guid/managers"),
ghttp.VerifyHeader(http.Header{
"accept": []string{"application/json"},
}),
ghttp.RespondWith(http.StatusOK, `{"resources":[]}`),
),
)
})
It("makes a request to CC", func() {
_, err := client.ListUsersInOrgForRoleWithNoUAA("org-guid", models.RoleOrgManager)
Expect(err).NotTo(HaveOccurred())
Expect(ccServer.ReceivedRequests()).To(HaveLen(1))
})
It("does not make a request to UAA", func() {
_, err := client.ListUsersInOrgForRoleWithNoUAA("org-guid", models.RoleOrgManager)
Expect(err).NotTo(HaveOccurred())
Expect(uaaServer.ReceivedRequests()).To(BeZero())
})
It("returns no users", func() {
users, err := client.ListUsersInOrgForRoleWithNoUAA("org-guid", models.RoleOrgManager)
Expect(err).NotTo(HaveOccurred())
Expect(len(users)).To(Equal(0))
})
})
Context("when CC returns an error", func() {
BeforeEach(func() {
ccServer.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", "/v2/organizations/org-guid/managers"),
ghttp.VerifyHeader(http.Header{
"accept": []string{"application/json"},
}),
ghttp.RespondWith(http.StatusGatewayTimeout, nil),
),
)
})
It("does not make a request to UAA", func() {
client.ListUsersInOrgForRoleWithNoUAA("org-guid", models.RoleOrgManager)
Expect(uaaServer.ReceivedRequests()).To(BeZero())
})
It("returns an error", func() {
_, err := client.ListUsersInOrgForRoleWithNoUAA("org-guid", models.RoleOrgManager)
httpErr, ok := err.(errors.HTTPError)
Expect(ok).To(BeTrue())
Expect(httpErr.StatusCode()).To(Equal(http.StatusGatewayTimeout))
})
})
})
})