예제 #1
0
 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
예제 #2
0
    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
예제 #3
0
 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
예제 #4
0
    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
예제 #5
0
    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)
예제 #6
0
    def test_valid_token_decode(self):
        decoded_token = authenticate(customer_id=self.customer.id,
                                     token=self.token)

        self.assertTrue(decoded_token)