def test_delete(self): """Check whether everything works ok when deleting entries""" # First, add a set of entries self.cmd.add('*****@*****.**') self.cmd.add('John Smith') self.cmd.add('Bitergia') self.cmd.add('John Doe') # Delete an entry bl = api.blacklist(self.db, 'Bitergia') self.assertEqual(len(bl), 1) code = self.cmd.delete('Bitergia') self.assertEqual(code, CMD_SUCCESS) self.assertRaises(NotFoundError, api.blacklist, self.db, 'Bitergia') code = self.cmd.delete('*****@*****.**') self.assertEqual(code, CMD_SUCCESS) # The final content of the registry should have # two entries bl = api.blacklist(self.db) self.assertEqual(len(bl), 2) e = bl[0] self.assertEqual(e.excluded, 'John Doe') e = bl[1] self.assertEqual(e.excluded, 'John Smith')
def test_delete(self): """Check whether everything works ok when deleting entries""" # First, add a set of entries self.cmd.add('*****@*****.**') self.cmd.add('John Smith') self.cmd.add('Bitergia') self.cmd.add('John Doe') # Delete an entry bl = api.blacklist(self.db, 'Bitergia') self.assertEqual(len(bl), 1) code = self.cmd.delete('Bitergia') self.assertEqual(code, CMD_SUCCESS) self.assertRaises(NotFoundError, api.blacklist, self.db, 'Bitergia') code = self.cmd.delete('*****@*****.**') self.assertEqual(code, CMD_SUCCESS) # The final content of the registry should have # two entries bl = api.blacklist(self.db) self.assertEqual(len(bl), 2) e = bl[0] self.assertEqual(e.excluded, 'John Doe') e = bl[1] self.assertEqual(e.excluded, 'John Smith')
def test_empty_entry(self): """Check behavior adding empty organizations""" code = self.cmd.add('') self.assertEqual(code, CMD_SUCCESS) output = sys.stdout.getvalue().strip() self.assertEqual(output, BLACKLIST_EMPTY_OUTPUT) # The blacklist should be empty bl = api.blacklist(self.db) self.assertEqual(len(bl), 0)
def test_empty_entry(self): """Check behavior adding empty organizations""" code = self.cmd.add('') self.assertEqual(code, CMD_SUCCESS) output = sys.stdout.getvalue().strip() self.assertEqual(output, BLACKLIST_EMPTY_OUTPUT) # The blacklist should be empty bl = api.blacklist(self.db) self.assertEqual(len(bl), 0)
def test_valid_file(self): """Check insertion of valid data from a file""" parser = self.get_parser(datadir('sortinghat_valid.json')) self.cmd.import_blacklist(parser) # Check the contents of the registry bl = api.blacklist(self.db) self.assertEqual(len(bl), 2) # John Smith b = bl[0] self.assertEqual(b.excluded, 'John Smith') b = bl[1] self.assertEqual(b.excluded, '*****@*****.**')
def test_valid_file(self): """Check insertion of valid data from a file""" parser = self.get_parser(datadir('sortinghat_valid.json')) self.cmd.import_blacklist(parser) # Check the contents of the registry bl = api.blacklist(self.db) self.assertEqual(len(bl), 2) # John Smith b = bl[0] self.assertEqual(b.excluded, 'John Smith') b = bl[1] self.assertEqual(b.excluded, '*****@*****.**')
def test_not_found_entry(self): """Check if it fails removing an entry that does not exists""" # It should print an error when the blacklist is empty code = self.cmd.delete('*****@*****.**') self.assertEqual(code, CODE_NOT_FOUND_ERROR) output = sys.stderr.getvalue().strip().split('\n')[0] self.assertEqual(output, BLACKLIST_NOT_FOUND_ERROR) # Add a pair of entries to check delete with a blacklist # with contents self.cmd.add('John Smith') self.cmd.add('*****@*****.**') # The error should be the same code = self.cmd.delete('*****@*****.**') self.assertEqual(code, CODE_NOT_FOUND_ERROR) output = sys.stderr.getvalue().strip().split('\n')[1] self.assertEqual(output, BLACKLIST_NOT_FOUND_ERROR) # Nothing has been deleted from the registry bl = api.blacklist(self.db) self.assertEqual(len(bl), 2)
def test_not_found_entry(self): """Check if it fails removing an entry that does not exists""" # It should print an error when the blacklist is empty code = self.cmd.delete('*****@*****.**') self.assertEqual(code, CODE_NOT_FOUND_ERROR) output = sys.stderr.getvalue().strip().split('\n')[0] self.assertEqual(output, BLACKLIST_NOT_FOUND_ERROR) # Add a pair of entries to check delete with a blacklist # with contents self.cmd.add('John Smith') self.cmd.add('*****@*****.**') # The error should be the same code = self.cmd.delete('*****@*****.**') self.assertEqual(code, CODE_NOT_FOUND_ERROR) output = sys.stderr.getvalue().strip('\n').split('\n')[-1] self.assertEqual(output, BLACKLIST_NOT_FOUND_ERROR) # Nothing has been deleted from the registry bl = api.blacklist(self.db) self.assertEqual(len(bl), 2)
def add_identities(cls, db, identities, backend): """ Load identities list from backend in Sorting Hat """ merge_identities = False logger.info("Adding the identities to SortingHat") if not merge_identities: logger.info("Not doing identities merge") total = 0 lidentities = len(identities) if merge_identities: merged_identities = [] # old identities merged into new ones blacklist = api.blacklist(db) matching = 'email-name' # Not active matcher = create_identity_matcher(matching, blacklist) for identity in identities: try: uuid = api.add_identity(db, backend, identity['email'], identity['name'], identity['username']) logger.debug("New sortinghat identity %s %s,%s,%s (%i/%i)" % \ (uuid, identity['username'], identity['name'], identity['email'], total, lidentities)) total += 1 if not merge_identities: continue # Don't do the merge here. Too slow in large projects # Time to merge matches = api.match_identities(db, uuid, matcher) if len(matches) > 1: u = api.unique_identities(db, uuid)[0] for m in matches: # First add the old uuid to the list of changed by merge uuids if m.uuid not in merged_identities: merged_identities.append(m.uuid) if m.uuid == uuid: continue # Merge matched identity into added identity api.merge_unique_identities(db, m.uuid, u.uuid) # uuid = m.uuid # u = api.unique_identities(db, uuid, backend)[0] # Include all identities related to this uuid # merged_identities.append(m.uuid) except AlreadyExistsError as ex: uuid = ex.uuid continue except WrappedValueError as ex: logging.warning("Trying to add a None identity. Ignoring it.") continue except UnicodeEncodeError as ex: logging.warning("UnicodeEncodeError. Ignoring it. %s %s %s" % \ (identity['email'], identity['name'], identity['username'])) continue except Exception as ex: logging.warning("Unknown exception adding identity. Ignoring it. %s %s %s" % \ (identity['email'], identity['name'], identity['username'])) continue if 'company' in identity and identity['company'] is not None: try: api.add_organization(db, identity['company']) api.add_enrollment(db, uuid, identity['company'], datetime(1900, 1, 1), datetime(2100, 1, 1)) except AlreadyExistsError: pass logger.info("Total NEW identities: %i" % (total)) if merge_identities: logger.info("Total NEW identities merged: %i" % \ (len(merged_identities))) return merged_identities else: return []
def add_identities(cls, db, identities, backend): """ Load identities list from backend in Sorting Hat """ merge_identities = False logger.info("Adding the identities to SortingHat") if not merge_identities: logger.info("Not doing identities merge") total = 0 lidentities = len(identities) if merge_identities: merged_identities = [] # old identities merged into new ones blacklist = api.blacklist(db) matching = 'email-name' # Not active matcher = create_identity_matcher(matching, blacklist) for identity in identities: try: uuid = api.add_identity(db, backend, identity['email'], identity['name'], identity['username']) logger.debug("New sortinghat identity %s %s,%s,%s (%i/%i)", uuid, identity['username'], identity['name'], identity['email'], total, lidentities) profile = {"name": identity['name'] if identity['name'] else identity['username'], "email": identity['email']} api.edit_profile(db, uuid, **profile) total += 1 if not merge_identities: continue # Don't do the merge here. Too slow in large projects # Time to merge matches = api.match_identities(db, uuid, matcher) if len(matches) > 1: u = api.unique_identities(db, uuid)[0] for m in matches: # First add the old uuid to the list of changed by merge uuids if m.uuid not in merged_identities: merged_identities.append(m.uuid) if m.uuid == uuid: continue # Merge matched identity into added identity api.merge_unique_identities(db, m.uuid, u.uuid) # uuid = m.uuid # u = api.unique_identities(db, uuid, backend)[0] # Include all identities related to this uuid # merged_identities.append(m.uuid) except AlreadyExistsError as ex: uuid = ex.uuid continue except WrappedValueError as ex: logging.warning("Trying to add a None identity. Ignoring it.") continue except UnicodeEncodeError as ex: logging.warning("UnicodeEncodeError. Ignoring it. %s %s %s" % \ (identity['email'], identity['name'], identity['username'])) continue except Exception as ex: logging.warning("Unknown exception adding identity. Ignoring it. %s %s %s" % \ (identity['email'], identity['name'], identity['username'])) traceback.print_exc() continue if 'company' in identity and identity['company'] is not None: try: api.add_organization(db, identity['company']) api.add_enrollment(db, uuid, identity['company'], datetime(1900, 1, 1), datetime(2100, 1, 1)) except AlreadyExistsError: pass logger.info("Total NEW identities: %i" % (total)) if merge_identities: logger.info("Total NEW identities merged: %i" % \ (len(merged_identities))) return merged_identities else: return []