Skip to content

Commit

Permalink
Merge pull request #898 from thunderstore-io/team-listviews-sorting
Browse files Browse the repository at this point in the history
Cyberstorm API: improve sorting on team-related list views
  • Loading branch information
anttimaki authored Oct 24, 2023
2 parents 746252c + ba10602 commit 7c9cfdc
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
56 changes: 56 additions & 0 deletions django/thunderstore/api/cyberstorm/tests/test_team.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import pytest
from django.contrib.auth import get_user_model
from rest_framework.test import APIClient

from thunderstore.account.factories import ServiceAccountFactory
from thunderstore.core.types import UserType
from thunderstore.repository.factories import TeamFactory, TeamMemberFactory
from thunderstore.repository.models.team import Team

User = get_user_model()


@pytest.mark.django_db
def test_team_detail_api_view__for_active_team__returns_data(
Expand Down Expand Up @@ -150,6 +153,35 @@ def test_team_members_api_view__for_member__returns_only_real_users(
assert result[0]["role"] == "member"


@pytest.mark.django_db
def test_team_members_api_view__for_member__sorts_results(
api_client: APIClient,
team: Team,
):
alice = User.objects.create(username="Alice")
bob = User.objects.create(username="Bob")
erin = User.objects.create(username="Erin")
dan = User.objects.create(username="Dan")
charlie = User.objects.create(username="Charlie")
TeamMemberFactory(team=team, user=alice, role="member")
TeamMemberFactory(team=team, user=bob, role="owner")
TeamMemberFactory(team=team, user=erin, role="member")
TeamMemberFactory(team=team, user=dan, role="owner")
TeamMemberFactory(team=team, user=charlie, role="member")
api_client.force_authenticate(alice)

response = api_client.get(f"/api/cyberstorm/team/{team.name}/members/")
result = response.json()

# Owners alphabetically, then members alphabetically.
assert len(result) == 5
assert result[0]["username"] == bob.username
assert result[1]["username"] == dan.username
assert result[2]["username"] == alice.username
assert result[3]["username"] == charlie.username
assert result[4]["username"] == erin.username


@pytest.mark.django_db
def test_team_service_accounts_api_view__for_member__returns_only_service_accounts(
api_client: APIClient,
Expand All @@ -167,3 +199,27 @@ def test_team_service_accounts_api_view__for_member__returns_only_service_accoun
assert result[0]["identifier"] == str(sa.uuid)
assert result[0]["name"] == sa.user.first_name
assert result[0]["last_used"] is None


@pytest.mark.django_db
def test_team_service_accounts_api_view__for_member__sorts_results(
api_client: APIClient,
team: Team,
user: UserType,
):
bob = User.objects.create(username="Bob", first_name="Bob")
charlie = User.objects.create(username="Charlie", first_name="Charlie")
alice = User.objects.create(username="Alice", first_name="Alice")
ServiceAccountFactory(owner=team, user=bob)
ServiceAccountFactory(owner=team, user=charlie)
ServiceAccountFactory(owner=team, user=alice)
TeamMemberFactory(team=team, user=user, role="member")
api_client.force_authenticate(user)

response = api_client.get(f"/api/cyberstorm/team/{team.name}/service-accounts/")
result = response.json()

assert len(result) == 3
assert result[0]["name"] == alice.first_name
assert result[1]["name"] == bob.first_name
assert result[2]["name"] == charlie.first_name
6 changes: 3 additions & 3 deletions django/thunderstore/api/cyberstorm/views/team.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from django.db.models import Q, QuerySet
from rest_framework.exceptions import NotFound, PermissionDenied
from rest_framework.filters import OrderingFilter
from rest_framework.generics import ListAPIView, RetrieveAPIView
from rest_framework.permissions import IsAuthenticated
from rest_framework.request import Request
Expand All @@ -11,6 +10,7 @@
CyberstormTeamMemberSerializer,
CyberstormTeamSerializer,
)
from thunderstore.api.ordering import StrictOrderingFilter
from thunderstore.api.utils import CyberstormAutoSchemaMixin
from thunderstore.repository.models.team import Team, TeamMember

Expand Down Expand Up @@ -45,7 +45,7 @@ def check_permissions(self, request: Request) -> None:

class TeamMembersAPIView(CyberstormAutoSchemaMixin, TeamRestrictedAPIView):
serializer_class = CyberstormTeamMemberSerializer
filter_backends = [OrderingFilter]
filter_backends = [StrictOrderingFilter]
ordering = ["-role", "user__username"]

def get_queryset(self) -> QuerySet[TeamMember]:
Expand All @@ -58,7 +58,7 @@ def get_queryset(self) -> QuerySet[TeamMember]:

class TeamServiceAccountsAPIView(CyberstormAutoSchemaMixin, TeamRestrictedAPIView):
serializer_class = CyberstormServiceAccountSerializer
filter_backends = [OrderingFilter]
filter_backends = [StrictOrderingFilter]
ordering = ["user__first_name"]

def get_queryset(self) -> QuerySet[ServiceAccount]:
Expand Down

0 comments on commit 7c9cfdc

Please sign in to comment.