def test_write_to_xls(self): """Tests writing users and groups.""" 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"], )) uags.add_group( Group( name='Group "3"', display_name='This is Group "3"', description='Another "group" for testing.', group_names=["Group 1", "Group 2"], )) 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"], )) # Testing for ability to handle embedded quotes. uags.add_user( User( name='User "3"', password="******", display_name='User "3"', mail="*****@*****.**", group_names=['Group "3"'], )) writer = UGXLSWriter() writer.write(uags, "test_uags")
def get_user_metadata(self): """ Returns a list of User objects based on the metadata. :return: A list of user objects. :rtype: list of User """ url = self.format_url(SyncUserAndGroups.USER_METADATA_URL) response = self.session.get(url, cookies=self.cookies) users = [] if response.status_code == 200: logging.info("Successfully got user metadata.") json_list = json.loads(response.text) for value in json_list: user = User(name=value.get("name", None), display_name=value.get("displayName", None), mail=value.get("mail", None), group_names=value.get("groupNames", None), visibility=value.get("visibility", None), created=value.get("created", None), id=value.get("id", None)) users.append(user) return users else: logging.error("Failed to get user metadata.") raise requests.ConnectionError( "Error getting user metadata (%d)" % response.status_code, response.text, )
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 parse_json(self, json_list): """ Parses a JSON list and creates a UserAndGroup object. :param json_list: List of JSON objects that represent users and groups. :returns: A user and group container with the users and groups. :rtype: UsersAndGroups """ auag = UsersAndGroups() for value in json_list: if str(value["principalTypeEnum"]).endswith("_USER"): user = User(name=value.get("name", None), display_name=value.get("displayName", None), mail=value.get("mail", None), group_names=value.get("groupNames", None), visibility=value.get("visibility", None), created=value.get("created", None), id=value.get("id", None)) auag.add_user(user) else: group = Group( name=value.get("name", None), display_name=value.get("displayName", None), description=value.get("description", None), group_names=value.get("groupNames", None), visibility=value.get("visibility", None), ) auag.add_group(group) return auag
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", group_names=["Group 1"], )) auag.add_user( User( name="User2", password="******", display_name="User 2", group_names=["Group 1", "Group 2"], )) # only works on Bill's AWS instance. sync = SyncUserAndGroups( tsurl=TS_URL, username=TS_USER, password=TS_PASSWORD, global_password="******", disable_ssl=True, ) sync.sync_users_and_groups(auag)
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", group_names=["group1"])) auag.add_user(User("user2", 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_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_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_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.assertEquals(u.groupNames, []) u.add_group("group 1") u.add_group("group 2") u.add_group("group 1") self.assertEquals(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.assertEquals(u.principalTypeEnum, "LOCAL_USER") self.assertEquals(u.name, "someuser") self.assertEquals(u.password, "mysecretpwd") self.assertEquals(u.mail, "*****@*****.**") self.assertEquals(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.assertEquals(u.principalTypeEnum, "LOCAL_USER") self.assertEquals(u.name, "someuser") self.assertEquals(u.password, "mysecretpwd") self.assertEquals(u.mail, "*****@*****.**") self.assertEquals(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", 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 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="Group 1", display_name="This is Group 1", description="A group for testing.", group_names=[], visibility=Visibility.DEFAULT, )) auag.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, )) # 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=["Group 1"], visibility=Visibility.NON_SHAREABLE, )) auag.add_user( User( name="User1", password="******", display_name="User 1", mail="*****@*****.**", group_names=["Group 1"], )) auag.add_user( User( name="User2", password="******", display_name="User 2", mail="*****@*****.**", group_names=["Group 1", "Group 2"], visibility=Visibility.NON_SHAREABLE, )) # Testing for ability to handle embedded quotes. 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, remove_deleted=True)
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 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)