mirror of
https://github.com/retspen/webvirtcloud
synced 2024-10-31 19:44:16 +00:00
Added admin app unit tests
This commit is contained in:
parent
85929b5327
commit
83b1dde673
4 changed files with 51 additions and 13 deletions
|
@ -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
|
||||
|
|
30
admin/tests.py
Normal file
30
admin/tests.py
Normal file
|
@ -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'))
|
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue