From 5ee2baedba4089b361008fccce5bb5820242141d Mon Sep 17 00:00:00 2001 From: Skia Date: Thu, 19 Nov 2015 11:23:08 +0100 Subject: [PATCH] Add registering tests --- core/models.py | 2 +- core/templates/core/register.html | 8 +++ core/tests.py | 98 ++++++++++++++++++++++++++++++- core/views.py | 5 +- 4 files changed, 109 insertions(+), 4 deletions(-) diff --git a/core/models.py b/core/models.py index bf737d8d..9a1e5d0f 100644 --- a/core/models.py +++ b/core/models.py @@ -34,7 +34,7 @@ class User(AbstractBaseUser, PermissionsMixin): first_name = models.CharField(_('first name'), max_length=30) last_name = models.CharField(_('last name'), max_length=30) email = models.EmailField(_('email address'), unique=True) - date_of_birth = models.DateTimeField(_('date of birth'), default="1970-01-01") + date_of_birth = models.DateTimeField(_('date of birth'), default="1970-01-01T00:00:00+01:00") nick_name = models.CharField(max_length=30, blank=True) is_staff = models.BooleanField( _('staff status'), diff --git a/core/templates/core/register.html b/core/templates/core/register.html index 4ca1300a..ad53b450 100644 --- a/core/templates/core/register.html +++ b/core/templates/core/register.html @@ -5,6 +5,14 @@ {% block content %}

{{ title }}

+{% if user_registered %} +Welcome {{ user_registered.get_display_name }}! +You successfully registred and you will soon receive a confirmation mail. + +Your username is {{ user_registered.username }}. + +{% endif %} +
{% csrf_token %} {{ form }} diff --git a/core/tests.py b/core/tests.py index 7ce503c2..9163af77 100644 --- a/core/tests.py +++ b/core/tests.py @@ -1,3 +1,97 @@ -from django.test import TestCase +from django.test import SimpleTestCase, Client +from django.core.urlresolvers import reverse + +from .models import User +from .forms import RegisteringForm, LoginForm + +class UserRegistrationTest(SimpleTestCase): + def test_register_user_form_ok(self): + """ + Should register a user correctly + """ + c = Client() + response = c.post(reverse('core:register'), {'first_name': 'Guy', + 'last_name': 'Carlier', + 'email': 'guy@git.an', + 'password1': 'plop', + 'password2': 'plop', + }) + self.assertTrue(response.status_code == 200) + self.assertTrue('TEST_REGISTER_USER_FORM_OK' in str(response.content)) + + def test_register_user_form_fail_password(self): + """ + Should not register a user correctly + """ + c = Client() + response = c.post(reverse('core:register'), {'first_name': 'Guy', + 'last_name': 'Carlier', + 'email': 'bibou@git.an', + 'password1': 'plop', + 'password2': 'plop2', + }) + self.assertTrue(response.status_code == 200) + self.assertTrue('TEST_REGISTER_USER_FORM_OK' not in str(response.content)) + + def test_register_user_form_fail_email(self): + """ + Should not register a user correctly + """ + c = Client() + response = c.post(reverse('core:register'), {'first_name': 'Guy', + 'last_name': 'Carlier', + 'email': 'bibou.git.an', + 'password1': 'plop', + 'password2': 'plop', + }) + self.assertTrue(response.status_code == 200) + self.assertTrue('TEST_REGISTER_USER_FORM_OK' not in str(response.content)) + + def test_register_user_form_fail_missing_name(self): + """ + Should not register a user correctly + """ + c = Client() + response = c.post(reverse('core:register'), {'first_name': 'Guy', + 'last_name': '', + 'email': 'bibou@git.an', + 'password1': 'plop', + 'password2': 'plop', + }) + self.assertTrue(response.status_code == 200) + self.assertTrue('TEST_REGISTER_USER_FORM_OK' not in str(response.content)) + + def test_register_user_form_fail_missing_first_name(self): + """ + Should not register a user correctly + """ + c = Client() + response = c.post(reverse('core:register'), {'first_name': '', + 'last_name': 'Carlier', + 'email': 'bibou@git.an', + 'password1': 'plop', + 'password2': 'plop', + }) + self.assertTrue(response.status_code == 200) + self.assertTrue('TEST_REGISTER_USER_FORM_OK' not in str(response.content)) + + def test_register_user_form_fail_already_exists(self): + """ + Should not register a user correctly + """ + c = Client() + c.post(reverse('core:register'), {'first_name': 'Guy', + 'last_name': 'Carlier', + 'email': 'bibou@git.an', + 'password1': 'plop', + 'password2': 'plop', + }) + response = c.post(reverse('core:register'), {'first_name': 'Bibou', + 'last_name': 'Carlier', + 'email': 'bibou@git.an', + 'password1': 'plop', + 'password2': 'plop', + }) + self.assertTrue(response.status_code == 200) + self.assertTrue('TEST_REGISTER_USER_FORM_OK' not in str(response.content)) -# Create your tests here. diff --git a/core/views.py b/core/views.py index 85986675..6f91edb8 100644 --- a/core/views.py +++ b/core/views.py @@ -13,15 +13,18 @@ def index(request): return render(request, "core/index.html", {'title': 'Bienvenue!'}) def register(request): + context = {'title': 'Register a user'} if request.method == 'POST': form = RegisteringForm(request.POST) if form.is_valid(): logging.debug("Registering "+form.cleaned_data['first_name']+form.cleaned_data['last_name']) u = form.save() + context['user_registered'] = u form = RegisteringForm() else: form = RegisteringForm() - return render(request, "core/register.html", {'title': 'Register a user', 'form': form.as_p()}) + context['form'] = form.as_p() + return render(request, "core/register.html", context) def login(request): if request.method == 'POST':