diff --git a/pkg/routes/devices.go b/pkg/routes/devices.go index f6c82b614..9648dff4f 100644 --- a/pkg/routes/devices.go +++ b/pkg/routes/devices.go @@ -91,6 +91,11 @@ var devicesFilters = common.ComposeFilters( QueryParam: "image_id", DBField: "devices.image_id", }), + // Filter handler for "groupUUID" + common.ContainFilterHandler(&common.Filter{ + QueryParam: "groupUUID", + DBField: "devices.group_uuid", + }), common.SortFilterHandler("devices", "name", "ASC"), ) diff --git a/pkg/routes/devices_test.go b/pkg/routes/devices_test.go index bb691f66d..7629be451 100644 --- a/pkg/routes/devices_test.go +++ b/pkg/routes/devices_test.go @@ -1070,3 +1070,91 @@ func TestGetDevicesViewWithinDevicesWitUUIDNotExist(t *testing.T) { Expect(recorder.Code).To(Equal(http.StatusBadRequest)) } + +func TestGetDevicesViewFilteringByGroup(t *testing.T) { + RegisterTestingT(t) + ctrl := gomock.NewController(t) + defer ctrl.Finish() + orgID := faker.UUIDHyphenated() + account := faker.UUIDHyphenated() + imageSet := &models.ImageSet{ + Name: "test", + Version: 1, + OrgID: orgID, + } + + result := db.DB.Create(imageSet) + Expect(result.Error).ToNot(HaveOccurred()) + imageV1 := &models.Image{ + Commit: &models.Commit{ + OSTreeCommit: faker.UUIDHyphenated(), + OrgID: orgID, + }, + Status: models.ImageStatusSuccess, + ImageSetID: &imageSet.ID, + Version: 1, + OrgID: orgID, + } + result = db.DB.Create(imageV1) + Expect(result.Error).ToNot(HaveOccurred()) + groupUUID := "123" + devices := []models.Device{ + { + Name: faker.Name(), + UUID: faker.UUIDHyphenated(), + Account: account, + OrgID: orgID, + ImageID: imageV1.ID, + GroupUUID: groupUUID, + }, + { + Name: faker.Name(), + UUID: faker.UUIDHyphenated(), + Account: account, + OrgID: orgID, + ImageID: imageV1.ID, + }, + } + result = db.DB.Create(devices) + Expect(result.Error).ToNot(HaveOccurred()) + deviceView := []models.DeviceView{ + { + DeviceID: devices[0].ID, + DeviceUUID: devices[0].UUID, + GroupUUID: devices[0].GroupUUID, + }, + } + + var mockDeviceService *mock_services.MockDeviceServiceInterface + var router chi.Router + var mockServices *dependencies.EdgeAPIServices + ctrl = gomock.NewController(GinkgoT()) + + mockDeviceService = mock_services.NewMockDeviceServiceInterface(ctrl) + mockServices = &dependencies.EdgeAPIServices{ + DeviceService: mockDeviceService, + Log: log.NewEntry(log.StandardLogger()), + } + router = chi.NewRouter() + router.Use(func(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + ctx := dependencies.ContextWithServices(r.Context(), mockServices) + next.ServeHTTP(w, r.WithContext(ctx)) + }) + }) + router.Route("/devices", MakeDevicesRouter) + + mockDeviceService.EXPECT().GetDevicesCount(gomock.Any()).Return(int64(1), nil) + mockDeviceService.EXPECT().GetDevicesView(30, 0, gomock.Any()).Return(&models.DeviceViewList{Total: 1, Devices: deviceView}, nil) + + url := fmt.Sprintf("/devices/devicesview?groupUUID=%v", groupUUID) + req, err := http.NewRequest("GET", url, nil) + fmt.Printf(req.URL.Host) + if err != nil { + Expect(err).ToNot(HaveOccurred()) + } + rr := httptest.NewRecorder() + router.ServeHTTP(rr, req) + Expect(rr.Code).To(Equal(http.StatusOK)) + +} diff --git a/pkg/routes/query_params.go b/pkg/routes/query_params.go index 7e8ddf92a..b42ddfe37 100644 --- a/pkg/routes/query_params.go +++ b/pkg/routes/query_params.go @@ -19,7 +19,7 @@ func initalizeQueryParamsArray() map[string][]string { m = make(map[string][]string) m["device-groups"] = []string{"limit", "offset", "name", "created_at", "updated_at", "sort_by"} m["devices"] = []string{"per_page", "page", "order_how", "hostname_or_id", "order_by"} - m["devicesview"] = []string{"limit", "offset", "name", "uuid", "update_available", "image_id", "sort_by", "created_at"} + m["devicesview"] = []string{"limit", "offset", "name", "uuid", "update_available", "image_id", "sort_by", "created_at", "groupUUID"} m["images"] = []string{"limit", "offset", "status", "name", "distribution", "created_at", "sort_by"} m["image-sets"] = []string{"id", "limit", "offset", "status", "name", "version", "sort_by"} m["thirdpartyrepo"] = []string{"limit", "offset", "name", "created_at", "updated_at", "imageID", "sort_by"}