I am developing a web application using Django 1.6 where I need users to log in to my account. I want to write a test case that checks the user login procedure.
I managed to get a working login page, that is, I was able to login. The following steps explain my installation. However, writing a test case for this procedure failed. At the moment, I canβt detect an error in my test and I'm not sure that this approach makes sense first of all. Can anybody help me?
Update: In the meantime, I noticed that the form error message was too non-specific. Apparently, the problem is caused by empty form fields that do not perform any stage of form validation. When I add errors {{ form.errors }} to the template, the response contains the following (formatted a little less nicely):
<ul class="errorlist"> <li>username <ul class="errorlist"> <li>This field is required.</li> </ul> </li> <li>password <ul class="errorlist"> <li>This field is required.</li> </ul> </li> </ul>
I'm still not sure how the most reasonable way to test this procedure. I am completely new to Django and the testing philosophy. Maybe this question does not fall directly into the scope of Django testing, where models, views, and forms (apparently) are intended for testing, rather than being separated from each other? This test more likely falls into the scope of an independent set of tests performing the black core / functional testing, for example, using Selenium?
I started like this:
Launched a new project, for example:
django-admin.py startproject login_test
added an entry in login_test/urls.py , for example:
urlpatterns = patterns('', url(r'^login/$', 'django.contrib.auth.views.login'), url(r'^admin/', include(admin.site.urls)), )
compiled the template /home/moooeeeep/login_test/templates/registration/login.html , for example:
{% if form.errors %} <p>Your username and password didn't match. Please try again.</p> {% endif %} {% if not user.is_active %} <form method="post" action="{% url 'django.contrib.auth.views.login' %}"> {% csrf_token %} {{ form.as_p }} <button type="submit">Login</button> <input type="hidden" name="next" value="{% url 'django.contrib.auth.views.login' %}" /> </form> {% else %} <p>You are logged in as <strong>{{ user.username }}</strong>.</p> {% endif %}
added the template directory in TEMPLATE_DIRS in login_test/settings.py , for example:
TEMPLATE_DIRS = ( '/home/moooeeeep/login_test/templates/', )
Synchronize the database to get auth-related tables, and add the superuser as follows:
python manage.py syncdb
If I had not missed a single step, I could log in to my site. When I log in, I get my displayed username, if I donβt see the form, if the login fails, it also displays an error message.
My test case, however, after sending the correct messages, receives a message about an unsuccessful response at the entrance, while the user is not logged in.
Here is my login_test/tests.py :
from django.contrib.auth.models import User from django.contrib.auth import SESSION_KEY from django.test import TestCase class LogInTest(TestCase): def setUp(self): self.credentials = { 'username': 'testuser', 'password': 'secret'} User.objects.create_user(**self.credentials) def test_login(self):
Here's the conclusion:
$ python manage.py test Creating test database for alias 'default'... F ====================================================================== FAIL: test_login (login_test.tests.LogInTest) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/moooeeeep/login_test/login_test/tests.py", line 16, in test_login self.assertTrue(response.context['user'].is_active) AssertionError: False is not true ---------------------------------------------------------------------- Ran 1 test in 0.008s FAILED (failures=1) Destroying test database for alias 'default'...
Here is my login_test/settings.py :
import os BASE_DIR = os.path.dirname(os.path.dirname(__file__))