Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_equal_related_unique_identity(self):
"""Test that all remains the same when to_uuid is the unique identity related to 'from_id'"""
# Add some unique identities and identities first
api.add_unique_identity(self.db, 'John Smith')
from_id = api.add_identity(self.db, 'scm', 'jsmith@example.com',
uuid='John Smith')
api.add_unique_identity(self.db, 'John Doe')
api.add_identity(self.db, 'scm', 'jdoe@example.com',
uuid='John Doe')
new_uuid = api.add_identity(self.db, 'scm', 'john.doe@example.com',
uuid='John Doe')
# Move the identity to the same unique identity
api.move_identity(self.db, from_id, 'John Smith')
# Nothing has happened
with self.db.connect() as session:
uidentities = session.query(UniqueIdentity).all()
self.assertEqual(len(uidentities), 2)
def load_test_dataset(self):
# Add some unique identities
uuid = api.add_identity(self.db, source='scm', email='jsmith@example.com', name='John Smith')
api.add_identity(self.db, source='scm', name='John Smith', uuid=uuid)
api.add_identity(self.db, source='scm', username='jsmith', uuid=uuid)
uuid = api.add_identity(self.db, source='alt', name='J. Smith', username='john_smith')
api.add_identity(self.db, source='alt', name='John Smith', username='jsmith', uuid=uuid)
api.add_identity(self.db, source='alt', email='jsmith', uuid=uuid)
uuid = api.add_identity(self.db, source='scm', name='Jane Rae')
api.add_identity(self.db, source='mls', email='jane.rae@example.net', name='Jane Rae Doe', uuid=uuid)
uuid = api.add_identity(self.db, source='scm', name='J. Smith', username='john_smith')
api.add_identity(self.db, source='scm', username='john_smith', uuid=uuid)
api.add_identity(self.db, source='mls', username='Smith. J', uuid=uuid)
api.add_identity(self.db, source='mls', email='JSmith@example.com', name='Smith. J', uuid=uuid)
uuid = api.add_identity(self.db, source='mls', email='jrae@example.net', name='Jane Rae Doe')
api.add_identity(self.db, source='mls', name='jrae', uuid=uuid)
uuid = api.add_identity(self.db, source='scm', name='jrae')
api.add_unique_identity(self.db, 'Smith J.')
api.add_identity(self.db, 'mls', 'JSmith@example.com',
uuid='Smith J.')
api.add_unique_identity(self.db, 'Jane Rae')
api.add_identity(self.db, 'scm', 'janerae@example.com', 'Jane Rae', uuid='Jane Rae')
api.add_unique_identity(self.db, 'JRae')
api.add_identity(self.db, 'mls', name='Jane Rae', username='jrae', uuid='JRae')
api.add_identity(self.db, 'scm', username='jrae', uuid='JRae')
api.add_identity(self.db, 'scm', 'janerae@example.com', uuid='JRae')
api.add_unique_identity(self.db, 'Jane')
api.add_identity(self.db, 'unknown', 'jane@example.com', 'Jane', uuid='Jane')
api.add_identity(self.db, 'unknown', 'jane@example.net', 'Jane', uuid='Jane')
api.add_identity(self.db, 'unknown', 'jane@example.org', 'Jane', uuid='Jane')
api.add_identity(self.db, 'unknown', 'jrae@example.org', 'Jane', uuid='Jane')
api.add_identity(self.db, 'unknown', 'jrae@example.com', 'Jane', uuid='Jane')
api.add_identity(self.db, 'unknown', 'janerae@example.com', 'Jane', uuid='Jane')
# Tests
get_uuids = lambda l: [u.uuid for u in l]
matcher = create_identity_matcher('default')
# John Smith
m1 = api.match_identities(self.db, 'John Smith', matcher)
uids = get_uuids(m1)
self.assertListEqual(uids, ['Smith J.'])
# Smith J.
api.add_domain(self.db, 'Bitergia', 'bitergia.com')
api.add_domain(self.db, 'Bitergia', 'bitergia.org')
api.add_organization(self.db, 'LibreSoft')
# Add some unique identities
jsmith_uuid = api.add_identity(self.db, 'scm', 'jsmith@us.example.com',
'John Smith', 'jsmith')
api.add_identity(self.db, 'scm', 'jsmith@example.net', 'John Smith',
uuid=jsmith_uuid)
api.add_identity(self.db, 'scm', 'jsmith@bitergia.com', 'John Smith', 'jsmith',
uuid=jsmith_uuid)
api.add_enrollment(self.db, jsmith_uuid, 'Bitergia')
# Add John Doe identity
api.add_identity(self.db, 'unknown', None, 'John Doe', 'jdoe')
# Add Jane Rae identity
jroe_uuid = api.add_identity(self.db, 'scm', 'jroe@example.com',
'Jane Roe', 'jroe')
api.add_identity(self.db, 'scm', 'jroe@example.com',
uuid=jroe_uuid)
api.add_identity(self.db, 'unknown', 'jroe@bitergia.com',
uuid=jroe_uuid)
# Add no valid email identity
api.add_identity(self.db, 'test', 'novalidemail@')
def test_existing_identity(self):
"""Check if it fails adding an identity that already exists"""
# Add a pair of identities first
api.add_identity(self.db, 'scm', 'jsmith@example.com')
api.add_identity(self.db, 'scm', 'jdoe@example.com')
# Insert the first identity again. It should raise AlreadyExistsError
with self.assertRaises(AlreadyExistsError) as context:
api.add_identity(self.db, 'scm', 'jsmith@example.com')
self.assertEqual(context.exception.uuid,
'334da68fcd3da4e799791f73dfada2afb22648c6')
# Insert the same identity with upper case letters.
# It should raise AlreadyExistsError
with self.assertRaises(AlreadyExistsError) as context:
api.add_identity(self.db, 'scm', 'JSMITH@example.com')
self.assertEqual(context.exception.uuid,
'334da68fcd3da4e799791f73dfada2afb22648c6')
def test_default_matcher(self):
"""Test default identity matcher"""
# Add some unique identities first
api.add_unique_identity(self.db, 'John Smith')
api.add_identity(self.db, 'scm', 'jsmith@example.com',
uuid='John Smith')
api.add_identity(self.db, 'scm', name='John Smith', uuid='John Smith')
api.add_identity(self.db, 'scm', username='jsmith', uuid='John Smith')
api.add_unique_identity(self.db, 'John Doe')
api.add_identity(self.db, 'mls', 'johndoe@example.com', uuid='John Doe')
api.add_identity(self.db, 'mls', 'johndoe@example.net', uuid='John Doe')
api.add_unique_identity(self.db, 'Smith J.')
api.add_identity(self.db, 'mls', 'JSmith@example.com',
uuid='Smith J.')
api.add_unique_identity(self.db, 'Jane Rae')
api.add_identity(self.db, 'scm', 'janerae@example.com', 'Jane Rae', uuid='Jane Rae')
api.add_unique_identity(self.db, 'JRae')
api.add_identity(self.db, 'mls', name='Jane Rae', username='jrae', uuid='JRae')
api.add_identity(self.db, 'scm', username='jrae', uuid='JRae')
api.add_identity(self.db, 'scm', 'janerae@example.com', uuid='JRae')
api.add_unique_identity(self.db, 'Jane')
api.add_identity(self.db, 'unknown', 'jane@example.com', 'Jane', uuid='Jane')
api.add_identity(self.db, 'unknown', 'jane@example.net', 'Jane', uuid='Jane')
api.add_identity(self.db, 'unknown', 'jane@example.org', 'Jane', uuid='Jane')
api.add_identity(self.db, 'unknown', 'jrae@example.org', 'Jane', uuid='Jane')
api.add_identity(self.db, 'unknown', 'jrae@example.com', 'Jane', uuid='Jane')
self.assertEqual(context.exception.uuid,
'334da68fcd3da4e799791f73dfada2afb22648c6')
# Insert the same identity with upper case letters.
# It should raise AlreadyExistsError
with self.assertRaises(AlreadyExistsError) as context:
api.add_identity(self.db, 'scm', 'JSMITH@example.com')
self.assertEqual(context.exception.uuid,
'334da68fcd3da4e799791f73dfada2afb22648c6')
# "None" tuples also raise an exception
api.add_identity(self.db, 'scm', "None", None, None)
with self.assertRaises(AlreadyExistsError) as context:
api.add_identity(self.db, 'scm', None, "None", None)
self.assertEqual(context.exception.uuid,
'f0999c4eed908d33365fa3435d9686d3add2412d')
def load_test_dataset(self):
# Add country
with self.db.connect() as session:
# Add a country
us = Country(code='US', name='United States of America', alpha3='USA')
session.add(us)
api.add_unique_identity(self.db, 'John Smith')
api.add_identity(self.db, 'scm', 'jsmith@example.com',
uuid='John Smith')
api.add_identity(self.db, 'scm', 'jsmith@example.com', 'John Smith',
uuid='John Smith')
api.edit_profile(self.db, 'John Smith', name='John Smith', is_bot=False)
api.add_unique_identity(self.db, 'John Doe')
api.add_identity(self.db, 'scm', 'jdoe@example.com',
uuid='John Doe')
api.edit_profile(self.db, 'John Doe', email='jdoe@example.com', is_bot=True,
country_code='US')
api.add_organization(self.db, 'Example')
api.add_enrollment(self.db, 'John Smith', 'Example')
api.add_enrollment(self.db, 'John Doe', 'Example')
api.add_organization(self.db, 'Bitergia')
api.add_enrollment(self.db, 'John Smith', 'Bitergia')
api.add_enrollment(self.db, 'John Doe', 'Bitergia',
def __load_identities(self, identities, uuid, verbose):
"""Store identities"""
self.log("-- loading identities", verbose)
for identity in identities:
try:
api.add_identity(self.db, identity.source, identity.email,
identity.name, identity.username, uuid)
self.new_uids.add(uuid)
except AlreadyExistsError as e:
self.warning(str(e), verbose)
with self.db.connect() as session:
stored_identity = find_identity(session, e.eid)
stored_uuid = stored_identity.uuid
if uuid != stored_uuid:
msg = "%s is already assigned to %s. Merging." % (uuid, stored_uuid)
self.warning(msg, verbose)
api.merge_unique_identities(self.db, uuid, stored_uuid)
if uuid in self.new_uids: