From 8a4748802bd42509453ebcf13671aaf228757d38 Mon Sep 17 00:00:00 2001 From: Andres Schumann Date: Tue, 17 Nov 2026 14:30:20 +0100 Subject: [PATCH 1/3] fix(dynamic): only allow putting userservices back to cache when explicitly enabled allow_putting_back_to_cache() returned True for any value other than "no". Services stored before the put_back_to_cache field existed load it as None, which the check treated as enabled, so logged-out machines were moved to cache instead of being removed. Allow putting back to cache only for "yes" and "snapshot". Add tests for unset, "no", "yes" and "snapshot". --- .../core/services/generics/dynamic/service.py | 2 +- .../test_dynamic_put_back_to_cache.py | 72 +++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 server/tests/core/services/generics/test_dynamic_put_back_to_cache.py diff --git a/server/src/uds/core/services/generics/dynamic/service.py b/server/src/uds/core/services/generics/dynamic/service.py index 865405d32..61f2ad762 100644 --- a/server/src/uds/core/services/generics/dynamic/service.py +++ b/server/src/uds/core/services/generics/dynamic/service.py @@ -110,7 +110,7 @@ def initialize(self, values: 'types.core.ValuesType') -> None: def allow_putting_back_to_cache(self) -> bool: if self.has_field('put_back_to_cache'): - return self.put_back_to_cache.value != 'no' + return self.put_back_to_cache.value in ('yes', 'snapshot') return False def restore_snapshot_on_back_to_cache(self) -> bool: diff --git a/server/tests/core/services/generics/test_dynamic_put_back_to_cache.py b/server/tests/core/services/generics/test_dynamic_put_back_to_cache.py new file mode 100644 index 000000000..c3d77f23e --- /dev/null +++ b/server/tests/core/services/generics/test_dynamic_put_back_to_cache.py @@ -0,0 +1,72 @@ +# +# Copyright (c) 2026 Virtual Cable S.L. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# * Neither the name of Virtual Cable S.L. nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +""" +Author: Andres Schumann, aschumann at virtualcable dot es +""" + +import uuid + +from uds.core import environment +from uds.core.services.generics.dynamic.service import DynamicService +from uds.core.util import fields + +from ....utils.test import UDSTestCase +from . import fixtures + + +class PutBackToCacheTestingService(fixtures.DynamicTestingService): + put_back_to_cache = fields.put_back_to_cache_field() + + +def create_service() -> DynamicService: + service_uuid = str(uuid.uuid4()) + return PutBackToCacheTestingService( + provider=fixtures.create_dynamic_provider(), + environment=environment.Environment.private_environment(service_uuid), + uuid=service_uuid, + ) + + +class DynamicPutBackToCacheTest(UDSTestCase): + def test_unset_value_does_not_allow_putting_back_to_cache(self) -> None: + service = create_service() + self.assertFalse(service.allow_putting_back_to_cache()) + + def test_no_does_not_allow_putting_back_to_cache(self) -> None: + service = create_service() + service.put_back_to_cache.value = "no" + self.assertFalse(service.allow_putting_back_to_cache()) + + def test_yes_allows_putting_back_to_cache(self) -> None: + service = create_service() + service.put_back_to_cache.value = "yes" + self.assertTrue(service.allow_putting_back_to_cache()) + + def test_snapshot_allows_putting_back_to_cache(self) -> None: + service = create_service() + service.put_back_to_cache.value = "snapshot" + self.assertTrue(service.allow_putting_back_to_cache()) From 32aa4f6c65ec45da732bcd98ab83fcd3f5f151bf Mon Sep 17 00:00:00 2001 From: Andres Schumann Date: Tue, 17 Nov 2026 15:49:03 +0100 Subject: [PATCH 2/3] fix(fields): default put_back_to_cache to "no" put_back_to_cache_field() had no default, so services without a stored value showed the first choice in the UI while the stored value was unset. Default the field to "no" so the UI and the stored value agree, and so services stored before the field existed load as "no". Add a test for the default value. --- server/src/uds/core/util/fields.py | 1 + .../core/services/generics/test_dynamic_put_back_to_cache.py | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/server/src/uds/core/util/fields.py b/server/src/uds/core/util/fields.py index aae3da1e4..3a001b3cc 100644 --- a/server/src/uds/core/util/fields.py +++ b/server/src/uds/core/util/fields.py @@ -497,6 +497,7 @@ def put_back_to_cache_field( order=order, label=_('Put back to cache'), tooltip=_('On machine releasy by logout, put it back to cache instead of deleting if possible.'), + default='no', choices=[ {'id': 'no', 'text': _('No. Never put it back to cache')}, {'id': 'yes', 'text': _('Yes, try to put it back to cache')}, diff --git a/server/tests/core/services/generics/test_dynamic_put_back_to_cache.py b/server/tests/core/services/generics/test_dynamic_put_back_to_cache.py index c3d77f23e..add79f876 100644 --- a/server/tests/core/services/generics/test_dynamic_put_back_to_cache.py +++ b/server/tests/core/services/generics/test_dynamic_put_back_to_cache.py @@ -52,6 +52,10 @@ def create_service() -> DynamicService: class DynamicPutBackToCacheTest(UDSTestCase): + def test_default_value_is_no(self) -> None: + service = create_service() + self.assertEqual(service.put_back_to_cache.value, "no") + def test_unset_value_does_not_allow_putting_back_to_cache(self) -> None: service = create_service() self.assertFalse(service.allow_putting_back_to_cache()) From fb879e3144b9959a533e2c5eb13463c72ad780db Mon Sep 17 00:00:00 2001 From: Andres Schumann Date: Tue, 17 Nov 2026 16:00:19 +0100 Subject: [PATCH 3/3] test(dynamic): cover legacy stored value for put_back_to_cache Services stored before the field existed keep the value "None" (as_str of an unset choice). Add a test that such a value does not allow putting machines back into cache, so the check cannot regress to "!= no". --- .../core/services/generics/test_dynamic_put_back_to_cache.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/server/tests/core/services/generics/test_dynamic_put_back_to_cache.py b/server/tests/core/services/generics/test_dynamic_put_back_to_cache.py index add79f876..510f8ee5c 100644 --- a/server/tests/core/services/generics/test_dynamic_put_back_to_cache.py +++ b/server/tests/core/services/generics/test_dynamic_put_back_to_cache.py @@ -65,6 +65,11 @@ def test_no_does_not_allow_putting_back_to_cache(self) -> None: service.put_back_to_cache.value = "no" self.assertFalse(service.allow_putting_back_to_cache()) + def test_legacy_none_value_does_not_allow_putting_back_to_cache(self) -> None: + service = create_service() + service.put_back_to_cache.value = "None" + self.assertFalse(service.allow_putting_back_to_cache()) + def test_yes_allows_putting_back_to_cache(self) -> None: service = create_service() service.put_back_to_cache.value = "yes"