Add registration form
This commit is contained in:
10
accounts/templates/accounts/registration.html
Normal file
10
accounts/templates/accounts/registration.html
Normal file
@@ -0,0 +1,10 @@
|
||||
{% extends 'front_template.html' %}
|
||||
|
||||
{% block body %}
|
||||
<p>For later convenience, please use your corporate ID as the username!</p>
|
||||
<form method="post" action="{% url 'accounts:register' %}">
|
||||
{% csrf_token %}
|
||||
{{ form.as_p }}
|
||||
<button type="submit">Register</button>
|
||||
</form>
|
||||
{% endblock %}
|
51
accounts/tests.py
Normal file
51
accounts/tests.py
Normal file
@@ -0,0 +1,51 @@
|
||||
from django.test import TestCase, Client
|
||||
from django_webtest import WebTest
|
||||
from django.contrib.auth.models import User
|
||||
from django.contrib.auth.forms import UserCreationForm
|
||||
|
||||
class FrontTest(TestCase):
|
||||
def setUp(self):
|
||||
self.client = Client()
|
||||
|
||||
def test_registration_page(self):
|
||||
response = self.client.get('/accounts/register')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
class RegFormTest(WebTest):
|
||||
def test_valid_data(self):
|
||||
form_data = {
|
||||
'username': 'test',
|
||||
'password1': 'password',
|
||||
'password2': 'password'
|
||||
}
|
||||
|
||||
form = UserCreationForm(form_data)
|
||||
self.assertTrue(form.is_valid())
|
||||
|
||||
user = form.save()
|
||||
self.assertEqual(user.username, 'test')
|
||||
# The password must be encrypted by now
|
||||
self.assertNotEqual(user.password, 'password')
|
||||
|
||||
def test_empty(self):
|
||||
form_data = {}
|
||||
form = UserCreationForm(form_data)
|
||||
self.assertFalse(form.is_valid())
|
||||
self.assertEqual(form.errors, {
|
||||
'username': ['This field is required.'],
|
||||
'password1': ['This field is required.'],
|
||||
'password2': ['This field is required.'],
|
||||
})
|
||||
|
||||
def test_form_error(self):
|
||||
page = self.app.get('/accounts/register')
|
||||
page = page.form.submit()
|
||||
self.assertContains(page, "This field is required.")
|
||||
|
||||
def test_form_success(self):
|
||||
page = self.app.get('/accounts/register')
|
||||
page.form['username'] = 'test'
|
||||
page.form['password1'] = 'password'
|
||||
page.form['password2'] = 'password'
|
||||
page = page.form.submit()
|
||||
self.assertRedirects(page, '/')
|
12
accounts/urls.py
Normal file
12
accounts/urls.py
Normal file
@@ -0,0 +1,12 @@
|
||||
from django.conf.urls import patterns, url
|
||||
|
||||
from .views import RegistrationFormView
|
||||
|
||||
urlpatterns = patterns(
|
||||
'',
|
||||
url(
|
||||
r'^register$',
|
||||
RegistrationFormView.as_view(),
|
||||
name = 'register'
|
||||
),
|
||||
)
|
23
accounts/views.py
Normal file
23
accounts/views.py
Normal file
@@ -0,0 +1,23 @@
|
||||
from django.shortcuts import render
|
||||
from django.views import generic
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.contrib.auth.forms import UserCreationForm
|
||||
|
||||
class RegistrationFormView(generic.View):
|
||||
form_class = UserCreationForm
|
||||
template_name = 'accounts/registration.html'
|
||||
|
||||
def get(self, request):
|
||||
form = self.form_class()
|
||||
return render(request, self.template_name, { 'form': form })
|
||||
|
||||
def post(self, request):
|
||||
form = self.form_class(request.POST)
|
||||
|
||||
if form.is_valid():
|
||||
form.save()
|
||||
|
||||
return HttpResponseRedirect(reverse('booking:index'))
|
||||
|
||||
return render(request, self.template_name, {'form': form})
|
Reference in New Issue
Block a user