Added tests for Admin Dashboard model, view, template, and table

This commit is contained in:
Elijah 2023-11-24 15:23:59 +01:00
parent de56631778
commit f25cee86c8
5 changed files with 150 additions and 0 deletions

0
idhub/tests/__init__.py Normal file
View File

View File

@ -0,0 +1,17 @@
from django.test import TestCase
from idhub.models import Event
from idhub_auth.models import User
class EventModelTest(TestCase):
@classmethod
def setUpTestData(cls):
user = User.objects.create(email='testuser@email.com')
Event.objects.create(message='Test Event', type=1, user=user)
def test_event_creation(self):
event = Event.objects.get(id=1)
self.assertEqual(event.message, 'Test Event')
self.assertEqual(event.get_type_name(), 'User registered')
# Add more tests for other model methods and properties

View File

@ -0,0 +1,65 @@
from datetime import datetime
from django.test import TestCase
from django.urls import reverse
from idhub_auth.models import User
from idhub.admin.tables import DashboardTable
from idhub.models import Event
class AdminDashboardTableTest(TestCase):
def setUp(self):
self.admin_user = User.objects.create_superuser(
email='adminuser@mail.com',
password='adminpass12')
@classmethod
def setUpTestData(cls):
# Creating test events with different dates
Event.objects.create(message='Event 1', type=1,
created=datetime(2023, 1, 3))
Event.objects.create(message='Event 2', type=2,
created=datetime(2023, 1, 2))
Event.objects.create(message='Event 3', type=3,
created=datetime(2023, 1, 25))
def test_sorting_by_date(self):
# Create the table
table = DashboardTable(Event.objects.all())
# Apply sorting
table.order_by = 'created'
# Fetch the sorted records
sorted_records = list(table.rows)
# Verify the order is as expected
self.assertTrue(sorted_records[0].record.created
< sorted_records[1].record.created)
self.assertTrue(sorted_records[1].record.created
< sorted_records[2].record.created)
def test_table_in_template(self):
self.client.login(email='adminuser@mail.com', password='adminpass12')
response = self.client.get(reverse('idhub:admin_dashboard'))
self.assertTemplateUsed(response, 'idhub/custom_table.html')
def test_table_data(self):
Event.objects.create(message="test_event", type=2)
Event.objects.create(message="test_event", type=9)
table = DashboardTable(Event.objects.all())
self.assertTrue(isinstance(table, DashboardTable))
self.assertEqual(len(table.rows), Event.objects.count())
def test_table_columns(self):
table = DashboardTable(Event.objects.all())
expected_columns = ['type', 'message', 'created']
for column in expected_columns:
self.assertIn(column, table.columns)
def test_pagination(self):
# TODO
pass

View File

@ -0,0 +1,18 @@
from django.urls import reverse
from django.test import Client, TestCase
from idhub_auth.models import User
class TemplateTest(TestCase):
def setUp(self):
self.client = Client()
self.admin_user = User.objects.create_superuser(
email='adminuser@mail.com',
password='adminpass12')
def test_dashboard_template(self):
self.client.login(email='adminuser@mail.com', password='adminpass12')
response = self.client.get(reverse('idhub:admin_dashboard'))
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'idhub/base_admin.html')

50
idhub/tests/test_views.py Normal file
View File

@ -0,0 +1,50 @@
from django.urls import reverse
from django.test import TestCase
from idhub_auth.models import User
class AdminDashboardViewTest(TestCase):
def setUp(self):
self.user = User.objects.create_user(email='normaluser@mail.com',
password='testpass12')
self.admin_user = User.objects.create_superuser(
email='adminuser@mail.com',
password='adminpass12')
def test_view_url_exists_at_desired_location(self):
response = self.client.get('/admin/dashboard/', follow=True)
self.assertEqual(response.status_code, 200)
def test_view_redirects_to_login_when_not_authenticated(self):
response = self.client.get(reverse("idhub:admin_dashboard"),
follow=True)
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'auth/login.html')
def test_view_redirects_on_incorrect_login_attempt(self):
self.client.login(email='adminuser@mail.com', password='wrongpass')
response = self.client.get(reverse('idhub:admin_dashboard'))
self.assertEqual(response.status_code, 302)
def test_view_redirects_to_login_on_incorrect_login_attempt(self):
self.client.login(email='adminuser@mail.com', password='wrongpass')
response = self.client.get(reverse('idhub:admin_dashboard'),
follow=True)
self.assertTemplateUsed(response, 'auth/login.html')
def test_login_admin_user(self):
self.client.login(email='adminuser@mail.com', password='adminpass12')
response = self.client.get(reverse('idhub:admin_dashboard'))
self.assertEqual(response.status_code, 200)
def test_view_uses_correct_template(self):
self.client.login(email='adminuser@mail.com', password='adminpass12')
response = self.client.get(reverse('idhub:admin_dashboard'))
self.assertTemplateUsed(response, 'idhub/admin/dashboard.html')