From 52980c529de28e6edd2f5117d6a174fe33bff4dc Mon Sep 17 00:00:00 2001 From: Real-Gecko Date: Fri, 29 May 2020 12:46:25 +0600 Subject: [PATCH] Added admin app unit tests --- accounts/apps.py | 12 ++++++------ admin/tests.py | 30 ++++++++++++++++++++++++++++++ instances/apps.py | 2 +- webvirtcloud/settings.py.template | 20 ++++++++++++++------ 4 files changed, 51 insertions(+), 13 deletions(-) create mode 100644 admin/tests.py diff --git a/accounts/apps.py b/accounts/apps.py index 4f128c1..94f4d27 100644 --- a/accounts/apps.py +++ b/accounts/apps.py @@ -10,19 +10,19 @@ def apply_change_password(sender, **kwargs): from django.conf import settings from django.contrib.auth.models import User, Permission if hasattr(settings, 'SHOW_PROFILE_EDIT_PASSWORD'): - print('\033[92mSHOW_PROFILE_EDIT_PASSWORD is found inside settings.py') - print('\033[92mApplying permission can_change_password for all users') + print('\033[92mSHOW_PROFILE_EDIT_PASSWORD is found inside settings.py\033[0m') + print('\033[92mApplying permission can_change_password for all users\033[0m') users = User.objects.all() permission = Permission.objects.get(codename='change_password') if settings.SHOW_PROFILE_EDIT_PASSWORD: - print('\033[91mWarning!!! Setting to True for all users') + print('\033[91mWarning!!! Setting to True for all users\033[0m') for user in users: user.user_permissions.add(permission) else: - print('\033[91mWarning!!! Setting to False for all users') + print('\033[91mWarning!!! Setting to False for all users\033[0m') for user in users: user.user_permissions.remove(permission) - print('\033[1mDon`t forget to remove the option from settings.py') + print('\033[1mDon`t forget to remove the option from settings.py\033[0m') def create_admin(sender, **kwargs): @@ -36,7 +36,7 @@ def create_admin(sender, **kwargs): for migration, rolled_back in plan: if migration.app_label == 'accounts' and migration.name == '0001_initial' and not rolled_back: if User.objects.count() == 0: - print('\033[92mCreating default admin user') + print('\033[92mCreating default admin user\033[0m') admin = User.objects.create_superuser('admin', None, 'admin') UserAttributes(user=admin, max_instances=-1, max_cpus=-1, max_memory=-1, max_disk_size=-1).save() break diff --git a/admin/tests.py b/admin/tests.py new file mode 100644 index 0000000..2db0d08 --- /dev/null +++ b/admin/tests.py @@ -0,0 +1,30 @@ +from django.test import TestCase +from django.contrib.auth.models import User +from .urls import urlpatterns +from django.shortcuts import reverse +from django.test import Client + + +class AdminTestCase(TestCase): + def setUp(self): + self.client.login(username='admin', password='admin') + User.objects.create_user(username='test', password='test') + + def test_profile(self): + response = self.client.get(reverse('profile')) + self.assertEqual(response.status_code, 200) + + response = self.client.get(reverse('account', args=[2])) + self.assertEqual(response.status_code, 200) + + def test_login_logout(self): + user = User.objects.get(username='test') + self.assertEqual(user.id, 2) + + client = Client() + + response = client.post(reverse('login'), {'username': 'test', 'password': 'test'}) + self.assertRedirects(response, reverse('profile')) + + response = client.get(reverse('logout')) + self.assertRedirects(response, reverse('login')) diff --git a/instances/apps.py b/instances/apps.py index f84e217..8b67b98 100644 --- a/instances/apps.py +++ b/instances/apps.py @@ -15,7 +15,7 @@ def migrate_can_clone_instances(sender, **kwargs): if migration.app_label == 'instances' and migration.name == '0002_permissionset' and not rolled_back: users = User.objects.all() permission = Permission.objects.get(codename='clone_instances') - print('\033[92mMigrating can_clone_instaces user attribute to permission') + print('\033[92mMigrating can_clone_instaces user attribute to permission\033[0m') for user in users: if user.userattributes: if user.userattributes.can_clone_instances: diff --git a/webvirtcloud/settings.py.template b/webvirtcloud/settings.py.template index e6d57e7..8809811 100644 --- a/webvirtcloud/settings.py.template +++ b/webvirtcloud/settings.py.template @@ -15,7 +15,6 @@ DEBUG = True ALLOWED_HOSTS = ['*'] # Application definition - INSTALLED_APPS = [ 'django.contrib.auth', 'django.contrib.contenttypes', @@ -91,9 +90,9 @@ AUTHENTICATION_BACKENDS = [ 'django.contrib.auth.backends.ModelBackend', ] -LOGIN_URL = '/accounts/login' +LOGIN_URL = '/accounts/login/' -LOGOUT_REDIRECT_URL = '/accounts/login' +LOGOUT_REDIRECT_URL = '/accounts/login/' LANGUAGE_CODE = 'en-us' @@ -114,14 +113,23 @@ STATICFILES_DIRS = [ LOGGING = { "version": 1, "disable_existing_loggers": False, - "handlers": {"mail_admins": {"level": "ERROR", "class": "django.utils.log.AdminEmailHandler"}}, + "handlers": { + "mail_admins": { + "level": "ERROR", + "class": "django.utils.log.AdminEmailHandler" + } + }, "loggers": { - "django.request": {"handlers": ["mail_admins"], "level": "ERROR", "propagate": True} + "django.request": { + "handlers": ["mail_admins"], + "level": "ERROR", + "propagate": True + } }, } # -## WebVirtCloud settings +# WebVirtCloud settings # # Websock port