Skip to content

Commit 9b890c4

Browse files
committed
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 <[email protected]>
1 parent caa64b3 commit 9b890c4

File tree

2 files changed

+52
-38
lines changed

2 files changed

+52
-38
lines changed

docker/auth/auth.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,6 @@ def convert_to_hostname(url):
8282
return url.replace('http://', '').replace('https://', '').split('/', 1)[0]
8383

8484

85-
def encode_auth(auth_info):
86-
return base64.b64encode(auth_info.get('username', '') + b':' +
87-
auth_info.get('password', ''))
88-
89-
9085
def decode_auth(auth):
9186
if isinstance(auth, six.string_types):
9287
auth = auth.encode('ascii')
@@ -121,7 +116,7 @@ def parse_auth(entries):
121116
conf[registry] = {
122117
'username': username,
123118
'password': password,
124-
'email': entry['email'],
119+
'email': entry.get('email'),
125120
'serveraddress': registry,
126121
}
127122
return conf

tests/unit/auth_test.py

Lines changed: 51 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import tempfile
1010

1111
from docker import auth
12+
from docker.auth.auth import parse_auth
1213
from docker import errors
1314

1415
from .. import base
@@ -104,88 +105,105 @@ def test_invalid_index_name(self):
104105
)
105106

106107

108+
def encode_auth(auth_info):
109+
return base64.b64encode(
110+
auth_info.get('username', '').encode('utf-8') + b':' +
111+
auth_info.get('password', '').encode('utf-8'))
112+
113+
107114
class ResolveAuthTest(base.BaseTestCase):
108-
auth_config = {
109-
'https://index.docker.io/v1/': {'auth': 'indexuser'},
110-
'my.registry.net': {'auth': 'privateuser'},
111-
'http://legacy.registry.url/v1/': {'auth': 'legacyauth'}
112-
}
115+
index_config = {'auth': encode_auth({'username': 'indexuser'})}
116+
private_config = {'auth': encode_auth({'username': 'privateuser'})}
117+
legacy_config = {'auth': encode_auth({'username': 'legacyauth'})}
118+
119+
auth_config = parse_auth({
120+
'https://index.docker.io/v1/': index_config,
121+
'my.registry.net': private_config,
122+
'http://legacy.registry.url/v1/': legacy_config,
123+
})
113124

114125
def test_resolve_authconfig_hostname_only(self):
115126
self.assertEqual(
116-
auth.resolve_authconfig(self.auth_config, 'my.registry.net'),
117-
{'auth': 'privateuser'}
127+
auth.resolve_authconfig(
128+
self.auth_config, 'my.registry.net'
129+
)['username'],
130+
'privateuser'
118131
)
119132

120133
def test_resolve_authconfig_no_protocol(self):
121134
self.assertEqual(
122-
auth.resolve_authconfig(self.auth_config, 'my.registry.net/v1/'),
123-
{'auth': 'privateuser'}
135+
auth.resolve_authconfig(
136+
self.auth_config, 'my.registry.net/v1/'
137+
)['username'],
138+
'privateuser'
124139
)
125140

126141
def test_resolve_authconfig_no_path(self):
127142
self.assertEqual(
128143
auth.resolve_authconfig(
129144
self.auth_config, 'http://my.registry.net'
130-
),
131-
{'auth': 'privateuser'}
145+
)['username'],
146+
'privateuser'
132147
)
133148

134149
def test_resolve_authconfig_no_path_trailing_slash(self):
135150
self.assertEqual(
136151
auth.resolve_authconfig(
137152
self.auth_config, 'http://my.registry.net/'
138-
),
139-
{'auth': 'privateuser'}
153+
)['username'],
154+
'privateuser'
140155
)
141156

142157
def test_resolve_authconfig_no_path_wrong_secure_proto(self):
143158
self.assertEqual(
144159
auth.resolve_authconfig(
145160
self.auth_config, 'https://my.registry.net'
146-
),
147-
{'auth': 'privateuser'}
161+
)['username'],
162+
'privateuser'
148163
)
149164

150165
def test_resolve_authconfig_no_path_wrong_insecure_proto(self):
151166
self.assertEqual(
152167
auth.resolve_authconfig(
153168
self.auth_config, 'http://index.docker.io'
154-
),
155-
{'auth': 'indexuser'}
169+
)['username'],
170+
'indexuser'
156171
)
157172

158173
def test_resolve_authconfig_path_wrong_proto(self):
159174
self.assertEqual(
160175
auth.resolve_authconfig(
161176
self.auth_config, 'https://my.registry.net/v1/'
162-
),
163-
{'auth': 'privateuser'}
177+
)['username'],
178+
'privateuser'
164179
)
165180

166181
def test_resolve_authconfig_default_registry(self):
167182
self.assertEqual(
168-
auth.resolve_authconfig(self.auth_config), {'auth': 'indexuser'}
183+
auth.resolve_authconfig(self.auth_config)['username'],
184+
'indexuser'
169185
)
170186

171187
def test_resolve_authconfig_default_explicit_none(self):
172188
self.assertEqual(
173-
auth.resolve_authconfig(self.auth_config, None),
174-
{'auth': 'indexuser'}
189+
auth.resolve_authconfig(self.auth_config, None)['username'],
190+
'indexuser'
175191
)
176192

177193
def test_resolve_authconfig_fully_explicit(self):
178194
self.assertEqual(
179195
auth.resolve_authconfig(
180196
self.auth_config, 'http://my.registry.net/v1/'
181-
),
182-
{'auth': 'privateuser'}
197+
)['username'],
198+
'privateuser'
183199
)
184200

185201
def test_resolve_authconfig_legacy_config(self):
186202
self.assertEqual(
187-
auth.resolve_authconfig(self.auth_config, 'legacy.registry.url'),
188-
{'auth': 'legacyauth'}
203+
auth.resolve_authconfig(
204+
self.auth_config, 'legacy.registry.url'
205+
)['username'],
206+
'legacyauth'
189207
)
190208

191209
def test_resolve_authconfig_no_match(self):
@@ -198,26 +216,27 @@ def test_resolve_registry_and_auth_library_image(self):
198216
self.assertEqual(
199217
auth.resolve_authconfig(
200218
self.auth_config, auth.resolve_repository_name(image)[0]
201-
),
202-
{'auth': 'indexuser'},
219+
)['username'],
220+
'indexuser',
203221
)
204222

205223
def test_resolve_registry_and_auth_hub_image(self):
206224
image = 'username/image'
207225
self.assertEqual(
208226
auth.resolve_authconfig(
209227
self.auth_config, auth.resolve_repository_name(image)[0]
210-
),
211-
{'auth': 'indexuser'},
228+
)['username'],
229+
'indexuser',
230+
)
212231
)
213232

214233
def test_resolve_registry_and_auth_private_registry(self):
215234
image = 'my.registry.net/image'
216235
self.assertEqual(
217236
auth.resolve_authconfig(
218237
self.auth_config, auth.resolve_repository_name(image)[0]
219-
),
220-
{'auth': 'privateuser'},
238+
)['username'],
239+
'privateuser',
221240
)
222241

223242
def test_resolve_registry_and_auth_unauthenticated_registry(self):

0 commit comments

Comments
 (0)