Exemple #1
0
    def test_change_password_2(self):
        request_data = {
            'old_password': '******',
            'new_password': '******',
            'username': '******'
        }
        login_data_1 = {
            'username': '******',
            'password': '******'
        }
        login_data_2 = {
            'username': '******',
            'password': '******'
        }
        serializer = ChangePasswordSerializer(data=request_data)
        login_serializer_1 = LoginSerializer(data=login_data_1)
        login_serializer_2 = LoginSerializer(data=login_data_2)
        self.assertTrue(login_serializer_1.is_valid(),
                        login_serializer_1.errors)
        self.assertTrue(login_serializer_2.is_valid(),
                        login_serializer_2.errors)

        self.assertIsNotNone(login_serializer_1.login(None))
        self.assertIsNone(login_serializer_2.login(None))

        self.assertTrue(serializer.is_valid(), serializer.errors)
        self.assertIsNotNone(serializer.save())

        self.assertIsNone(login_serializer_1.login(None))
        self.assertIsNotNone(login_serializer_2.login(None))
Exemple #2
0
    def login(self, request):
        '''
        handle user's login when POST to /api/user/login/
        '''
        if request.user.is_authenticated:
            return Response(UserSerializerForMe(request.user).data,
                            status=status.HTTP_200_OK)
        serializer = LoginSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        username = serializer.validated_data['username']
        password = serializer.validated_data['password']
        if not User.objects.filter(username__iexact=username).exists():
            return Response(
                {
                    u'detail': u'您输入的账号不存在,请重新输入',
                    u'field': u'username'
                },
                status=status.HTTP_401_UNAUTHORIZED)

        user = authenticate(username=username, password=password)
        if user is None:
            return Response({
                u'detail': u'您的密码有误,请重新输入',
                u'field': 'password'
            }, status.HTTP_401_UNAUTHORIZED)

        django_login(request, user)
        request.session.set_expiry(60 * 60 * 24 * 60)

        return Response(UserSerializerForMe(request.user).data,
                        status=status.HTTP_200_OK)
Exemple #3
0
 def post(self, request):
     serializer = LoginSerializer(data=request.data)
     serializer.is_valid(raise_exception=True)
     user = serializer.validated_data["user"]
     django_login(request, user)
     token, created = Token.objects.get_or_create(user=user)
     return Response({"token": token.key, 'status': status.HTTP_200_OK})
Exemple #4
0
 def test_login_4(self):
     request_data = {
         'username': '******',
         'password': '******',
         'aaa': 'aaa'
     }
     serializer = LoginSerializer(data=request_data)
     self.assertTrue(serializer.is_valid(), serializer.errors)
Exemple #5
0
 def post(self,request,**kwargs):
     serializers = LoginSerializer(data=request.data)
     if serializers.is_valid():
         user = serializers.login(request)
         if user:
             return Response(res_format(UserProfileSerializers(user).data,status=Message.SUCCESS),
                             status=status.HTTP_200_OK)
         else:
             return Response(res_format('Incorrect username or password',status=Message.ERROR))
     print('serializers.errors')
     return Response(res_format(serializers.errors,Message.ERROR))
Exemple #6
0
    def post(self, request):
        serializer = LoginSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        data = serializer.validated_data
        username, password = data.get('username', ''), data.get('password', '')
        customer = get_object_or_404(Customer.objects.all(), username=username)

        if not check_password(password, customer.password):
            raise ValidationError('wrong password', code=HTTP_403_FORBIDDEN)

        token = get_or_create_token(customer)
        return Response(data={'auth_token': token.key}, status=HTTP_200_OK)
Exemple #7
0
 def post(self, request):
     params = request.data
     try:
         user = Account.objects.get(mobile=params['mobile'])
         if user.is_user_verified:
             if user.check_password(params['password']):
                 login(request, user)
                 serializer = LoginSerializer(user)
                 return Response(
                     {
                         "response_message": "Login Successfully",
                         "data": serializer.data,
                         "token": user.create_jwt()
                     },
                     status=status.HTTP_200_OK)
             return Response(
                 {'response_message': "Please enter valid password."},
                 status=status.HTTP_400_BAD_REQUEST)
         user.otp_creation()
         user.sent_otp()
         return Response(
             {'response_message': "Please verify your otp first"},
             status=status.HTTP_400_BAD_REQUEST)
     except Account.DoesNotExist:
         return Response(
             {
                 'response_message':
                 "This mobile no is not associated with {{cookiecutter.project_name}}"
             },
             status=status.HTTP_400_BAD_REQUEST)
    def post(self, request, format=None):
        serializer = LoginSerializer(data=request.data)
        if serializer.is_valid():
            username = serializer.data.get('username')
            password = serializer.data.get('password')
            try:
                user = User.objects.get(username=username)
            except ObjectDoesNotExist:
                return Response({"message": "用户名不存在"}, status=status.HTTP_401_UNAUTHORIZED)
            if user.check_password(password):
                token = Token.objects.filter(user_id=user.pk).first()
                print(token)
                if token:
                    token.delete()
                content = {}
                token = md5(username)
                Token.objects.update_or_create(defaults={'key': token}, user_id=user.pk)
                content['username'] = user.username
                content['email'] = user.email
                content['token'] = token

                return Response(content, status=status.HTTP_200_OK)
        return Response({"message": "密码不正确"}, status=status.HTTP_401_UNAUTHORIZED)
Exemple #9
0
 def test_login_3(self):
     request_data = {'username': '******', 'password-123': 'password'}
     serializer = LoginSerializer(data=request_data)
     self.assertFalse(serializer.is_valid(), serializer.errors)
Exemple #10
0
 def post(self, request):
     serializer = LoginSerializer(data=request.data)
     serializer.is_valid(raise_exception=True)
     user = serializer.validated_data['user']
     token, created = Token.objects.get_or_create(user=user)
     return Response({'token': token.key})