def test_syncing_user_and_groups_without_password(self): """ Tests adding users and groups to ThoughtSpot. """ auag = UsersAndGroups() auag.add_group( Group( name="Group 1", display_name="This is Group 1", description="A group for testing.", group_names=[], ) ) auag.add_group( Group( name="Group 2", display_name="This is Group 2", description="Another group for testing.", group_names=["Group 1"], ) ) auag.add_user( User( name="User1", password="******", display_name="User 1", mail="*****@*****.**", group_names=["Group 1"], ) ) auag.add_user( User( name="User2", password="******", mail="*****@*****.**", display_name="User 2", group_names=["Group 1", "Group 2"], ) ) sync = SyncUserAndGroups( tsurl=TS_URL, username=TS_USER, password=TS_PASSWORD, global_password="******", disable_ssl=True, ) sync.sync_users_and_groups(auag)
def get_users_and_groups(self, args): """ Called by the app to get users and groups. :param args: Passed in arguments. :type args: argparse.Namespace :return: Users and groups that were read. :rtype: UsersAndGroups """ ugs = UsersAndGroups() with open(args.input_filename) as csvfile: reader = csv.DictReader(csvfile) for row in reader: # create user and add to groups. ugs.add_user( User(name=row['name'], password=row['password'], mail=row['mail'], visibility=row['visibility'], group_names=row['groups'])) # add the groups ugs.add_group( Group(name=row['groups'], description=row['groups'], visibility=row['visibility'])) return ugs
def test_to_json(self): """Tests converting to JSON""" auag = UsersAndGroups() auag.add_group(Group("group1")) auag.add_group(Group("group2", group_names=["group1"])) auag.add_user(User("user1", group_names=["group1"])) auag.add_user(User("user2", group_names=["group1", "group2"])) json_str = auag.to_json() self.assertTrue(json_str.startswith("[{ ")) self.assertTrue(json_str.endswith("}]")) self.assertTrue('"name":"user1"' in json_str) self.assertTrue('"name":"user2"' in json_str) self.assertTrue('"name":"group1"' in json_str) self.assertTrue('"name":"group2"' in json_str)
def test_duplicate_users(self): """Tests adding users with the same name, but duplicate in case.""" auag = UsersAndGroups() auag.add_user(User("user1")) with self.assertRaises(Exception): auag.add_user(User("user1")) with self.assertRaises(Exception): auag.add_user(User("User1")) self.assertEqual(auag.number_users(), 1) self.assertTrue(auag.has_user("user1")) self.assertTrue(auag.has_user("User1")) auag.remove_user("user1") self.assertFalse(auag.has_user("user1")) self.assertEqual(auag.number_users(), 0)
def test_is_valid(self): """Tests validating users and groups.""" auag = UsersAndGroups() auag.add_group(Group("group1")) auag.add_group(Group("group2", group_names=["group1"])) auag.add_user(User("user1", mail="*****@*****.**", group_names=["group1"])) auag.add_user(User("user2", mail="*****@*****.**", group_names=["group1", "group2"])) results = auag.is_valid() self.assertTupleEqual((results.result, results.issues), (True, [])) auag.add_user( User("user3", group_names=["group3"]) ) # group3 doesn't exist. results = auag.is_valid() self.assertFalse(results.result)
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_non_shareable_user_to_json(self): """Tests converting a non-shareable user to JSON.""" u = User( name="someuser", password="******", mail="*****@*****.**", display_name="Some User", visibility=Visibility.NON_SHAREABLE, ) json = u.to_json() self.assertTrue('"principalTypeEnum":"LOCAL_USER"' in json) self.assertTrue('"name":"someuser"' in json) self.assertTrue('"password":"******"' in json) self.assertTrue('"mail":"*****@*****.**"' in json) self.assertTrue('"displayName":"Some User"' in json) self.assertTrue('"visibility":"' + Visibility.NON_SHAREABLE + '"' in json) self.assertTrue(json[0], "{") self.assertTrue(json.endswith("}"))
def test_user_to_json(self): """Tests converting a user to JSON.""" u = User( name="someuser", password="******", mail="*****@*****.**", display_name="Some User", created="1234", ) json = u.to_json() self.assertTrue('"principalTypeEnum":"LOCAL_USER"' in json) self.assertTrue('"name":"someuser"' in json) self.assertTrue('"password":"******"' in json) self.assertTrue('"mail":"*****@*****.**"' in json) self.assertTrue('"displayName":"Some User"' in json) self.assertTrue('"visibility":"' + Visibility.DEFAULT + '"' in json) self.assertTrue('"created":"' + "1234" + '"' in json) self.assertTrue(json[0], "{") self.assertTrue(json.endswith("}"))
def get_test_json(): """Creates some JSON for testing.""" uags = UsersAndGroups() uags.add_group( Group( name="Group 1", display_name="This is Group 1", description="A group for testing.", group_names=[], )) uags.add_group( Group( name="Group 2", display_name="This is Group 2", description="Another group for testing.", group_names=["Group 1"], visibility=Visibility.NON_SHAREABLE, )) uags.add_user( User( name="User1", password="******", display_name="User 1", mail="*****@*****.**", group_names=["Group 1"], )) uags.add_user( User( name="User2", password="******", display_name="User 2", mail="*****@*****.**", group_names=["Group 1", "Group 2"], visibility=Visibility.NON_SHAREABLE, )) return uags.to_json()
def test_add_groups_to_user(self): u = User(name="just_the_groups") self.assertEqual(u.groupNames, []) u.add_group("group 1") u.add_group("group 2") u.add_group("group 1") self.assertEqual(u.groupNames, ["group 1", "group 2"])
def test_create_non_shareable_user(self): """Tests creation of a new user.""" u = User( name="someuser", password="******", mail="*****@*****.**", display_name="Some User", visibility=Visibility.NON_SHAREABLE, ) self.assertEqual(u.principalTypeEnum, "LOCAL_USER") self.assertEqual(u.name, "someuser") self.assertEqual(u.password, "mysecretpwd") self.assertEqual(u.mail, "*****@*****.**") self.assertEqual(u.displayName, "Some User") self.assertEqual(u.visibility, Visibility.NON_SHAREABLE)
def test_create_user(self): """Tests creation of a new user.""" u = User( name="someuser", password="******", mail="*****@*****.**", display_name="Some User", created="1234", ) self.assertEqual(u.principalTypeEnum, "LOCAL_USER") self.assertEqual(u.name, "someuser") self.assertEqual(u.password, "mysecretpwd") self.assertEqual(u.mail, "*****@*****.**") self.assertEqual(u.displayName, "Some User") self.assertEqual(u.visibility, Visibility.DEFAULT) self.assertEqual(u.created, "1234")
def test_update_password(self): """ Tests updating a user password. """ sync = SyncUserAndGroups( tsurl=TS_URL, username=TS_USER, password=TS_PASSWORD, disable_ssl=True, ) auag = UsersAndGroups() auag.add_user( User(name="userx", mail="*****@*****.**", display_name="User X", password="******") ) # sync updates sync.sync_users_and_groups(users_and_groups=auag) sync.update_user_password( userid="userx", currentpassword=TS_PASSWORD, password="******" )
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"])
def create_common_users_and_groups(self): """ Creates a set of users and groups that can be used in multiple tests. """ auag = UsersAndGroups() auag.add_group( Group( name="Group1", display_name="This is Group 1", description="A group for testing.", group_names=[], visibility=Visibility.DEFAULT, ) ) auag.add_group( Group( name="Group2", display_name="This is Group 2", description="Another group for testing.", group_names=["Group1"], visibility=Visibility.NON_SHAREABLE, ) ) # Testing for ability to handle embedded quotes. auag.add_group( Group( name='Group"3"', display_name='This is Group "3"', description='Another "group" for testing.', group_names=["Group1"], visibility=Visibility.NON_SHAREABLE, ) ) auag.add_user( User( name="User1", password="******", display_name="User 1", mail="*****@*****.**", group_names=["Group1"], ) ) auag.add_user( User( name="User2", password="******", display_name="User 2", mail="*****@*****.**", group_names=["Group1", "Group2"], visibility=Visibility.NON_SHAREABLE, ) ) auag.add_user( User( name='User"3"', password="******", display_name='User "3"', mail="*****@*****.**", group_names=['Group"3"'], ) ) print(auag) sync = SyncUserAndGroups( tsurl=TS_URL, username=TS_USER, password=TS_PASSWORD, disable_ssl=True, ) sync.sync_users_and_groups(auag)
def test_read_ugs_from_excel(self): """Writes a test file, then reads from it.""" uags_out = UsersAndGroups() uags_out.add_user( User( name="user1", password="******", display_name="User 1", mail="*****@*****.**", group_names=["Group1"], visibility=Visibility.DEFAULT, )) uags_out.add_user( User( name="user2", password="******", display_name="User 2", mail="*****@*****.**", group_names=["Group1", "Group2"], visibility=Visibility.DEFAULT, )) uags_out.add_user( User( name="user3", password="******", display_name="User 3", mail="*****@*****.**", group_names=["Group3"], visibility=Visibility.NON_SHAREABLE, )) uags_out.add_group( Group( name="Group1", display_name="Group 1", description="Test group 1", visibility=Visibility.DEFAULT, )) uags_out.add_group( Group( name="Group2", display_name="Group 2", description="Test group 2", group_names=["Group1"], visibility=Visibility.DEFAULT, )) uags_out.add_group( Group( name="Group3", display_name="Group 3", description="Test group 3", group_names=["Group1", "Group2"], visibility=Visibility.NON_SHAREABLE, )) excel_filename = "test_read_write.xlsx" UGXLSWriter().write(uags_out, excel_filename) uags_in = UGXLSReader().read_from_excel(excel_filename) os.remove(excel_filename) # Verify the users. user = uags_in.get_user("user1") self.assertIsNotNone(user) self.assertEqual("user1", user.name) self.assertEqual("pwd1", user.password) self.assertEqual("User 1", user.displayName) self.assertEqual("*****@*****.**", user.mail) self.assertEqual(["Group1"], user.groupNames) self.assertEqual(Visibility.DEFAULT, user.visibility) user = uags_in.get_user("user2") self.assertIsNotNone(user) self.assertEqual("user2", user.name) self.assertEqual("pwd2", user.password) self.assertEqual("User 2", user.displayName) self.assertEqual("*****@*****.**", user.mail) self.assertEqual(["Group1", "Group2"], user.groupNames) self.assertEqual(Visibility.DEFAULT, user.visibility) user = uags_in.get_user("user3") self.assertIsNotNone(user) self.assertEqual("user3", user.name) self.assertEqual("pwd3", user.password) self.assertEqual("User 3", user.displayName) self.assertEqual("*****@*****.**", user.mail) self.assertEqual(["Group3"], user.groupNames) self.assertEqual(Visibility.NON_SHAREABLE, user.visibility) # Verify the groups. group = uags_in.get_group("Group1") self.assertEqual("Group1", group.name) self.assertEqual("Group 1", group.displayName) self.assertEqual("Test group 1", group.description) self.assertEqual([], group.groupNames) self.assertEqual(Visibility.DEFAULT, group.visibility) group = uags_in.get_group("Group2") self.assertEqual("Group2", group.name) self.assertEqual("Group 2", group.displayName) self.assertEqual("Test group 2", group.description) self.assertEqual(["Group1"], group.groupNames) self.assertEqual(Visibility.DEFAULT, group.visibility) group = uags_in.get_group("Group3") self.assertEqual("Group3", group.name) self.assertEqual("Group 3", group.displayName) self.assertEqual("Test group 3", group.description) self.assertEqual(["Group1", "Group2"], group.groupNames) self.assertEqual(Visibility.NON_SHAREABLE, group.visibility)
def test_blank_values_in_json(self): """Tests missing values are being left out of JSON""" u = User("user1") json = u.to_json() self.assertFalse(", ," in json)