From f3727dbeff0dabe064ad3dc46510d59d1a526c74 Mon Sep 17 00:00:00 2001 From: David Dworken Date: Sun, 15 Oct 2023 13:09:48 -0700 Subject: [PATCH] Wire through a flag so that we can track when installations come from tests, and delete those from the DB more aggressively --- backend/server/internal/server/api_handlers.go | 3 ++- backend/server/internal/server/util.go | 4 ++-- client/lib/lib.go | 6 +++++- shared/data.go | 3 +++ 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/backend/server/internal/server/api_handlers.go b/backend/server/internal/server/api_handlers.go index 46e4956..c4c9152 100644 --- a/backend/server/internal/server/api_handlers.go +++ b/backend/server/internal/server/api_handlers.go @@ -216,11 +216,12 @@ func (s *Server) apiRegisterHandler(w http.ResponseWriter, r *http.Request) { } userId := getRequiredQueryParam(r, "user_id") deviceId := getRequiredQueryParam(r, "device_id") + isIntegrationTestDevice := getOptionalQueryParam(r, "is_integration_test_device", false) == "true" existingDevicesCount, err := s.db.CountDevicesForUser(r.Context(), userId) checkGormError(err) fmt.Printf("apiRegisterHandler: existingDevicesCount=%d\n", existingDevicesCount) - if err := s.db.CreateDevice(r.Context(), &shared.Device{UserId: userId, DeviceId: deviceId, RegistrationIp: getRemoteAddr(r), RegistrationDate: time.Now()}); err != nil { + if err := s.db.CreateDevice(r.Context(), &shared.Device{UserId: userId, DeviceId: deviceId, RegistrationIp: getRemoteAddr(r), RegistrationDate: time.Now(), IsIntegrationTestDevice: isIntegrationTestDevice}); err != nil { checkGormError(err) } diff --git a/backend/server/internal/server/util.go b/backend/server/internal/server/util.go index 218ac67..1db8376 100644 --- a/backend/server/internal/server/util.go +++ b/backend/server/internal/server/util.go @@ -82,9 +82,9 @@ func getRequiredQueryParam(r *http.Request, queryParam string) string { return val } -func getOptionalQueryParam(r *http.Request, queryParam string, isTestEnvironment bool) string { +func getOptionalQueryParam(r *http.Request, queryParam string, isRequiredInTestEnvironment bool) string { val := r.URL.Query().Get(queryParam) - if val == "" && isTestEnvironment { + if val == "" && isRequiredInTestEnvironment { panic(fmt.Sprintf("request to %s is missing optional query param=%#v that is required in test environments", r.URL, queryParam)) } return val diff --git a/client/lib/lib.go b/client/lib/lib.go index ff93e24..530d718 100644 --- a/client/lib/lib.go +++ b/client/lib/lib.go @@ -83,7 +83,11 @@ func Setup(userSecret string, isOffline bool) error { return nil } ctx := hctx.MakeContext() - _, err = ApiGet(ctx, "/api/v1/register?user_id="+data.UserId(userSecret)+"&device_id="+config.DeviceId) + registerPath := "/api/v1/register?user_id=" + data.UserId(userSecret) + "&device_id=" + config.DeviceId + if os.Getenv("HISHTORY_TEST") != "" { + registerPath += "&is_integration_test_device=true" + } + _, err = ApiGet(ctx, registerPath) if err != nil { return fmt.Errorf("failed to register device with backend: %w", err) } diff --git a/shared/data.go b/shared/data.go index bde9e51..105a535 100644 --- a/shared/data.go +++ b/shared/data.go @@ -29,6 +29,9 @@ type Device struct { // david@daviddworken.com and I can clear it from your device entries. RegistrationIp string `json:"registration_ip"` RegistrationDate time.Time `json:"registration_date"` + // Test devices, that should be aggressively cleaned from the DB + // TODO: Clean these from the DB + IsIntegrationTestDevice bool `json:"is_integration_test_device"` } // Represents a request to get all history entries from a given device. Used as part of bootstrapping