fixed space test and space api endpoint

This commit is contained in:
vabene1111
2024-08-24 09:18:57 +02:00
parent 39ab8b00c4
commit 8fe6f5c141
4 changed files with 26 additions and 6 deletions

View File

@@ -1 +1 @@
<?xml version="1.0" encoding="utf-8"?><testsuites><testsuite name="pytest" errors="0" failures="0" skipped="0" tests="5" time="35.973" timestamp="2024-08-18T14:19:20.591136" hostname="vabene-pc"><testcase classname="cookbook.tests.api.test_api_space" name="test_list_permission[arg0]" time="27.231" /><testcase classname="cookbook.tests.api.test_api_space" name="test_list_permission[arg1]" time="27.784" /><testcase classname="cookbook.tests.api.test_api_space" name="test_list_permission[arg3]" time="28.126" /><testcase classname="cookbook.tests.api.test_api_space" name="test_list_permission[arg4]" time="28.153" /><testcase classname="cookbook.tests.api.test_api_space" name="test_list_permission[arg2]" time="28.177" /></testsuite></testsuites>
<?xml version="1.0" encoding="utf-8"?><testsuites><testsuite name="pytest" errors="0" failures="0" skipped="0" tests="1" time="25.056" timestamp="2024-08-24T09:17:04.424827" hostname="vabene-pc"><testcase classname="cookbook.tests.api.test_api_space" name="test_list_multiple" time="24.455" /></testsuite></testsuites>

File diff suppressed because one or more lines are too long

View File

@@ -2,9 +2,12 @@ import json
import pytest
from django.contrib import auth
from django.contrib.auth.models import Group
from django.urls import reverse
from django_scopes import scopes_disabled
from cookbook.models import UserSpace
LIST_URL = 'api:space-list'
DETAIL_URL = 'api:space-detail'
@@ -26,6 +29,23 @@ def test_list_permission(arg, request, space_1, a1_s1):
assert len(json.loads(result.content)) == arg[2]
def test_list_multiple(u1_s1, space_1, space_2):
# only member of one space
u1_response = json.loads(u1_s1.get(reverse(LIST_URL)).content)
assert len(u1_response) == 1
# add user to a second space
us = UserSpace.objects.create(user=auth.get_user(u1_s1), space=space_2)
us.groups.add(Group.objects.get(name='admin'))
u1_response = json.loads(u1_s1.get(reverse(LIST_URL)).content)
assert len(u1_response) == 2
# test /current/ endpoint to only return active space
u1_response = json.loads(u1_s1.get(reverse('api:space-current')).content)
assert u1_response['id'] == space_1.id
@pytest.mark.parametrize("arg", [
['a_u', 403],
['g1_s1', 403],

View File

@@ -422,7 +422,7 @@ class SpaceViewSet(viewsets.ModelViewSet):
http_method_names = ['get', 'patch']
def get_queryset(self):
return self.queryset.filter(id__in=UserSpace.objects.filter(user=self.request.user))
return self.queryset.filter(id__in=UserSpace.objects.filter(user=self.request.user).values_list('space_id', flat=True))
@extend_schema(responses=SpaceSerializer(many=False))
@decorators.action(detail=False, pagination_class=None, methods=['GET'], serializer_class=SpaceSerializer, )