A continuation of HomeBox the inventory and organization system built for the Home User
fix: make sure that quantities are finite
Matthew Kilgore5c769fa0b603
diff --git a/backend/internal/data/repo/repo_items.go b/backend/internal/data/repo/repo_items.goindex f5c39b85..2393256e 100644--- a/backend/internal/data/repo/repo_items.go+++ b/backend/internal/data/repo/repo_items.go@@ -3,6 +3,7 @@ package repoimport ("context""fmt"+ "math""time""github.com/google/uuid"@@ -611,7 +612,19 @@ func (e *ItemsRepository) SetAssetID(ctx context.Context, gid uuid.UUID, id uuidreturn err}+func validateQuantity(op string, quantity float64) error {+ if math.IsNaN(quantity) || math.IsInf(quantity, 0) {+ return fmt.Errorf("%s: invalid quantity: must be a finite number", op)+ }++ return nil+}+func (e *ItemsRepository) Create(ctx context.Context, gid uuid.UUID, data ItemCreate) (ItemOut, error) {+ if err := validateQuantity("create item", data.Quantity); err != nil {+ return ItemOut{}, err+ }+q := e.db.Item.Create().SetImportRef(data.ImportRef).SetName(data.Name).@@ -655,6 +668,10 @@ type ItemCreateFromTemplate struct {// CreateFromTemplate creates an item with all template data in a single transaction.func (e *ItemsRepository) CreateFromTemplate(ctx context.Context, gid uuid.UUID, data ItemCreateFromTemplate) (ItemOut, error) {+ if err := validateQuantity("create item from template", data.Quantity); err != nil {+ return ItemOut{}, err+ }+tx, err := e.db.Tx(ctx)if err != nil {return ItemOut{}, err@@ -876,6 +893,10 @@ func (e *ItemsRepository) WipeInventory(ctx context.Context, gid uuid.UUID, wipe}func (e *ItemsRepository) UpdateByGroup(ctx context.Context, gid uuid.UUID, data ItemUpdate) (ItemOut, error) {+ if err := validateQuantity("update item", data.Quantity); err != nil {+ return ItemOut{}, err+ }+q := e.db.Item.Update().Where(item.ID(data.ID), item.HasGroupWith(group.ID(gid))).SetName(data.Name).SetDescription(data.Description).@@ -1059,6 +1080,10 @@ func (e *ItemsRepository) Patch(ctx context.Context, gid, id uuid.UUID, data Ite}if data.Quantity != nil {+ if err := validateQuantity("patch item", *data.Quantity); err != nil {+ return err+ }+q.SetQuantity(*data.Quantity)}diff --git a/backend/internal/data/repo/repo_items_test.go b/backend/internal/data/repo/repo_items_test.goindex 68147992..ac89f7a9 100644--- a/backend/internal/data/repo/repo_items_test.go+++ b/backend/internal/data/repo/repo_items_test.go@@ -2,6 +2,7 @@ package repoimport ("context"+ "math""strings""testing""time"@@ -150,6 +151,23 @@ func TestItemsRepository_Create_WithFractionalQuantity(t *testing.T) {require.NoError(t, err)}+func TestItemsRepository_Create_RejectsNonFiniteQuantity(t *testing.T) {+ location, err := tRepos.Locations.Create(context.Background(), tGroup.ID, locationFactory())+ require.NoError(t, err)++ itm := itemFactory()+ itm.LocationID = location.ID+ itm.Quantity = math.NaN()++ _, err = tRepos.Items.Create(context.Background(), tGroup.ID, itm)+ require.Error(t, err)+ assert.Contains(t, err.Error(), "invalid quantity: must be a finite number")++ // Cleanup+ err = tRepos.Locations.delete(context.Background(), location.ID)+ require.NoError(t, err)+}+func TestItemsRepository_Create_Location(t *testing.T) {location, err := tRepos.Locations.Create(context.Background(), tGroup.ID, locationFactory())require.NoError(t, err)@@ -316,6 +334,48 @@ func TestItemsRepository_Update_WithFractionalQuantity(t *testing.T) {assert.InDelta(t, 2.75, got.Quantity, 0.000001)}+func TestItemsRepository_Update_RejectsNonFiniteQuantity(t *testing.T) {+ entity := useItems(t, 1)[0]++ updateData := ItemUpdate{+ ID: entity.ID,+ Name: entity.Name,+ LocationID: entity.Location.ID,+ Quantity: math.Inf(1),+ }++ _, err := tRepos.Items.UpdateByGroup(context.Background(), tGroup.ID, updateData)+ require.Error(t, err)+ assert.Contains(t, err.Error(), "invalid quantity: must be a finite number")+}++func TestItemsRepository_Patch_RejectsNonFiniteQuantity(t *testing.T) {+ entity := useItems(t, 1)[0]++ quantity := math.Inf(-1)+ err := tRepos.Items.Patch(context.Background(), tGroup.ID, entity.ID, ItemPatch{Quantity: &quantity})+ require.Error(t, err)+ assert.Contains(t, err.Error(), "invalid quantity: must be a finite number")+}++func TestItemsRepository_CreateFromTemplate_RejectsNonFiniteQuantity(t *testing.T) {+ location, err := tRepos.Locations.Create(context.Background(), tGroup.ID, locationFactory())+ require.NoError(t, err)++ _, err = tRepos.Items.CreateFromTemplate(context.Background(), tGroup.ID, ItemCreateFromTemplate{+ Name: fk.Str(10),+ Description: fk.Str(20),+ Quantity: math.NaN(),+ LocationID: location.ID,+ })+ require.Error(t, err)+ assert.Contains(t, err.Error(), "invalid quantity: must be a finite number")++ // Cleanup+ err = tRepos.Locations.delete(context.Background(), location.ID)+ require.NoError(t, err)+}+func TestItemRepository_GetAllCustomFields(t *testing.T) {const FieldsCount = 5