implemented pagination for my_products and my_company
This commit is contained in:
@@ -33,8 +33,6 @@ class CompanyViewSetTest(APITestCase):
|
||||
def test_anon_user_cannot_create_instance(self):
|
||||
"""Not logged-in user cannot create new instance
|
||||
"""
|
||||
instances = [self.factory() for n in range(random.randint(1,5))]
|
||||
|
||||
# Query endpoint
|
||||
response = self.client.post(self.endpoint, data={})
|
||||
# Assert access is forbidden
|
||||
@@ -76,6 +74,20 @@ class CompanyViewSetTest(APITestCase):
|
||||
# Assert access is forbidden
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
|
||||
def test_anon_user_can_paginate_instances(self):
|
||||
"""Not logged-in user can paginate instances
|
||||
"""
|
||||
instances = [self.factory() for n in range(12)]
|
||||
# Request list
|
||||
url = f"{self.endpoint}?limit=5&offset=10"
|
||||
response = self.client.get(url)
|
||||
|
||||
# Assert access is allowed
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
# assert only 2 instances in response
|
||||
payload = response.json()
|
||||
self.assertEquals(2, len(payload['results']))
|
||||
|
||||
def test_anon_user_can_filter_tags(self):
|
||||
# create instances
|
||||
expected_instance = [
|
||||
@@ -299,6 +311,27 @@ class MyCompanyViewTest(APITestCase):
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEquals(len(user_instances), len(payload))
|
||||
|
||||
def test_auth_user_can_paginate_instances(self):
|
||||
"""authenticated user can paginate instances
|
||||
"""
|
||||
|
||||
# Authenticate
|
||||
token = get_tokens_for_user(self.user)
|
||||
self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {token['access']}")
|
||||
|
||||
# create instances
|
||||
instances = [self.factory(creator=self.user) for n in range(12)]
|
||||
|
||||
# Request list
|
||||
url = f"{self.endpoint}?limit=5&offset=10"
|
||||
response = self.client.get(url)
|
||||
|
||||
# Assert access is allowed
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
# assert only 2 instances in response
|
||||
payload = response.json()
|
||||
self.assertEquals(2, len(payload))
|
||||
|
||||
def test_anon_user_cannot_access(self):
|
||||
# send in request
|
||||
response = self.client.get(self.endpoint)
|
||||
|
||||
@@ -158,6 +158,17 @@ class CompanyViewSet(viewsets.ModelViewSet):
|
||||
@api_view(['GET',])
|
||||
@permission_classes([IsAuthenticated,])
|
||||
def my_company(request):
|
||||
qs = Company.objects.filter(creator=request.user)
|
||||
company_serializer = CompanySerializer(qs, many=True)
|
||||
return Response(data=company_serializer.data)
|
||||
limit = request.GET.get('limit')
|
||||
offset = request.GET.get('offset')
|
||||
qs = Company.objects.filter(creator=request.user)
|
||||
company_serializer = CompanySerializer(qs, many=True)
|
||||
data = company_serializer.data
|
||||
# RESULTS PAGINATION
|
||||
if limit is not None and offset is not None:
|
||||
limit = int(limit)
|
||||
offset = int(offset)
|
||||
data = data[offset:(limit+offset)]
|
||||
elif limit is not None:
|
||||
limit = int(limit)
|
||||
data = data[:limit]
|
||||
return Response(data=data)
|
||||
|
||||
@@ -34,6 +34,9 @@ class ProductViewSetTest(APITestCase):
|
||||
self.password = ''.join(random.choices(string.ascii_uppercase, k = 10))
|
||||
self.user = CustomUserFactory(email="test@mail.com", password=self.password, is_active=True)
|
||||
|
||||
def tearDown(self):
|
||||
self.model.objects.all().delete()
|
||||
|
||||
# anon user
|
||||
def test_anon_user_cannot_create_instance(self):
|
||||
"""Not logged-in user cannot create new instance
|
||||
@@ -192,6 +195,26 @@ class ProductViewSetTest(APITestCase):
|
||||
self.assertEquals(len(expected_instance), len(payload))
|
||||
|
||||
# authenticated user
|
||||
def test_auth_user_can_paginate_instances(self):
|
||||
"""authenticated user can paginate instances
|
||||
"""
|
||||
# Authenticate
|
||||
token = get_tokens_for_user(self.user)
|
||||
self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {token['access']}")
|
||||
|
||||
# create instances
|
||||
instances = [self.factory(creator=self.user) for n in range(12)]
|
||||
|
||||
# Request list
|
||||
url = f"{self.endpoint}?limit=5&offset=10"
|
||||
response = self.client.get(url)
|
||||
|
||||
# Assert access is allowed
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
# assert only 2 instances in response
|
||||
payload = response.json()
|
||||
self.assertEquals(2, len(payload['results']))
|
||||
|
||||
def test_auth_user_can_list_instances(self):
|
||||
"""Regular logged-in user can list instance
|
||||
"""
|
||||
@@ -926,6 +949,26 @@ class MyProductsViewTest(APITestCase):
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEquals(len(user_instances), len(payload))
|
||||
|
||||
def test_auth_user_can_paginate_instances(self):
|
||||
"""authenticated user can paginate instances
|
||||
"""
|
||||
# Authenticate
|
||||
token = get_tokens_for_user(self.user)
|
||||
self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {token['access']}")
|
||||
|
||||
# create instances
|
||||
instances = [self.factory(creator=self.user) for n in range(12)]
|
||||
|
||||
# Request list
|
||||
url = f"{self.endpoint}?limit=5&offset=10"
|
||||
response = self.client.get(url)
|
||||
|
||||
# Assert access is allowed
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
# assert only 2 instances in response
|
||||
payload = response.json()
|
||||
self.assertEquals(2, len(payload))
|
||||
|
||||
def test_anon_user_cannot_access(self):
|
||||
# send in request
|
||||
response = self.client.get(self.endpoint)
|
||||
|
||||
@@ -54,9 +54,20 @@ class ProductViewSet(viewsets.ModelViewSet):
|
||||
@api_view(['GET',])
|
||||
@permission_classes([IsAuthenticated,])
|
||||
def my_products(request):
|
||||
qs = Product.objects.filter(creator=request.user)
|
||||
product_serializer = ProductSerializer(qs, many=True)
|
||||
return Response(data=product_serializer.data)
|
||||
limit = request.GET.get('limit')
|
||||
offset = request.GET.get('offset')
|
||||
qs = Product.objects.filter(creator=request.user)
|
||||
product_serializer = ProductSerializer(qs, many=True)
|
||||
data = product_serializer.data
|
||||
# RESULTS PAGINATION
|
||||
if limit is not None and offset is not None:
|
||||
limit = int(limit)
|
||||
offset = int(offset)
|
||||
data = data[offset:(limit+offset)]
|
||||
elif limit is not None:
|
||||
limit = int(limit)
|
||||
data = data[:limit]
|
||||
return Response(data=data)
|
||||
|
||||
|
||||
@api_view(['POST',])
|
||||
|
||||
Reference in New Issue
Block a user