duck-booking-tool/accounts/tests.py

118 lines
3.0 KiB
Python
Raw Permalink Normal View History

2015-10-20 14:26:25 +00:00
# -*- coding: utf-8
"""
Account management module for the Duck Booking Tool backend
"""
2015-01-12 15:51:54 +00:00
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):
2015-10-20 14:26:25 +00:00
"""
Test front-end capabilities of the accounts module
"""
2015-01-12 15:51:54 +00:00
def setUp(self):
self.client = Client()
2015-10-20 14:26:25 +00:00
self.admin = User.objects.create_user(username='admin',
password='password')
2014-12-17 12:30:58 +00:00
def test_login_page(self):
2015-10-20 14:26:25 +00:00
"""
Test for the existence of the login page
"""
2014-12-17 12:30:58 +00:00
response = self.client.get('/accounts/login')
self.assertEqual(response.status_code, 200)
def test_login(self):
2015-10-20 14:26:25 +00:00
"""
Test login functionality
"""
2014-12-17 12:30:58 +00:00
response = self.client.post('/accounts/login', {
'next': '/',
'username': 'admin',
'password': 'password'})
self.assertRedirects(response, '/')
def test_logout(self):
2015-10-20 14:26:25 +00:00
"""
Test the logout page
"""
self.client.login(username='admin', password='aeou')
2014-12-17 12:30:58 +00:00
response = self.client.get('/accounts/logout')
self.assertRedirects(response, '/')
2015-01-12 15:51:54 +00:00
def test_registration_page(self):
2015-10-20 14:26:25 +00:00
"""
Test for existence of the registration page
"""
2015-01-12 15:51:54 +00:00
response = self.client.get('/accounts/register')
self.assertEqual(response.status_code, 200)
class RegFormTest(WebTest):
2015-10-20 14:26:25 +00:00
"""
Test case for the registration form
"""
2015-01-12 15:51:54 +00:00
def test_valid_data(self):
2015-10-20 14:26:25 +00:00
"""
Test valid registration without actual HTTP requests
"""
2015-01-12 15:51:54 +00:00
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):
2015-10-20 14:26:25 +00:00
"""
Test empty registration form
"""
2015-01-12 15:51:54 +00:00
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):
2015-10-20 14:26:25 +00:00
"""
Test incomplete registration
"""
2015-01-12 15:51:54 +00:00
page = self.app.get('/accounts/register')
page = page.form.submit()
self.assertContains(page, "This field is required.")
def test_form_success(self):
2015-10-20 14:26:25 +00:00
"""
Test for successful registrations
"""
2015-01-12 15:51:54 +00:00
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, '/')