def test_eq(self):
        user1 = TokenUser({api_settings.USER_ID_CLAIM: 1})
        user2 = TokenUser({api_settings.USER_ID_CLAIM: 2})
        user3 = TokenUser({api_settings.USER_ID_CLAIM: 1})

        self.assertNotEqual(user1, user2)
        self.assertEqual(user1, user3)
    def test_is_superuser(self):
        payload = {api_settings.USER_ID_CLAIM: 42}
        user = TokenUser(payload)

        self.assertFalse(user.is_superuser)

        payload["is_superuser"] = True
        user = TokenUser(payload)

        self.assertTrue(user.is_superuser)
    def test_is_staff(self):
        payload = {api_settings.USER_ID_CLAIM: 42}
        user = TokenUser(payload)

        self.assertFalse(user.is_staff)

        payload['is_staff'] = True
        user = TokenUser(payload)

        self.assertTrue(user.is_staff)
    def setUp(self):
        self.token = AuthToken()
        self.token[api_settings.USER_ID_CLAIM] = 42
        self.token["username"] = "******"
        self.token["some_other_stuff"] = "arstarst"

        self.user = TokenUser(self.token)
    def setUp(self):
        self.token = AuthToken()
        self.token[api_settings.USER_ID_CLAIM] = 42
        self.token['username'] = '******'
        self.token['some_other_stuff'] = 'arstarst'

        self.user = TokenUser(self.token)
Пример #6
0
    def post(self, request):
        try:
            token = AccessToken(request.data["access"])
            token.check_exp()

        except Exception as e:
            return Response(invalidToken)

        user_id = TokenUser(token).id
        user = User.objects.filter(id=user_id).first()

        setOfClothes = SetOfClothes.objects.all().filter(users=user)

        if not setOfClothes:
            return Response({"response": "user have not set of clothes"})

        listClothes = []
        # print(setOfClothes)
        for i in setOfClothes:
            clothes = Clothes.objects.all().filter(set_Of_Clothes=i.id)

            serializer = ClothesSerializer(clothes, many=True)
            # listClothes.append({
            #     f'set {i.id}': serializer.data
            # })
            listClothes.append({f'{i.name_Set_Of_Clothes}': serializer.data})

        return Response(listClothes)
Пример #7
0
def _token_get_user_id(token):
    """
    The TokenUser class assumes tokens will have a recognizable user
    identifier claim.
    """
    try:
        Token = SlidingToken(token)
        assert api_settings.USER_ID_CLAIM in Token
        return TokenUser(Token).id
    except:
        raise InvalidJwtToken(detail='Token 失效')
Пример #8
0
    def get_user(self, validated_token):
        """
        Returns a stateless user object which is backed by the given validated
        token.
        """
        if api_settings.USER_ID_CLAIM not in validated_token:
            # The TokenUser class assumes tokens will have a recognizable user
            # identifier claim.
            raise InvalidToken(
                _('Token contained no recognizable user identification'))

        return TokenUser(validated_token)
Пример #9
0
def get_user(scope):
    try:
        headers = dict(scope.get('headers'))
        authorization = headers.get(b'authorization')

        token = AccessToken(authorization.decode().split(' ')[1])
        user_id = TokenUser(token).id
        user = get_user_model().objects.get(id=user_id)

    except Exception as UnauthenticatedUser:
        user = AnonymousUser()

    return user
Пример #10
0
    def put(self, request):
        try:
            token = AccessToken(request.data[0]["access"])
            token.check_exp()

        except Exception as e:
            return Response(invalidToken)

        if not existClothes(request):
            return Response(status=status.HTTP_400_BAD_REQUEST)

        # if existCloneSetOfClothes(request):
        #     return Response(status=status.HTTP_400_BAD_REQUEST)

        # get user in token
        user_id = TokenUser(token).id
        user = User.objects.filter(id=user_id).first()

        # create a new set of clothes and associate with a user
        setOfClothes = SetOfClothes.objects.create(
            users=user,
            name_Set_Of_Clothes=request.data[1]
            ["nameSetOfClothes"])  #bulk=False

        for i in range(2, len(request.data)):
            # print(str(request.data[i]))
            clothes = Clothes.objects.get_or_create(
                name_Clothes=request.data[i]["nameClothes"],
                type_Clothes=request.data[i]["typeClothes"],
                description=request.data[i]["description"],
                price=request.data[i]["price"],
                link_Image=request.data[i]["linkImage"],
                link_Source=request.data[i]["linkSource"])
            id_clothes = Clothes.objects.get(
                link_Source=request.data[i]["linkSource"]).id
            setOfClothes.clothes_set.add(id_clothes)

        return Response({"nameSetOfClothes": setOfClothes.name_Set_Of_Clothes})