vendredi 12 février 2021

Django best practices for testing built-in authentication views

I read somewhere that: In testing a web-application, one should test all aspects of one's own code, but not any libraries or functionality provided as part of Python or Django. Now I'm trying to figure out what this means in regard of the built-in authentication system provided by Django. More precisely, I use the built in authentication system, i.e.,

urlpatterns = [
    path('', include('django.contrib.auth.urls')),
]

and now I want to write some tests.

First Question: Should I write tests for checking that every url exists and uses the right template for every view? I.e., should I write

def test_view_url_exists_at_desired_location(self):
        response = self.client.get('/login/')
        self.assertEqual(response.status_code, 200)

def test_view_url_accessible_by_name(self):
        response = self.client.get(reverse('login'))
        self.assertEqual(response.status_code, 200)

def test_view_uses_correct_template(self):
        response = self.client.get(reverse('login'))
        self.assertTemplateUsed(response, 'registration/login.html')

for all 8 views? I feel like I should test something, but at the same time it feels kind of redundant to test the same thing 8 times.

Second Question: I guess I don't have to test the functionality of the views if I use the built in user model, for example, I don't have to test that the login view actually logs-in a user etc. However, I'm using a custom user model (based on AbstractBaseUser) where email is used instead of username for authentication. Since I don't use the built-in user model should I test that the authentication views still work? If so, what should I test exactly?

Third Question: In my settings file I have

LOGOUT_REDIRECT_URL = '/login/'
LOGIN_REDIRECT_URL = '/'
LOGIN_URL = '/login/'

I guess I should write tests for that?

Fourth Question: Is there anything else I should consider related to this?

Aucun commentaire:

Enregistrer un commentaire