Skip to content

Commit 872040e

Browse files
committed
chore: add test for credentials parsing
1 parent 2a48690 commit 872040e

File tree

4 files changed

+88
-1
lines changed

4 files changed

+88
-1
lines changed

requirements-dev.in

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
keyring
12
mock
23
pip-tools
34
pytest
45
pytest-cov
5-
responses
6+
responses

requirements-dev.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,20 @@ idna==3.10
1818
# via requests
1919
iniconfig==2.1.0
2020
# via pytest
21+
jaraco-classes==3.4.0
22+
# via keyring
23+
jaraco-context==6.0.1
24+
# via keyring
25+
jaraco-functools==4.2.1
26+
# via keyring
27+
keyring==25.6.0
28+
# via -r requirements-dev.in
2129
mock==5.2.0
2230
# via -r requirements-dev.in
31+
more-itertools==10.7.0
32+
# via
33+
# jaraco-classes
34+
# jaraco-functools
2335
packaging==25.0
2436
# via
2537
# build

test/test_cloud_manager.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
import json
22

3+
import pytest
34
import responses
45
from conftest import Mock
56

7+
from upcloud_api import CloudManager
8+
from upcloud_api.errors import UpCloudClientError
9+
610

711
class TestCloudManagerBasic:
12+
def test_no_credentials(self):
13+
with pytest.raises(UpCloudClientError):
14+
CloudManager()
15+
816
@responses.activate
917
def test_get_account(self, manager):
1018
data = Mock.mock_get("account")

test/test_credentials.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import keyring
2+
3+
from upcloud_api import Credentials
4+
5+
6+
class DictBackend(keyring.backend.KeyringBackend):
7+
priority = 1
8+
9+
def __init__(self, secrets=None):
10+
super().__init__()
11+
self._secrets = secrets or {}
12+
13+
def set_password(self, servicename, username, password):
14+
pass
15+
16+
def get_password(self, servicename, username):
17+
return self._secrets.get(servicename, {}).get(username)
18+
19+
def delete_password(self, servicename, username):
20+
pass
21+
22+
23+
class TestCredentials:
24+
def test_precedence(self, monkeypatch):
25+
param_basic = 'Basic cGFyYW1fdXNlcjpwYXJhbV9wYXNz'
26+
param_bearer = 'Bearer param_token'
27+
env_basic = 'Basic ZW52X3VzZXI6ZW52X3Bhc3M='
28+
env_bearer = 'Bearer env_token'
29+
keyring_basic = 'Basic ZW52X3VzZXI6a2V5cmluZ19wYXNz'
30+
keyring_bearer = 'Bearer keyring_token'
31+
32+
backend = DictBackend(
33+
{
34+
"UpCloud": {
35+
"env_user": "keyring_pass",
36+
"": "keyring_token",
37+
}
38+
}
39+
)
40+
keyring.set_keyring(backend)
41+
42+
credentials = Credentials.parse()
43+
assert credentials.authorization == keyring_bearer
44+
45+
monkeypatch.setenv("UPCLOUD_USERNAME", 'env_user')
46+
47+
credentials = Credentials.parse()
48+
assert credentials.authorization == keyring_basic
49+
50+
monkeypatch.setenv("UPCLOUD_PASSWORD", 'env_pass')
51+
52+
credentials = Credentials.parse(username='param_user', password='param_pass')
53+
assert credentials.authorization == param_basic
54+
55+
credentials = Credentials.parse()
56+
assert credentials.authorization == env_basic
57+
58+
monkeypatch.setenv("UPCLOUD_TOKEN", 'env_token')
59+
credentials = Credentials.parse(username='param_user', password='param_pass')
60+
assert credentials.authorization == param_basic
61+
62+
credentials = Credentials.parse()
63+
assert credentials.authorization == env_bearer
64+
65+
credentials = Credentials.parse(token='param_token')
66+
assert credentials.authorization == param_bearer

0 commit comments

Comments
 (0)