From 67884017f8e3f50efda65f5f3e923fa9f129de97 Mon Sep 17 00:00:00 2001 From: imperosol Date: Wed, 25 Jun 2025 01:22:13 +0200 Subject: [PATCH] fix old permanences having end replaced by activity --- counter/tests/test_counter.py | 51 +++++++++++++++++++++++++++++++++++ counter/views/auth.py | 8 +++--- 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/counter/tests/test_counter.py b/counter/tests/test_counter.py index 5b96a471..d90f9510 100644 --- a/counter/tests/test_counter.py +++ b/counter/tests/test_counter.py @@ -17,6 +17,7 @@ from datetime import timedelta from decimal import Decimal import pytest +from dateutil.relativedelta import relativedelta from django.conf import settings from django.contrib.auth.models import Permission, make_password from django.core.cache import cache @@ -823,3 +824,53 @@ class TestClubCounterClickAccess(TestCase): self.client.force_login(self.user) res = self.client.get(self.click_url) assert res.status_code == 200 + + +@pytest.mark.django_db +class TestCounterLogout: + def test_logout_simple(self, client: Client): + perm_counter = baker.make(Counter, type="BAR") + permanence = baker.make( + Permanency, + counter=perm_counter, + start=now() - timedelta(hours=1), + activity=now() - timedelta(minutes=10), + ) + with freeze_time(): + res = client.post( + reverse("counter:logout", kwargs={"counter_id": permanence.counter_id}), + data={"user_id": permanence.user_id}, + ) + assertRedirects( + res, + reverse( + "counter:details", kwargs={"counter_id": permanence.counter_id} + ), + ) + permanence.refresh_from_db() + assert permanence.end == now() + + def test_logout_doesnt_change_old_permanences(self, client: Client): + perm_counter = baker.make(Counter, type="BAR") + permanence = baker.make( + Permanency, + counter=perm_counter, + start=now() - timedelta(hours=1), + activity=now() - timedelta(minutes=10), + ) + old_end = now() - relativedelta(year=10) + old_permanence = baker.make( + Permanency, + counter=perm_counter, + end=old_end, + activity=now() - relativedelta(year=8), + ) + with freeze_time(): + client.post( + reverse("counter:logout", kwargs={"counter_id": permanence.counter_id}), + data={"user_id": permanence.user_id}, + ) + permanence.refresh_from_db() + assert permanence.end == now() + old_permanence.refresh_from_db() + assert old_permanence.end == old_end diff --git a/counter/views/auth.py b/counter/views/auth.py index 87cce72c..eba165d0 100644 --- a/counter/views/auth.py +++ b/counter/views/auth.py @@ -13,10 +13,10 @@ # # -from django.db.models import F from django.http import HttpRequest, HttpResponseRedirect from django.shortcuts import get_object_or_404, redirect from django.utils import timezone +from django.utils.timezone import now from django.views.decorators.http import require_POST from core.views.forms import LoginForm @@ -47,7 +47,7 @@ def counter_login(request: HttpRequest, counter_id: int) -> HttpResponseRedirect @require_POST def counter_logout(request: HttpRequest, counter_id: int) -> HttpResponseRedirect: """End the permanency of a user in this counter.""" - Permanency.objects.filter(counter=counter_id, user=request.POST["user_id"]).update( - end=F("activity") - ) + Permanency.objects.filter( + counter=counter_id, user=request.POST["user_id"], end=None + ).update(end=now()) return redirect("counter:details", counter_id=counter_id)