fixed tests and linter

This commit is contained in:
Alysia Broddrick 2023-09-12 21:48:27 -07:00
parent 4292c38b47
commit 29b8c72911
No known key found for this signature in database
GPG key ID: 03917052CD0F06B7
8 changed files with 168 additions and 154 deletions

View file

@ -274,7 +274,7 @@ class DomainAdmin(ListHeaderAdmin):
def do_remove_client_hold(self, request, obj):
try:
obj.revertClientHold()
obj.revert_client_hold()
obj.save()
except Exception as err:
self.message_user(request, err, messages.ERROR)

View file

@ -96,7 +96,7 @@ class Domain(TimeStampedModel, DomainHelper):
# for human review or third-party action. A transform command that
# is processed, but whose requested action is pending, is noted with
# response code 1001.
DNS_NEEDED = "pendingCreate"
PENDING_CREATE = "pendingCreate"
PENDING_DELETE = "pendingDelete"
PENDING_RENEW = "pendingRenew"
PENDING_TRANSFER = "pendingTransfer"
@ -230,6 +230,7 @@ class Domain(TimeStampedModel, DomainHelper):
hosts = self._get_property("hosts")
except Exception as err:
# Don't throw error as this is normal for a new domain
# TODO - 433 error handling ticket should address this
logger.info("Domain is missing nameservers %s" % err)
return []
@ -411,7 +412,8 @@ class Domain(TimeStampedModel, DomainHelper):
# TODO - ticket 433 human readable error handling here
def _update_domain_with_contact(self, contact: PublicContact, rem=False):
"""adds or removes a contact from a domain"""
"""adds or removes a contact from a domain
rem being true indicates the contact will be removed from registry"""
logger.info(
"_update_domain_with_contact() received type %s " % contact.contact_type
)
@ -468,8 +470,7 @@ class Domain(TimeStampedModel, DomainHelper):
return self.get_default_security_contact()
def _add_registrant_to_existing_domain(self, contact: PublicContact):
self._update_epp_contact(contact=contact)
"""Used to change the registrant contact on an existing domain"""
updateDomain = commands.UpdateDomain(
name=self.name, registrant=contact.registry_id
)
@ -489,6 +490,7 @@ class Domain(TimeStampedModel, DomainHelper):
does not create the PublicContact object, this should be made beforehand
(call save() on a public contact to trigger the contact setters
which inturn call this function)
Will throw error if contact type is not the same as expectType
Raises ValueError if expected type doesn't match the contact type"""
if expectedType != contact.contact_type:
raise ValueError(
@ -533,11 +535,7 @@ class Domain(TimeStampedModel, DomainHelper):
logger.info(
"_set_singleton_contact()-> updating domain, removing old contact"
)
if isEmptySecurity:
existing_contact = PublicContact.objects.filter(
domain=self, contact_type=contact.contact_type
).get()
else:
existing_contact = (
PublicContact.objects.exclude(registry_id=contact.registry_id)
.filter(domain=self, contact_type=contact.contact_type)
@ -552,9 +550,6 @@ class Domain(TimeStampedModel, DomainHelper):
try:
self._update_domain_with_contact(contact=existing_contact, rem=True)
existing_contact.delete()
if isEmptySecurity:
# security is empty so set the default security object again
self.get_default_security_contact().save()
except Exception as err:
logger.error(
"Raising error after removing and adding a new contact"
@ -646,13 +641,13 @@ class Domain(TimeStampedModel, DomainHelper):
"""This domain should not be active.
may raises RegistryError, should be caught or handled correctly by caller"""
request = commands.UpdateDomain(name=self.name, add=[self.clientHoldStatus()])
registry.send(request)
registry.send(request, cleaned=True)
def _remove_client_hold(self):
"""This domain is okay to be active.
may raises RegistryError, should be caught or handled correctly by caller"""
request = commands.UpdateDomain(name=self.name, rem=[self.clientHoldStatus()])
registry.send(request)
registry.send(request, cleaned=True)
def _delete_domain(self):
"""This domain should be deleted from the registry
@ -790,7 +785,7 @@ class Domain(TimeStampedModel, DomainHelper):
administrative_contact.domain = self
administrative_contact.save()
@transition(field="state", source=State.DNS_NEEDED, target=State.ON_HOLD)
@transition(field="state", source=State.READY, target=State.ON_HOLD)
def place_client_hold(self):
"""place a clienthold on a domain (no longer should resolve)"""
# TODO - ensure all requirements for client hold are made here
@ -799,8 +794,8 @@ class Domain(TimeStampedModel, DomainHelper):
self._place_client_hold()
# TODO -on the client hold ticket any additional error handling here
@transition(field="state", source=State.ON_HOLD, target=State.DNS_NEEDED)
def revertClientHold(self):
@transition(field="state", source=State.ON_HOLD, target=State.READY)
def revert_client_hold(self):
"""undo a clienthold placed on a domain"""
logger.info("clientHold()-> inside clientHold")
@ -830,6 +825,8 @@ class Domain(TimeStampedModel, DomainHelper):
"""
# TODO - in nameservers tickets 848 and 562
# check here if updates need to be made
# consider adding these checks as constraints
# within the transistion itself
nameserverList = self.nameservers
logger.info("Changing to ready state")
if len(nameserverList) < 2 or len(nameserverList) > 13:
@ -995,7 +992,7 @@ class Domain(TimeStampedModel, DomainHelper):
# extract properties from response
# (Ellipsis is used to mean "null")
##convert this to use PublicContactInstead
# convert this to use PublicContactInstead
contact = {
"id": domainContact.contact,
"type": domainContact.type,

View file

@ -36,7 +36,6 @@ class PublicContact(TimeStampedModel):
case PublicContact.ContactTypeChoices.ADMINISTRATIVE:
self.domain.administrative_contact = self
case PublicContact.ContactTypeChoices.TECHNICAL:
print("in technical of the public contact class")
self.domain.technical_contact = self
case PublicContact.ContactTypeChoices.SECURITY:
self.domain.security_contact = self

View file

@ -10,7 +10,7 @@
<div class="submit-row">
{% if original.state == original.State.READY %}
<input type="submit" value="Place hold" name="_place_client_hold">
{% elif original.state == original.State.ONHOLD %}
{% elif original.state == original.State.ON_HOLD %}
<input type="submit" value="Remove hold" name="_remove_client_hold">
{% endif %}
<input id="manageDomainSubmitButton" type="submit" value="Manage Domain" name="_edit_domain">

View file

@ -1,10 +1,12 @@
import datetime
import os
import logging
from contextlib import contextmanager
import random
from string import ascii_uppercase
from unittest.mock import Mock
from django.test import TestCase
from unittest.mock import MagicMock, Mock, patch
from typing import List, Dict
from django.conf import settings
@ -18,8 +20,15 @@ from registrar.models import (
DomainInvitation,
User,
DomainInformation,
PublicContact,
Domain,
)
from epplibwrapper import (
commands,
common,
RegistryError,
ErrorCode,
)
logger = logging.getLogger(__name__)
@ -532,3 +541,121 @@ def generic_domain_object(domain_type, object_name):
mock = AuditedAdminMockData()
application = mock.create_full_dummy_domain_object(domain_type, object_name)
return application
class MockEppLib(TestCase):
class fakedEppObject(object):
""""""
def __init__(self, auth_info=..., cr_date=..., contacts=..., hosts=...):
self.auth_info = auth_info
self.cr_date = cr_date
self.contacts = contacts
self.hosts = hosts
mockDataInfoDomain = fakedEppObject(
"fakepw",
cr_date=datetime.datetime(2023, 5, 25, 19, 45, 35),
contacts=[common.DomainContact(contact="123", type="security")],
hosts=["fake.host.com"],
)
infoDomainNoContact = fakedEppObject(
"security",
cr_date=datetime.datetime(2023, 5, 25, 19, 45, 35),
contacts=[],
hosts=["fake.host.com"],
)
mockDataInfoContact = fakedEppObject(
"anotherPw", cr_date=datetime.datetime(2023, 7, 25, 19, 45, 35)
)
mockDataInfoHosts = fakedEppObject(
"lastPw", cr_date=datetime.datetime(2023, 8, 25, 19, 45, 35)
)
def mockSend(self, _request, cleaned):
"""Mocks the registry.send function used inside of domain.py
registry is imported from epplibwrapper
returns objects that simulate what would be in a epp response
but only relevant pieces for tests"""
if isinstance(_request, commands.InfoDomain):
if getattr(_request, "name", None) == "security.gov":
return MagicMock(res_data=[self.infoDomainNoContact])
return MagicMock(res_data=[self.mockDataInfoDomain])
elif isinstance(_request, commands.InfoContact):
return MagicMock(res_data=[self.mockDataInfoContact])
elif (
isinstance(_request, commands.CreateContact)
and getattr(_request, "id", None) == "fail"
and self.mockedSendFunction.call_count == 3
):
# use this for when a contact is being updated
# sets the second send() to fail
raise RegistryError(code=ErrorCode.OBJECT_EXISTS)
return MagicMock(res_data=[self.mockDataInfoHosts])
def setUp(self):
"""mock epp send function as this will fail locally"""
self.mockSendPatch = patch("registrar.models.domain.registry.send")
self.mockedSendFunction = self.mockSendPatch.start()
self.mockedSendFunction.side_effect = self.mockSend
def _convertPublicContactToEpp(
self, contact: PublicContact, disclose_email=False, createContact=True
):
DF = common.DiscloseField
fields = {DF.FAX, DF.VOICE, DF.ADDR}
if not disclose_email:
fields.add(DF.EMAIL)
di = common.Disclose(
flag=False,
fields=fields,
types={DF.ADDR: "loc"},
)
# check docs here looks like we may have more than one address field but
addr = common.ContactAddr(
[
getattr(contact, street)
for street in ["street1", "street2", "street3"]
if hasattr(contact, street)
], # type: ignore
city=contact.city,
pc=contact.pc,
cc=contact.cc,
sp=contact.sp,
) # type: ignore
pi = common.PostalInfo(
name=contact.name,
addr=addr,
org=contact.org,
type="loc",
)
ai = common.ContactAuthInfo(pw="2fooBAR123fooBaz")
if createContact:
return commands.CreateContact(
id=contact.registry_id,
postal_info=pi, # type: ignore
email=contact.email,
voice=contact.voice,
fax=contact.fax,
auth_info=ai,
disclose=di,
vat=None,
ident=None,
notify_email=None,
) # type: ignore
else:
return commands.UpdateContact(
id=contact.registry_id,
postal_info=pi,
email=contact.email,
voice=contact.voice,
fax=contact.fax,
)
def tearDown(self):
self.mockSendPatch.stop()

View file

@ -24,6 +24,7 @@ from .common import (
create_user,
create_ready_domain,
multiple_unalphabetical_domain_objects,
MockEppLib,
)
from django.contrib.sessions.backends.db import SessionStore
from django.contrib.auth import get_user_model
@ -38,17 +39,17 @@ import logging
logger = logging.getLogger(__name__)
class TestDomainAdmin(TestCase):
class TestDomainAdmin(MockEppLib):
def setUp(self):
self.site = AdminSite()
self.admin = DomainAdmin(model=Domain, admin_site=self.site)
self.client = Client(HTTP_HOST="localhost:8080")
self.superuser = create_superuser()
self.staffuser = create_user()
super().setUp()
def test_place_and_remove_hold(self):
domain = create_ready_domain()
# get admin page and assert Place Hold button
p = "userpass"
self.client.login(username="staffuser", password=p)
@ -88,8 +89,11 @@ class TestDomainAdmin(TestCase):
raise
def tearDown(self):
Domain.objects.all().delete()
# DomainInformation.objects.all().delete()
# DomainApplication.objects.all().delete()
# Domain.objects.all().delete()
User.objects.all().delete()
super().tearDown()
class TestDomainApplicationAdmin(TestCase):

View file

@ -5,135 +5,22 @@ This file tests the various ways in which the registrar interacts with the regis
"""
from django.test import TestCase
from django.db.utils import IntegrityError
from unittest.mock import patch, MagicMock, call
from unittest.mock import patch, call
import datetime
from registrar.models import Domain # add in DomainApplication, User,
from registrar.models import Domain
from unittest import skip
from epplibwrapper import commands, common, RegistryError, ErrorCode
from registrar.models.domain_application import DomainApplication
from registrar.models.domain_information import DomainInformation
from registrar.models.draft_domain import DraftDomain
from registrar.models.public_contact import PublicContact
from registrar.models.user import User
from .common import MockEppLib
class MockEppLib(TestCase):
class fakedEppObject(object):
""""""
def __init__(self, auth_info=..., cr_date=..., contacts=..., hosts=...):
self.auth_info = auth_info
self.cr_date = cr_date
self.contacts = contacts
self.hosts = hosts
mockDataInfoDomain = fakedEppObject(
"fakepw",
cr_date=datetime.datetime(2023, 5, 25, 19, 45, 35),
contacts=[common.DomainContact(contact="123", type="security")],
hosts=["fake.host.com"],
)
infoDomainNoContact = fakedEppObject(
"security",
cr_date=datetime.datetime(2023, 5, 25, 19, 45, 35),
contacts=[],
hosts=["fake.host.com"],
)
mockDataInfoContact = fakedEppObject(
"anotherPw", cr_date=datetime.datetime(2023, 7, 25, 19, 45, 35)
)
mockDataInfoHosts = fakedEppObject(
"lastPw", cr_date=datetime.datetime(2023, 8, 25, 19, 45, 35)
)
def mockSend(self, _request, cleaned):
"""Mocks the registry.send function used inside of domain.py
registry is imported from epplibwrapper
returns objects that simulate what would be in a epp response
but only relevant pieces for tests"""
if isinstance(_request, commands.InfoDomain):
if getattr(_request, "name", None) == "security.gov":
return MagicMock(res_data=[self.infoDomainNoContact])
return MagicMock(res_data=[self.mockDataInfoDomain])
elif isinstance(_request, commands.InfoContact):
return MagicMock(res_data=[self.mockDataInfoContact])
elif (
isinstance(_request, commands.CreateContact)
and getattr(_request, "id", None) == "fail"
and self.mockedSendFunction.call_count == 3
):
# use this for when a contact is being updated
# sets the second send() to fail
raise RegistryError(code=ErrorCode.OBJECT_EXISTS)
return MagicMock(res_data=[self.mockDataInfoHosts])
def setUp(self):
"""mock epp send function as this will fail locally"""
self.mockSendPatch = patch("registrar.models.domain.registry.send")
self.mockedSendFunction = self.mockSendPatch.start()
self.mockedSendFunction.side_effect = self.mockSend
def _convertPublicContactToEpp(
self, contact: PublicContact, disclose_email=False, createContact=True
):
DF = common.DiscloseField
fields = {DF.FAX, DF.VOICE, DF.ADDR}
if not disclose_email:
fields.add(DF.EMAIL)
di = common.Disclose(
flag=False,
fields=fields,
types={DF.ADDR: "loc"},
)
# check docs here looks like we may have more than one address field but
addr = common.ContactAddr(
[
getattr(contact, street)
for street in ["street1", "street2", "street3"]
if hasattr(contact, street)
], # type: ignore
city=contact.city,
pc=contact.pc,
cc=contact.cc,
sp=contact.sp,
) # type: ignore
pi = common.PostalInfo(
name=contact.name,
addr=addr,
org=contact.org,
type="loc",
)
ai = common.ContactAuthInfo(pw="2fooBAR123fooBaz")
if createContact:
return commands.CreateContact(
id=contact.registry_id,
postal_info=pi, # type: ignore
email=contact.email,
voice=contact.voice,
fax=contact.fax,
auth_info=ai,
disclose=di,
vat=None,
ident=None,
notify_email=None,
) # type: ignore
else:
return commands.UpdateContact(
id=contact.registry_id,
postal_info=pi,
email=contact.email,
voice=contact.voice,
fax=contact.fax,
)
def tearDown(self):
self.mockSendPatch.stop()
from epplibwrapper import (
commands,
common,
)
class TestDomainCache(MockEppLib):
@ -264,20 +151,21 @@ class TestDomainCreation(TestCase):
"""
raise
@skip("assertion broken with mock addition")
def test_empty_domain_creation(self):
"""Can't create a completely empty domain."""
with self.assertRaises(IntegrityError):
with self.assertRaisesRegex(IntegrityError, "name"):
Domain.objects.create()
def test_minimal_creation(self):
"""Can create with just a name."""
Domain.objects.create(name="igorville.gov")
@skip("assertion broken with mock addition")
def test_duplicate_creation(self):
"""Can't create domain if name is not unique."""
Domain.objects.create(name="igorville.gov")
with self.assertRaises(IntegrityError):
with self.assertRaisesRegex(IntegrityError, "name"):
Domain.objects.create(name="igorville.gov")
@skip("cannot activate a domain without mock registry")

View file

@ -1132,7 +1132,6 @@ class TestDomainDetail(TestWithDomainPermissions, WebTest):
self.app.set_user(self.user.username)
self.client.force_login(self.user)
##here
def test_domain_detail_link_works(self):
home_page = self.app.get("/")
self.assertContains(home_page, "igorville.gov")