JsonResponse is been read as string during django_nose testing

73 Views Asked by At

having a head ache here. I have this simple API that I want to test out but can't so far. It works partly. The error happens if the user is blocked or deleted, which says:

 'str' object has no attribute status_code.

enter image description here

But I think I am indeed responding with a JsonResponse in all cases. Worse yet, it works correctly for correct user and when the email doesn't exist. And when checking from postman, it responds correctly with status 400 and correct message. Am I missing something with my tests?

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)

My test:

class ForgotPwdTest(APITestCase):
    """ Test module for initiating forgot password """
    def setUp(self):
        self.valid_payload = {
            'email': '[email protected]'
        }
        self.invalid_email_payload = {
            'email': '[email protected]'
        }
        self.blocked_user_payload = {
            'email': '[email protected]'
        }      
        self.deleted_user_payload = {
            'email': '[email protected]'
        } 

        user_valid = {'first_name': 'John', 'email': '[email protected]','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': '[email protected]','password' : 'password', 'status' : 0}
        self.user_valid = User.objects.create_user(**user_blocked)
        user_deleted = {'first_name': 'John', 'email': '[email protected]','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'))
0

There are 0 best solutions below