bunch of stuff
This commit is contained in:
@@ -312,12 +312,11 @@ class MyCompanyViewTest(APITestCase):
|
||||
self.user.set_password(self.password)
|
||||
self.user.save()
|
||||
|
||||
def tearDown(self):
|
||||
self.model.objects.all().delete()
|
||||
|
||||
def test_auth_user_gets_data(self):
|
||||
# create instance
|
||||
user_instances = [self.factory(creator=self.user) for i in range(5)]
|
||||
company = CompanyFactory()
|
||||
self.user.company = company
|
||||
self.user.save()
|
||||
|
||||
# Authenticate
|
||||
token = get_tokens_for_user(self.user)
|
||||
@@ -325,32 +324,10 @@ class MyCompanyViewTest(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(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['results']))
|
||||
self.assertEquals(payload['company']['id'], company.id)
|
||||
|
||||
def test_anon_user_cannot_access(self):
|
||||
# send in request
|
||||
|
||||
@@ -155,6 +155,16 @@ class CompanyViewSet(viewsets.ModelViewSet):
|
||||
return Response(message)
|
||||
|
||||
|
||||
@api_view(['GET'])
|
||||
@permission_classes([IsAuthenticated])
|
||||
def my_company(request):
|
||||
if request.user.company:
|
||||
serializer = CompanySerializer(request.user.company)
|
||||
return Response({'company': serializer.data})
|
||||
else:
|
||||
return Response(status=status.HTTP_406_NOT_ACCEPTABLE)
|
||||
|
||||
'''
|
||||
class MyCompanyViewSet(viewsets.ModelViewSet):
|
||||
model = Company
|
||||
serializer_class = CompanySerializer
|
||||
@@ -165,7 +175,7 @@ class MyCompanyViewSet(viewsets.ModelViewSet):
|
||||
|
||||
def perform_create(self, serializer):
|
||||
serializer.save(creator=self.request.user)
|
||||
|
||||
'''
|
||||
|
||||
class AdminCompanyViewSet(viewsets.ModelViewSet):
|
||||
""" Allows user with role 'SITE_ADMIN' to access all company instances
|
||||
|
||||
Reference in New Issue
Block a user