mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-07-13 14:35:14 +02:00
Use downloaded domains list to back available API
This commit is contained in:
parent
7e02661416
commit
53906d339f
4 changed files with 236 additions and 149 deletions
|
@ -5,7 +5,7 @@ import json
|
|||
from django.contrib.auth import get_user_model
|
||||
from django.test import TestCase, RequestFactory
|
||||
|
||||
from ..views import available
|
||||
from ..views import available, _domains, in_domains
|
||||
|
||||
class AvailableViewTest(TestCase):
|
||||
|
||||
|
@ -25,6 +25,39 @@ 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."""
|
||||
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("igorville.gov", domains)
|
||||
# all the entries have dots
|
||||
self.assertNotIn("gsa", domains)
|
||||
|
||||
def test_in_domains(self):
|
||||
self.assertTrue(in_domains("gsa.gov"))
|
||||
# input is lowercased so GSA.GOV should be found
|
||||
self.assertTrue(in_domains("GSA.GOV"))
|
||||
# This domain should not have been registered
|
||||
self.assertFalse(in_domains("igorville.gov"))
|
||||
# all the entries have dots
|
||||
self.assertFalse(in_domains("gsa"))
|
||||
|
||||
def test_not_available_domain(self):
|
||||
"""gsa.gov is not available"""
|
||||
request = self.factory.get("/available/gsa.gov")
|
||||
request.user = self.user
|
||||
response = available(request, domain="gsa.gov")
|
||||
self.assertFalse(json.loads(response.content)["available"])
|
||||
|
||||
def test_available_domain(self):
|
||||
"""igorville.gov is still available"""
|
||||
request = self.factory.get("/available/igorville.gov")
|
||||
request.user = self.user
|
||||
response = available(request, domain="igorville.gov")
|
||||
self.assertTrue(json.loads(response.content)["available"])
|
||||
|
||||
|
||||
class AvailableAPITest(TestCase):
|
||||
|
||||
|
|
|
@ -6,6 +6,41 @@ from django.http import JsonResponse
|
|||
|
||||
from django.contrib.auth.decorators import login_required
|
||||
|
||||
import requests
|
||||
|
||||
from cachetools.func import ttl_cache
|
||||
|
||||
DOMAIN_FILE_URL = "https://raw.githubusercontent.com/cisagov/dotgov-data/main/current-full.csv"
|
||||
|
||||
|
||||
# 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)
|
||||
def _domains():
|
||||
"""Return a list of the current .gov domains.
|
||||
|
||||
Fetch a file from DOMAIN_FILE_URL, parse the CSV for the domain,
|
||||
lowercase everything and return the list.
|
||||
"""
|
||||
file_contents = requests.get(DOMAIN_FILE_URL).text
|
||||
domains = set()
|
||||
# skip the first line
|
||||
for line in file_contents.splitlines()[1:]:
|
||||
# get the domain before the first comma
|
||||
domain = line.split(",", 1)[0]
|
||||
# lowercase everything
|
||||
domains.add(domain.lower())
|
||||
return domains
|
||||
|
||||
|
||||
def in_domains(domain):
|
||||
"""Return true if the given domain is in the domains list.
|
||||
|
||||
The given domain is lowercased to match against the domains list.
|
||||
"""
|
||||
return domain.lower() in _domains()
|
||||
|
||||
|
||||
@require_http_methods(["GET"])
|
||||
@login_required
|
||||
def available(request, domain=""):
|
||||
|
@ -15,5 +50,6 @@ def available(request, domain=""):
|
|||
Response is a JSON dictionary with the key "available" and value true or
|
||||
false.
|
||||
"""
|
||||
return JsonResponse({"available": False})
|
||||
# a domain is available if it is NOT in the list of current domains
|
||||
return JsonResponse({"available": not in_domains(domain)})
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue