Skip to content

Commit 93ea8ad

Browse files
committed
refactor
1 parent 82b6689 commit 93ea8ad

File tree

3 files changed

+53
-43
lines changed

3 files changed

+53
-43
lines changed

src/aleph/sdk/conf.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ class Settings(BaseSettings):
3434
CODE_USES_SQUASHFS: bool = which("mksquashfs") is not None # True if command exists
3535

3636
# Dns resolver
37-
IPFS_DOMAINS = "ipfs.public.aleph.sh"
38-
PROGRAM_DOMAINS = "program.public.aleph.sh"
39-
ROOT_DOMAIN = "static.public.aleph.sh"
40-
RESOLVERS = ["1.1.1.1", "8.8.8.8"]
37+
DNS_IPFS_DOMAIN = "ipfs.public.aleph.sh"
38+
DNS_PROGRAM_DOMAIN = "program.public.aleph.sh"
39+
DNS_ROOT_DOMAIN = "static.public.aleph.sh"
40+
DNS_RESOLVERS = ["1.1.1.1", "1.0.0.1"]
4141

4242
class Config:
4343
env_prefix = "ALEPH_"

src/aleph/sdk/domain.py

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class AlephDNS:
99
def __init__(self):
10-
self.resolver = aiodns.DNSResolver(servers=settings.RESOLVERS)
10+
self.resolver = aiodns.DNSResolver(servers=settings.DNS_RESOLVERS)
1111
self.fqdn_matcher = re.compile(r"https?://?")
1212

1313
async def query(self, name: str, query_type: str):
@@ -41,69 +41,66 @@ async def get_control(self, url: str):
4141
if query is not None and len(query) > 0:
4242
return query[0].text
4343

44-
async def check_domain_configured(self, domain, _type, owner):
44+
async def check_domain_configured(self, domain, target, owner):
4545
try:
46-
print("Check...", _type)
47-
return await self.check_domain(domain, _type, owner)
46+
print("Check...", target)
47+
return await self.check_domain(domain, target, owner)
4848
except Exception as error:
4949
raise DomainConfigurationError(error)
5050

51-
async def check_domain(self, url: str, _type: str, owner: Optional[str] = None):
52-
# if _type.lower() == 'ipfs':
53-
return await self.check_ipfs_domain(url, _type, owner)
54-
# elif _type.lower() == 'program':
55-
# pass
51+
async def check_domain(self, url: str, target: str, owner: Optional[str] = None):
52+
return await self.check_ipfs_domain(url, target, owner)
53+
5654

5755
async def check_ipfs_domain(
58-
self, url: str, _type: str, owner: Optional[str] = None
56+
self, url: str, target: str, owner: Optional[str] = None
5957
):
6058
status = {"cname": True, "owner_proof": False}
6159

62-
_type = _type.lower()
60+
target = target.lower()
6361
domain = self.url_to_domain(url)
6462

65-
if _type == "ipfs":
63+
if target == "ipfs":
6664
status["delegation"] = False
6765

6866
# check1: CNAME value should be ipfs or program
6967
res = await self.query(domain, "CNAME")
70-
if _type.lower() == "ipfs":
71-
expected_value = settings.IPFS_DOMAINS
68+
if target.lower() == "ipfs":
69+
expected_value = settings.DNS_IPFS_DOMAIN
7270
else:
73-
expected_value = settings.PROGRAM_DOMAINS
71+
expected_value = settings.DNS_PROGRAM_DOMAIN
7472

7573
assert_error = (
7674
f"CNAME record not found: {domain}",
7775
f"Create a CNAME record for {domain} with values {expected_value}",
7876
status,
7977
)
80-
81-
assert res is not None, assert_error
82-
assert hasattr(res, "cname"), assert_error
78+
if res is None or not hasattr(res, "cname"):
79+
raise DomainConfigurationError(assert_error)
8380

