diff --git a/CHANGELOG.md b/CHANGELOG.md index 0cbc8b0c2..be2a177e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,7 @@ - ✨ **Betalte arrangementer med Vipps betaling**. Det kan nå opprettes arrangementer som krever betaling for å melde seg på. Denne betalingen betales via Vipps. - ⚡ **Nyheter** Fondesforvalere kan nå lage nyheter. - ⚡ **Arrangementer** Du kan nå se hvilken plass du har på ventelisten til et arrangement. +- ✨ **Profil** Filtrere kommende og tidligere arrangementer ## Versjon 2022.10.13 diff --git a/app/content/views/user.py b/app/content/views/user.py index 727c2197b..e76c7451b 100644 --- a/app/content/views/user.py +++ b/app/content/views/user.py @@ -276,11 +276,17 @@ def get_user_detail_strikes(self, request, *args, **kwargs): @action(detail=False, methods=["get"], url_path="me/events") def get_user_events(self, request, *args, **kwargs): registrations = request.user.registrations.all() + + # Apply the filter + filter_field = self.request.query_params.get("expired") + event_has_ended = CaseInsensitiveBooleanQueryParam(filter_field) + events = [ registration.event for registration in registrations - if not registration.event.expired + if registration.event.expired == event_has_ended ] + return self.paginate_response( data=events, serializer=EventListSerializer, context={"request": request} ) diff --git a/app/tests/content/test_user_integration.py b/app/tests/content/test_user_integration.py index 27930833c..139f3bd9a 100644 --- a/app/tests/content/test_user_integration.py +++ b/app/tests/content/test_user_integration.py @@ -1,9 +1,13 @@ +from datetime import timedelta + +from django.utils import timezone from django.utils.text import slugify from rest_framework import status import pytest from app.common.enums import AdminGroup, GroupType +from app.content.factories.event_factory import EventFactory from app.content.factories.registration_factory import RegistrationFactory from app.content.factories.strike_factory import StrikeFactory from app.content.factories.user_factory import UserFactory @@ -32,6 +36,10 @@ def _get_user_detail_url(user): return f"{API_USER_BASE_URL}{user.user_id}/" +def _get_user_events_url(): + return f"{API_USER_BASE_URL}me/events/" + + def _get_user_post_data(): return { "email": "ola@nordmann.org", @@ -469,3 +477,93 @@ def test_destroy_other_user_as_index_user(member, user, api_client): response = client.delete(url) assert response.status_code == status.HTTP_200_OK + + +@pytest.mark.django_db +def test_list_expired_user_events(member, api_client): + """ "All the events listed as expired should be expired""" + client = api_client(user=member) + + two_days_ago = timezone.now() - timedelta(days=2) + event = EventFactory(end_date=two_days_ago) + + registration = RegistrationFactory(user=member, event=event) + + url = _get_user_events_url() + + query_params = {"expired": "true"} + + response = client.get(url, data=query_params) + + assert response.status_code == status.HTTP_200_OK + + registrations = response.json().get("results") + for registration in registrations: + assert registration.get("expired") + + +@pytest.mark.django_db +def test_list_unexpired_user_events(member, api_client): + """All the events listed as unexpired should be unexpired""" + client = api_client(user=member) + + two_days_ago = timezone.now() - timedelta(days=2) + event = EventFactory(end_date=two_days_ago) + + registration = RegistrationFactory(user=member, event=event) + + url = _get_user_events_url() + + query_params = {"expired": "false"} + + response = client.get(url, data=query_params) + + assert response.status_code == status.HTTP_200_OK + + registrations = response.json().get("results") + for registration in registrations: + assert not registration.get("expired") + + +@pytest.mark.django_db +def test_list_expired_user_events_with_a_blank_query_params(member, api_client): + """All the events listed should be unexpired when returning a blank query params""" + client = api_client(user=member) + + two_days_ago = timezone.now() - timedelta(days=2) + event = EventFactory(end_date=two_days_ago) + + registration = RegistrationFactory(user=member, event=event) + + url = _get_user_events_url() + + query_params = {"expired": ""} + + response = client.get(url, data=query_params) + + assert response.status_code == status.HTTP_200_OK + + registrations = response.json().get("results") + for registration in registrations: + assert not registration.get("expired") + + +@pytest.mark.django_db +def test_list_expired_user_events_with_no_query_params(member, api_client): + """All the events listed should be unexpired with no query params""" + client = api_client(user=member) + + two_days_ago = timezone.now() - timedelta(days=2) + event = EventFactory(end_date=two_days_ago) + + registration = RegistrationFactory(user=member, event=event) + + url = _get_user_events_url() + + response = client.get(url) + + assert response.status_code == status.HTTP_200_OK + + registrations = response.json().get("results") + for registration in registrations: + assert not registration.get("expired")