forked from testcontainers/testcontainers-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_auth.py
More file actions
86 lines (71 loc) · 2.61 KB
/
Copy pathtest_auth.py
File metadata and controls
86 lines (71 loc) · 2.61 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import json
import pytest
from testcontainers.core.auth import parse_docker_auth_config, DockerAuthInfo
def test_parse_docker_auth_config_encoded():
auth_config_json = '{"auths":{"https://index.docker.io/v1/":{"auth":"dXNlcm5hbWU6cGFzc3dvcmQ="}}}'
auth_info = parse_docker_auth_config(auth_config_json)
assert auth_info
assert len(auth_info) == 1
assert auth_info[0] == DockerAuthInfo(
registry="https://index.docker.io/v1/",
username="username",
password="password",
)
def test_parse_docker_auth_config_cred_helpers():
auth_dict = {"credHelpers": {"<aws_account_id>.dkr.ecr.<region>.amazonaws.com": "ecr-login"}}
auth_config_json = json.dumps(auth_dict)
assert parse_docker_auth_config(auth_config_json) is None
def test_parse_docker_auth_config_store():
auth_dict = {"credsStore": "ecr-login"}
auth_config_json = json.dumps(auth_dict)
assert parse_docker_auth_config(auth_config_json) is None
def test_parse_docker_auth_config_encoded_multiple():
auth_dict = {
"auths": {
"localhost:5000": {"auth": "dXNlcjE6cGFzczE=="},
"https://example.com": {"auth": "dXNlcl9uZXc6cGFzc19uZXc=="},
"example2.com": {"auth": "YWJjOjEyMw==="},
}
}
auth_config_json = json.dumps(auth_dict)
auth_info = parse_docker_auth_config(auth_config_json)
assert auth_info
assert len(auth_info) == 3
assert auth_info[0] == DockerAuthInfo(
registry="localhost:5000",
username="user1",
password="pass1",
)
assert auth_info[1] == DockerAuthInfo(
registry="https://example.com",
username="user_new",
password="pass_new",
)
assert auth_info[2] == DockerAuthInfo(
registry="example2.com",
username="abc",
password="123",
)
def test_parse_docker_auth_config_unknown():
auth_config_str = '{"key": "value"}'
assert parse_docker_auth_config(auth_config_str) is None
def test_parse_docker_auth_config_error():
auth_config_str = "bad//string"
with pytest.raises(ValueError):
parse_docker_auth_config(auth_config_str)
def test_parse_docker_auth_all():
test_dict = {
"auths": {
"localhost:5000": {"auth": "dXNlcjE6cGFzczE=="},
},
"credHelpers": {"<aws_account_id>.dkr.ecr.<region>.amazonaws.com": "ecr-login"},
"credsStore": "ecr-login",
}
auth_config_json = json.dumps(test_dict)
assert parse_docker_auth_config(auth_config_json) == [
DockerAuthInfo(
registry="localhost:5000",
username="user1",
password="pass1",
)
]