Commit f9e80bcf authored by Chris's avatar Chris
Browse files

Cleanup and tweak stuff

parent 6c15f50a
Pipeline #6039 passed with stage
in 1 minute and 10 seconds
......@@ -11,7 +11,14 @@
"module": "debug",
"jinja": true,
"justMyCode": false,
"console": "externalTerminal"
},
{
"name": "Pytest",
"type": "python",
"request": "launch",
"module": "pytest",
"jinja": true,
"justMyCode": false,
}
]
}
\ No newline at end of file
}
import re
import pydantic
class DomainName(str):
"""
Domain name validation type.
"""
__domain_regex = re.compile(
r"^(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*$",
re.IGNORECASE,
)
@classmethod
def __get_validators__(cls):
# one or more validators may be yielded which will be called in the
# order to validate the input, each validator will receive as an input
# the value returned from the previous validator
yield cls.validate
@classmethod
def __modify_schema__(cls, field_schema):
# __modify_schema__ should mutate the dict it receives in place,
# the returned value will be ignored
field_schema.update(
# simplified pattenr for brevity
pattern='[subdomain].domain.tld',
# some example domains
examples=['example.com', 'site.example.com'],
)
@classmethod
def validate(cls, v):
if not isinstance(v, str):
raise TypeError('string required')
m = cls.__domain_regex.fullmatch(v)
if not m:
raise ValueError('Invalid format for domain name')
# you could also return a string here which would mean model.post_code
# would be a string, pydantic won't care but you could end up with some
# confusion since the value's type won't match the type annotation
# exactly
return cls(v)
def __repr__(self):
return f'DomainName({super().__repr__()})'
\ No newline at end of file
......@@ -71,7 +71,7 @@ async def test_get_cachet_status_all_good(
_mock_response, total, operational, degraded = mock_responses[mock_response]
mock_app.expected_cachet_response = _mock_response
res = await api.get("/check/status/")
assert res.status_code == 200
assert res.status_code == status.HTTP_200_OK
assert "services" in res.json()
services = res.json()["services"]
# There should be `total` services
......@@ -94,7 +94,7 @@ async def test_upstream_error(api: AsyncClient) -> None:
local_mock_client.get = mock.AsyncMock(side_effect=KeyError)
with mock.patch("isitup.checks.cachet.AsyncClient", local_mock_client):
res = await api.get("/check/status/")
assert res.status_code == 500
assert res.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR
assert res.json()["detail"] == "Upstream API gave unexpected result."
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment