mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-05-17 18:09:25 +02:00
Update availability API to use EPP availability check
This commit is contained in:
parent
1904c7e461
commit
3ed4c0e4fb
5 changed files with 108 additions and 25 deletions
1
src/!
Normal file
1
src/!
Normal file
|
@ -0,0 +1 @@
|
|||
null
|
|
@ -7,21 +7,34 @@ from django.test import TestCase, RequestFactory
|
|||
|
||||
from ..views import available, _domains, in_domains
|
||||
from .common import less_console_noise
|
||||
from registrar.tests.common import MockEppLib
|
||||
from unittest.mock import MagicMock, patch, call
|
||||
|
||||
from epplibwrapper import (
|
||||
commands,
|
||||
common,
|
||||
extensions,
|
||||
responses,
|
||||
RegistryError,
|
||||
ErrorCode,
|
||||
)
|
||||
|
||||
API_BASE_PATH = "/api/v1/available/"
|
||||
from registrar.models import Domain
|
||||
|
||||
|
||||
class AvailableViewTest(TestCase):
|
||||
class AvailableViewTest(MockEppLib):
|
||||
|
||||
"""Test that the view function works as expected."""
|
||||
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
self.user = get_user_model().objects.create(username="username")
|
||||
self.factory = RequestFactory()
|
||||
|
||||
def test_view_function(self):
|
||||
request = self.factory.get(API_BASE_PATH + "test.gov")
|
||||
request.user = self.user
|
||||
|
||||
response = available(request, domain="test.gov")
|
||||
# has the right text in it
|
||||
self.assertContains(response, "available")
|
||||
|
@ -29,28 +42,43 @@ class AvailableViewTest(TestCase):
|
|||
response_object = json.loads(response.content)
|
||||
self.assertIn("available", response_object)
|
||||
|
||||
def test_domain_list(self):
|
||||
"""Test the domain list that is returned from Github.
|
||||
def test_makes_calls(self):
|
||||
gsa_available = in_domains("gsa.gov")
|
||||
igorville_available = in_domains("igorvilleremixed.gov")
|
||||
|
||||
This does not mock out the external file, it is actually fetched from
|
||||
the internet.
|
||||
"""
|
||||
domains = _domains()
|
||||
self.assertIn("gsa.gov", domains)
|
||||
# entries are all lowercase so GSA.GOV is not in the set
|
||||
self.assertNotIn("GSA.GOV", domains)
|
||||
self.assertNotIn("igorvilleremixed.gov", domains)
|
||||
# all the entries have dots
|
||||
self.assertNotIn("gsa", domains)
|
||||
self.mockedSendFunction.assert_has_calls(
|
||||
[
|
||||
call(
|
||||
commands.CheckDomain(
|
||||
["gsa.gov"],
|
||||
),
|
||||
cleaned=True,
|
||||
),
|
||||
call(
|
||||
commands.CheckDomain(
|
||||
["igorvilleremixed.gov"],
|
||||
),
|
||||
cleaned=True,
|
||||
)
|
||||
]
|
||||
)
|
||||
|
||||
def test_in_domains(self):
|
||||
self.assertTrue(in_domains("gsa.gov"))
|
||||
gsa_available = in_domains("gsa.gov")
|
||||
gsa_caps_available = in_domains("GSA.gov")
|
||||
igorville_available = in_domains("igorvilleremixed.gov")
|
||||
|
||||
self.assertTrue(gsa_available)
|
||||
# input is lowercased so GSA.GOV should be found
|
||||
self.assertTrue(in_domains("GSA.GOV"))
|
||||
self.assertTrue(gsa_caps_available)
|
||||
# This domain should not have been registered
|
||||
self.assertFalse(in_domains("igorvilleremixed.gov"))
|
||||
|
||||
self.assertFalse(igorville_available)
|
||||
|
||||
def test_in_domains_dotgov(self):
|
||||
gsa_available = in_domains("gsa.gov")
|
||||
gsa_caps_available = in_domains("GSA.gov")
|
||||
igorville_available = in_domains("igorvilleremixed.gov")
|
||||
|
||||
"""Domain searches work without trailing .gov"""
|
||||
self.assertTrue(in_domains("gsa"))
|
||||
# input is lowercased so GSA.GOV should be found
|
||||
|
@ -58,6 +86,14 @@ class AvailableViewTest(TestCase):
|
|||
# This domain should not have been registered
|
||||
self.assertFalse(in_domains("igorvilleremixed"))
|
||||
|
||||
def test_in_domains_capitalized(self):
|
||||
gsa_available = in_domains("gsa.gov")
|
||||
capitalized_gsa_available = in_domains("GSA.gov")
|
||||
|
||||
"""Domain searches work without case sensitivity"""
|
||||
self.assertTrue(in_domains("gsa.gov"))
|
||||
self.assertTrue(in_domains("GSA.gov"))
|
||||
|
||||
def test_not_available_domain(self):
|
||||
"""gsa.gov is not available"""
|
||||
request = self.factory.get(API_BASE_PATH + "gsa.gov")
|
||||
|
@ -86,13 +122,17 @@ class AvailableViewTest(TestCase):
|
|||
request.user = self.user
|
||||
response = available(request, domain=bad_string)
|
||||
self.assertFalse(json.loads(response.content)["available"])
|
||||
# domain set to raise error successfully raises error
|
||||
with self.assertRaises(RegistryError):
|
||||
error_domain_available = available(request, "errordomain.gov")
|
||||
|
||||
|
||||
class AvailableAPITest(TestCase):
|
||||
class AvailableAPITest(MockEppLib):
|
||||
|
||||
"""Test that the API can be called as expected."""
|
||||
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
self.user = get_user_model().objects.create(username="username")
|
||||
|
||||
def test_available_get(self):
|
||||
|
|
|
@ -59,12 +59,12 @@ def in_domains(domain):
|
|||
given domain doesn't end with .gov, ".gov" is added when looking for
|
||||
a match.
|
||||
"""
|
||||
domain = domain.lower()
|
||||
Domain = apps.get_model("registrar.Domain")
|
||||
if domain.endswith(".gov"):
|
||||
return domain.lower() in _domains()
|
||||
return Domain.available(domain)
|
||||
else:
|
||||
# domain search string doesn't end with .gov, add it on here
|
||||
return (domain + ".gov") in _domains()
|
||||
return Domain.available(domain + ".gov")
|
||||
|
||||
|
||||
@require_http_methods(["GET"])
|
||||
|
|
|
@ -30,6 +30,7 @@ from epplibwrapper import (
|
|||
info,
|
||||
RegistryError,
|
||||
ErrorCode,
|
||||
responses,
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
@ -824,7 +825,41 @@ class MockEppLib(TestCase):
|
|||
raise RegistryError(
|
||||
code=ErrorCode.OBJECT_ASSOCIATION_PROHIBITS_OPERATION
|
||||
)
|
||||
|
||||
elif isinstance(_request, commands.CheckDomain):
|
||||
if "gsa.gov" in getattr(_request, "names", None):
|
||||
return MagicMock(
|
||||
res_data=[
|
||||
responses.check.CheckDomainResultData(
|
||||
name="gsa.gov", avail=True, reason=None
|
||||
),
|
||||
]
|
||||
)
|
||||
elif "GSA.gov" in getattr(_request, "names", None):
|
||||
return MagicMock(
|
||||
res_data=[
|
||||
responses.check.CheckDomainResultData(
|
||||
name="GSA.gov", avail=True, reason=None
|
||||
),
|
||||
]
|
||||
)
|
||||
elif "igorvilleremixed.gov" in getattr(_request, "names", None):
|
||||
return MagicMock(
|
||||
res_data=[
|
||||
responses.check.CheckDomainResultData(
|
||||
name="igorvilleremixed.gov", avail=False, reason=None
|
||||
),
|
||||
]
|
||||
)
|
||||
elif "errordomain.gov" in getattr(_request, "names", None):
|
||||
raise RegistryError("Registry cannot find domain availability.")
|
||||
else:
|
||||
return MagicMock(
|
||||
res_data=[
|
||||
responses.check.CheckDomainResultData(
|
||||
name="domainnotfound.gov", avail=False, reason="In Use"
|
||||
)
|
||||
],
|
||||
)
|
||||
return MagicMock(res_data=[self.mockDataInfoHosts])
|
||||
|
||||
def setUp(self):
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
"""Test form validation requirements."""
|
||||
|
||||
from django.test import TestCase
|
||||
from django.test import TestCase, RequestFactory
|
||||
|
||||
from registrar.forms.application_wizard import (
|
||||
CurrentSitesForm,
|
||||
|
@ -16,9 +16,16 @@ from registrar.forms.application_wizard import (
|
|||
AboutYourOrganizationForm,
|
||||
)
|
||||
from registrar.forms.domain import ContactForm
|
||||
from registrar.tests.common import MockEppLib
|
||||
from django.contrib.auth import get_user_model
|
||||
|
||||
|
||||
class TestFormValidation(TestCase):
|
||||
class TestFormValidation(MockEppLib):
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
self.user = get_user_model().objects.create(username="username")
|
||||
self.factory = RequestFactory()
|
||||
|
||||
def test_org_contact_zip_invalid(self):
|
||||
form = OrganizationContactForm(data={"zipcode": "nah"})
|
||||
self.assertEqual(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue