Unit tests, refactoring

This commit is contained in:
zandercymatics 2024-03-20 11:02:25 -06:00
parent e0aa729133
commit 54603cbfad
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
5 changed files with 101 additions and 8 deletions

View file

@ -204,6 +204,16 @@ from .common import less_console_noise
# <test code goes here>
```
Or alternatively, if you prefer using a decorator, just use:
```python
from .common import less_console_noise_decorator
@less_console_noise_decorator
def some_function():
# <test code goes here>
```
### Accessibility Testing in the browser
We use the [ANDI](https://www.ssa.gov/accessibility/andi/help/install.html) browser extension

View file

@ -1,18 +1,42 @@
{% load i18n static %}
<table class="dja-user-detail-table">
<table class="dja-user-detail-table" {% if field_name %} id="id_detail_table__{{field_name}}" {% endif %}>
<tbody>
<tr>
<th class="padding-left-0" scope="row">Title</th>
{% if user.title or user.contact.title %}
{% if user.contact.title %}
<td>{{ user.contact.title }}</td>
{% else %}
<td>{{ user.title }}</td>
{% endif %}
{% else %}
<td>Nothing found</td>
{% endif %}
</tr>
<tr>
<th class="padding-left-0" scope="row">Email</th>
{% if user.email or user.contact.email %}
{% if user.contact.email %}
<td>{{ user.contact.email }}</td>
{% else %}
<td>{{ user.email }}</td>
{% endif %}
{% else %}
<td>Nothing found</td>
{% endif %}
</tr>
<tr>
<th class="padding-left-0" scope="row">Phone</th>
{% if user.phone or user.contact.phone %}
{% if user.contact.phone %}
<td>{{ user.contact.phone }}</td>
{% else %}
<td>{{ user.phone }}</td>
{% endif %}
{% else %}
<td>Nothing found</td>
{% endif %}
</tr>
</tbody>
</table>

View file

@ -31,11 +31,11 @@ This is using a custom implementation fieldset.html (see admin/fieldset.html)
{% endif %}
</div>
{% if field.field.name == "creator" %}
{% include "django/admin/includes/domain_request_detail_table.html" with user=original.creator %}
{% include "django/admin/includes/domain_request_detail_table.html" with user=original.creator field_name="creator" %}
{% elif field.field.name == "submitter" %}
{% include "django/admin/includes/domain_request_detail_table.html" with user=original.submitter %}
{% include "django/admin/includes/domain_request_detail_table.html" with user=original.submitter field_name="submitter" %}
{% elif field.field.name == "authorizing_official" %}
{% include "django/admin/includes/domain_request_detail_table.html" with user=original.authorizing_official %}
{% include "django/admin/includes/domain_request_detail_table.html" with user=original.authorizing_official field_name="authorizing_official" %}
{% elif field.field.name == "other_contacts" %}
<details class="margin-top-1 dja-detail-table">
<summary class="padding-1 dja-details-summary">Details</summary>

View file

@ -734,6 +734,7 @@ def completed_domain_request(
"""A completed domain request."""
if not user:
user = get_user_model().objects.create(username="username" + str(uuid.uuid4())[:8])
ao, _ = Contact.objects.get_or_create(
first_name="Testy",
last_name="Tester",

View file

@ -2,6 +2,7 @@ from datetime import date
from django.test import TestCase, RequestFactory, Client, override_settings
from django.contrib.admin.sites import AdminSite
from contextlib import ExitStack
from api.tests.common import less_console_noise_decorator
from django_webtest import WebTest # type: ignore
from django.contrib import messages
from django.urls import reverse
@ -1193,6 +1194,63 @@ class TestDomainRequestAdmin(MockEppLib):
# Test that approved domain exists and equals requested domain
self.assertEqual(domain_request.requested_domain.name, domain_request.approved_domain.name)
@less_console_noise_decorator
def test_creator_has_detail_table(self):
"""Tests if the "creator" field has the detail table which displays title, email, and phone"""
# Create fake creator
_creator = User.objects.create(
username="MrMeoward",
first_name = "Meoward",
last_name = "Jones",
email="meoward.jones@igorville.gov",
phone="(555) 123 12345",
title="Treat inspector"
)
# Create a fake domain request
domain_request = completed_domain_request(
status=DomainRequest.DomainRequestStatus.IN_REVIEW,
creator=_creator
)
p = "userpass"
self.client.login(username="staffuser", password=p)
response = self.client.get(
"/admin/registrar/domainrequest/{}/change/".format(domain_request.pk),
follow=True,
)
# Make sure the page loaded, and that we're on the right page
self.assertEqual(response.status_code, 200)
self.assertContains(response, domain_request.requested_domain.name)
# Check that the modal has the right content
# Check for the header
# Check for the creator
self.assertContains(response, "Meoward Jones")
# Check that it is readonly
self.assertContains(response, "")
# Check for the right title, email, and phone number in the response
# Title
self.assertContains(response, "Treat inspector")
# Email
self.assertContains(response, "meoward.jones@igorville.gov")
# Phone number
self.assertContains(response, "(555) 123 12345")
# Check for table titles
# Title. We only need to check for the end tag
# (Otherwise this test will fail if we change classes, etc)
self.assertContains(response, "Title</th>")
self.assertContains(response, "Email</th>")
self.assertContains(response, "Phone</th>")
def test_save_model_sets_restricted_status_on_user(self):
with less_console_noise():
# make sure there is no user with this email
@ -1339,9 +1397,9 @@ class TestDomainRequestAdmin(MockEppLib):
"about_your_organization",
"requested_domain",
"approved_domain",
"alternative_domains",
"other_contacts",
"current_websites",
"alternative_domains",
"purpose",
"submitter",
"no_other_contacts_rationale",