Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

device-group-view: add enforce-edge-groups #2317

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pkg/routes/devicegroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,13 +385,19 @@ func GetDeviceGroupDetailsByID(w http.ResponseWriter, r *http.Request) {
// @Router /device-groups/{ID}/view [get]
func GetDeviceGroupDetailsByIDView(w http.ResponseWriter, r *http.Request) {
ctxServices := dependencies.ServicesFromContext(r.Context())
orgID := readOrgID(w, r, ctxServices.Log)
if orgID == "" {
return
}

deviceGroup := getContextDeviceGroup(w, r)
if deviceGroup == nil {
return
}

var deviceGroupDetails models.DeviceGroupDetailsView
deviceGroupDetails.DeviceGroup = deviceGroup
deviceGroupDetails.DeviceDetails.EnforceEdgeGroups = utility.EnforceEdgeGroups(orgID)
if int(len(deviceGroup.Devices)) == 0 {
respondWithJSONBody(w, ctxServices.Log, &deviceGroupDetails)
return
Expand Down
59 changes: 59 additions & 0 deletions pkg/routes/devicegroups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -974,5 +974,64 @@ var _ = Describe("DeviceGroup routes", func() {
Expect(err).ToNot(HaveOccurred())
Expect(responseDevicesView.EnforceEdgeGroups).To(BeFalse())
})

Context("GetDeviceGroupDetailsByIDView", func() {

It("should return EnforceEdgeGroups value: true", func() {
err := os.Setenv(feature.EnforceEdgeGroups.EnvVar, "true")
Expect(err).ToNot(HaveOccurred())
deviceGroup := models.DeviceGroup{
OrgID: OrgID, Name: faker.Name(),
Devices: []models.Device{
{UUID: faker.UUIDHyphenated(), OrgID: OrgID},
{UUID: faker.UUIDHyphenated(), OrgID: OrgID},
},
}

mockDeviceGroupsService.EXPECT().GetDeviceGroupByID(fmt.Sprintf("%d", deviceGroup.ID)).Return(&deviceGroup, nil)
mockDeviceService.EXPECT().GetDevicesCount(gomock.Any()).Return(int64(len(deviceGroup.Devices)), nil)
mockDeviceService.EXPECT().GetDevicesView(gomock.Any(), gomock.Any(), gomock.Any()).Return(&models.DeviceViewList{}, nil)

req, err := http.NewRequest("GET", fmt.Sprintf("/device-groups/%d/view", deviceGroup.ID), nil)
Expect(err).ToNot(HaveOccurred())

responseRecorder := httptest.NewRecorder()
router.ServeHTTP(responseRecorder, req)

Expect(responseRecorder.Code).To(Equal(http.StatusOK))
respBody, err := io.ReadAll(responseRecorder.Body)
Expect(err).ToNot(HaveOccurred())
Expect(string(respBody)).ToNot(BeEmpty())

var deviceGroupDetails models.DeviceGroupDetailsView
err = json.Unmarshal(respBody, &deviceGroupDetails)
Expect(err).ToNot(HaveOccurred())
Expect(deviceGroupDetails.DeviceDetails.EnforceEdgeGroups).To(BeTrue())
})

It("should return EnforceEdgeGroups value: false", func() {
err := os.Unsetenv(feature.EnforceEdgeGroups.EnvVar)
Expect(err).ToNot(HaveOccurred())
deviceGroup := models.DeviceGroup{Model: models.Model{ID: 10}, OrgID: OrgID, Name: faker.Name()}

mockDeviceGroupsService.EXPECT().GetDeviceGroupByID(fmt.Sprintf("%d", deviceGroup.ID)).Return(&deviceGroup, nil)

req, err := http.NewRequest("GET", fmt.Sprintf("/device-groups/%d/view", deviceGroup.ID), nil)
Expect(err).ToNot(HaveOccurred())

responseRecorder := httptest.NewRecorder()
router.ServeHTTP(responseRecorder, req)

Expect(responseRecorder.Code).To(Equal(http.StatusOK))
respBody, err := io.ReadAll(responseRecorder.Body)
Expect(err).ToNot(HaveOccurred())
Expect(string(respBody)).ToNot(BeEmpty())

var deviceGroupDetails models.DeviceGroupDetailsView
err = json.Unmarshal(respBody, &deviceGroupDetails)
Expect(err).ToNot(HaveOccurred())
Expect(deviceGroupDetails.DeviceDetails.EnforceEdgeGroups).To(BeFalse())
})
})
})
})
Loading