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_unique_enrollments(self):
"""Check if there is only one tuple with the same values"""
with self.assertRaisesRegex(IntegrityError, DUP_CHECK_ERROR):
uid = UniqueIdentity(uuid='John Smith')
self.session.add(uid)
org = Organization(name='Example')
self.session.add(org)
rol1 = Enrollment(uidentity=uid, organization=org)
rol2 = Enrollment(uidentity=uid, organization=org)
self.session.add(rol1)
self.session.add(rol2)
self.session.commit()
def test_match_with_sources_list(self):
"""Test match when a list of sources to filter is given"""
jsmith = UniqueIdentity(uuid='jsmith')
jsmith.identities = [Identity(name='John Smith', email='jsmith@example.com', source='scm'),
Identity(name='John Smith', source='scm'),
Identity(username='jsmith', source='scm'),
Identity(email='', source='scm')]
jsmith_alt = UniqueIdentity(uuid='J. Smith')
jsmith_alt.identities = [Identity(name='John Smith JR', email='jsmith@example.com', source='alt'),
Identity(name='John Smith', username='jsmith', source='alt'),
Identity(email='', source='alt'),
Identity(email='jsmith', source='alt')]
# With these lists there are not matches
matcher = EmailMatcher(sources=['github'])
result = matcher.match(jsmith, jsmith_alt)
self.assertEqual(result, False)
matcher = EmailMatcher(sources=['scm'])
result = matcher.match(jsmith, jsmith_alt)
self.assertEqual(result, False)
# Only when scm and alt are combined there is a match
matcher = EmailMatcher(sources=['scm', 'alt'])
def test_match_same_uuid(self):
"""Test if there is a match when compares identities with the same UUID"""
uid1 = UniqueIdentity(uuid='John Smith')
uid2 = UniqueIdentity(uuid='John Smith')
matcher = EmailMatcher()
result = matcher.match(uid1, uid2)
self.assertEqual(result, True)
result = matcher.match(uid2, uid1)
self.assertEqual(result, True)
uid1 = UniqueIdentity(uuid=None)
uid2 = UniqueIdentity(uuid=None)
result = matcher.match(uid1, uid2)
self.assertEqual(result, False)
def test_match_with_sources_list(self):
"""Test match when a list of sources to filter is given"""
jsmith = UniqueIdentity(uuid='jsmith')
jsmith.identities = [Identity(name='John Smith', email='jsmith@example.com', source='scm', uuid='jsmith'),
Identity(name='John Smith', source='scm', uuid='jsmith'),
Identity(username='jsmith', source='github', uuid='jsmith'),
Identity(email='', source='scm', uuid='jsmith')]
john_smith = UniqueIdentity(uuid='js')
john_smith.identities = [Identity(name='J. Smith', username='john_smith', source='scm'),
Identity(username='jsmith', source='GitHub-API'),
Identity(name='Smith. J', source='mls'),
Identity(name='Smith. J', email='JSmith@example.com', source='mls')]
# With these lists there are not matches
matcher = GitHubMatcher(sources=['scm'])
result = matcher.match(jsmith, john_smith)
self.assertEqual(result, False)
matcher = GitHubMatcher(sources=['github'])
result = matcher.match(jsmith, john_smith)
self.assertEqual(result, False)
# Only when github-api and github are combined there is a match
matcher = GitHubMatcher(sources=['github-api', 'github'])
def test_match(self):
"""Test match method"""
# Let's define some identities first
jsmith = UniqueIdentity(uuid='jsmith')
jsmith.identities = [Identity(name='John Smith', email='jsmith@example.com', source='scm'),
Identity(name='John Smith', source='scm'),
Identity(username='jsmith', source='scm'),
Identity(email='', source='scm')]
john_smith = UniqueIdentity(uuid='js')
john_smith.identities = [Identity(name='J. Smith', username='john_smith', source='scm'),
Identity(username='john_smith', source='scm'),
Identity(name='Smith. J', source='mls'),
Identity(name='Smith. J', email='JSmith@example.com', source='mls')]
jsmith_alt = UniqueIdentity(uuid='J. Smith')
jsmith_alt.identities = [Identity(name='J. Smith', username='john_smith', source='alt'),
Identity(name='John Smith', username='jsmith', source='alt'),
Identity(email='', source='alt'),
Identity(email='jsmith', source='alt')]
jsmith_not_email = UniqueIdentity(uuid='John Smith')
jsmith_not_email.identities = [Identity(email='jsmith', source='mls')]
jrae = UniqueIdentity(uuid='jrae')
jrae.identities = [Identity(name='Jane Rae', source='scm'),
def test_filter_identities_with_blacklist(self):
"""Test if identities are filtered when there is a blacklist"""
# Let's define some identities first
jsmith = UniqueIdentity(uuid='jsmith')
jsmith.identities = [Identity(name='John Smith', email='jsmith@example.com', source='scm', uuid='jsmith'),
Identity(name='John Smith', source='scm', uuid='jsmith'),
Identity(name='John Smith JR', source='scm', uuid='jsmith'),
Identity(username='jsmith', source='scm', uuid='jsmith'),
Identity(email='', source='scm', uuid='jsmith')]
jrae = UniqueIdentity(uuid='jrae')
jrae.identities = [Identity(name='Jane Rae', source='scm', uuid='jrae'),
Identity(name='Jane Rae Doe', email='jane.rae@example.net', source='mls', uuid='jrae'),
Identity(name='jrae', source='scm', uuid='jrae'),
Identity(email='JRAE@example.net', source='scm', uuid='jrae')]
# Tests
bl = [MatchingBlacklist(excluded='John Smith'),
MatchingBlacklist(excluded='jrae@example.net')]
def test_last_modified(self):
"""Check if last modification date is updated"""
uuid = '1234567890ABCDFE'
with self.db.connect() as session:
uidentity = UniqueIdentity(uuid=uuid)
session.add(uidentity)
before_dt = datetime.datetime.utcnow()
with self.db.connect() as session:
uidentity = api.find_unique_identity(session, uuid)
api.add_identity(session, uidentity, uuid, 'scm',
name='John Smith',
email='jsmith@example.org',
username='jsmith')
after_dt = datetime.datetime.utcnow()
with self.db.connect() as session:
identity = api.find_identity(session, uuid)
self.assertIsInstance(identity.last_modified, datetime.datetime)
# we want the KeyError if name is missing
name = yid['profile']['name']
is_bot = profile.get('is_bot', False)
emails = yid.get('email', None)
if emails and self.email_validation:
self.__validate_email(emails[0])
enrollments = yid.get('enrollments', None)
uuid = str(yid_counter)
yid_counter += 1
uid = UniqueIdentity(uuid=uuid)
prf = Profile(name=name, is_bot=is_bot)
uid.profile = prf
# now it is time to add the identities for name, emails and backends
sh_identities = __create_sh_identities(name, emails, yid)
uid.identities += sh_identities
if enrollments:
affiliations = self.__parse_affiliations_yml(enrollments)
uid.enrollments += affiliations
self._identities[uuid] = uid
except KeyError as e:
error = "Attribute %s not found" % e.args
msg = self.GRIMOIRELAB_INVALID_FORMAT % {'error': error}
else:
identity = Identity(username=alias, source=self.source)
uid.identities.append(identity)
# Create unique identities from enrollments list
for email in self.__raw_identities:
# Do we have it from aliases?
if email in self._identities:
uid = self._identities[email]
elif email in self.__raw_aliases:
canonical = self.__raw_aliases[email]
uid = self._identities[canonical]
else:
uid = UniqueIdentity(uuid=email)
identity = Identity(email=email, source=self.source)
uid.identities.append(identity)
self._identities[email] = uid
# Assign enrollments
enrs = self.__raw_identities[email]
enrs.sort(key=lambda r: r[1])
start_date = MIN_PERIOD_DATE
for rol in enrs:
name = rol[0]
org = self._organizations.get(name, None)
]
}
:parse data: JSON object to parse
:raise InvalidFormatError: raised when the format of the JSON is
not valid.
"""
try:
for mozillian in json['results']:
name = self.__encode(mozillian['full_name']['value'])
email = self.__encode(mozillian['email']['value'])
username = self.__encode(mozillian['username'])
uuid = username
uid = UniqueIdentity(uuid=uuid)
identity = Identity(name=name, email=email, username=username,
source=self.source, uuid=uuid)
uid.identities.append(identity)
# Alternate emails
for alt_email in mozillian['alternate_emails']:
alt_email = self.__encode(alt_email['email'])
if alt_email == email:
continue
identity = Identity(name=name, email=alt_email, username=username,
source=self.source, uuid=uuid)
uid.identities.append(identity)
# IRC account