-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathgroups_test.go
218 lines (195 loc) · 7.12 KB
/
groups_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
package uaa_test
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
uaa "github.com/cloudfoundry-community/go-uaa"
. "github.com/onsi/gomega"
"github.com/sclevine/spec"
)
const groupResponse string = `{
"id" : "00000000-0000-0000-0000-000000000001",
"meta" : {
"version" : 1,
"created" : "2017-01-15T16:54:15.677Z",
"lastModified" : "2017-08-15T16:54:15.677Z"
},
"displayName" : "cloud_controller.read",
"description" : "View details of your applications and services",
"members" : [ {
"origin" : "uaa",
"type" : "USER",
"value" : "fb5f32e1-5cb3-49e6-93df-6df9c8c8bd70"
} ],
"zoneID" : "uaa",
"schemas" : [ "urn:scim:schemas:core:1.0" ]
}`
var groupListResponse = fmt.Sprintf(PaginatedResponseTmpl, UaaAdminGroupResponse, CloudControllerReadGroupResponse)
const CloudControllerReadGroupResponse string = `{
"id" : "00000000-0000-0000-0000-000000000002",
"meta" : {
"version" : 1,
"created" : "2017-01-15T16:54:15.677Z",
"lastModified" : "2017-08-15T16:54:15.677Z"
},
"displayName" : "cloud_controller.read",
"description" : "View details of your applications and services",
"members" : [ {
"origin" : "uaa",
"type" : "USER",
"value" : "fb5f32e1-5cb3-49e6-93df-6df9c8c8bd70"
} ],
"zoneID" : "uaa",
"schemas" : [ "urn:scim:schemas:core:1.0" ]
}`
const UaaAdminGroupResponse string = `{
"id" : "00000000-0000-0000-0000-000000000001",
"meta" : {
"version" : 1,
"created" : "2017-01-15T16:54:15.677Z",
"lastModified" : "2017-08-15T16:54:15.677Z"
},
"displayName" : "uaa.admin",
"description" : "Act as an administrator throughout the UAA",
"members" : [ {
"origin" : "uaa",
"type" : "USER",
"value" : "fb5f32e1-5cb3-49e6-93df-6df9c8c8bd70"
} ],
"zoneID" : "uaa",
"schemas" : [ "urn:scim:schemas:core:1.0" ]
}`
var testGroupValue uaa.Group = uaa.Group{
ID: "00000000-0000-0000-0000-000000000001",
DisplayName: "uaa.admin",
}
const testGroupJSON string = `{ "id" : "00000000-0000-0000-0000-000000000001", "displayName": "uaa.admin" }`
func testGroupsExtra(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)
}))
a, _ = uaa.New(s.URL, uaa.WithNoAuthentication())
})
it.After(func() {
if s != nil {
s.Close()
}
})
when("GetGroupByName()", func() {
when("when no group name is specified", func() {
it("returns an error", func() {
_, err := a.GetGroupByName("", "")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal("group name may not be blank"))
})
})
when("when no origin is specified", func() {
it("looks up a group with a SCIM filter", func() {
group := uaa.Group{DisplayName: "uaa.admin"}
response := PaginatedResponse(group)
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.GroupsEndpoint))
Expect(req.URL.Query().Get("filter")).To(Equal(`displayName eq "uaa.admin"`))
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte(response))
Expect(err).NotTo(HaveOccurred())
})
g, err := a.GetGroupByName("uaa.admin", "")
Expect(err).NotTo(HaveOccurred())
Expect(g.DisplayName).To(Equal("uaa.admin"))
})
it("returns an error when request fails", 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.GroupsEndpoint))
Expect(req.URL.Query().Get("filter")).To(Equal(`displayName eq "uaa.admin"`))
w.WriteHeader(http.StatusInternalServerError)
})
_, err := a.GetGroupByName("uaa.admin", "")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("An error"))
})
it("returns an error when no groups are found", 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.GroupsEndpoint))
Expect(req.URL.Query().Get("filter")).To(Equal(`displayName eq "uaa.admin"`))
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte(PaginatedResponse()))
Expect(err).NotTo(HaveOccurred())
})
_, err := a.GetGroupByName("uaa.admin", "")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal(`group uaa.admin not found`))
})
})
when("when attributes are specified", func() {
it("adds them to the GET request", 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.GroupsEndpoint))
Expect(req.URL.Query().Get("filter")).To(Equal(`displayName eq "uaa.admin"`))
Expect(req.URL.Query().Get("attributes")).To(Equal(`displayName`))
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte(PaginatedResponse(uaa.Group{DisplayName: "uaa.admin"})))
Expect(err).NotTo(HaveOccurred())
})
_, err := a.GetGroupByName("uaa.admin", "displayName")
Expect(err).NotTo(HaveOccurred())
})
})
})
when("AddGroupMember()", func() {
it("adds a membership", func() {
membershipJSON := `{"origin":"uaa","type":"USER","value":"user-id-1"}`
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(fmt.Sprintf("%s/%s/members", uaa.GroupsEndpoint, "group-id-1")))
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte(membershipJSON))
Expect(err).NotTo(HaveOccurred())
})
err := a.AddGroupMember("group-id-1", "user-id-1", "", "")
Expect(err).NotTo(HaveOccurred())
Expect(called).To(Equal(1))
})
})
when("RemoveGroupMember", func() {
it("removes a membership", func() {
membershipJSON := `{"origin":"uaa","type":"USER","value":"user-id-1"}`
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(fmt.Sprintf("%s/%s/members", uaa.GroupsEndpoint, "group-id-1")))
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte(membershipJSON))
Expect(err).NotTo(HaveOccurred())
})
err := a.AddGroupMember("group-id-1", "user-id-1", "", "")
Expect(err).NotTo(HaveOccurred())
Expect(called).To(Equal(1))
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(fmt.Sprintf("%s/%s/members/%s", uaa.GroupsEndpoint, "group-id-1", "user-id-1")))
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte(membershipJSON))
Expect(err).NotTo(HaveOccurred())
})
err = a.RemoveGroupMember("group-id-1", "user-id-1", "", "")
Expect(err).NotTo(HaveOccurred())
Expect(called).To(Equal(2))
})
})
}