def test_adding_and_removing_users(self): """Tests adding and removing users.""" auag = UsersAndGroups() auag.add_user(User("user1")) auag.add_user(User("user2")) self.assertTrue(auag.has_user("user1")) self.assertFalse(auag.has_user("user6")) self.assertEqual(auag.number_users(), 2) auag.remove_user("user1") self.assertFalse(auag.has_user("user1")) self.assertEqual(auag.number_users(), 1) self.assertTrue(auag.has_user("user2")) u = auag.get_user("user2") self.assertTrue(u.name, "user2") self.assertIsNone(auag.get_user("noone"))
def test_duplicate_users(self): """Tests creating duplicate users with different flags.""" auag = UsersAndGroups() # create a duplicate with default flag to raise an error. auag.add_user(User(name="user1")) with self.assertRaises(Exception): auag.add_user(User(name="user1")) # create with overwrite. auag.add_user( User(name="user2", mail="*****@*****.**", group_names=["group2"]), duplicate=UsersAndGroups.OVERWRITE_ON_DUPLICATE, ) u = auag.get_user("user2") self.assertEqual(u.name, "user2") self.assertEqual(u.mail, "*****@*****.**") self.assertEqual(u.groupNames, ["group2"]) auag.add_user( User(name="user2", mail="*****@*****.**", group_names=["group3"]), duplicate=UsersAndGroups.OVERWRITE_ON_DUPLICATE, ) u = auag.get_user("user2") self.assertEqual(u.name, "user2") self.assertEqual(u.mail, "*****@*****.**") self.assertEqual(u.groupNames, ["group3"]) # create with update. auag.add_user( User(name="user3", mail="*****@*****.**", group_names=["group2"]), duplicate=UsersAndGroups.UPDATE_ON_DUPLICATE, ) u = auag.get_user("user3") self.assertEqual(u.name, "user3") self.assertEqual(u.mail, "*****@*****.**") self.assertEqual(u.groupNames, ["group2"]) auag.add_user( User(name="user3", mail="*****@*****.**", group_names=["group3"]), duplicate=UsersAndGroups.UPDATE_ON_DUPLICATE, ) u = auag.get_user("user3") self.assertEqual(u.mail, "*****@*****.**") self.assertEqual(u.groupNames, ["group3", "group2"]) # create with ignore. auag.add_user( User(name="user4", mail="*****@*****.**", group_names=["group2"]), duplicate=UsersAndGroups.IGNORE_ON_DUPLICATE, ) u = auag.get_user("user4") self.assertEqual(u.name, "user4") self.assertEqual(u.mail, "*****@*****.**") self.assertEqual(u.groupNames, ["group2"]) auag.add_user( User(name="user4", mail="*****@*****.**", group_names=["group3"]), duplicate=UsersAndGroups.IGNORE_ON_DUPLICATE, ) u = auag.get_user("user4") self.assertEqual(u.name, "user4") self.assertEqual(u.mail, "*****@*****.**") self.assertEqual(u.groupNames, ["group2"])