Skip to content

Commit

Permalink
Merge pull request #391 from cisagov/sspj/domain-validation
Browse files Browse the repository at this point in the history
Domain Validation and Form Refactoring
  • Loading branch information
SSPJ committed Feb 6, 2023
2 parents 52208c0 + 070f85c commit 2b902be
Show file tree
Hide file tree
Showing 51 changed files with 904 additions and 919 deletions.
11 changes: 2 additions & 9 deletions src/api/tests/test_available.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import json

from django.core.exceptions import BadRequest
from django.contrib.auth import get_user_model
from django.test import TestCase, RequestFactory

Expand Down Expand Up @@ -85,8 +84,8 @@ def test_error_handling(self):
bad_string = "blah!;"
request = self.factory.get(API_BASE_PATH + bad_string)
request.user = self.user
with self.assertRaisesMessage(BadRequest, "Invalid"):
available(request, domain=bad_string)
response = available(request, domain=bad_string)
self.assertFalse(json.loads(response.content)["available"])


class AvailableAPITest(TestCase):
Expand All @@ -108,9 +107,3 @@ def test_available_post(self):
with less_console_noise():
response = self.client.post(API_BASE_PATH + "nonsense")
self.assertEqual(response.status_code, 405)

def test_available_bad_input(self):
self.client.force_login(self.user)
with less_console_noise():
response = self.client.get(API_BASE_PATH + "blah!;")
self.assertEqual(response.status_code, 400)
28 changes: 24 additions & 4 deletions src/api/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""Internal API views"""

from django.apps import apps
from django.core.exceptions import BadRequest
from django.views.decorators.http import require_http_methods
from django.http import JsonResponse

Expand All @@ -17,6 +15,19 @@
)


DOMAIN_API_MESSAGES = {
"required": "Enter the .gov domain you want. Don’t include “www” or “.gov.”"
" For example, if you want www.city.gov, you would enter “city”"
" (without the quotes).",
"extra_dots": "Enter the .gov domain you want without any periods.",
"unavailable": "That domain isn’t available. Try entering another one."
" Contact us if you need help coming up with a domain.",
"invalid": "Enter a domain using only letters,"
" numbers, or hyphens (though we don't recommend using hyphens).",
"success": "That domain is available!",
}


# this file doesn't change that often, nor is it that big, so cache the result
# in memory for ten minutes
@ttl_cache(ttl=600)
Expand Down Expand Up @@ -72,6 +83,15 @@ def available(request, domain=""):
Domain.string_could_be_domain(domain)
or Domain.string_could_be_domain(domain + ".gov")
):
raise BadRequest("Invalid request.")
return JsonResponse(
{"available": False, "message": DOMAIN_API_MESSAGES["invalid"]}
)
# a domain is available if it is NOT in the list of current domains
return JsonResponse({"available": not in_domains(domain)})
if in_domains(domain):
return JsonResponse(
{"available": False, "message": DOMAIN_API_MESSAGES["unavailable"]}
)
else:
return JsonResponse(
{"available": True, "message": DOMAIN_API_MESSAGES["success"]}
)

0 comments on commit 2b902be

Please sign in to comment.