-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathtest_credentials.py
More file actions
66 lines (47 loc) · 2.03 KB
/
test_credentials.py
File metadata and controls
66 lines (47 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import keyring
from upcloud_api import Credentials
class DictBackend(keyring.backend.KeyringBackend):
priority = 1
def __init__(self, secrets=None):
super().__init__()
self._secrets = secrets or {}
def set_password(self, servicename, username, password):
pass
def get_password(self, servicename, username):
return self._secrets.get(servicename, {}).get(username)
def delete_password(self, servicename, username):
pass
class TestCredentials:
def test_precedence(self, monkeypatch):
param_basic = 'Basic cGFyYW1fdXNlcjpwYXJhbV9wYXNz'
param_bearer = 'Bearer param_token'
env_basic = 'Basic ZW52X3VzZXI6ZW52X3Bhc3M='
env_bearer = 'Bearer env_token'
keyring_basic = 'Basic ZW52X3VzZXI6a2V5cmluZ19wYXNz'
keyring_bearer = 'Bearer keyring_token'
backend = DictBackend(
{
"UpCloud": {
"env_user": "keyring_pass",
"": "keyring_token",
}
}
)
keyring.set_keyring(backend)
credentials = Credentials.parse()
assert credentials.authorization == keyring_bearer
monkeypatch.setenv("UPCLOUD_USERNAME", 'env_user')
credentials = Credentials.parse()
assert credentials.authorization == keyring_basic
monkeypatch.setenv("UPCLOUD_PASSWORD", 'env_pass')
credentials = Credentials.parse(username='param_user', password='param_pass')
assert credentials.authorization == param_basic
credentials = Credentials.parse()
assert credentials.authorization == env_basic
monkeypatch.setenv("UPCLOUD_TOKEN", 'env_token')
credentials = Credentials.parse(username='param_user', password='param_pass')
assert credentials.authorization == param_basic
credentials = Credentials.parse()
assert credentials.authorization == env_bearer
credentials = Credentials.parse(token='param_token')
assert credentials.authorization == param_bearer