updated comments, updated test_models

This commit is contained in:
David Kennedy 2024-07-16 13:10:52 -04:00
parent bc75600202
commit a2558bd762
No known key found for this signature in database
GPG key ID: 6528A5386E66B96B
5 changed files with 233 additions and 158 deletions

View file

@ -181,7 +181,12 @@ class TestDomainRequestAdminForm(TestCase):
class TestDomainInvitationAdmin(TestCase):
"""Tests for the DomainInvitation page"""
"""Tests for the DomainInvitationAdmin class as super user
Notes:
all tests share superuser; do not change this model in tests
tests have available superuser, client, and admin
"""
@classmethod
def setUpClass(cls):
@ -246,6 +251,12 @@ class TestDomainInvitationAdmin(TestCase):
class TestHostAdmin(TestCase):
"""Tests for the HostAdmin class as super user
Notes:
all tests share superuser; do not change this model in tests
tests have available superuser, client, and admin
"""
@classmethod
def setUpClass(cls):
@ -317,6 +328,12 @@ class TestHostAdmin(TestCase):
class TestDomainInformationAdmin(TestCase):
"""Tests for the DomainInformationAdmin class as super or staff user
Notes:
all tests share superuser/staffuser; do not change these models in tests
tests have available staffuser, superuser, client, test_helper and admin
"""
@classmethod
def setUpClass(cls):
@ -699,6 +716,12 @@ class TestDomainInformationAdmin(TestCase):
class TestUserDomainRoleAdmin(TestCase):
"""Tests for the UserDomainRoleAdmin class as super user
Notes:
all tests share superuser; do not change this model in tests
tests have available superuser, client, test_helper and admin
"""
@classmethod
def setUpClass(cls):
@ -876,6 +899,12 @@ class TestUserDomainRoleAdmin(TestCase):
fake_user.delete()
class TestListHeaderAdmin(TestCase):
"""Tests for the ListHeaderAdmin class as super user
Notes:
all tests share superuser; do not change this model in tests
tests have available superuser, client and admin
"""
@classmethod
def setUpClass(cls):
@ -957,6 +986,13 @@ class TestListHeaderAdmin(TestCase):
class TestMyUserAdmin(MockDbForSharedTests):
"""Tests for the MyUserAdmin class as super or staff user
Notes:
all tests share superuser/staffuser; do not change these models in tests
all tests share MockDb; do not change models defined therein in tests
tests have available staffuser, superuser, client, test_helper and admin
"""
@classmethod
def setUpClass(cls):

View file

@ -36,6 +36,12 @@ logger = logging.getLogger(__name__)
class TestDomainAdminAsStaff(MockEppLib):
"""Test DomainAdmin class as staff user.
Notes:
all tests share staffuser; do not change staffuser model in tests
tests have available staffuser, client, and admin
"""
@classmethod
def setUpClass(self):
@ -367,6 +373,12 @@ class TestDomainAdminAsStaff(MockEppLib):
class TestDomainAdminWClient(TestCase):
"""Test DomainAdmin class as super user.
Notes:
all tests share superuser; tests must not update superuser
tests have available superuser, client, and admin
"""
@classmethod
def setUpClass(self):
@ -651,6 +663,14 @@ class TestDomainAdminWClient(TestCase):
class TestDomainAdminWebTest(MockEppLib, WebTest):
"""Test DomainAdmin class as super user, using WebTest.
WebTest allows for easier handling of forms and html responses.
Notes:
all tests share superuser; tests must not update superuser
tests have available superuser, app, and admin
"""
# csrf checks do not work with WebTest.
# We disable them here. TODO for another ticket.
csrf_checks = False

View file

