shared mock data for dnssec unit tests

This commit is contained in:
David Kennedy 2023-10-06 12:04:56 -04:00
parent 88bcb1eedc
commit a0f7cd5e1d
No known key found for this signature in database
GPG key ID: 6528A5386E66B96B
2 changed files with 94 additions and 137 deletions

View file

@ -7,7 +7,7 @@ import random
from string import ascii_uppercase from string import ascii_uppercase
from django.test import TestCase from django.test import TestCase
from unittest.mock import MagicMock, Mock, patch from unittest.mock import MagicMock, Mock, patch
from typing import List, Dict from typing import List, Dict, Mapping, Any
from django.conf import settings from django.conf import settings
from django.contrib.auth import get_user_model, login from django.contrib.auth import get_user_model, login
@ -26,6 +26,7 @@ from registrar.models import (
from epplibwrapper import ( from epplibwrapper import (
commands, commands,
common, common,
extensions,
RegistryError, RegistryError,
ErrorCode, ErrorCode,
) )
@ -584,6 +585,37 @@ class MockEppLib(TestCase):
mockDataInfoHosts = fakedEppObject( mockDataInfoHosts = fakedEppObject(
"lastPw", cr_date=datetime.datetime(2023, 8, 25, 19, 45, 35) "lastPw", cr_date=datetime.datetime(2023, 8, 25, 19, 45, 35)
) )
addDsData1 = {
"keyTag": 1234,
"alg": 3,
"digestType": 1,
"digest": "ec0bdd990b39feead889f0ba613db4adec0bdd99",
}
addDsData2 = {
"keyTag": 2345,
"alg": 3,
"digestType": 1,
"digest": "ec0bdd990b39feead889f0ba613db4adecb4adec",
}
keyDataDict = {
"flags": 257,
"protocol": 3,
"alg": 1,
"pubKey": "AQPJ////4Q==",
}
dnssecExtensionWithDsData: Mapping[str, Any] = {
"dsData": [common.DSData(**addDsData1)]
}
dnssecExtensionWithMultDsData: Mapping[str, Any] = {
"dsData": [
common.DSData(**addDsData1),
common.DSData(**addDsData2),
],
}
dnssecExtensionWithKeyData: Mapping[str, Any] = {
"maxSigLife": 3215,
"keyData": [common.DNSSECKeyData(**keyDataDict)],
}
def mockSend(self, _request, cleaned): def mockSend(self, _request, cleaned):
"""Mocks the registry.send function used inside of domain.py """Mocks the registry.send function used inside of domain.py
@ -593,6 +625,30 @@ class MockEppLib(TestCase):
if isinstance(_request, commands.InfoDomain): if isinstance(_request, commands.InfoDomain):
if getattr(_request, "name", None) == "security.gov": if getattr(_request, "name", None) == "security.gov":
return MagicMock(res_data=[self.infoDomainNoContact]) return MagicMock(res_data=[self.infoDomainNoContact])
elif getattr(_request, "name", None) == "dnssec-dsdata.gov":
return MagicMock(
res_data=[self.mockDataInfoDomain],
extensions=[
extensions.DNSSECExtension(**self.dnssecExtensionWithDsData)
],
)
elif getattr(_request, "name", None) == "dnssec-multdsdata.gov":
return MagicMock(
res_data=[self.mockDataInfoDomain],
extensions=[
extensions.DNSSECExtension(**self.dnssecExtensionWithMultDsData)
],
)
elif getattr(_request, "name", None) == "dnssec-keydata.gov":
return MagicMock(
res_data=[self.mockDataInfoDomain],
extensions=[
extensions.DNSSECExtension(**self.dnssecExtensionWithKeyData)
],
)
elif getattr(_request, "name", None) == "dnssec-none.gov":
# this case is not necessary, but helps improve readability
return MagicMock(res_data=[self.mockDataInfoDomain])
return MagicMock(res_data=[self.mockDataInfoDomain]) return MagicMock(res_data=[self.mockDataInfoDomain])
elif isinstance(_request, commands.InfoContact): elif isinstance(_request, commands.InfoContact):
return MagicMock(res_data=[self.mockDataInfoContact]) return MagicMock(res_data=[self.mockDataInfoContact])
@ -614,6 +670,11 @@ class MockEppLib(TestCase):
raise RegistryError( raise RegistryError(
code=ErrorCode.OBJECT_ASSOCIATION_PROHIBITS_OPERATION code=ErrorCode.OBJECT_ASSOCIATION_PROHIBITS_OPERATION
) )
elif (
isinstance(_request, commands.UpdateDomain)
and getattr(_request, "name", None) == "dnssec-invalid.gov"
):
raise RegistryError(code=ErrorCode.PARAMETER_VALUE_RANGE_ERROR)
return MagicMock(res_data=[self.mockDataInfoHosts]) return MagicMock(res_data=[self.mockDataInfoHosts])
def setUp(self): def setUp(self):

View file

@ -3,7 +3,6 @@ Feature being tested: Registry Integration
This file tests the various ways in which the registrar interacts with the registry. This file tests the various ways in which the registrar interacts with the registry.
""" """
from typing import Mapping, Any
from django.test import TestCase from django.test import TestCase
from django.db.utils import IntegrityError from django.db.utils import IntegrityError
from unittest.mock import MagicMock, patch, call from unittest.mock import MagicMock, patch, call
@ -803,37 +802,7 @@ class TestRegistrantDNSSEC(MockEppLib):
super().setUp() super().setUp()
# for the tests, need a domain in the unknown state # for the tests, need a domain in the unknown state
self.domain, _ = Domain.objects.get_or_create(name="fake.gov") self.domain, _ = Domain.objects.get_or_create(name="fake.gov")
self.addDsData1 = {
"keyTag": 1234,
"alg": 3,
"digestType": 1,
"digest": "ec0bdd990b39feead889f0ba613db4adec0bdd99",
}
self.addDsData2 = {
"keyTag": 2345,
"alg": 3,
"digestType": 1,
"digest": "ec0bdd990b39feead889f0ba613db4adecb4adec",
}
self.keyDataDict = {
"flags": 257,
"protocol": 3,
"alg": 1,
"pubKey": "AQPJ////4Q==",
}
self.dnssecExtensionWithDsData: Mapping[str, Any] = {
"dsData": [common.DSData(**self.addDsData1)]
}
self.dnssecExtensionWithMultDsData: Mapping[str, Any] = {
"dsData": [
common.DSData(**self.addDsData1),
common.DSData(**self.addDsData2),
],
}
self.dnssecExtensionWithKeyData: Mapping[str, Any] = {
"maxSigLife": 3215,
"keyData": [common.DNSSECKeyData(**self.keyDataDict)],
}
def tearDown(self): def tearDown(self):
Domain.objects.all().delete() Domain.objects.all().delete()
@ -852,26 +821,13 @@ class TestRegistrantDNSSEC(MockEppLib):
""" """
# make sure to stop any other patcher so there are no conflicts domain, _ = Domain.objects.get_or_create(name="dnssec-dsdata.gov")
self.mockSendPatch.stop()
def side_effect(_request, cleaned): domain.dnssecdata = self.dnssecExtensionWithDsData
return MagicMock(
res_data=[self.mockDataInfoDomain],
extensions=[
extensions.DNSSECExtension(**self.dnssecExtensionWithDsData)
],
)
patcher = patch("registrar.models.domain.registry.send")
mocked_send = patcher.start()
mocked_send.side_effect = side_effect
self.domain.dnssecdata = self.dnssecExtensionWithDsData
# get the DNS SEC extension added to the UpdateDomain command and # get the DNS SEC extension added to the UpdateDomain command and
# verify that it is properly sent # verify that it is properly sent
# args[0] is the _request sent to registry # args[0] is the _request sent to registry
args, _ = mocked_send.call_args args, _ = self.mockedSendFunction.call_args
# assert that the extension matches # assert that the extension matches
self.assertEquals( self.assertEquals(
args[0].extensions[0], args[0].extensions[0],
@ -880,12 +836,12 @@ class TestRegistrantDNSSEC(MockEppLib):
), ),
) )
# test that the dnssecdata getter is functioning properly # test that the dnssecdata getter is functioning properly
dnssecdata_get = self.domain.dnssecdata dnssecdata_get = domain.dnssecdata
mocked_send.assert_has_calls( self.mockedSendFunction.assert_has_calls(
[ [
call( call(
commands.UpdateDomain( commands.UpdateDomain(
name="fake.gov", name="dnssec-dsdata.gov",
nsset=None, nsset=None,
keyset=None, keyset=None,
registrant=None, registrant=None,
@ -895,7 +851,7 @@ class TestRegistrantDNSSEC(MockEppLib):
), ),
call( call(
commands.InfoDomain( commands.InfoDomain(
name="fake.gov", name="dnssec-dsdata.gov",
), ),
cleaned=True, cleaned=True,
), ),
@ -906,8 +862,6 @@ class TestRegistrantDNSSEC(MockEppLib):
dnssecdata_get.dsData, self.dnssecExtensionWithDsData["dsData"] dnssecdata_get.dsData, self.dnssecExtensionWithDsData["dsData"]
) )
patcher.stop()
def test_dnssec_is_idempotent(self): def test_dnssec_is_idempotent(self):
""" """
Scenario: Registrant adds DNS data twice, due to a UI glitch Scenario: Registrant adds DNS data twice, due to a UI glitch
@ -923,32 +877,19 @@ class TestRegistrantDNSSEC(MockEppLib):
""" """
# make sure to stop any other patcher so there are no conflicts domain, _ = Domain.objects.get_or_create(name="dnssec-dsdata.gov")
self.mockSendPatch.stop()
def side_effect(_request, cleaned):
return MagicMock(
res_data=[self.mockDataInfoDomain],
extensions=[
extensions.DNSSECExtension(**self.dnssecExtensionWithDsData)
],
)
patcher = patch("registrar.models.domain.registry.send")
mocked_send = patcher.start()
mocked_send.side_effect = side_effect
# set the dnssecdata once # set the dnssecdata once
self.domain.dnssecdata = self.dnssecExtensionWithDsData domain.dnssecdata = self.dnssecExtensionWithDsData
# set the dnssecdata again # set the dnssecdata again
self.domain.dnssecdata = self.dnssecExtensionWithDsData domain.dnssecdata = self.dnssecExtensionWithDsData
# test that the dnssecdata getter is functioning properly # test that the dnssecdata getter is functioning properly
dnssecdata_get = self.domain.dnssecdata dnssecdata_get = domain.dnssecdata
mocked_send.assert_has_calls( self.mockedSendFunction.assert_has_calls(
[ [
call( call(
commands.UpdateDomain( commands.UpdateDomain(
name="fake.gov", name="dnssec-dsdata.gov",
nsset=None, nsset=None,
keyset=None, keyset=None,
registrant=None, registrant=None,
@ -958,7 +899,7 @@ class TestRegistrantDNSSEC(MockEppLib):
), ),
call( call(
commands.UpdateDomain( commands.UpdateDomain(
name="fake.gov", name="dnssec-dsdata.gov",
nsset=None, nsset=None,
keyset=None, keyset=None,
registrant=None, registrant=None,
@ -968,7 +909,7 @@ class TestRegistrantDNSSEC(MockEppLib):
), ),
call( call(
commands.InfoDomain( commands.InfoDomain(
name="fake.gov", name="dnssec-dsdata.gov",
), ),
cleaned=True, cleaned=True,
), ),
@ -979,8 +920,6 @@ class TestRegistrantDNSSEC(MockEppLib):
dnssecdata_get.dsData, self.dnssecExtensionWithDsData["dsData"] dnssecdata_get.dsData, self.dnssecExtensionWithDsData["dsData"]
) )
patcher.stop()
def test_user_adds_dnssec_data_multiple_dsdata(self): def test_user_adds_dnssec_data_multiple_dsdata(self):
""" """
Scenario: Registrant adds DNSSEC data with multiple DSData. Scenario: Registrant adds DNSSEC data with multiple DSData.
@ -994,26 +933,13 @@ class TestRegistrantDNSSEC(MockEppLib):
""" """
# make sure to stop any other patcher so there are no conflicts domain, _ = Domain.objects.get_or_create(name="dnssec-multdsdata.gov")
self.mockSendPatch.stop()
def side_effect(_request, cleaned): domain.dnssecdata = self.dnssecExtensionWithMultDsData
return MagicMock(
res_data=[self.mockDataInfoDomain],
extensions=[
extensions.DNSSECExtension(**self.dnssecExtensionWithMultDsData)
],
)
patcher = patch("registrar.models.domain.registry.send")
mocked_send = patcher.start()
mocked_send.side_effect = side_effect
self.domain.dnssecdata = self.dnssecExtensionWithMultDsData
# get the DNS SEC extension added to the UpdateDomain command # get the DNS SEC extension added to the UpdateDomain command
# and verify that it is properly sent # and verify that it is properly sent
# args[0] is the _request sent to registry # args[0] is the _request sent to registry
args, _ = mocked_send.call_args args, _ = self.mockedSendFunction.call_args
# assert that the extension matches # assert that the extension matches
self.assertEquals( self.assertEquals(
args[0].extensions[0], args[0].extensions[0],
@ -1022,12 +948,12 @@ class TestRegistrantDNSSEC(MockEppLib):
), ),
) )
# test that the dnssecdata getter is functioning properly # test that the dnssecdata getter is functioning properly
dnssecdata_get = self.domain.dnssecdata dnssecdata_get = domain.dnssecdata
mocked_send.assert_has_calls( self.mockedSendFunction.assert_has_calls(
[ [
call( call(
commands.UpdateDomain( commands.UpdateDomain(
name="fake.gov", name="dnssec-multdsdata.gov",
nsset=None, nsset=None,
keyset=None, keyset=None,
registrant=None, registrant=None,
@ -1037,7 +963,7 @@ class TestRegistrantDNSSEC(MockEppLib):
), ),
call( call(
commands.InfoDomain( commands.InfoDomain(
name="fake.gov", name="dnssec-multdsdata.gov",
), ),
cleaned=True, cleaned=True,
), ),
@ -1048,8 +974,6 @@ class TestRegistrantDNSSEC(MockEppLib):
dnssecdata_get.dsData, self.dnssecExtensionWithMultDsData["dsData"] dnssecdata_get.dsData, self.dnssecExtensionWithMultDsData["dsData"]
) )
patcher.stop()
def test_user_adds_dnssec_keydata(self): def test_user_adds_dnssec_keydata(self):
""" """
Scenario: Registrant adds DNSSEC data. Scenario: Registrant adds DNSSEC data.
@ -1063,26 +987,13 @@ class TestRegistrantDNSSEC(MockEppLib):
""" """
# make sure to stop any other patcher so there are no conflicts domain, _ = Domain.objects.get_or_create(name="dnssec-keydata.gov")
self.mockSendPatch.stop()
def side_effect(_request, cleaned): domain.dnssecdata = self.dnssecExtensionWithKeyData
return MagicMock(
res_data=[self.mockDataInfoDomain],
extensions=[
extensions.DNSSECExtension(**self.dnssecExtensionWithKeyData)
],
)
patcher = patch("registrar.models.domain.registry.send")
mocked_send = patcher.start()
mocked_send.side_effect = side_effect
self.domain.dnssecdata = self.dnssecExtensionWithKeyData
# get the DNS SEC extension added to the UpdateDomain command # get the DNS SEC extension added to the UpdateDomain command
# and verify that it is properly sent # and verify that it is properly sent
# args[0] is the _request sent to registry # args[0] is the _request sent to registry
args, _ = mocked_send.call_args args, _ = self.mockedSendFunction.call_args
# assert that the extension matches # assert that the extension matches
self.assertEquals( self.assertEquals(
args[0].extensions[0], args[0].extensions[0],
@ -1091,12 +1002,12 @@ class TestRegistrantDNSSEC(MockEppLib):
), ),
) )
# test that the dnssecdata getter is functioning properly # test that the dnssecdata getter is functioning properly
dnssecdata_get = self.domain.dnssecdata dnssecdata_get = domain.dnssecdata
mocked_send.assert_has_calls( self.mockedSendFunction.assert_has_calls(
[ [
call( call(
commands.UpdateDomain( commands.UpdateDomain(
name="fake.gov", name="dnssec-keydata.gov",
nsset=None, nsset=None,
keyset=None, keyset=None,
registrant=None, registrant=None,
@ -1106,7 +1017,7 @@ class TestRegistrantDNSSEC(MockEppLib):
), ),
call( call(
commands.InfoDomain( commands.InfoDomain(
name="fake.gov", name="dnssec-keydata.gov",
), ),
cleaned=True, cleaned=True,
), ),
@ -1117,8 +1028,6 @@ class TestRegistrantDNSSEC(MockEppLib):
dnssecdata_get.keyData, self.dnssecExtensionWithKeyData["keyData"] dnssecdata_get.keyData, self.dnssecExtensionWithKeyData["keyData"]
) )
patcher.stop()
def test_update_is_unsuccessful(self): def test_update_is_unsuccessful(self):
""" """
Scenario: An update to the dns data is unsuccessful Scenario: An update to the dns data is unsuccessful
@ -1126,27 +1035,14 @@ class TestRegistrantDNSSEC(MockEppLib):
Then a user-friendly error message is returned for displaying on the web Then a user-friendly error message is returned for displaying on the web
""" """
# make sure to stop any other patcher so there are no conflicts domain, _ = Domain.objects.get_or_create(name="dnssec-invalid.gov")
self.mockSendPatch.stop()
def side_effect(_request, cleaned):
raise RegistryError(code=ErrorCode.PARAMETER_VALUE_RANGE_ERROR)
patcher = patch("registrar.models.domain.registry.send")
mocked_send = patcher.start()
mocked_send.side_effect = side_effect
# if RegistryError is raised, view formats user-friendly
# error message if error is_client_error, is_session_error, or
# is_server_error; so test for those conditions
with self.assertRaises(RegistryError) as err: with self.assertRaises(RegistryError) as err:
self.domain.dnssecdata = self.dnssecExtensionWithDsData domain.dnssecdata = self.dnssecExtensionWithDsData
self.assertTrue( self.assertTrue(
err.is_client_error() or err.is_session_error() or err.is_server_error() err.is_client_error() or err.is_session_error() or err.is_server_error()
) )
patcher.stop()
class TestAnalystClientHold(MockEppLib): class TestAnalystClientHold(MockEppLib):
"""Rule: Analysts may suspend or restore a domain by using client hold""" """Rule: Analysts may suspend or restore a domain by using client hold"""