Skip to content

Commit

Permalink
Merge pull request #1094 from cisagov/dk/1028-domain-available
Browse files Browse the repository at this point in the history
1028 - Check Domain availability via epp - Testing
  • Loading branch information
dave-kennedy-ecs authored Oct 2, 2023
2 parents 23e2aa4 + e686eaf commit 3951682
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/epplibwrapper/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@
from .client import CLIENT, commands
from .errors import RegistryError, ErrorCode
from epplib.models import common
from epplib import responses
except ImportError:
pass

__all__ = [
"CLIENT",
"commands",
"common",
"responses",
"ErrorCode",
"RegistryError",
]
111 changes: 110 additions & 1 deletion src/registrar/tests/test_models_domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"""
from django.test import TestCase
from django.db.utils import IntegrityError
from unittest.mock import patch, call
from unittest.mock import MagicMock, patch, call
import datetime
from registrar.models import Domain

Expand All @@ -20,6 +20,7 @@
from epplibwrapper import (
commands,
common,
responses,
RegistryError,
ErrorCode,
)
Expand Down Expand Up @@ -268,6 +269,114 @@ def tearDown(self) -> None:
super().tearDown()


class TestDomainAvailable(MockEppLib):
"""Test Domain.available"""

# No SetUp or tearDown necessary for these tests

def test_domain_available(self):
"""
Scenario: Testing whether an available domain is available
Should return True
Mock response to mimic EPP Response
Validate CheckDomain command is called
Validate response given mock
"""

def side_effect(_request, cleaned):
return MagicMock(
res_data=[
responses.check.CheckDomainResultData(
name="available.gov", avail=True, reason=None
)
],
)

patcher = patch("registrar.models.domain.registry.send")
mocked_send = patcher.start()
mocked_send.side_effect = side_effect

available = Domain.available("available.gov")
mocked_send.assert_has_calls(
[
call(
commands.CheckDomain(
["available.gov"],
),
cleaned=True,
)
]
)
self.assertTrue(available)
patcher.stop()

def test_domain_unavailable(self):
"""
Scenario: Testing whether an unavailable domain is available
Should return False
Mock response to mimic EPP Response
Validate CheckDomain command is called
Validate response given mock
"""

def side_effect(_request, cleaned):
return MagicMock(
res_data=[
responses.check.CheckDomainResultData(
name="unavailable.gov", avail=False, reason="In Use"
)
],
)

patcher = patch("registrar.models.domain.registry.send")
mocked_send = patcher.start()
mocked_send.side_effect = side_effect

available = Domain.available("unavailable.gov")
mocked_send.assert_has_calls(
[
call(
commands.CheckDomain(
["unavailable.gov"],
),
cleaned=True,
)
]
)
self.assertFalse(available)
patcher.stop()

def test_domain_available_with_value_error(self):
"""
Scenario: Testing whether an invalid domain is available
Should throw ValueError
Validate ValueError is raised
"""
with self.assertRaises(ValueError):
Domain.available("invalid-string")

def test_domain_available_unsuccessful(self):
"""
Scenario: Testing behavior when registry raises a RegistryError
Validate RegistryError is raised
"""

def side_effect(_request, cleaned):
raise RegistryError(code=ErrorCode.COMMAND_SYNTAX_ERROR)

patcher = patch("registrar.models.domain.registry.send")
mocked_send = patcher.start()
mocked_send.side_effect = side_effect

with self.assertRaises(RegistryError):
Domain.available("raises-error.gov")
patcher.stop()


class TestRegistrantContacts(MockEppLib):
"""Rule: Registrants may modify their WHOIS data"""

Expand Down

0 comments on commit 3951682

Please sign in to comment.