8481
assert_error = (
8582
f"{domain} should have a valid CNAME value, {res.cname} provided",
8683
f"Create a CNAME record for {domain} with values {expected_value}",
8784
status,
8885
)
89-
assert res.cname in expected_value, assert_error
90-
status["cname"] = True
86+
if res.cname != expected_value:
87+
raise DomainConfigurationError(assert_error)
9188

92-
if _type.lower() == "ipfs":
89+
status["cname"] = True
90+
if target.lower() == "ipfs":
9391
# check2: CNAME value of _dnslink.__custom_domain__
9492
# should be _dnslink.__custom_domain__.static.public.aleph.sh
9593
res = await self.query(f"_dnslink.{domain}", "CNAME")
9694

97-
expected_value = f"_dnslink.{domain}.{settings.ROOT_DOMAIN}"
95+
expected_value = f"_dnslink.{domain}.{settings.DNS_ROOT_DOMAIN}"
9896
assert_error = (
9997
f"CNAME record not found: _dnslink.{domain}",
10098
f"Create a CNAME record for _dnslink.{domain} with value: {expected_value}",
10199
status,
102100
)
101+
if res is None or not hasattr(res, "cname") or res.cname != expected_value:
102+
raise DomainConfigurationError(assert_error)
103103

104-
assert res is not None, assert_error
105-
assert hasattr(res, "cname"), assert_error
106-
assert res.cname == expected_value, assert_error
107104
status["delegation"] = True
108105

109106
# check3: TXT value of _control.__custom_domain__ should be the address of the owner
@@ -113,14 +110,19 @@ async def check_ipfs_domain(
113110
f'Create a TXT record for _control.{domain} with value = "owner address"',
114111
status,
115112
)
116-
assert owner_address is not None, assert_error
113+
if owner_address is None:
114+
raise DomainConfigurationError(assert_error)
117115

118116
if owner is not None:
119-
assert owner_address == owner, (
120-
f"Owner address mismatch, got: {owner} expected: {owner_address}",
121-
f"",
122-
status,
123-
)
124-
status["owner_proof"] = True
117+
if owner == owner_address:
118+
status["owner_proof"] = True
119+
else:
120+
raise DomainConfigurationError(
121+
(
122+
f"Owner address mismatch, got: {owner} expected: {owner_address}",
123+
f"",
124+
status,
125+
)
126+
)
125127

126128
return status

tests/unit/test_domains.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import asyncio
33

44
from aleph.sdk.domain import AlephDNS
5+
from aleph.sdk.exceptions import DomainConfigurationError
56

67

78
@pytest.mark.asyncio
@@ -15,7 +16,7 @@ async def test_url_to_domain():
1516

1617

1718
@pytest.mark.asyncio
18-
async def test_get_ipv6_adress():
19+
async def test_get_ipv6_address():
1920
alephdns = AlephDNS()
2021
url = "https://aleph.im"
2122
ipv6_address = await alephdns.get_ipv6_address(url)
@@ -32,11 +33,18 @@ async def test_dnslink():
3233
assert dnslink is not None
3334

3435

35-
"""
3636
@pytest.mark.asyncio
37-
async def test_cname():
37+
async def test_configured_domain():
3838
alephdns = AlephDNS()
39-
url = 'https://custom_domain_test.aleph.sh'
40-
check = await alephdns.custom_domain_check(url)
41-
assert check is not None
42-
"""
39+
url = 'https://custom-domain-unit-test.aleph.sh'
40+
status = await alephdns.check_domain(url, "ipfs", "0xfakeaddress")
41+
assert type(status) is dict
42+
43+
44+
@pytest.mark.asyncio
45+
async def test_not_configured_domain():
46+
alephdns = AlephDNS()
47+
url = 'https://not-configured-domain.aleph.sh'
48+
with pytest.raises(DomainConfigurationError):
49+
status = await alephdns.check_domain(url, "ipfs", "0xfakeaddress")
50+

0 commit comments

Comments
 (0)