From 27fc3560454ff5383fec5226e3d07106d2555a35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Wed, 17 May 2017 17:19:42 +0100 Subject: [PATCH 1/2] Add a test for the user creation view --- users/tests.py | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/users/tests.py b/users/tests.py index 7ce503c..4d439ba 100644 --- a/users/tests.py +++ b/users/tests.py @@ -1,3 +1,34 @@ -from django.test import TestCase +from django.test import TestCase, Client +from django.conf import settings +from django.contrib.auth.models import User -# Create your tests here. + +class TestUserCreation(TestCase): + def test_create_view(self): + """Create a user using the user creation form""" + user_data = { + "username": "MrsFoobar", + "first_name": "Baz", + "last_name": "Foobar", + "email": "baz@foobar.net", + } + + data = user_data.copy() + data["password1"] = "4zwY5jdI" + data["password2"] = "4zwY5jdI" + data["key"] = settings.CREATE_USER_KEY + + client = Client() + resp = client.post("/user/create/", data) + + # The user redirection means successful form validation + self.assertRedirects(resp, "/") + + # The user should know exist + user = ( + User.objects + .filter(username=data["username"]) + .values(*user_data.keys()) + [0] + ) + self.assertEqual(user_data, user) From f105b17e3f8757c3dbd0c2314d677563825117d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Thu, 18 May 2017 16:53:29 +0100 Subject: [PATCH 2/2] replace `[0]` by `.get()` for a more robust test --- users/tests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/users/tests.py b/users/tests.py index 4d439ba..b0ef62b 100644 --- a/users/tests.py +++ b/users/tests.py @@ -29,6 +29,6 @@ class TestUserCreation(TestCase): User.objects .filter(username=data["username"]) .values(*user_data.keys()) - [0] + .get() ) self.assertEqual(user_data, user)