@ -42,6 +42,12 @@ logger = logging.getLogger(__name__)
@boto3_mocking.patching
class TestDomainRequestAdmin(MockEppLib):
"""Test DomainRequestAdmin class as either staff or super user.
Notes:
all tests share superuser/staffuser; do not change these models in tests
tests have available staffuser, superuser, client, admin and test_helper
"""
@classmethod
def setUpClass(self):
@ -61,6 +67,20 @@ class TestDomainRequestAdmin(MockEppLib):
)
self.mock_client = MockSESClient()
def tearDown(self):
super().tearDown()
Domain.objects.all().delete()
DomainInformation.objects.all().delete()
DomainRequest.objects.all().delete()
Contact.objects.all().delete()
Website.objects.all().delete()
self.mock_client.EMAILS_SENT.clear()
@classmethod
def tearDownClass(self):
super().tearDownClass()
User.objects.all().delete()
@less_console_noise_decorator
def test_has_model_description(self):
"""Tests if this model has a model description on the table view"""
@ -1854,18 +1874,4 @@ class TestDomainRequestAdmin(MockEppLib):
# Check if response contains expected_html
self.assertIn(expected_html, response_content)
def tearDown(self):
super().tearDown()
Domain.objects.all().delete()
DomainInformation.objects.all().delete()
DomainRequest.objects.all().delete()
Contact.objects.all().delete()
Website.objects.all().delete()
self.mock_client.EMAILS_SENT.clear()
@classmethod
def tearDownClass(self):
super().tearDownClass()
User.objects.all().delete()

View file

