Skip to content

Commit 22e6ec8

Browse files
committed
chore: add test for credentials parsing
1 parent 2a48690 commit 22e6ec8

File tree

4 files changed

+86
-1
lines changed

4 files changed

+86
-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
5+
46
from conftest import Mock
7+
from upcloud_api import CloudManager
8+
from upcloud_api.errors import UpCloudClientError
59

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

0 commit comments

Comments
 (0)