2020-05-29 09:47:13 +00:00
|
|
|
from django.contrib.auth.models import Group, User
|
|
|
|
from django.core.exceptions import ObjectDoesNotExist
|
2020-05-29 06:46:25 +00:00
|
|
|
from django.shortcuts import reverse
|
2020-05-29 09:47:13 +00:00
|
|
|
from django.test import TestCase
|
2020-05-29 08:00:51 +00:00
|
|
|
|
2020-05-29 09:47:13 +00:00
|
|
|
from accounts.models import UserAttributes
|
2020-05-29 06:46:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AdminTestCase(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.client.login(username='admin', password='admin')
|
2020-05-29 09:47:13 +00:00
|
|
|
# User.objects.create_user(username='test', password='test')
|
2020-05-29 06:46:25 +00:00
|
|
|
|
2020-05-29 09:47:13 +00:00
|
|
|
# def test_user(self):
|
|
|
|
# user = User.objects.get(username='test')
|
|
|
|
# self.assertEqual(user.id, 2)
|
|
|
|
|
|
|
|
def test_group_list(self):
|
|
|
|
response = self.client.get(reverse('admin:group_list'))
|
2020-05-29 06:46:25 +00:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
2020-05-29 09:47:13 +00:00
|
|
|
def test_groups(self):
|
|
|
|
response = self.client.post(reverse('admin:group_create'), {'name': 'Test Group'})
|
|
|
|
self.assertRedirects(response, reverse('admin:group_list'))
|
|
|
|
|
|
|
|
group = Group.objects.get(name='Test Group')
|
|
|
|
self.assertEqual(group.id, 1)
|
|
|
|
|
|
|
|
response = self.client.post(reverse('admin:group_update', args=[1]), {'name': 'Updated Group Test'})
|
|
|
|
self.assertRedirects(response, reverse('admin:group_list'))
|
|
|
|
|
|
|
|
group = Group.objects.get(id=1)
|
|
|
|
self.assertEqual(group.name, 'Updated Group Test')
|
|
|
|
|
|
|
|
response = self.client.post(reverse('admin:group_delete', args=[1]))
|
|
|
|
self.assertRedirects(response, reverse('admin:group_list'))
|
|
|
|
|
|
|
|
with self.assertRaises(ObjectDoesNotExist):
|
|
|
|
Group.objects.get(id=1)
|
|
|
|
|
|
|
|
def test_user_list(self):
|
|
|
|
response = self.client.get(reverse('admin:user_list'))
|
2020-05-29 06:46:25 +00:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
2020-05-29 09:47:13 +00:00
|
|
|
def test_users(self):
|
|
|
|
response = self.client.post(
|
|
|
|
reverse('admin:user_create'),
|
|
|
|
{
|
|
|
|
'username': 'test',
|
|
|
|
'password': 'test',
|
|
|
|
'max_instances': 1,
|
|
|
|
'max_cpus': 1,
|
|
|
|
'max_memory': 1024,
|
|
|
|
'max_disk_size': 4,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
self.assertRedirects(response, reverse('admin:user_list'))
|
|
|
|
|
2020-05-29 06:46:25 +00:00
|
|
|
user = User.objects.get(username='test')
|
|
|
|
self.assertEqual(user.id, 2)
|
|
|
|
|
2020-05-29 09:47:13 +00:00
|
|
|
ua: UserAttributes = UserAttributes.objects.get(id=2)
|
|
|
|
self.assertEqual(ua.user_id, 2)
|
|
|
|
self.assertEqual(ua.max_instances, 1)
|
|
|
|
self.assertEqual(ua.max_cpus, 1)
|
|
|
|
self.assertEqual(ua.max_memory, 1024)
|
|
|
|
self.assertEqual(ua.max_disk_size, 4)
|
2020-05-29 06:46:25 +00:00
|
|
|
|
2020-05-29 09:47:13 +00:00
|
|
|
response = self.client.post(
|
|
|
|
reverse('admin:user_update', args=[2]),
|
|
|
|
{
|
|
|
|
'username': 'utest',
|
|
|
|
'max_instances': 2,
|
|
|
|
'max_cpus': 2,
|
|
|
|
'max_memory': 2048,
|
|
|
|
'max_disk_size': 8,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
self.assertRedirects(response, reverse('admin:user_list'))
|
2020-05-29 06:46:25 +00:00
|
|
|
|
2020-05-29 09:47:13 +00:00
|
|
|
user = User.objects.get(id=2)
|
|
|
|
self.assertEqual(user.username, 'utest')
|
|
|
|
|
|
|
|
ua: UserAttributes = UserAttributes.objects.get(id=2)
|
|
|
|
self.assertEqual(ua.user_id, 2)
|
|
|
|
self.assertEqual(ua.max_instances, 2)
|
|
|
|
self.assertEqual(ua.max_cpus, 2)
|
|
|
|
self.assertEqual(ua.max_memory, 2048)
|
|
|
|
self.assertEqual(ua.max_disk_size, 8)
|
|
|
|
|
|
|
|
response = self.client.get(reverse('admin:user_block', args=[2]))
|
|
|
|
user = User.objects.get(id=2)
|
|
|
|
self.assertFalse(user.is_active)
|
|
|
|
|
|
|
|
response = self.client.get(reverse('admin:user_unblock', args=[2]))
|
|
|
|
user = User.objects.get(id=2)
|
|
|
|
self.assertTrue(user.is_active)
|
|
|
|
|
|
|
|
response = self.client.post(reverse('admin:user_delete', args=[2]))
|
|
|
|
self.assertRedirects(response, reverse('admin:user_list'))
|
|
|
|
|
|
|
|
with self.assertRaises(ObjectDoesNotExist):
|
|
|
|
User.objects.get(id=2)
|
|
|
|
|
|
|
|
def test_user_logs(self):
|
|
|
|
response = self.client.get(reverse('admin:logs'))
|
|
|
|
self.assertEqual(response.status_code, 200)
|