diff --git a/homeassistant/components/portainer/services.py b/homeassistant/components/portainer/services.py index 1d480ad3459c..ac878bad2669 100644 --- a/homeassistant/components/portainer/services.py +++ b/homeassistant/components/portainer/services.py @@ -216,7 +216,8 @@ async def recreate_container(call: ServiceCall) -> None: def async_setup_services(hass: HomeAssistant) -> None: """Set up services.""" - hass.services.async_register( + service.async_register_admin_service( + hass, DOMAIN, SERVICE_PRUNE_IMAGES, prune_images, diff --git a/tests/components/portainer/test_services.py b/tests/components/portainer/test_services.py index 5edca2373e84..f34b6e35b277 100644 --- a/tests/components/portainer/test_services.py +++ b/tests/components/portainer/test_services.py @@ -197,14 +197,25 @@ async def test_service_prune_build_cache_portainer_exceptions( mock_portainer_client.prune_build_cache.assert_called_once() -async def test_service_prune_build_cache_requires_admin( +@pytest.mark.parametrize( + ("service", "client_method"), + [ + pytest.param(SERVICE_PRUNE_IMAGES, "images_prune", id="prune_images"), + pytest.param( + SERVICE_PRUNE_BUILD_CACHE, "prune_build_cache", id="prune_build_cache" + ), + ], +) +async def test_service_requires_admin( hass: HomeAssistant, hass_read_only_user: MockUser, device_registry: DeviceRegistry, mock_portainer_client: AsyncMock, mock_config_entry: MockConfigEntry, + service: str, + client_method: str, ) -> None: - """Test prune build cache service is only available to admins.""" + """Test the prune services are only available to admins.""" await setup_integration(hass, mock_config_entry) device = device_registry.async_get_device_by_identifier( (DOMAIN, TEST_DEVICE_IDENTIFIER), mock_config_entry.entry_id @@ -214,12 +225,12 @@ async def test_service_prune_build_cache_requires_admin( with pytest.raises(Unauthorized): await hass.services.async_call( DOMAIN, - SERVICE_PRUNE_BUILD_CACHE, + service, {ATTR_DEVICE_ID: device.id}, blocking=True, context=Context(user_id=hass_read_only_user.id), ) - mock_portainer_client.prune_build_cache.assert_not_called() + getattr(mock_portainer_client, client_method).assert_not_called() @pytest.mark.parametrize(