Added tests for the User model and view

This commit is contained in:
Elijah 2023-11-30 17:06:00 +01:00 committed by Elahi
parent 5fcfc86f01
commit 4aedfe6a5c
3 changed files with 88 additions and 34 deletions

View File

@ -8,34 +8,4 @@
{{ subtitle }}
</h3>
{% render_table table %}
{% comment %}<div class="table-responsive">
<table class="table table-striped table-sm">
<thead>
<tr>
<th scope="col"><button type="button" class="btn btn-green-admin border border-dark">{% trans 'Last name' %}</button></th>
<th scope="col"><button type="button" class="btn btn-grey border border-dark">{% trans 'First name' %}</button></th>
<th scope="col"><button type="button" class="btn btn-grey border border-dark">Email</button></th>
<th scope="col" class="text-center"><button type="button" class="btn btn-grey border border-dark">{% trans 'Membership' %}</button></th>
<th scope="col" class="text-center"><button type="button" class="btn btn-grey border border-dark">{% trans 'Role' %}</button></th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
{% for user in users %}
<tr>
<td>{{ user.last_name|default:'' }}</td>
<td>{{ user.first_name|default:'' }}</td>
<td>{{ user.email }}</td>
<td class="text-center">
{{ user.get_memberships }}
</td>
<td class="text-center">
{{ user.get_roles }}
</td>
<td><a type="button" class="text-primary" href="{% url 'idhub:admin_people' user.id %}" title="{% trans 'View' %}"><i class="bi bi-eye"></i></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>{% endcomment %}
{% endblock %}

View File

@ -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

View File

@ -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)