0

在这里头疼。我有这个简单的 API,我想测试但目前还不能。它部分起作用。如果用户被阻止或删除,则会发生错误,它说:

 'str' object has no attribute status_code.

在此处输入图像描述

但我认为我确实在所有情况下都用 JsonResponse 回应。更糟糕的是,它适用于正确的用户并且当电子邮件不存在时。当从邮递员那里检查时,它会正确响应状态 400 和正确的消息。我的测试是否遗漏了什么?

class ForgotPwd(APIView):
    permission_classes = []

    def post(self,request, format = None):
        email  = request.data.get('email' , '').strip()
        reply = {}
        status = 400
        filters = {}
        if email:
            filters['email'] = email
            try:
                user_info = User.objects.values('id', 'status', 'email').get(**filters)
                if user_info['status'] == 0:
                    reply['detail'] = _('userBlocked')
                elif user_info['status'] == 2:
                    reply['detail'] = _('userMissing')
                else:
                    # send email
                    reply['detail'] = _('pwdResetLinkSent')
                    status = 200
            except BaseException as e:
                reply['detail'] = _('errorNoAccount')
        else:
            reply['detail'] = _('errorNoAccount')

        return JsonResponse(reply,status=status)

我的测试:

class ForgotPwdTest(APITestCase):
    """ Test module for initiating forgot password """
    def setUp(self):
        self.valid_payload = {
            'email': 'valid@gmail.com'
        }
        self.invalid_email_payload = {
            'email': 'invalid@yahoo.com'
        }
        self.blocked_user_payload = {
            'email': 'blocked@gmail.com'
        }      
        self.deleted_user_payload = {
            'email': 'deleted@gmail.com'
        } 

        user_valid = {'first_name': 'John', 'email': 'valid@gmail.com','password' : 'password', 'status': 1}
        self.user_valid = User.objects.create_user(**user_valid)



        # create users for other test cases
        user_blocked = {'first_name': 'John', 'email': 'blocked@gmail.com','password' : 'password', 'status' : 0}
        self.user_valid = User.objects.create_user(**user_blocked)
        user_deleted = {'first_name': 'John', 'email': 'deleted@gmail.com','password' : 'password', 'status' : 2}
        self.user_valid = User.objects.create_user(**user_deleted)

        self.url = reverse('forgotpwd')

    def test_valid_forgotpwd(self):
        """
        Ensure a valid user can start a forgot password process: WORKS
        """
        response = self.client.post(self.url , self.valid_payload, format='json')
        self.assertEqual(response.status_code, 200)

    '''
    def test_missing_email(self):
        """ The email is not registered in the system : WORKS """
        response = self.client.post(
            self.url,
            self.invalid_email_payload
        )
        self.assertEqual(response.status_code, 400)
        self.assertEqual(response.json()['detail'], _('errorNoAccount'))
    '''

    def test_blocked_user(self):
        """ Blocked user cannot initiate forgot password token """
        response = self.client.post(
            self.url ,
            self.blocked_user_payload
        )
        self.assertEqual(response.status_code, 400) # fails response is string
        self.assertContains(response.json()['detail'], _('userBlocked'))

    def test_deleted_user(self):
        """ Deleted user cannot initiate forgot password token """
        response = self.client.post(
            self.url ,
            self.deleted_user_payload,
            content_type='application/json'
        )
        self.assertEqual(response.status_code, 400) # fails response is string
        self.assertEqual(response['detail'], _('userMissing'))
4

0 回答 0