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/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 new file mode 100644 index 000000000..510f8ee5c --- /dev/null +++ b/server/tests/core/services/generics/test_dynamic_put_back_to_cache.py @@ -0,0 +1,81 @@ +# +# 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_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()) + + 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_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" + 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())