2017-11-13 00:43:40 +00:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
from django.contrib.auth.models import User
|
|
|
|
|
from django.core.management import call_command
|
2020-02-14 18:23:58 +00:00
|
|
|
|
from django.test import Client as HttpClient, override_settings, TestCase
|
2020-02-14 17:48:25 +00:00
|
|
|
|
from django.utils import timezone
|
2017-11-13 00:43:40 +00:00
|
|
|
|
|
|
|
|
|
from faker import Factory
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FormsTestCase(TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
|
|
|
|
super(FormsTestCase, cls).setUpClass()
|
|
|
|
|
fake = Factory.create()
|
|
|
|
|
call_command('migrate', verbosity=0)
|
|
|
|
|
call_command('fake', verbosity=0)
|
|
|
|
|
|
|
|
|
|
cls.c = HttpClient()
|
|
|
|
|
|
|
|
|
|
fake_user = fake.simple_profile()
|
|
|
|
|
cls.credentials = {
|
|
|
|
|
'username': fake_user['username'],
|
|
|
|
|
'password': fake.password()
|
|
|
|
|
}
|
|
|
|
|
cls.user = User.objects.create_user(
|
|
|
|
|
is_superuser=True, **cls.credentials)
|
|
|
|
|
|
2019-04-19 02:52:17 +00:00
|
|
|
|
cls.settings_template = {
|
|
|
|
|
'first_name': 'User',
|
|
|
|
|
'last_name': 'Name',
|
|
|
|
|
'email': 'user@user.user',
|
|
|
|
|
'dashboard_refresh_rate': '',
|
2020-01-04 16:09:28 +00:00
|
|
|
|
'language': 'en',
|
2020-02-14 17:48:25 +00:00
|
|
|
|
'timezone': timezone.get_default_timezone_name(),
|
2020-01-04 16:09:28 +00:00
|
|
|
|
'next': '/user/settings/'
|
2019-04-19 02:52:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-02 21:20:15 +00:00
|
|
|
|
def test_change_password(self):
|
|
|
|
|
self.c.login(**self.credentials)
|
|
|
|
|
|
|
|
|
|
page = self.c.get('/user/password/')
|
|
|
|
|
self.assertEqual(page.status_code, 200)
|
|
|
|
|
|
|
|
|
|
params = {
|
|
|
|
|
'old_password': 'wrong',
|
|
|
|
|
'new_password1': 'mynewpassword',
|
|
|
|
|
'new_password2': 'notmynewpassword'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
page = self.c.post('/user/password/', params)
|
|
|
|
|
self.assertEqual(page.status_code, 200)
|
|
|
|
|
self.assertFormError(page, 'form', 'old_password',
|
|
|
|
|
'Your old password was entered incorrectly. '
|
|
|
|
|
'Please enter it again.')
|
|
|
|
|
|
|
|
|
|
params['old_password'] = self.credentials['password']
|
|
|
|
|
page = self.c.post('/user/password/', params)
|
|
|
|
|
self.assertEqual(page.status_code, 200)
|
|
|
|
|
self.assertFormError(page, 'form', 'new_password2',
|
2020-01-04 14:37:42 +00:00
|
|
|
|
"The two password fields didn’t match.")
|
2017-12-02 21:20:15 +00:00
|
|
|
|
|
|
|
|
|
params['new_password2'] = 'mynewpassword'
|
|
|
|
|
page = self.c.post('/user/password/', params)
|
2017-12-06 19:39:41 +00:00
|
|
|
|
self.assertEqual(page.status_code, 200)
|
2017-11-13 00:43:40 +00:00
|
|
|
|
|
2017-12-11 22:18:22 +00:00
|
|
|
|
def test_user_forms(self):
|
2017-12-13 18:49:10 +00:00
|
|
|
|
self.user.is_staff = True
|
|
|
|
|
self.user.save()
|
2017-12-11 22:18:22 +00:00
|
|
|
|
self.c.login(**self.credentials)
|
|
|
|
|
|
|
|
|
|
params = {
|
|
|
|
|
'username': 'username',
|
|
|
|
|
'first_name': 'User',
|
|
|
|
|
'last_name': 'Name',
|
|
|
|
|
'email': 'user@user.user',
|
|
|
|
|
'password1': 'password',
|
|
|
|
|
'password2': 'password'
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-11 22:25:25 +00:00
|
|
|
|
page = self.c.post('/users/add/', params)
|
2017-12-11 22:18:22 +00:00
|
|
|
|
self.assertEqual(page.status_code, 302)
|
|
|
|
|
new_user = User.objects.get(username='username')
|
|
|
|
|
self.assertIsInstance(new_user, User)
|
|
|
|
|
|
|
|
|
|
params['first_name'] = 'Changed'
|
2017-12-13 23:12:32 +00:00
|
|
|
|
page = self.c.post('/users/{}/edit/'.format(new_user.id), params)
|
2017-12-11 22:18:22 +00:00
|
|
|
|
self.assertEqual(page.status_code, 302)
|
|
|
|
|
new_user.refresh_from_db()
|
|
|
|
|
self.assertEqual(new_user.first_name, params['first_name'])
|
|
|
|
|
|
2017-12-11 22:25:25 +00:00
|
|
|
|
page = self.c.post('/users/{}/delete/'.format(new_user.id))
|
2017-12-11 22:18:22 +00:00
|
|
|
|
self.assertEqual(page.status_code, 302)
|
|
|
|
|
self.assertQuerysetEqual(User.objects.filter(username='username'), [])
|
|
|
|
|
|
2017-11-13 00:43:40 +00:00
|
|
|
|
def test_user_settings(self):
|
2017-12-02 21:20:15 +00:00
|
|
|
|
self.c.login(**self.credentials)
|
|
|
|
|
|
2019-04-19 02:52:17 +00:00
|
|
|
|
params = self.settings_template.copy()
|
|
|
|
|
params['first_name'] = 'New First Name'
|
2017-11-13 00:43:40 +00:00
|
|
|
|
|
2019-04-19 02:52:17 +00:00
|
|
|
|
page = self.c.post('/user/settings/', params, follow=True)
|
|
|
|
|
self.assertEqual(page.status_code, 200)
|
|
|
|
|
self.assertContains(page, 'New First Name')
|
|
|
|
|
|
|
|
|
|
def test_user_settings_invalid(self):
|
|
|
|
|
self.c.login(**self.credentials)
|
|
|
|
|
|
|
|
|
|
params = self.settings_template.copy()
|
|
|
|
|
params['email'] = 'Not an email address'
|
2017-11-13 00:43:40 +00:00
|
|
|
|
|
|
|
|
|
page = self.c.post('/user/settings/', params)
|
|
|
|
|
self.assertEqual(page.status_code, 200)
|
|
|
|
|
self.assertFormError(page, 'user_form', 'email',
|
|
|
|
|
'Enter a valid email address.')
|
2019-04-19 02:52:17 +00:00
|
|
|
|
|
|
|
|
|
def test_user_settings_language(self):
|
|
|
|
|
self.c.login(**self.credentials)
|
|
|
|
|
|
|
|
|
|
params = self.settings_template.copy()
|
|
|
|
|
params['language'] = 'fr'
|
|
|
|
|
page = self.c.post('/user/settings/', data=params, follow=True)
|
|
|
|
|
self.assertContains(page, 'Paramètres Utilisateur')
|
2020-02-14 18:23:58 +00:00
|
|
|
|
|
|
|
|
|
@override_settings(TIME_ZONE='US/Eastern')
|
|
|
|
|
def test_user_settings_timezone(self):
|
|
|
|
|
self.c.login(**self.credentials)
|
|
|
|
|
|
|
|
|
|
self.assertEqual(timezone.get_default_timezone_name(), 'US/Eastern')
|
|
|
|
|
params = self.settings_template.copy()
|
|
|
|
|
params['timezone'] = 'US/Pacific'
|
|
|
|
|
page = self.c.post('/user/settings/', data=params, follow=True)
|
|
|
|
|
self.assertEqual(page.status_code, 200)
|
|
|
|
|
self.assertEqual(timezone.get_current_timezone_name(),
|
|
|
|
|
params['timezone'])
|