From 4aedfe6a5c5f13a5ae9a3539b0e887fda56eef70 Mon Sep 17 00:00:00 2001 From: Elijah Date: Thu, 30 Nov 2023 17:06:00 +0100 Subject: [PATCH] Added tests for the User model and view --- idhub/templates/idhub/admin/people.html | 30 --------------- idhub/tests/test_models.py | 43 +++++++++++++++++++++- idhub/tests/test_views.py | 49 +++++++++++++++++++++++-- 3 files changed, 88 insertions(+), 34 deletions(-) diff --git a/idhub/templates/idhub/admin/people.html b/idhub/templates/idhub/admin/people.html index a40b318..4862f82 100644 --- a/idhub/templates/idhub/admin/people.html +++ b/idhub/templates/idhub/admin/people.html @@ -8,34 +8,4 @@ {{ subtitle }} {% render_table table %} -{% comment %}
- - - - - - - - - - - - - {% for user in users %} - - - - - - - - - {% endfor %} - -
{{ user.last_name|default:'' }}{{ user.first_name|default:'' }}{{ user.email }} - {{ user.get_memberships }} - - {{ user.get_roles }} -
-
{% endcomment %} {% endblock %} diff --git a/idhub/tests/test_models.py b/idhub/tests/test_models.py index 62bcdb3..351bfab 100644 --- a/idhub/tests/test_models.py +++ b/idhub/tests/test_models.py @@ -14,4 +14,45 @@ class EventModelTest(TestCase): self.assertEqual(event.message, 'Test Event') self.assertEqual(event.get_type_name(), 'User registered') - # Add more tests for other model methods and properties + +class UserTest(TestCase): + """ + Tests the very basic aspects of the User model, + like field properties and methods behaving as expected. + Further testing is recommended. + """ + + def setUp(self): + self.user = User.objects.create( + email="test@example.com", + is_admin=True, + first_name="Dummy", + last_name="Dummyson" + ) + + def test_field_properties(self): + user = User.objects.get(email="test@example.com") + self.assertEqual(user._meta.get_field('email').max_length, 255) + self.assertTrue(user._meta.get_field('email').unique) + self.assertTrue(user._meta.get_field('is_active').default) + self.assertFalse(user._meta.get_field('is_admin').default) + + def test_string_representation(self): + self.assertEqual(str(self.user), "test@example.com") + + def test_has_perm(self): + self.assertTrue(self.user.has_perm(None)) + + def test_has_module_perms(self): + self.assertTrue(self.user.has_module_perms(None)) + + def test_is_staff_property(self): + self.assertTrue(self.user.is_staff) + + def test_get_memberships(self): + # TODO + pass + + def test_get_roles(self): + # TODO + pass diff --git a/idhub/tests/test_views.py b/idhub/tests/test_views.py index cb9bbd5..fd131c0 100644 --- a/idhub/tests/test_views.py +++ b/idhub/tests/test_views.py @@ -1,7 +1,8 @@ from django.urls import reverse -from django.test import TestCase +from django.test import TestCase, RequestFactory from idhub_auth.models import User +from idhub.admin.views import PeopleListView class AdminDashboardViewTest(TestCase): @@ -38,13 +39,55 @@ class AdminDashboardViewTest(TestCase): self.assertTemplateUsed(response, 'auth/login.html') def test_login_admin_user(self): - self.client.login(email='adminuser@example.org', password='adminpass12') + self.client.login(email='adminuser@example.org', + 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@example.org', password='adminpass12') + self.client.login(email='adminuser@example.org', + password='adminpass12') response = self.client.get(reverse('idhub:admin_dashboard')) self.assertTemplateUsed(response, 'idhub/admin/dashboard.html') + + +class PeopleListViewTest(TestCase): + + def setUp(self): + # Set up a RequestFactory to create mock requests + self.factory = RequestFactory() + + # Create some user instances for testing + self.user = User.objects.create_user(email='normaluser@example.org', + password='testpass12') + self.admin_user = User.objects.create_superuser( + email='adminuser@example.org', + password='adminpass12') + + # Create a request object for the view + self.request = self.factory.get(reverse('idhub:admin_people_list')) + + self.request.user = self.admin_user + + def test_template_used(self): + response = PeopleListView.as_view()(self.request) + + self.assertEqual(response.template_name[0], "idhub/admin/people.html") + + def test_context_data(self): + response = PeopleListView.as_view()(self.request) + + self.assertIn('users', response.context_data) + + # Assuming 2 users were created + self.assertEqual(len(response.context_data['users']), 2) + + def test_get_queryset(self): + view = PeopleListView() + view.setup(self.request) + queryset = view.get_queryset() + + # Assuming 2 users in the database + self.assertEqual(queryset.count(), 2)