summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAanand Prasad <aanand.prasad@gmail.com>2015-12-10 12:36:51 +0000
committerAanand Prasad <aanand.prasad@gmail.com>2015-12-10 12:42:54 +0000
commit9b890c4540dd82cff67ac69568df9bbcc155d07e (patch)
treea128d4c2c0d2ebdaa3c67a5f3471ff2310f926da
parentcaa64b3f6a70f9401b07802fc130956064742024 (diff)
downloaddocker-py-9b890c4540dd82cff67ac69568df9bbcc155d07e.tar.gz
Refactor resolve_authconfig tests
The structure of the fake config dictionary was not reflective of what actual parsed config looks like. Signed-off-by: Aanand Prasad <aanand.prasad@gmail.com>
-rw-r--r--docker/auth/auth.py7
-rw-r--r--tests/unit/auth_test.py83
2 files changed, 52 insertions, 38 deletions
diff --git a/docker/auth/auth.py b/docker/auth/auth.py
index f771ded..1a44ce2 100644
--- a/docker/auth/auth.py
+++ b/docker/auth/auth.py
@@ -82,11 +82,6 @@ def convert_to_hostname(url):
return url.replace('http://', '').replace('https://', '').split('/', 1)[0]
-def encode_auth(auth_info):
- return base64.b64encode(auth_info.get('username', '') + b':' +
- auth_info.get('password', ''))
-
-
def decode_auth(auth):
if isinstance(auth, six.string_types):
auth = auth.encode('ascii')
@@ -121,7 +116,7 @@ def parse_auth(entries):
conf[registry] = {
'username': username,
'password': password,
- 'email': entry['email'],
+ 'email': entry.get('email'),
'serveraddress': registry,
}
return conf
diff --git a/tests/unit/auth_test.py b/tests/unit/auth_test.py
index 8e0b1d4..6a0f510 100644
--- a/tests/unit/auth_test.py
+++ b/tests/unit/auth_test.py
@@ -9,6 +9,7 @@ import shutil
import tempfile
from docker import auth
+from docker.auth.auth import parse_auth
from docker import errors
from .. import base
@@ -104,88 +105,105 @@ class ResolveRepositoryNameTest(base.BaseTestCase):
)
+def encode_auth(auth_info):
+ return base64.b64encode(
+ auth_info.get('username', '').encode('utf-8') + b':' +
+ auth_info.get('password', '').encode('utf-8'))
+
+
class ResolveAuthTest(base.BaseTestCase):
- auth_config = {
- 'https://index.docker.io/v1/': {'auth': 'indexuser'},
- 'my.registry.net': {'auth': 'privateuser'},
- 'http://legacy.registry.url/v1/': {'auth': 'legacyauth'}
- }
+ index_config = {'auth': encode_auth({'username': 'indexuser'})}
+ private_config = {'auth': encode_auth({'username': 'privateuser'})}
+ legacy_config = {'auth': encode_auth({'username': 'legacyauth'})}
+
+ auth_config = parse_auth({
+ 'https://index.docker.io/v1/': index_config,
+ 'my.registry.net': private_config,
+ 'http://legacy.registry.url/v1/': legacy_config,
+ })
def test_resolve_authconfig_hostname_only(self):
self.assertEqual(
- auth.resolve_authconfig(self.auth_config, 'my.registry.net'),
- {'auth': 'privateuser'}
+ auth.resolve_authconfig(
+ self.auth_config, 'my.registry.net'
+ )['username'],
+ 'privateuser'
)
def test_resolve_authconfig_no_protocol(self):
self.assertEqual(
- auth.resolve_authconfig(self.auth_config, 'my.registry.net/v1/'),
- {'auth': 'privateuser'}
+ auth.resolve_authconfig(
+ self.auth_config, 'my.registry.net/v1/'
+ )['username'],
+ 'privateuser'
)
def test_resolve_authconfig_no_path(self):
self.assertEqual(
auth.resolve_authconfig(
self.auth_config, 'http://my.registry.net'
- ),
- {'auth': 'privateuser'}
+ )['username'],
+ 'privateuser'
)
def test_resolve_authconfig_no_path_trailing_slash(self):
self.assertEqual(
auth.resolve_authconfig(
self.auth_config, 'http://my.registry.net/'
- ),
- {'auth': 'privateuser'}
+ )['username'],
+ 'privateuser'
)
def test_resolve_authconfig_no_path_wrong_secure_proto(self):
self.assertEqual(
auth.resolve_authconfig(
self.auth_config, 'https://my.registry.net'
- ),
- {'auth': 'privateuser'}
+ )['username'],
+ 'privateuser'
)
def test_resolve_authconfig_no_path_wrong_insecure_proto(self):
self.assertEqual(
auth.resolve_authconfig(
self.auth_config, 'http://index.docker.io'
- ),
- {'auth': 'indexuser'}
+ )['username'],
+ 'indexuser'
)
def test_resolve_authconfig_path_wrong_proto(self):
self.assertEqual(
auth.resolve_authconfig(
self.auth_config, 'https://my.registry.net/v1/'
- ),
- {'auth': 'privateuser'}
+ )['username'],
+ 'privateuser'
)
def test_resolve_authconfig_default_registry(self):
self.assertEqual(
- auth.resolve_authconfig(self.auth_config), {'auth': 'indexuser'}
+ auth.resolve_authconfig(self.auth_config)['username'],
+ 'indexuser'
)
def test_resolve_authconfig_default_explicit_none(self):
self.assertEqual(
- auth.resolve_authconfig(self.auth_config, None),
- {'auth': 'indexuser'}
+ auth.resolve_authconfig(self.auth_config, None)['username'],
+ 'indexuser'
)
def test_resolve_authconfig_fully_explicit(self):
self.assertEqual(
auth.resolve_authconfig(
self.auth_config, 'http://my.registry.net/v1/'
- ),
- {'auth': 'privateuser'}
+ )['username'],
+ 'privateuser'
)
def test_resolve_authconfig_legacy_config(self):
self.assertEqual(
- auth.resolve_authconfig(self.auth_config, 'legacy.registry.url'),
- {'auth': 'legacyauth'}
+ auth.resolve_authconfig(
+ self.auth_config, 'legacy.registry.url'
+ )['username'],
+ 'legacyauth'
)
def test_resolve_authconfig_no_match(self):
@@ -198,8 +216,8 @@ class ResolveAuthTest(base.BaseTestCase):
self.assertEqual(
auth.resolve_authconfig(
self.auth_config, auth.resolve_repository_name(image)[0]
- ),
- {'auth': 'indexuser'},
+ )['username'],
+ 'indexuser',
)
def test_resolve_registry_and_auth_hub_image(self):
@@ -207,8 +225,9 @@ class ResolveAuthTest(base.BaseTestCase):
self.assertEqual(
auth.resolve_authconfig(
self.auth_config, auth.resolve_repository_name(image)[0]
- ),
- {'auth': 'indexuser'},
+ )['username'],
+ 'indexuser',
+ )
)
def test_resolve_registry_and_auth_private_registry(self):
@@ -216,8 +235,8 @@ class ResolveAuthTest(base.BaseTestCase):
self.assertEqual(
auth.resolve_authconfig(
self.auth_config, auth.resolve_repository_name(image)[0]
- ),
- {'auth': 'privateuser'},
+ )['username'],
+ 'privateuser',
)
def test_resolve_registry_and_auth_unauthenticated_registry(self):