fixes to the my_ views, and their tests
This commit is contained in:
@@ -2,7 +2,7 @@ from rest_framework import serializers
|
||||
from companies.models import Company
|
||||
|
||||
from drf_extra_fields.geo_fields import PointField
|
||||
|
||||
from tagulous.serializers.json import Serializer as TagSerializer
|
||||
from utils.tag_serializers import TagListSerializerField, TaggitSerializer
|
||||
|
||||
class CompanySerializer(TaggitSerializer, serializers.ModelSerializer):
|
||||
|
||||
@@ -259,14 +259,23 @@ class MyCompanyViewTest(APITestCase):
|
||||
self.user.save()
|
||||
|
||||
def test_auth_user_gets_data(self):
|
||||
# create instance
|
||||
user_instances = [
|
||||
self.factory(creator=self.user),
|
||||
self.factory(creator=self.user),
|
||||
]
|
||||
|
||||
# Authenticate
|
||||
token = get_tokens_for_user(self.user)
|
||||
self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {token['access']}")
|
||||
|
||||
# Query endpoint
|
||||
response = self.client.get(self.endpoint)
|
||||
payload = response.json()
|
||||
|
||||
# Assert forbidden code
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEquals(len(user_instances), len(payload))
|
||||
|
||||
def test_anon_user_cannot_access(self):
|
||||
# send in request
|
||||
|
||||
@@ -125,5 +125,5 @@ class CompanyViewSet(viewsets.ModelViewSet):
|
||||
@permission_classes([IsAuthenticated,])
|
||||
def my_company(request):
|
||||
qs = Company.objects.filter(creator=request.user)
|
||||
company_serializer = CompanySerializer(qs)
|
||||
company_serializer = CompanySerializer(qs, many=True)
|
||||
return Response(data=company_serializer.data)
|
||||
|
||||
@@ -521,8 +521,11 @@ class MyUserViewTest(APITestCase):
|
||||
|
||||
# Query endpoint
|
||||
response = self.client.get(self.endpoint)
|
||||
payload = response.json()
|
||||
|
||||
# Assert forbidden code
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEquals(self.email, payload['email'])
|
||||
|
||||
def test_anon_user_cannot_access(self):
|
||||
# send in request
|
||||
|
||||
@@ -128,7 +128,7 @@ def create_company_user(request):
|
||||
}
|
||||
try:
|
||||
user = models.CustomUser.objects.create(email=user_data['email'])
|
||||
except IntegrityError as e:
|
||||
except IntegrityError as e:
|
||||
return Response({"errors": {"details": str(e)}}, status=status.HTTP_409_CONFLICT)
|
||||
|
||||
try:
|
||||
@@ -159,9 +159,13 @@ def create_company_user(request):
|
||||
@api_view(['GET',])
|
||||
@permission_classes([IsAuthenticated,])
|
||||
def my_user(request):
|
||||
qs = User.objects.filter(email=request.user.email)
|
||||
user_serializer = core_serializers.CustomUserReadSerializer(qs, many=True)
|
||||
return Response(data=user_serializer.data)
|
||||
try:
|
||||
instance = User.objects.get(email=request.user.email)
|
||||
user_serializer = core_serializers.CustomUserReadSerializer(instance)
|
||||
return Response(data=user_serializer.data)
|
||||
except Exception as e:
|
||||
return Response({'error': {str(type(e))}}, status=500)
|
||||
|
||||
|
||||
|
||||
@api_view(['POST',])
|
||||
|
||||
@@ -500,14 +500,23 @@ class MyProductsViewTest(APITestCase):
|
||||
self.user.save()
|
||||
|
||||
def test_auth_user_gets_data(self):
|
||||
# create instance
|
||||
user_instances = [
|
||||
self.factory(creator=self.user),
|
||||
self.factory(creator=self.user),
|
||||
]
|
||||
|
||||
# Authenticate
|
||||
token = get_tokens_for_user(self.user)
|
||||
self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {token['access']}")
|
||||
|
||||
# Query endpoint
|
||||
response = self.client.get(self.endpoint)
|
||||
payload = response.json()
|
||||
|
||||
# Assert forbidden code
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEquals(len(user_instances), len(payload))
|
||||
|
||||
def test_anon_user_cannot_access(self):
|
||||
# send in request
|
||||
|
||||
@@ -56,7 +56,7 @@ class ProductViewSet(viewsets.ModelViewSet):
|
||||
@permission_classes([IsAuthenticated,])
|
||||
def my_products(request):
|
||||
qs = Product.objects.filter(creator=request.user)
|
||||
product_serializer = ProductSerializer(qs)
|
||||
product_serializer = ProductSerializer(qs, many=True)
|
||||
return Response(data=product_serializer.data)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user