From 92bff1da93cdf5e7b05cfc341b6af595f0afb65c Mon Sep 17 00:00:00 2001 From: Sergio Oliveira Date: Thu, 26 Nov 2015 16:51:59 -0200 Subject: [PATCH] Added test cases --- colab/accounts/tests/test_forms.py | 42 ++++++++++++++++++++++++++++++++++++++++-- colab/accounts/tests/utils.py | 6 ++++++ 2 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 colab/accounts/tests/utils.py diff --git a/colab/accounts/tests/test_forms.py b/colab/accounts/tests/test_forms.py index fab174f..dd8e775 100644 --- a/colab/accounts/tests/test_forms.py +++ b/colab/accounts/tests/test_forms.py @@ -6,16 +6,54 @@ Objective: Test parameters, and behavior. import datetime from mock import patch -from django.test import TestCase +from django.test import TestCase, override_settings from django.core.urlresolvers import reverse from colab.accounts.forms import UserCreationForm, UserChangeForm,\ - UserUpdateForm, UserForm, get_lists_choices + UserUpdateForm, UserForm, get_lists_choices, SetPasswordForm from colab.accounts import forms as accounts_forms from colab.accounts.models import User from colab.accounts.utils import mailman +class SetPasswordFormTestCase(TestCase): + + TEST_COLAB_APPS = { + 'test_plugin': { + 'password_validators': ( + 'colab.accounts.tests.utils.password_validator', + ) + } + } + + @property + def user(self): + return User.objects.create_user(username='test_user', + email='test@example.com') + + @property + def valid_form_data(self): + return {'new_password1': '12345', + 'new_password2': '12345'} + + def test_no_custom_validators(self): + form = SetPasswordForm(self.user, data=self.valid_form_data) + self.assertTrue(form.is_valid(), True) + + @override_settings(COLAB_APPS=TEST_COLAB_APPS) + @patch('colab.accounts.tests.utils.password_validator') + def test_custom_validator(self, validator): + form = SetPasswordForm(self.user, data=self.valid_form_data) + self.assertTrue(form.is_valid()) + validator.assert_called_with('12345') + + @override_settings(COLAB_APPS=TEST_COLAB_APPS) + def test_custom_validator_raise_error(self): + form = SetPasswordForm(self.user, data=self.valid_form_data) + self.assertFalse(form.is_valid()) + self.assertEqual(form.errors['new_password1'][0], 'Test error') + + class FormTest(TestCase): def setUp(self): diff --git a/colab/accounts/tests/utils.py b/colab/accounts/tests/utils.py new file mode 100644 index 0000000..1d08d2d --- /dev/null +++ b/colab/accounts/tests/utils.py @@ -0,0 +1,6 @@ + +from django.forms import ValidationError + +def password_validator(password): + raise ValidationError('Test error') + -- libgit2 0.21.2