@ -1,5 +1,6 @@
from django.test import TestCase
from django.db.utils import IntegrityError
from django.db import transaction
from unittest.mock import patch
from django.contrib.auth import get_user_model
@ -23,15 +24,12 @@ from registrar.models.transition_domain import TransitionDomain
from registrar.models.verified_by_staff import VerifiedByStaff # type: ignore
from registrar.utility.constants import BranchChoices
from .common import MockSESClient, less_console_noise, completed_domain_request, set_domain_request_investigators
from .common import MockSESClient, less_console_noise, completed_domain_request, set_domain_request_investigators, create_test_user
from django_fsm import TransitionNotAllowed
from waffle.testutils import override_flag
from api.tests.common import less_console_noise_decorator
# Test comment for push -- will remove
# The DomainRequest submit method has a side effect of sending an email
# with AWS SES, so mock that out in all of these test cases
@boto3_mocking.patching
class TestDomainRequest(TestCase):
@less_console_noise_decorator
@ -92,6 +90,11 @@ class TestDomainRequest(TestCase):
def tearDown(self):
super().tearDown()
DomainInformation.objects.all().delete()
DomainRequest.objects.all().delete()
DraftDomain.objects.all().delete()
Domain.objects.all().delete()
User.objects.all().delete()
self.mock_client.EMAILS_SENT.clear()
def assertNotRaises(self, exception_type):
@ -121,23 +124,22 @@ class TestDomainRequest(TestCase):
self.assertEqual(domain_request.federal_agency, expected_federal_agency)
def test_empty_create_fails(self):
"""Can't create a completely empty domain request.
NOTE: something about theexception this test raises messes up with the
atomic block in a custom tearDown method for the parent test class."""
"""Can't create a completely empty domain request."""
with less_console_noise():
with transaction.atomic():
with self.assertRaisesRegex(IntegrityError, "creator"):
DomainRequest.objects.create()
@less_console_noise_decorator
def test_minimal_create(self):
"""Can create with just a creator."""
with less_console_noise():
user, _ = User.objects.get_or_create(username="testy")
domain_request = DomainRequest.objects.create(creator=user)
self.assertEqual(domain_request.status, DomainRequest.DomainRequestStatus.STARTED)
@less_console_noise_decorator
def test_full_create(self):
"""Can create with all fields."""
with less_console_noise():
user, _ = User.objects.get_or_create(username="testy")
contact = Contact.objects.create()
com_website, _ = Website.objects.get_or_create(website="igorville.com")
@ -166,9 +168,9 @@ class TestDomainRequest(TestCase):
domain_request.other_contacts.add(contact)
domain_request.save()
@less_console_noise_decorator
def test_domain_info(self):
"""Can create domain info with all fields."""
with less_console_noise():
user, _ = User.objects.get_or_create(username="testy")
contact = Contact.objects.create()
domain, _ = Domain.objects.get_or_create(name="igorville.gov")
@ -194,8 +196,8 @@ class TestDomainRequest(TestCase):
self.assertEqual(information.domain.id, domain.id)
self.assertEqual(information.id, domain.domain_info.id)
@less_console_noise_decorator
def test_status_fsm_submit_fail(self):
with less_console_noise():
user, _ = User.objects.get_or_create(username="testy")
domain_request = DomainRequest.objects.create(creator=user)
@ -205,8 +207,8 @@ class TestDomainRequest(TestCase):
# can't submit a domain request with a null domain name
domain_request.submit()
@less_console_noise_decorator
def test_status_fsm_submit_succeed(self):
with less_console_noise():
user, _ = User.objects.get_or_create(username="testy")
site = DraftDomain.objects.create(name="igorville.gov")
domain_request = DomainRequest.objects.create(creator=user, requested_domain=site)
@ -218,6 +220,7 @@ class TestDomainRequest(TestCase):
domain_request.submit()
self.assertEqual(domain_request.status, domain_request.DomainRequestStatus.SUBMITTED)
@less_console_noise_decorator
def check_email_sent(
self, domain_request, msg, action, expected_count, expected_content=None, expected_email="mayor@igorville.com"
):
@ -225,7 +228,6 @@ class TestDomainRequest(TestCase):
with self.subTest(msg=msg, action=action):
with boto3_mocking.clients.handler_for("sesv2", self.mock_client):
with less_console_noise():
# Perform the specified action
action_method = getattr(domain_request, action)
action_method()
@ -899,11 +901,11 @@ class TestDomainRequest(TestCase):
with self.assertRaises(TransitionNotAllowed):
self.approved_domain_request.reject_with_prejudice()
@less_console_noise_decorator
def test_approve_from_rejected_clears_rejection_reason(self):
"""When transitioning from rejected to approved on a domain request,
the rejection_reason is cleared."""
with less_console_noise():
# Create a sample domain request
domain_request = completed_domain_request(status=DomainRequest.DomainRequestStatus.REJECTED)
domain_request.rejection_reason = DomainRequest.RejectionReasons.DOMAIN_PURPOSE
@ -915,11 +917,11 @@ class TestDomainRequest(TestCase):
self.assertEqual(domain_request.status, DomainRequest.DomainRequestStatus.APPROVED)
self.assertEqual(domain_request.rejection_reason, None)
@less_console_noise_decorator
def test_in_review_from_rejected_clears_rejection_reason(self):
"""When transitioning from rejected to in_review on a domain request,
the rejection_reason is cleared."""
with less_console_noise():
# Create a sample domain request
domain_request = completed_domain_request(status=DomainRequest.DomainRequestStatus.REJECTED)
domain_request.domain_is_not_active = True
@ -932,11 +934,11 @@ class TestDomainRequest(TestCase):
self.assertEqual(domain_request.status, DomainRequest.DomainRequestStatus.IN_REVIEW)
self.assertEqual(domain_request.rejection_reason, None)
@less_console_noise_decorator
def test_action_needed_from_rejected_clears_rejection_reason(self):
"""When transitioning from rejected to action_needed on a domain request,
the rejection_reason is cleared."""
with less_console_noise():
# Create a sample domain request
domain_request = completed_domain_request(status=DomainRequest.DomainRequestStatus.REJECTED)
domain_request.domain_is_not_active = True
@ -949,27 +951,27 @@ class TestDomainRequest(TestCase):
self.assertEqual(domain_request.status, DomainRequest.DomainRequestStatus.ACTION_NEEDED)
self.assertEqual(domain_request.rejection_reason, None)
@less_console_noise_decorator
def test_has_rationale_returns_true(self):
"""has_rationale() returns true when a domain request has no_other_contacts_rationale"""
with less_console_noise():
self.started_domain_request.no_other_contacts_rationale = "You talkin' to me?"
self.started_domain_request.save()
self.assertEquals(self.started_domain_request.has_rationale(), True)
@less_console_noise_decorator
def test_has_rationale_returns_false(self):
"""has_rationale() returns false when a domain request has no no_other_contacts_rationale"""
with less_console_noise():
self.assertEquals(self.started_domain_request.has_rationale(), False)
@less_console_noise_decorator
def test_has_other_contacts_returns_true(self):
"""has_other_contacts() returns true when a domain request has other_contacts"""
with less_console_noise():
# completed_domain_request has other contacts by default
self.assertEquals(self.started_domain_request.has_other_contacts(), True)
@less_console_noise_decorator
def test_has_other_contacts_returns_false(self):
"""has_other_contacts() returns false when a domain request has no other_contacts"""
with less_console_noise():
domain_request = completed_domain_request(
status=DomainRequest.DomainRequestStatus.STARTED, name="no-others.gov", has_other_contacts=False
)
@ -1072,8 +1074,13 @@ class TestInvitations(TestCase):
self.invitation, _ = DomainInvitation.objects.get_or_create(email=self.email, domain=self.domain)
self.user, _ = User.objects.get_or_create(email=self.email)
def tearDown(self):
super().tearDown()
# clean out the roles each time
UserDomainRole.objects.all().delete()
self.domain.delete()
self.invitation.delete()
User.objects.all().delete()
@less_console_noise_decorator
def test_retrieval_creates_role(self):
@ -1630,17 +1637,16 @@ class TestDomainInformationCustomSave(TestCase):
class TestDomainRequestIncomplete(TestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.factory = RequestFactory()
cls.user = create_test_user()
@less_console_noise_decorator
def setUp(self):
super().setUp()
self.factory = RequestFactory()
username = "test_user"
first_name = "First"
last_name = "Last"
email = "info@example.com"
self.user = get_user_model().objects.create(
username=username, first_name=first_name, last_name=last_name, email=email
)
so, _ = Contact.objects.get_or_create(
first_name="Meowy",
last_name="Meoward",
@ -1698,6 +1704,11 @@ class TestDomainRequestIncomplete(TestCase):
DomainRequest.objects.all().delete()
Contact.objects.all().delete()
@classmethod
def tearDownClass(cls):
super().tearDownClass()
cls.user.delete()
@less_console_noise_decorator
def test_is_federal_complete(self):
self.assertTrue(self.domain_request._is_federal_complete())

View file

@ -8,6 +8,7 @@ from api.tests.common import less_console_noise_decorator
from registrar.models.contact import Contact
from registrar.models.domain import Domain
from registrar.models.draft_domain import DraftDomain
from registrar.models.federal_agency import FederalAgency
from registrar.models.portfolio import Portfolio
from registrar.models.public_contact import PublicContact
from registrar.models.user import User
@ -988,16 +989,17 @@ class PortfoliosTests(TestWithUser, WebTest):
def setUp(self):
super().setUp()
self.user.save()
self.client.force_login(self.user)
self.domain, _ = Domain.objects.get_or_create(name="sampledomain.gov", state=Domain.State.READY)
self.role, _ = UserDomainRole.objects.get_or_create(
user=self.user, domain=self.domain, role=UserDomainRole.Roles.MANAGER
)
self.portfolio, _ = Portfolio.objects.get_or_create(creator=self.user, organization_name="xyz inc")
self.federal_agency = FederalAgency.objects.create()
self.portfolio, _ = Portfolio.objects.get_or_create(creator=self.user, organization_name="xyz inc", federal_agency=self.federal_agency)
def tearDown(self):
Portfolio.objects.all().delete()
self.federal_agency.delete()
super().tearDown()
PublicContact.objects.filter(domain=self.domain).delete()
UserDomainRole.objects.all().delete()