From 5ab22ba9475d843d513e6ad915a1902a594e4845 Mon Sep 17 00:00:00 2001 From: Real-Gecko Date: Fri, 29 May 2020 15:47:13 +0600 Subject: [PATCH] Added accounts test(moved from admin) Actuall added admin tests --- accounts/tests.py | 29 ++++++++++++- admin/tests.py | 103 ++++++++++++++++++++++++++++++++++++++++------ admin/views.py | 2 +- 3 files changed, 118 insertions(+), 16 deletions(-) diff --git a/accounts/tests.py b/accounts/tests.py index 7ce503c..4d6c01e 100644 --- a/accounts/tests.py +++ b/accounts/tests.py @@ -1,3 +1,28 @@ -from django.test import TestCase +from django.contrib.auth.models import User +from django.shortcuts import reverse +from django.test import Client, TestCase -# Create your tests here. + +class AccountsTestCase(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/admin/tests.py b/admin/tests.py index f174c26..09049dc 100644 --- a/admin/tests.py +++ b/admin/tests.py @@ -1,30 +1,107 @@ -from django.contrib.auth.models import User +from django.contrib.auth.models import Group, User +from django.core.exceptions import ObjectDoesNotExist from django.shortcuts import reverse -from django.test import Client, TestCase +from django.test import TestCase -from .urls import urlpatterns +from accounts.models import UserAttributes class AdminTestCase(TestCase): def setUp(self): self.client.login(username='admin', password='admin') - User.objects.create_user(username='test', password='test') + # User.objects.create_user(username='test', password='test') - def test_profile(self): - response = self.client.get(reverse('profile')) + # 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')) self.assertEqual(response.status_code, 200) - response = self.client.get(reverse('account', args=[2])) + 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')) self.assertEqual(response.status_code, 200) - def test_login_logout(self): + 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')) + user = User.objects.get(username='test') self.assertEqual(user.id, 2) - client = Client() + 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) - response = client.post(reverse('login'), {'username': 'test', 'password': 'test'}) - self.assertRedirects(response, reverse('profile')) + 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')) - response = client.get(reverse('logout')) - self.assertRedirects(response, reverse('login')) + 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) diff --git a/admin/views.py b/admin/views.py index 85a4370..955f23f 100644 --- a/admin/views.py +++ b/admin/views.py @@ -1,5 +1,4 @@ from django.conf import settings -from django.contrib.auth.decorators import user_passes_test from django.contrib.auth.models import Group, User from django.core.paginator import Paginator from django.shortcuts import get_object_or_404, redirect, render @@ -30,6 +29,7 @@ def group_create(request): if form.is_valid(): form.save() return redirect('admin:group_list') + return render( request, 'admin/common/form.html',