Merge pull request #2756 from alphagov/clean-up-domains

Clean up domains file
This commit is contained in:
Chris Hill-Scott
2019-02-14 11:27:34 +00:00
committed by GitHub
4 changed files with 352 additions and 1037 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -563,7 +563,10 @@ class AgreementInfo:
def _get_info(self): def _get_info(self):
details = self.domains.get(self._match) or {} details = self.domains.get(self._match, {})
if details is None:
raise TypeError('Domain must have details ({})'.format(self._domain))
if isinstance(details, str): if isinstance(details, str):
self.is_canonical = False self.is_canonical = False

52
domains.py Executable file
View File

@@ -0,0 +1,52 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import yaml
from itertools import chain
from operator import itemgetter
from sys import argv
from app.utils import AgreementInfo
_dir_path = os.path.dirname(os.path.realpath(__file__))
if len(argv) < 2:
raise TypeError('Must specify `orgs` or `domains` as the first argument to this script')
with open('{}/app/domains.yml'.format(_dir_path)) as source:
data = yaml.load(source)
for domain, details in data.items():
if isinstance(details, dict):
# Were looking at the canonical domain
data[domain]['domains'] = [domain]
for domain, details in data.items():
if isinstance(details, str):
# This is an alias, lets add it to the canonical domain
data[AgreementInfo(domain).canonical_domain]['domains'].append(domain)
out_data = [
details for domain, details in data.items()
if isinstance(details, dict)
]
if argv[1] == 'orgs':
print(yaml.dump(out_data)) # noqa
elif argv[1] == 'domains':
print( # noqa
sorted(
set(
chain.from_iterable(
map(
itemgetter('domains'), out_data
)
)
)
)
)
else:
raise TypeError('Must specify `orgs` or `domains` as the first argument to this script')

View File

@@ -1,4 +1,4 @@
from collections import OrderedDict from collections import Counter, OrderedDict
from csv import DictReader from csv import DictReader
from io import StringIO from io import StringIO
from pathlib import Path from pathlib import Path
@@ -391,6 +391,11 @@ def test_get_domain_info_for_branding_request():
) )
def test_domains_are_lowercased():
for domain in AgreementInfo.domains.keys():
assert domain == domain.lower()
def test_validate_government_domain_data(): def test_validate_government_domain_data():
for domain in AgreementInfo.domains.keys(): for domain in AgreementInfo.domains.keys():
@@ -403,17 +408,25 @@ def test_validate_government_domain_data():
True, False, None True, False, None
} }
assert ( assert isinstance(agreement_info.owner, str) and agreement_info.owner.strip()
agreement_info.owner is None
) or (
isinstance(agreement_info.owner, str)
)
assert agreement_info.agreement_signed in { assert agreement_info.agreement_signed in {
True, False, None True, False, None
} }
def test_domain_data_is_canonicalized():
for owner, count in Counter(
AgreementInfo(domain).owner
for domain in AgreementInfo.domains.keys()
if AgreementInfo(domain).is_canonical
).most_common():
if count > 1:
raise ValueError(
'{} entries in domains.yml for {}'.format(count, owner)
)
def test_validate_email_domain_data(): def test_validate_email_domain_data():
for domain in GovernmentEmailDomain.domains.keys(): for domain in GovernmentEmailDomain.domains.keys():