Skip to content

Commit cbce869

Browse files
committed
fix: dynamic group status is not checked for standard groups
1 parent 0343ca8 commit cbce869

File tree

6 files changed

+230
-39
lines changed

6 files changed

+230
-39
lines changed

app/group_exporter.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"ctRestClient/rest"
55
"encoding/json"
66
"fmt"
7+
"slices"
78
)
89

910
type GroupName2IDMap map[string]int
@@ -38,13 +39,20 @@ func (g groupExporter) ExportGroupMembers(
3839
return nil, fmt.Errorf("failed to get group by name: %v", err)
3940
}
4041

41-
dynamicGroup, err := dynamicGroupsEndpoint.GetGroupStatus(ctGroup.ID)
42+
dynamicGroupsResponse, err := dynamicGroupsEndpoint.GetAllDynamicGroups()
4243
if err != nil {
43-
return nil, fmt.Errorf("failed to get dynamic group status, %w", err)
44+
return nil, fmt.Errorf("failed to get all dynamic groups, %w", err)
4445
}
4546

46-
if *dynamicGroup.Status != "active" {
47-
return nil, &GroupNotActiveError{GroupName: groupName}
47+
if slices.Contains(dynamicGroupsResponse.GroupIDs, ctGroup.ID) {
48+
dynamicGroup, err := dynamicGroupsEndpoint.GetGroupStatus(ctGroup.ID)
49+
if err != nil {
50+
return nil, fmt.Errorf("failed to get dynamic group status, %w", err)
51+
}
52+
53+
if *dynamicGroup.Status != "active" {
54+
return nil, &GroupNotActiveError{GroupName: groupName}
55+
}
4856
}
4957

5058
groupMembers, err := groupsEndpoint.GetGroupMembers(ctGroup.ID)

app/group_exporter_test.go

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -73,36 +73,47 @@ var _ = Describe("GroupExporter", func() {
7373
Expect(personData[1]).To(MatchJSON(person2))
7474
})
7575

76-
It("returns an error if fetching the dynamic group status fails", func() {
77-
dynamicGroupsEndpoint.GetGroupStatusReturns(
78-
rest.DynamicGroupsStatusResponse{}, errors.New("boom"),
79-
)
80-
81-
personData, err := groupExporter.ExportGroupMembers(
82-
"group1",
83-
groupsEndpoint,
84-
dynamicGroupsEndpoint,
85-
personsEndpoint,
86-
)
87-
88-
Expect(err.Error()).To(Equal("failed to get dynamic group status, boom"))
89-
Expect(personData).To(BeNil())
90-
})
91-
92-
It("returns an error if the dynamic group status is not active", func() {
93-
dynamicGroupsEndpoint.GetGroupStatusReturns(
94-
rest.DynamicGroupsStatusResponse{Status: ptr("not-active")}, nil,
95-
)
96-
97-
personData, err := groupExporter.ExportGroupMembers(
98-
"group1",
99-
groupsEndpoint,
100-
dynamicGroupsEndpoint,
101-
personsEndpoint,
102-
)
103-
104-
Expect(err.Error()).To(Equal("dynamic group 'group1' is not active"))
105-
Expect(personData).To(BeNil())
76+
var _ = Context("group is a dynamic group", func() {
77+
78+
BeforeEach(func() {
79+
// The id 1 of the group "group1" is contained in the list of dynamic group IDs
80+
// thus "group1" is a dynamic group
81+
dynamicGroupsEndpoint.GetAllDynamicGroupsReturns(
82+
rest.DynamicGroupsResponse{GroupIDs: []int{1}}, nil,
83+
)
84+
})
85+
86+
It("returns an error if fetching the dynamic group status fails", func() {
87+
dynamicGroupsEndpoint.GetGroupStatusReturns(
88+
rest.DynamicGroupsStatusResponse{}, errors.New("boom"),
89+
)
90+
91+
personData, err := groupExporter.ExportGroupMembers(
92+
"group1",
93+
groupsEndpoint,
94+
dynamicGroupsEndpoint,
95+
personsEndpoint,
96+
)
97+
98+
Expect(err.Error()).To(Equal("failed to get dynamic group status, boom"))
99+
Expect(personData).To(BeNil())
100+
})
101+
102+
It("returns an error if the dynamic group status is not active", func() {
103+
dynamicGroupsEndpoint.GetGroupStatusReturns(
104+
rest.DynamicGroupsStatusResponse{Status: ptr("not-active")}, nil,
105+
)
106+
107+
personData, err := groupExporter.ExportGroupMembers(
108+
"group1",
109+
groupsEndpoint,
110+
dynamicGroupsEndpoint,
111+
personsEndpoint,
112+
)
113+
114+
Expect(err.Error()).To(Equal("dynamic group 'group1' is not active"))
115+
Expect(personData).To(BeNil())
116+
})
106117
})
107118

108119
It("returns an error if group members cannot be resolved", func() {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
//counterfeiter:generate . DynamicGroupsEndpoint
1313
type DynamicGroupsEndpoint interface {
1414
GetGroupStatus(groupID int) (DynamicGroupsStatusResponse, error)
15+
16+
GetAllDynamicGroups() (DynamicGroupsResponse, error)
1517
}
1618

1719
type dynamicGroupsEndpoint struct {
@@ -58,3 +60,34 @@ func (c dynamicGroupsEndpoint) GetGroupStatus(groupID int) (DynamicGroupsStatusR
5860

5961
return response, nil
6062
}
63+
64+
func (c dynamicGroupsEndpoint) GetAllDynamicGroups() (DynamicGroupsResponse, error) {
65+
req, err := http.NewRequest("GET", "", nil)
66+
if err != nil {
67+
return DynamicGroupsResponse{}, fmt.Errorf("failed to create request, %w", err)
68+
}
69+
70+
req.URL.Path = "/api/dynamicgroups"
71+
72+
resp, err := c.httpclient.Do(req)
73+
if err != nil {
74+
return DynamicGroupsResponse{}, fmt.Errorf("failed to send request, %w", err)
75+
}
76+
defer resp.Body.Close()
77+
78+
if resp.StatusCode != http.StatusOK {
79+
return DynamicGroupsResponse{}, fmt.Errorf("received non-200 response code: %d", resp.StatusCode)
80+
}
81+
82+
body, err := io.ReadAll(resp.Body)
83+
if err != nil {
84+
return DynamicGroupsResponse{}, fmt.Errorf("failed to read response body, %w", err)
85+
}
86+
87+
var response DynamicGroupsResponse
88+
if err := json.Unmarshal(body, &response); err != nil {
89+
return DynamicGroupsResponse{}, fmt.Errorf("response body is not containing expected json, %w", err)
90+
}
91+
92+
return response, nil
93+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@ package rest
33
type DynamicGroupsStatusResponse struct {
44
Status *string `json:"dynamicGroupStatus"`
55
}
6+
7+
type DynamicGroupsResponse struct {
8+
GroupIDs []int `json:"data"`
9+
}

rest/dynamic_groups_test.go

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,19 @@ var _ = Describe("DynamicGroupsEndpoint", func() {
2222

2323
BeforeEach(func() {
2424
httpClient = &httpclientfakes.FakeHTTPClient{}
25+
})
26+
27+
var _ = Describe("GetGroupStatus", func() {
2528

26-
httpResponse = &http.Response{
29+
BeforeEach(func(){
30+
httpResponse = &http.Response{
2731
StatusCode: 200,
2832
Body: io.NopCloser(bytes.NewBufferString(
2933
`{
3034
"dynamicGroupStatus": "active"
3135
}`)),
32-
}
33-
})
34-
35-
var _ = Describe("GetGroupStatus", func() {
36+
}
37+
})
3638

3739
It("returns a group status", func() {
3840

@@ -98,4 +100,67 @@ var _ = Describe("DynamicGroupsEndpoint", func() {
98100
Expect(err.Error()).To(Equal("response body is missing dynamicGroupStatus field"))
99101
})
100102
})
103+
104+
var _ = Describe("GetAllDynamicGroups", func() {
105+
106+
BeforeEach(func(){
107+
httpResponse = &http.Response{
108+
StatusCode: 200,
109+
Body: io.NopCloser(bytes.NewBufferString(
110+
`{
111+
"data": [0,1,2,3]
112+
}`)),
113+
}
114+
})
115+
116+
It("returns a list of dynamic groups", func() {
117+
118+
httpClient.DoReturns(httpResponse, nil)
119+
120+
endpoint := rest.NewDynamicGroupsEndpoint(httpClient)
121+
groups, err := endpoint.GetAllDynamicGroups()
122+
123+
Expect(err).NotTo(HaveOccurred())
124+
Expect(groups.GroupIDs).To(Equal([]int{0, 1, 2, 3}))
125+
request := httpClient.DoArgsForCall(0)
126+
Expect(request.URL.Path).To(Equal("/api/dynamicgroups"))
127+
})
128+
129+
It("returns an error if the request cannot be send", func() {
130+
httpClient.DoReturns(nil, errors.New("request failed"))
131+
132+
endpoint := rest.NewDynamicGroupsEndpoint(httpClient)
133+
_, err := endpoint.GetAllDynamicGroups()
134+
135+
Expect(err).To(HaveOccurred())
136+
Expect(err.Error()).To(Equal("failed to send request, request failed"))
137+
})
138+
139+
It("returns an error if the status code is wrong", func() {
140+
httpResponse := &http.Response{
141+
StatusCode: 404,
142+
Body: io.NopCloser(bytes.NewBufferString(`{}`))}
143+
httpClient.DoReturns(httpResponse, nil)
144+
145+
endpoint := rest.NewDynamicGroupsEndpoint(httpClient)
146+
_, err := endpoint.GetAllDynamicGroups()
147+
148+
Expect(err).To(HaveOccurred())
149+
Expect(err.Error()).To(Equal("received non-200 response code: 404"))
150+
})
151+
152+
It("returns an error if the response body is an invalid json response", func() {
153+
httpResponse := &http.Response{
154+
StatusCode: 200,
155+
Body: io.NopCloser(bytes.NewBufferString(
156+
``))}
157+
httpClient.DoReturns(httpResponse, nil)
158+
159+
endpoint := rest.NewDynamicGroupsEndpoint(httpClient)
160+
_, err := endpoint.GetAllDynamicGroups()
161+
162+
Expect(err).To(HaveOccurred())
163+
Expect(err.Error()).To(ContainSubstring("response body is not containing expected json"))
164+
})
165+
})
101166
})

rest/restfakes/fake_dynamic_groups_endpoint.go

Lines changed: 70 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)