From 67d57783061e265bf7c57face82552f6d75f328b Mon Sep 17 00:00:00 2001 From: gabrielburnworth Date: Wed, 28 Aug 2024 14:38:12 -0700 Subject: [PATCH] add curve_id validation to points --- app/mutations/points/create.rb | 4 ++++ app/mutations/points/helpers.rb | 23 +++++++++++++++++++ app/mutations/points/update.rb | 4 ++++ spec/mutations/points/create_spec.rb | 33 ++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+) create mode 100644 app/mutations/points/helpers.rb diff --git a/app/mutations/points/create.rb b/app/mutations/points/create.rb index afbbef6ba3..9e99995076 100644 --- a/app/mutations/points/create.rb +++ b/app/mutations/points/create.rb @@ -2,6 +2,7 @@ module Points class Create < Mutations::Command + include Points::Helpers # WHY 1000?: # * This limit is placed for _technical_ # reasons, not business reasons. If it were @@ -65,6 +66,9 @@ def validate return unless safe_pointer_kind? # Security critical always goes first. validate_resource_count validate_tool if klass_ == ToolSlot + validate_water_curve_id + validate_spread_curve_id + validate_height_curve_id name ||= default_name end diff --git a/app/mutations/points/helpers.rb b/app/mutations/points/helpers.rb new file mode 100644 index 0000000000..3deaa3fa21 --- /dev/null +++ b/app/mutations/points/helpers.rb @@ -0,0 +1,23 @@ +module Points + module Helpers + BAD_CURVE_ID = "Curve ID is not valid" + + def validate_water_curve_id + if water_curve_id && !device.curves.exists?(water_curve_id) + add_error :water_curve_id, :water_curve_id, BAD_CURVE_ID + end + end + + def validate_spread_curve_id + if spread_curve_id && !device.curves.exists?(spread_curve_id) + add_error :spread_curve_id, :spread_curve_id, BAD_CURVE_ID + end + end + + def validate_height_curve_id + if height_curve_id && !device.curves.exists?(height_curve_id) + add_error :height_curve_id, :height_curve_id, BAD_CURVE_ID + end + end + end + end diff --git a/app/mutations/points/update.rb b/app/mutations/points/update.rb index 4d40d499f2..6d7bbf130b 100644 --- a/app/mutations/points/update.rb +++ b/app/mutations/points/update.rb @@ -2,6 +2,7 @@ module Points class Update < Mutations::Command + include Points::Helpers required do model :device, class: Device model :point, class: Point @@ -28,6 +29,9 @@ class Update < Mutations::Command def validate prevent_removal_of_in_use_tools + validate_water_curve_id + validate_spread_curve_id + validate_height_curve_id end def execute diff --git a/spec/mutations/points/create_spec.rb b/spec/mutations/points/create_spec.rb index 556f9d946a..4f73fa84bf 100644 --- a/spec/mutations/points/create_spec.rb +++ b/spec/mutations/points/create_spec.rb @@ -57,4 +57,37 @@ def with_fake_limits(soft_limit = 2, hard_limit = 3) expect(errors.fetch("point_limit")).to be end end + + it "validates curve ids: ok" do + water_curve = FactoryBot.create(:curve, type: "water", device: device) + spread_curve = FactoryBot.create(:curve, type: "spread", device: device) + height_curve = FactoryBot.create(:curve, type: "height", device: device) + params = { x: 0, + y: 0, + z: 0, + water_curve_id: water_curve.id, + spread_curve_id: spread_curve.id, + height_curve_id: height_curve.id, + device: device, + pointer_type: "GenericPointer" } + expect(Points::Create.run(params).errors).to be nil + end + + it "validates curve ids: ko" do + curve = FactoryBot.create(:curve, type: "water", device: device) + params = { x: 0, + y: 0, + z: 0, + water_curve_id: -1, + height_curve_id: -1, + spread_curve_id: -1, + device: device, + pointer_type: "GenericPointer" } + + errors = Points::Create.run(params).errors + expect(errors).to be + expect(errors.fetch("water_curve_id")).to be + expect(errors.fetch("spread_curve_id")).to be + expect(errors.fetch("height_curve_id")).to be + end end