Skip to content

Commit

Permalink
fixed issue expired events (#743)
Browse files Browse the repository at this point in the history
* fixed issue expired events

* needs a fix

* fixed bugs

* format

* format
  • Loading branch information
MadsNyl authored Dec 12, 2023
1 parent 0545831 commit 250a995
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 19 deletions.
1 change: 1 addition & 0 deletions app/common/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class Groups(ChoiceEnum):
JUBKOM = "JubKom"
REDAKSJONEN = "Redaksjonen"
FONDET = "Forvaltningsgruppen"
PLASK = "Plask"


class AppModel(ChoiceEnum):
Expand Down
26 changes: 16 additions & 10 deletions app/content/views/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,26 @@ def _list_queryset(self):
):
return self.queryset

activity = self.request.query_params.get("activity", "false").lower() == "true"
category = Category.objects.filter(text=CategoryEnum.ACTIVITY).first()
expired = self.request.query_params.get("expired", "false").lower() == "true"
activity = self.request.query_params.get("activity", None)

if activity and category:
return self._list_activity_queryset(category, expired, time)
if activity is None:
if expired:
return self.queryset.filter(end_date__lt=time).order_by("-start_date")
return self.queryset.filter(end_date__gte=time)

if expired:
return self.queryset.filter(end_date__lt=time).order_by("-start_date")
category = Category.objects.filter(text=CategoryEnum.ACTIVITY).first()
if category and activity.lower() == "true":
return self._list_activity_queryset(category, expired, time)

if category:
return self.queryset.filter(end_date__gte=time).filter(
~Q(category=category)
)
if category and activity.lower() == "false":
if expired:
return (
self.queryset.filter(end_date__lt=time)
.filter(~Q(category=category))
.order_by("-start_date")
)
return self.queryset.filter(~Q(category=category))

return self.queryset.filter(end_date__gte=time)

Expand Down
6 changes: 6 additions & 0 deletions app/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ def jubkom_member(member):
return member


@pytest.fixture()
def plask_member(member):
add_user_to_group_with_name(member, Groups.PLASK)
return member


@pytest.fixture()
def member_client(member):
return get_api_client(user=member)
Expand Down
62 changes: 53 additions & 9 deletions app/tests/content/test_event_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,18 +171,31 @@ def permission_test_util(

@pytest.mark.django_db
def test_list_as_anonymous_user(default_client, event):
"""An anonymous user should be able to list all events that are not activities."""
"""An anonymous user should be able to list all events. Activities should included."""

category = Category.objects.create(text="Aktivitet")
activity = EventFactory(category=category)

activity.category = category
activity.save()
EventFactory(category=category)

event.category = None
event.save()

response = default_client.get(API_EVENTS_BASE_URL)
assert response.status_code == 200
assert response.json().get("count") == 2


@pytest.mark.django_db
def test_list_events_as_anonymous_user(default_client, event):
"""An anonymous user should be able to list all events. Activities should not be included."""

category = Category.objects.create(text="Aktivitet")
EventFactory(category=category)

event.category = None
event.save()

response = default_client.get(f"{API_EVENTS_BASE_URL}?activity=false")

assert response.status_code == 200
assert response.json().get("count") == 1

Expand All @@ -192,10 +205,7 @@ def test_list_activities_as_anonymous_user(default_client, event):
"""An anonymous user should be able to list all activities."""

category = Category.objects.create(text="Aktivitet")
activity = EventFactory(category=category)

activity.category = category
activity.save()
EventFactory(category=category)

event.category = None
event.save()
Expand All @@ -206,6 +216,40 @@ def test_list_activities_as_anonymous_user(default_client, event):
assert response.json().get("count") == 1


@pytest.mark.django_db
def test_list_expired_events_as_anonymous_user(default_client, event):
"""
An anonymous user should be able to list all expired events.
"""

two_days_ago = now() - timedelta(days=1)
event.end_date = two_days_ago
event.save()

response = default_client.get(f"{API_EVENTS_BASE_URL}?expired=true")

assert response.status_code == 200
assert response.json().get("count") == 1


@pytest.mark.django_db
def test_list_expired_activities_as_anonymous_user(default_client, event):
"""
An anonymous user should be able to list all expired activities.
"""

two_days_ago = now() - timedelta(days=1)
category = Category.objects.create(text="Aktivitet")
event.end_date = two_days_ago
event.category = category
event.save()

response = default_client.get(f"{API_EVENTS_BASE_URL}?expired=true")

assert response.status_code == 200
assert response.json().get("count") == 1


@pytest.mark.django_db
def test_retrieve_as_anonymous_user(default_client, event):
"""An anonymous user should be able to retrieve an event."""
Expand Down

0 comments on commit 250a995

Please sign in to comment.