Skip to content

Commit 3400138

Browse files
authored
Allow to set-credentials to all hosts (old style) (#76)
1 parent 81a85c1 commit 3400138

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

stompman/config.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def unescaped_passcode(self) -> str:
3636
return unquote(self.passcode)
3737

3838
@classmethod
39-
def from_pydantic_multihost_hosts(cls, hosts: list[MultiHostHostLike]) -> list[Self]:
39+
def from_pydantic_multihost_hosts(cls, hosts: list[MultiHostHostLike]) -> list[Self]: # noqa: C901
4040
"""Create connection parameters from `pydantic_code.MultiHostUrl.hosts()`.
4141
4242
.. code-block:: python
@@ -52,7 +52,8 @@ def from_pydantic_multihost_hosts(cls, hosts: list[MultiHostHostLike]) -> list[S
5252
5353
async with stompman.Client(
5454
servers=stompman.ConnectionParameters.from_pydantic_multihost_hosts(
55-
ArtemisDsn("tcp://lev:pass@host1:61616,host2:61617,host3:61618").hosts()
55+
ArtemisDsn("tcp://user:pass@host1:61616,host2:61617,host3:61618").hosts()
56+
# or: ArtemisDsn("tcp://user1:pass1@host1:61616,user2:pass2@host2:61617,user3:pass@host3:61618").hosts()
5657
),
5758
):
5859
...
@@ -81,12 +82,18 @@ def from_pydantic_multihost_hosts(cls, hosts: list[MultiHostHostLike]) -> list[S
8182
else:
8283
all_credentials.append((username, password))
8384

84-
if not all_credentials:
85-
msg = "username and password must be set"
86-
raise ValueError(msg)
87-
if len(all_credentials) != 1:
88-
msg = "only one username-password pair must be set"
89-
raise ValueError(msg)
90-
91-
login, passcode = all_credentials[0]
92-
return [cls(host=host, port=port, login=login, passcode=passcode) for (host, port) in all_hosts]
85+
match len(all_credentials):
86+
case value if value == len(all_hosts):
87+
return [
88+
cls(host=host, port=port, login=username, passcode=password)
89+
for ((host, port), (username, password)) in zip(all_hosts, all_credentials, strict=True)
90+
]
91+
case 1:
92+
username, password = all_credentials[0]
93+
return [cls(host=host, port=port, login=username, passcode=password) for (host, port) in all_hosts]
94+
case 0:
95+
msg = "username and password must be set"
96+
raise ValueError(msg)
97+
case _:
98+
msg = "all username-password pairs or only one pair must be set"
99+
raise ValueError(msg)

tests/test_config.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class MultiHostHostLikeFactory(TypedDictFactory[MultiHostHostLike]): ...
2222

2323

2424
class TestConnectionParametersFromPydanticMultiHostHosts:
25-
def test_ok(self, faker: faker.Faker) -> None:
25+
def test_ok_with_one_credentials(self, faker: faker.Faker) -> None:
2626
hosts: list[MultiHostHostLike] = [
2727
{"host": "host1", "port": 1, "username": None, "password": None},
2828
{"host": "host2", "port": 2, "username": None, "password": None},
@@ -42,6 +42,23 @@ def test_ok(self, faker: faker.Faker) -> None:
4242
stompman.ConnectionParameters("host4", 4, "lev", "pass"),
4343
]
4444

45+
def test_ok_with_all_credentials(self, faker: faker.Faker) -> None:
46+
hosts: list[MultiHostHostLike] = [
47+
{"host": "host1", "port": 1, "username": "user1", "password": "pass1"},
48+
{"host": "host2", "port": 2, "username": "user2", "password": "pass2"},
49+
{"host": "host3", "port": 3, "username": "user3", "password": "pass3"},
50+
{"host": "host4", "port": 4, "username": "user4", "password": "pass4"},
51+
]
52+
53+
result = stompman.ConnectionParameters.from_pydantic_multihost_hosts(hosts)
54+
55+
assert result == [
56+
stompman.ConnectionParameters("host1", 1, "user1", "pass1"),
57+
stompman.ConnectionParameters("host2", 2, "user2", "pass2"),
58+
stompman.ConnectionParameters("host3", 3, "user3", "pass3"),
59+
stompman.ConnectionParameters("host4", 4, "user4", "pass4"),
60+
]
61+
4562
def test_no_host_or_port_or_both(self, faker: faker.Faker) -> None:
4663
cases: list[MultiHostHostLike] = [
4764
{"host": None, "port": faker.pyint(), "username": faker.pystr(), "password": faker.pystr()},
@@ -88,5 +105,5 @@ def test_multiple_credentials(self, faker: faker.Faker) -> None:
88105
{"host": faker.pystr(), "port": faker.pyint(), "username": faker.pystr(), "password": faker.pystr()},
89106
]
90107

91-
with pytest.raises(ValueError, match="only one username-password pair must be set"):
108+
with pytest.raises(ValueError, match="all username-password pairs or only one pair must be set"):
92109
stompman.ConnectionParameters.from_pydantic_multihost_hosts(hosts)

0 commit comments

Comments
 (0)