Use longest possible matching URL for auth, refs #3

This commit is contained in:
Simon Willison 2023-07-17 16:03:05 -07:00
commit 98d714c5e1
3 changed files with 41 additions and 10 deletions

View file

@ -1,4 +1,4 @@
from dclient.utils import url_matches_prefix
from dclient.utils import token_for_url, url_matches_prefix
import pytest
@ -39,3 +39,29 @@ import pytest
)
def test_url_matches_prefix(url, prefix_url, expected):
assert url_matches_prefix(url, prefix_url) == expected
@pytest.mark.parametrize(
"url,tokens,expected",
(
("https://foo.com/bar", {"https://foo.com": "foo"}, "foo"),
("https://foo.com/bar", {"https://foo.com/baz": "baz"}, None),
(
"https://foo.com/bar",
{"https://foo.com": "foo", "https://foo.com/bar": "bar"},
"bar",
),
(
"https://foo.com/bar/baz",
{
"https://foo.com": "foo",
"https://foo.com/bar/baz": "baz",
"https://foo.com/bar": "bar",
},
"baz",
),
),
)
def test_token_for_url(url, tokens, expected):
# Should always return longest matching of the available options
assert token_for_url(url, tokens) == expected