def get(self, request, customer_id): try: token = request.META['HTTP_AUTHORIZATION'] authenticated = authenticate(customer_id=customer_id, token=token) except KeyError: response = Response(status=status.HTTP_400_BAD_REQUEST) else: if authenticated: customer = get_object_or_404(Customer, id=customer_id) serializer = CustomerSerializer(customer) response = Response(serializer.data, status=status.HTTP_200_OK) else: response = Response(status=status.HTTP_401_UNAUTHORIZED) return response
def post(self, request, customer_id, product_id): try: token = request.META['HTTP_AUTHORIZATION'] authenticated = authenticate(customer_id=customer_id, token=token) except KeyError: response = Response(status=status.HTTP_400_BAD_REQUEST) else: if authenticated: wishlist = get_or_update(customer_id, product_id) serializer = WishlistSerializer(wishlist) response = Response(serializer.data, status=status.HTTP_200_OK) else: response = Response(status=status.HTTP_401_UNAUTHORIZED) return response
def delete(self, request, customer_id): try: token = request.META['HTTP_AUTHORIZATION'] authenticated = authenticate(customer_id=customer_id, token=token) except KeyError: response = Response(status=status.HTTP_400_BAD_REQUEST) else: if authenticated: wishlist = get_object_or_404(Wishlist, customer__id=customer_id) wishlist.delete() response = Response(status=status.HTTP_204_NO_CONTENT) else: response = Response(status=status.HTTP_401_UNAUTHORIZED) return response
def put(self, request, customer_id): try: token = request.META['HTTP_AUTHORIZATION'] authenticated = authenticate(customer_id=customer_id, token=token) except KeyError: response = Response(status=status.HTTP_400_BAD_REQUEST) else: if authenticated: customer = get_object_or_404(Customer, id=customer_id) serializer = CustomerSerializer(customer, data=request.data, partial=True) if serializer.is_valid(): serializer.save() response = Response(serializer.data) else: response = Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) else: response = Response(status=status.HTTP_401_UNAUTHORIZED) return response
def test_invalid_token(self): wrong_token = self.token[:-1] decoded_token = authenticate(customer_id=self.customer.id, token=wrong_token) self.assertFalse(decoded_token)
def test_valid_token_decode(self): decoded_token = authenticate(customer_id=self.customer.id, token=self.token) self.assertTrue(decoded_token)