fix(136521333): Handle None email_or_id in avatar code

Fixes https://www.pivotaltracker.com/story/show/136521333
This commit is contained in:
Joseph Schorr 2016-12-21 15:00:55 -05:00
parent 732ab67b57
commit ef80471a39
3 changed files with 39 additions and 12 deletions

View file

@ -77,7 +77,11 @@ class BaseAvatar(object):
} }
""" """
colors = self.colors colors = self.colors
hash_value = hashlib.md5(email_or_id.strip().lower()).hexdigest()
# Note: email_or_id may be None if gotten from external auth when email is disabled,
# so use the username in that case.
username_email_or_id = email_or_id or name
hash_value = hashlib.md5(username_email_or_id.strip().lower()).hexdigest()
byte_count = int(math.ceil(math.log(len(colors), 16))) byte_count = int(math.ceil(math.log(len(colors), 16)))
byte_data = hash_value[0:byte_count] byte_data = hash_value[0:byte_count]

View file

@ -48,10 +48,22 @@ class TestJWTEndToEnd(ApiTestCase, EndToEndAuthMixin):
def get_authentication(self): def get_authentication(self):
return fake_jwt() return fake_jwt()
class TestKeystoneEndToEnd(ApiTestCase, EndToEndAuthMixin): class TestKeystone3EndToEnd(ApiTestCase, EndToEndAuthMixin):
def get_authentication(self): def get_authentication(self):
return fake_keystone(3) return fake_keystone(3)
class TestLDAPNoEmailEndToEnd(ApiTestCase, EndToEndAuthMixin):
def get_authentication(self):
return mock_ldap(requires_email=False)
class TestJWTNoEmailEndToEnd(ApiTestCase, EndToEndAuthMixin):
def get_authentication(self):
return fake_jwt(requires_email=False)
class TestKeystone3NoEmailEndToEnd(ApiTestCase, EndToEndAuthMixin):
def get_authentication(self):
return fake_keystone(3, requires_email=False)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

View file

@ -24,7 +24,7 @@ def _create_ldap(requires_email=True):
@contextmanager @contextmanager
def mock_ldap(requires_email=True): def mock_ldap(requires_email=True):
mockldap = MockLdap({ mock_data = {
'dc=quay,dc=io': {'dc': ['quay', 'io']}, 'dc=quay,dc=io': {'dc': ['quay', 'io']},
'ou=employees,dc=quay,dc=io': { 'ou=employees,dc=quay,dc=io': {
'dc': ['quay', 'io'], 'dc': ['quay', 'io'],
@ -84,20 +84,31 @@ def mock_ldap(requires_email=True):
'userPassword': ['somepass'], 'userPassword': ['somepass'],
'mail': ['foosecondary@bar.com'] 'mail': ['foosecondary@bar.com']
}, },
}) }
if not requires_email:
for path in mock_data:
mock_data[path].pop('mail', None)
mockldap = MockLdap(mock_data)
def initializer(uri, trace_level=0): def initializer(uri, trace_level=0):
obj = mockldap[uri] obj = mockldap[uri]
# Seed to "support" wildcard queries, which MockLDAP does not support natively. # Seed to "support" wildcard queries, which MockLDAP does not support natively.
obj.search_s.seed('ou=employees,dc=quay,dc=io', 2, '(|(uid=cool*)(mail=cool*))')([ cool_block = {
('uid=cool.user,ou=employees,dc=quay,dc=io', {
'dc': ['quay', 'io'], 'dc': ['quay', 'io'],
'ou': 'employees', 'ou': 'employees',
'uid': ['cool.user', 'referred'], 'uid': ['cool.user', 'referred'],
'userPassword': ['somepass'], 'userPassword': ['somepass'],
'mail': ['foo@bar.com'] 'mail': ['foo@bar.com']
}) }
if not requires_email:
cool_block.pop('mail', None)
obj.search_s.seed('ou=employees,dc=quay,dc=io', 2, '(|(uid=cool*)(mail=cool*))')([
('uid=cool.user,ou=employees,dc=quay,dc=io', cool_block)
]) ])
obj.search_s.seed('ou=otheremployees,dc=quay,dc=io', 2, '(|(uid=cool*)(mail=cool*))')([]) obj.search_s.seed('ou=otheremployees,dc=quay,dc=io', 2, '(|(uid=cool*)(mail=cool*))')([])