0

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': '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'))
Nie Selam
  • 1,343
  • 2
  • 24
  • 56
  • Your issue doesn't seem to have anything to do with the code you've posted, since the URL you've posted to in each case is for login, not forgot password. – Daniel Roseman Jan 13 '19 at 15:10
  • Sorry....just removed it. Also, it was being over-written down the line as well. – Nie Selam Jan 13 '19 at 15:12
  • May be something mess. I tested in my local environment and it return int status code ike `AssertionError: 404 != 400`. Can you debug what actually string return by `response.status_code` – shafik Jan 13 '19 at 15:21
  • @ShafikurRahman: any way I can do that? I tried printing to the screen with sys module but failed to inspect what response returns. – Nie Selam Jan 13 '19 at 15:30
  • use this line `print(response.status_code)` and run the test. – shafik Jan 13 '19 at 15:32
  • @ShafikurRahman: it returns 400. And response is indeed JsonResponse type, not string as well but it still gives me the above error. – Nie Selam Jan 13 '19 at 15:46
  • Check your code again. Ma be somewhere you write `response.status_code, statud_code,`. This is not in your question. Check and let me know – shafik Jan 13 '19 at 15:50
  • Nop, nothing..all seems in good shape. I guess I will come back to it after a while since it is giving me no clue. – Nie Selam Jan 13 '19 at 16:16

0 Answers0