0

I have a custom abstract base user and basic login with knox view, I made some simple tests to the register and login process, however all the testcases fail to assertion error:401!=200 and when I use pdb.set_trace to know the sent data it always has that error

(Pdb) res
<Response status_code=401, "application/json">
(Pdb) res.data
{'detail': ErrorDetail(string='Authentication credentials were not provided.', code='not_authenticated')} 

Here is the test setup

from rest_framework.test import APITestCase
from django.urls import reverse

class TestSetUp(APITestCase):

    def setUp(self):
        self.register_url = reverse('knox_register')
        self.login_url = reverse('knox_login')

        self.correct_data = {
            'email':"user@gmail.com",
            'password': "Abc1234#",
        }
        self.wrong_email_format = {
            'email': "user@gmail",
            'password': "Abc1234#",
        }
        self.missing_data = {
            'email':"user@gmail.com",
            'password':"",
        }
        self.wrong_password_format = {
            'email': "user@gmail.com",
            'password': "123",
        }
        return super().setUp()

    def tearDown(self):
        return super().tearDown()

and the test_view

from .test_setup import TestSetUp
import pdb

class TestViews(TestSetUp):

    #register with no data
    def test_user_cannot_register_with_no_data(self):
        res = self.client.post(self.register_url)

        self.assertEqual(res.status_code,400)

    #register with correct data
    def test_user_register(self):
        self.client.force_authenticate(None)
        res = self.client.post(
            self.register_url, self.correct_data, format="json")
        #pdb.set_trace()
        self.assertEqual(res.status_code,200)

    #register with wrong email format

    def test_register_with_wrong_email_format(self):
        res = self.client.post(
            self.register_url, self.wrong_email_format)
        self.assertEqual(res.status_code, 400)

    # register with wrong password format
    def test_register_with_wrong_password_format(self):
        res = self.client.post(
            self.register_url, self.wrong_password_format)
        self.assertEqual(res.status_code, 400)

    #register with missing_data
    def test_register_with_missing_data(self):
        res = self.client.post(
            self.register_url, self.missing_data)
        self.assertEqual(res.status_code, 400)

    #login with correct Credentials
    def test_user_login(self):
        self.client.post(
            self.register_url, self.correct_data,format="json")
        res = self.client.post(
            self.login_url,self.correct_data, format="json")
        #pdb.set_trace()
        self.assertEqual(res.status_code,200)

    # login with no Credentials
    def test_login(self):
        res = self.client.post(
            self.login_url, self.correct_data, format="json")
        # pdb.set_trace()
        self.assertEqual(res.status_code, 400)

    #login with no data
    def test_user_cannot_login_with_no_data(self):
        res = self.client.post(self.login_url)
        self.assertEqual(res.status_code,400)

Menna Magdy
  • 355
  • 1
  • 2
  • 14

1 Answers1

0

What are the permission and authentication classes for both login and registration views? If no classes have been defined on the views, they will be using default values. Please see https://www.django-rest-framework.org/tutorial/4-authentication-and-permissions/#authenticating-with-the-api