Improved tests
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
from django.apps import apps
|
||||
from six import string_types
|
||||
|
||||
from dav_base.tests.generic import AppSetting, AppsTestCase
|
||||
|
||||
@@ -8,6 +7,6 @@ class TestCase(AppsTestCase):
|
||||
app_config = apps.get_app_config('dav_auth')
|
||||
|
||||
settings = (
|
||||
AppSetting('login_redirect_url', 'root', string_types),
|
||||
AppSetting('logout_redirect_url', 'root', string_types),
|
||||
AppSetting('login_redirect_url', 'root', str),
|
||||
AppSetting('logout_redirect_url', 'root', str),
|
||||
)
|
||||
|
||||
@@ -147,7 +147,7 @@ class SetPasswordFormTestCase(FormsTestCase):
|
||||
|
||||
def test_save(self):
|
||||
new_passwords = [
|
||||
'"ä§ Mellon12'
|
||||
'"ä§ Mellon12',
|
||||
'mellon12' * 128,
|
||||
]
|
||||
|
||||
@@ -162,7 +162,7 @@ class SetPasswordFormTestCase(FormsTestCase):
|
||||
@skip('Function is implemented in SetPasswordView instead of SetPasswordForm')
|
||||
def test_save_with_mail(self): # pragma: no cover
|
||||
new_passwords = [
|
||||
'"ä§ Mellon12'
|
||||
'"ä§ Mellon12',
|
||||
'mellon12' * 128,
|
||||
]
|
||||
|
||||
|
||||
@@ -5,7 +5,8 @@ from django.test import TestCase, Client
|
||||
|
||||
|
||||
class ModelsTestCase(TestCase):
|
||||
@skip('I do not know, why the user.save() does not raise an exception')
|
||||
@skip('user.save() should raise an ValidationError when the user name is to long.'
|
||||
' But it does not. I do not know why.')
|
||||
def test_username_length(self):
|
||||
max_length = 150 # Hard coded in django.contrib.auth.models.AbstractUser
|
||||
username_ok = 'u' * max_length
|
||||
@@ -16,7 +17,8 @@ class ModelsTestCase(TestCase):
|
||||
first_name='A',
|
||||
last_name='User',
|
||||
email='a.user@example.com')
|
||||
user.save()
|
||||
with self.assertRaises(Exception):
|
||||
user.save()
|
||||
|
||||
def test_last_login(self):
|
||||
user_model = get_user_model()
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from django.apps import apps
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.contrib.messages import get_messages
|
||||
from django.core import mail as django_mail
|
||||
from django.shortcuts import resolve_url
|
||||
from django.test import TestCase
|
||||
@@ -35,7 +36,8 @@ class ViewsTestCase(TestCase):
|
||||
cls.login_message = gettext('Benutzer angemeldet: %(username)s')
|
||||
cls.logout_message = gettext('Benutzer abgemeldet.')
|
||||
cls.set_password_message = gettext('Passwort gespeichert.')
|
||||
cls.weak_password_warning_message = 'Dein Passwort entspricht nicht mehr den aktuellen Passwortrichtlinien.'
|
||||
cls.weak_password_warning_message = gettext('Dein Passwort entspricht nicht mehr den aktuellen Passwortrichtlinien.')
|
||||
cls.new_password_sent_message = gettext('Neues Passwort versendet.')
|
||||
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
@@ -60,6 +62,12 @@ class ViewsTestCase(TestCase):
|
||||
field = response.context['form'].fields['password']
|
||||
self.assertTrue(field.required)
|
||||
|
||||
def test_integrated_login_invalid_user(self):
|
||||
response = self.client.post(self.login_url, {'username': 'toor', 'password': self.test_password})
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertFormError(response.context['form'], None, self.wrong_credentials_message)
|
||||
self.assertFalse(response.context['user'].is_authenticated, 'User is logged in')
|
||||
|
||||
def test_integrated_login_inactive_user(self):
|
||||
user = self.user
|
||||
user.is_active = False
|
||||
@@ -80,15 +88,17 @@ class ViewsTestCase(TestCase):
|
||||
|
||||
def test_integrated_login_succeed(self):
|
||||
username = self.user.username
|
||||
message = self.login_message % {'username': username}
|
||||
expected_message = self.login_message % {'username': username}
|
||||
|
||||
response = self.client.post(self.login_url, {'username': username, 'password': self.test_password})
|
||||
self.assertEqual(response.status_code, 302)
|
||||
self.assertEqual(response.url, self.login_redirect_url)
|
||||
|
||||
response = self.client.get(response.url)
|
||||
self.assertContains(response, message)
|
||||
self.assertNotContains(response, self.weak_password_warning_message)
|
||||
messages = list(get_messages(response.wsgi_request))
|
||||
self.assertEqual(len(messages), 1)
|
||||
self.assertEqual(messages[0].message, expected_message)
|
||||
self.assertContains(response, expected_message)
|
||||
|
||||
self.assertTrue(response.context['user'].is_authenticated, 'Login failed')
|
||||
|
||||
@@ -96,7 +106,7 @@ class ViewsTestCase(TestCase):
|
||||
username = self.user.username
|
||||
password = TEST_WEAK_PASSWORD
|
||||
|
||||
message = self.login_message % {'username': username}
|
||||
expected_message = self.login_message % {'username': username}
|
||||
|
||||
user_model = get_user_model()
|
||||
user = user_model.objects.get(username=username)
|
||||
@@ -110,7 +120,11 @@ class ViewsTestCase(TestCase):
|
||||
self.assertEqual(response.url, self.login_redirect_url)
|
||||
|
||||
response = self.client.get(response.url)
|
||||
self.assertContains(response, message)
|
||||
messages = list(get_messages(response.wsgi_request))
|
||||
self.assertEqual(len(messages), 2)
|
||||
self.assertEqual(messages[0].message, expected_message)
|
||||
self.assertContains(response, expected_message)
|
||||
self.assertIn(self.weak_password_warning_message, messages[1].message)
|
||||
self.assertContains(response, self.weak_password_warning_message)
|
||||
|
||||
self.assertTrue(response.context['user'].is_authenticated, 'Login failed')
|
||||
@@ -123,6 +137,8 @@ class ViewsTestCase(TestCase):
|
||||
self.assertEqual(response.url, self.logout_redirect_url)
|
||||
|
||||
response = self.client.get(response.url)
|
||||
messages = list(get_messages(response.wsgi_request))
|
||||
self.assertEqual(messages[0].message, self.logout_message)
|
||||
self.assertContains(response, self.logout_message)
|
||||
|
||||
self.assertFalse(response.context['user'].is_authenticated, 'Logout failed')
|
||||
@@ -172,6 +188,8 @@ class ViewsTestCase(TestCase):
|
||||
self.assertEqual(len(django_mail.outbox), 0)
|
||||
|
||||
response = self.client.get(response.url)
|
||||
messages = list(get_messages(response.wsgi_request))
|
||||
self.assertEqual(messages[0].message, self.set_password_message)
|
||||
self.assertContains(response, self.set_password_message)
|
||||
|
||||
self.client.logout()
|
||||
@@ -203,6 +221,8 @@ class ViewsTestCase(TestCase):
|
||||
self.assertIn(new_password, mail.body)
|
||||
|
||||
response = self.client.get(response.url)
|
||||
messages = list(get_messages(response.wsgi_request))
|
||||
self.assertEqual(messages[0].message, self.set_password_message)
|
||||
self.assertContains(response, self.set_password_message)
|
||||
|
||||
self.client.logout()
|
||||
@@ -228,6 +248,9 @@ class ViewsTestCase(TestCase):
|
||||
location = self.recreate_password_url
|
||||
|
||||
response = self.client.post(location, {'username': self.user.username})
|
||||
messages = list(get_messages(response.wsgi_request))
|
||||
self.assertEqual(len(messages), 1)
|
||||
self.assertEqual(messages[0].message, self.new_password_sent_message)
|
||||
self.assertRedirects(response, self.login_url)
|
||||
|
||||
self.assertEqual(len(django_mail.outbox), 1)
|
||||
@@ -242,3 +265,14 @@ class ViewsTestCase(TestCase):
|
||||
|
||||
self.assertFalse(self.client.login(username=self.test_username, password=self.test_password),
|
||||
'Old password still valid')
|
||||
|
||||
def test_recreate_password_invalid_user(self):
|
||||
location = self.recreate_password_url
|
||||
|
||||
response = self.client.post(location, {'username': 'toor'})
|
||||
messages = list(get_messages(response.wsgi_request))
|
||||
self.assertEqual(len(messages), 1)
|
||||
self.assertEqual(messages[0].message, self.new_password_sent_message)
|
||||
self.assertRedirects(response, self.login_url)
|
||||
|
||||
self.assertEqual(len(django_mail.outbox), 0)
|
||||
|
||||
@@ -86,8 +86,8 @@ class EmailTestMixin:
|
||||
for expected_recipient in recipients:
|
||||
if isinstance(expected_recipient, AbstractUser):
|
||||
expected_recipient = '"%s" <%s>' % (expected_recipient.get_full_name(), expected_recipient.email)
|
||||
recipients = mail.recipients()
|
||||
self.assertIn(expected_recipient, recipients)
|
||||
real_recipients = mail.recipients()
|
||||
self.assertIn(expected_recipient, real_recipients)
|
||||
|
||||
def assertSubject(self, mail, subject): # pylint: disable=invalid-name
|
||||
expected_subject = '{} {}'.format(self.email_subject_prefix, subject)
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
from django.apps import apps
|
||||
from six import string_types
|
||||
|
||||
from .generic import AppSetting, AppsTestCase
|
||||
|
||||
@@ -8,7 +7,7 @@ class TestCase(AppsTestCase):
|
||||
app_config = apps.get_app_config('dav_base')
|
||||
|
||||
settings = (
|
||||
AppSetting('email_sender', None, string_types),
|
||||
AppSetting('email_base_url', None, string_types),
|
||||
AppSetting('email_subject_prefix', '', string_types),
|
||||
AppSetting('email_sender', None, str),
|
||||
AppSetting('email_base_url', None, str),
|
||||
AppSetting('email_subject_prefix', '', str),
|
||||
)
|
||||
|
||||
@@ -36,18 +36,18 @@ class DAVNumberValidatorTestCase(ValidatorTestMixin, SimpleTestCase):
|
||||
'131/321/1', # Ortsgruppennummer nicht zweistellig
|
||||
'131/00/', # Fehlende Mitgliedsnummer
|
||||
'131/00', # Fehlende Mitgliedsnummer
|
||||
'7654321' # Mitgliedsnummer mehr als sechs Stellen
|
||||
'999999*321' # Kategorienummer nicht vierstellig
|
||||
'999999*54321' # Kategorienummer nicht vierstellig
|
||||
'999999*9999*321' # DAV-Eintrittsjahr nicht vierstellig
|
||||
'999999*9999*54321' # DAV-Eintrittsjahr nicht vierstellig
|
||||
'999999*9999*9999*321' # Sektions-Eintrittsjahr nicht vierstellig
|
||||
'999999*9999*9999*54321' # Sektions-Eintrittsjahr nicht vierstellig
|
||||
'999999*9999*9999*9999*7654321' # Geburtsdatum nicht achtstellig
|
||||
'999999*9999*9999*9999*987654321' # Geburtsdatum nicht achtstellig
|
||||
'' # Leerstring
|
||||
' 1' # Leerzeichen am Anfang
|
||||
'54321 0001 2004 2014 ' # Leerzeichen am Ende
|
||||
'7654321', # Mitgliedsnummer mehr als sechs Stellen
|
||||
'999999*321', # Kategorienummer nicht vierstellig
|
||||
'999999*54321', # Kategorienummer nicht vierstellig
|
||||
'999999*9999*321', # DAV-Eintrittsjahr nicht vierstellig
|
||||
'999999*9999*54321', # DAV-Eintrittsjahr nicht vierstellig
|
||||
'999999*9999*9999*321', # Sektions-Eintrittsjahr nicht vierstellig
|
||||
'999999*9999*9999*54321', # Sektions-Eintrittsjahr nicht vierstellig
|
||||
'999999*9999*9999*9999*7654321', # Geburtsdatum nicht achtstellig
|
||||
'999999*9999*9999*9999*987654321', # Geburtsdatum nicht achtstellig
|
||||
'', # Leerstring
|
||||
' 1', # Leerzeichen am Anfang
|
||||
'54321 0001 2004 2014 ', # Leerzeichen am Ende
|
||||
'abc', # Nicht numerisch
|
||||
'131/00/abc', # Nicht numerisch
|
||||
'abc/00/131', # Nicht numerisch
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
import os
|
||||
from tempfile import mkdtemp as _mkdtemp
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
from django.apps import apps
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from six import string_types
|
||||
|
||||
from dav_base.tests.generic import AppSetting, AppsTestCase
|
||||
|
||||
@@ -9,5 +8,5 @@ class TestCase(AppsTestCase):
|
||||
app_config = apps.get_app_config('dav_registration')
|
||||
|
||||
settings = (
|
||||
AppSetting('privacy_policy', ImproperlyConfigured, string_types),
|
||||
AppSetting('privacy_policy', ImproperlyConfigured, str),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user