Writing tests for a custom auth

32 views
Skip to first unread message

Valery Melou

unread,
Oct 10, 2014, 8:24:45 AM10/10/14
to django...@googlegroups.com
Hi everybody.
I'm creating a custom auth user model to remove 'first_name' and 'last_name' attributes from the default one and to make the 'email' field required and unique. I would like to know which tests I'm supposed to write to make sure that my model support each feature of the default one.

Russell Keith-Magee

unread,
Oct 10, 2014, 11:49:51 PM10/10/14
to Django Users
Hi Valery,

We don't currently ship an integration test to validate your custom User model, for two reasons:

 1) The requirements for a User model depends on what features you're using - the core requirements are listed in the docs, but depending on exactly what you're doing with the User model, there might be other integration requirements.

 2) We haven't got a good way to distribute a test suite for a model that we haven't defined.

That said, the baked in test suite in django.contrib.auth does test a number of tests to ensure that custom User models work; if you're looking for a starting point, that suite might be a good starting point. Any test that isn't marked @skipIfCustomUser needs to pass regardless of the user model. Tests labelled @override_settings(AUTH_USER_MODEL='auth.ExtensionUser') are tests for specific user models, which will give you an idea of the sort of tests you might need to add for your own model.

I hope that helps. If, as a result of doing this, you end up with a template for a good set of integration tests for a custom model, this would be a good contribution back to the community - a "template" project for a custom User model app that others can fork and modify as required would be one way for Django to distribute an integration test suite.

Yours,
Russ Magee %-)

On Fri, Oct 10, 2014 at 4:24 PM, Valery Melou <valer...@gmail.com> wrote:
Hi everybody.
I'm creating a custom auth user model to remove 'first_name' and 'last_name' attributes from the default one and to make the 'email' field required and unique. I would like to know which tests I'm supposed to write to make sure that my model support each feature of the default one.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/91e947b3-b3d4-4cdc-b7d0-b3426eeea668%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Valery Melou

unread,
Oct 11, 2014, 8:13:34 AM10/11/14
to django...@googlegroups.com
Thanks for your answer Russell.
I've been browsing the django.contrib.auth.test module and I was wondering if I should just rewrite all tests marked @sikpIfCustomUser. If so, there is a lot of work to do. Anyway I will start by checking those marked @override_settings(AUTH_USER_MODEL='auth.ExtensionUser') as you suggested.
Thanks again.

Russell Keith-Magee

unread,
Oct 11, 2014, 10:06:43 PM10/11/14
to Django Users
Hi Valery,

You probably don't need to reproduce everything that is labeled @skipIfCustomUser - those tests are the "core" tests to make sure the basic auth mechanisms work as expected. The remaining tests are the ones that exist to make sure that a couple of key patterns in custom Users work as expected. If you're writing a test suite for your own user model, you don't need to re-validated that the core logic works as expected - you just need to validate that your extensions play well with the core. 

Yours,
Russ Magee %-)

Reply all
Reply to author
Forward
0 new messages