From 0e3cdf7295650d4bf611de846b0132803fbf01bc Mon Sep 17 00:00:00 2001 From: Jeffrey Goldberg Date: Sun, 26 Nov 2023 17:46:15 -0600 Subject: [PATCH] ! some tests fail but there are tests --- rendercv/data_model.py | 8 ++++++-- tests/test_data_model.py | 14 +++++++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/rendercv/data_model.py b/rendercv/data_model.py index d1a861f..0a96edf 100644 --- a/rendercv/data_model.py +++ b/rendercv/data_model.py @@ -1083,13 +1083,17 @@ class Connection(BaseModel): # URL_PREFIX_RE = %r{\Ahttp(s?)://[^/]+} pattern = re.compile(r""" - @? # Optional @ prefix + ^\w* # ignore leading spaces + @? # Optional @ prefix (?P[a-z0-9_]+([a-z0-9_.-]+[a-z0-9_]+)?) # username part - @ # separator + @ # separator (?P[a-z0-9_]+([a-z0-9_.-]+[a-z0-9_]+)?) # domain part + \w*$ # ignore trailing whitespace """, re.VERBOSE | re.IGNORECASE) m = pattern.match(id) + if m is None: + raise ValueError("Invalid mastodon address") uname = m.group("uname") domain = m.group("domain") diff --git a/tests/test_data_model.py b/tests/test_data_model.py index 63cdc47..8415f85 100644 --- a/tests/test_data_model.py +++ b/tests/test_data_model.py @@ -5,7 +5,7 @@ import json from rendercv import data_model from datetime import date as Date -from pydantic import ValidationError +from pydantic import ValidationError, HttpUrl class TestDataModel(unittest.TestCase): @@ -855,13 +855,21 @@ class TestDataModel(unittest.TestCase): def test_mastodon_parsing(self): mastodon_name = 'jpgoldberg@ioc.exchange' - expected = "https://ioc.exchange/@jpgoldberg" + expected = HttpUrl("https://ioc.exchange/@jpgoldberg") result = data_model.Connection.MastodonUname2Url(mastodon_name) with self.subTest("Without '@' prefix"): self.assertEqual(result, expected) mastodon_name = '@jpgoldberg@ioc.exchange' - expected = "https://ioc.exchange/@jpgoldberg" + expected = HttpUrl("https://ioc.exchange/@jpgoldberg") result = data_model.Connection.MastodonUname2Url(mastodon_name) with self.subTest("With '@' prefix"): self.assertEqual(result, expected) + + mastodon_name = '@too@many@symbols' + with self.subTest("Too many '@' symbols"): + with self.assertRaises(ValueError): + data_model.Connection.MastodonUname2Url(mastodon_name) + +if __name__ == '__main__': + unittest.main()