From c74149175af8b66dd8c3865a22578ac517b32857 Mon Sep 17 00:00:00 2001 From: Ali Naqvi Date: Thu, 13 Nov 2025 21:55:06 +0800 Subject: [PATCH 1/2] refactor: [PPT-2293] Allow support users to start/stop modules --- spec/controllers/modules_spec.cr | 38 +++++++++++++++++++++ src/placeos-rest-api/controllers/modules.cr | 6 ++-- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/spec/controllers/modules_spec.cr b/spec/controllers/modules_spec.cr index 23674f36..6ff966bc 100644 --- a/spec/controllers/modules_spec.cr +++ b/spec/controllers/modules_spec.cr @@ -469,5 +469,43 @@ module PlaceOS::Api end end end + + describe "POST /:id/start and POST /:id/stop", focus: true do + it "allows support users to start and stop modules" do + control_system = Model::Generator.control_system + control_system.zones << Spec::Authentication.org_zone.id.as(String) + control_system.zones_will_change! + control_system.save! + + driver = Model::Generator.driver(role: Model::Driver::Role::Logic).save! + mod = Model::Generator.module(driver: driver, control_system: control_system) + mod.running = false + mod.save! + + id = mod.id.as(String) + start_path = File.join(Modules.base_route, id, "start") + stop_path = File.join(Modules.base_route, id, "stop") + + # Test that admin user can start a module + result = client.post( + path: start_path, + headers: Spec::Authentication.headers(sys_admin: false, support: true), + ) + + result.status_code.should eq 200 + mod.reload! + mod.running.should be_true + + # Test that admin user can stop a module + result = client.post( + path: stop_path, + headers: Spec::Authentication.headers(sys_admin: false, support: true), + ) + + result.status_code.should eq 200 + mod.reload! + mod.running.should be_false + end + end end end diff --git a/src/placeos-rest-api/controllers/modules.cr b/src/placeos-rest-api/controllers/modules.cr index 963009cc..8a7b9157 100644 --- a/src/placeos-rest-api/controllers/modules.cr +++ b/src/placeos-rest-api/controllers/modules.cr @@ -21,7 +21,7 @@ module PlaceOS::Api before_action :can_write, only: [:create, :update, :destroy, :remove] before_action :check_admin, except: [:index, :create, :update, :destroy, :state, :show, :ping, :start, :stop] - before_action :check_support, only: [:state, :show, :ping, :show_error] + before_action :check_support, only: [:state, :show, :ping, :show_error, :start, :stop] ############################################################################################### @@ -298,7 +298,7 @@ module PlaceOS::Api @[AC::Route::POST("/:id/start")] def start : Nil return if current_module.running == true - can_modify?(current_module) + can_modify?(current_module) unless user_support? current_module.update_fields(running: true) # Changes cleared on a successful update @@ -312,7 +312,7 @@ module PlaceOS::Api @[AC::Route::POST("/:id/stop")] def stop : Nil return unless current_module.running - can_modify?(current_module) + can_modify?(current_module) unless user_support? current_module.update_fields(running: false) # Changes cleared on a successful update From a519f03b51337da4be57f75684f244af7645c93a Mon Sep 17 00:00:00 2001 From: Stephen von Takach Date: Fri, 14 Nov 2025 09:50:38 +1100 Subject: [PATCH 2/2] chore: remove focused spec --- spec/controllers/modules_spec.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/controllers/modules_spec.cr b/spec/controllers/modules_spec.cr index 6ff966bc..b7c090d3 100644 --- a/spec/controllers/modules_spec.cr +++ b/spec/controllers/modules_spec.cr @@ -470,7 +470,7 @@ module PlaceOS::Api end end - describe "POST /:id/start and POST /:id/stop", focus: true do + describe "POST /:id/start and POST /:id/stop" do it "allows support users to start and stop modules" do control_system = Model::Generator.control_system control_system.zones << Spec::Authentication.org_zone.id.as(String)