Skip to content

Commit 4ad1f66

Browse files
naqvisstakach
andauthored
refactor: [PPT-2293] Allow support users to start/stop modules (#421)
* refactor: [PPT-2293] Allow support users to start/stop modules * chore: remove focused spec --------- Co-authored-by: Stephen von Takach <steve@place.technology>
1 parent 3578887 commit 4ad1f66

2 files changed

Lines changed: 41 additions & 3 deletions

File tree

spec/controllers/modules_spec.cr

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,5 +469,43 @@ module PlaceOS::Api
469469
end
470470
end
471471
end
472+
473+
describe "POST /:id/start and POST /:id/stop" do
474+
it "allows support users to start and stop modules" do
475+
control_system = Model::Generator.control_system
476+
control_system.zones << Spec::Authentication.org_zone.id.as(String)
477+
control_system.zones_will_change!
478+
control_system.save!
479+
480+
driver = Model::Generator.driver(role: Model::Driver::Role::Logic).save!
481+
mod = Model::Generator.module(driver: driver, control_system: control_system)
482+
mod.running = false
483+
mod.save!
484+
485+
id = mod.id.as(String)
486+
start_path = File.join(Modules.base_route, id, "start")
487+
stop_path = File.join(Modules.base_route, id, "stop")
488+
489+
# Test that admin user can start a module
490+
result = client.post(
491+
path: start_path,
492+
headers: Spec::Authentication.headers(sys_admin: false, support: true),
493+
)
494+
495+
result.status_code.should eq 200
496+
mod.reload!
497+
mod.running.should be_true
498+
499+
# Test that admin user can stop a module
500+
result = client.post(
501+
path: stop_path,
502+
headers: Spec::Authentication.headers(sys_admin: false, support: true),
503+
)
504+
505+
result.status_code.should eq 200
506+
mod.reload!
507+
mod.running.should be_false
508+
end
509+
end
472510
end
473511
end

src/placeos-rest-api/controllers/modules.cr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module PlaceOS::Api
2121
before_action :can_write, only: [:create, :update, :destroy, :remove]
2222

2323
before_action :check_admin, except: [:index, :create, :update, :destroy, :state, :show, :ping, :start, :stop]
24-
before_action :check_support, only: [:state, :show, :ping, :show_error]
24+
before_action :check_support, only: [:state, :show, :ping, :show_error, :start, :stop]
2525

2626
###############################################################################################
2727

@@ -298,7 +298,7 @@ module PlaceOS::Api
298298
@[AC::Route::POST("/:id/start")]
299299
def start : Nil
300300
return if current_module.running == true
301-
can_modify?(current_module)
301+
can_modify?(current_module) unless user_support?
302302
current_module.update_fields(running: true)
303303

304304
# Changes cleared on a successful update
@@ -312,7 +312,7 @@ module PlaceOS::Api
312312
@[AC::Route::POST("/:id/stop")]
313313
def stop : Nil
314314
return unless current_module.running
315-
can_modify?(current_module)
315+
can_modify?(current_module) unless user_support?
316316
current_module.update_fields(running: false)
317317

318318
# Changes cleared on a successful update

0 commit comments

Comments
 (0)