예제 #1
0
def run_app():
    parser = argparse.ArgumentParser()
    parser.add_argument("--filename", help="file containing data to load.")
    args = parser.parse_args()

    json_data = ""
    if args.filename:
        print(f"Validating JSON format and content from {args.filename}")
        with open(args.filename, "r") as infile:
            json_data = infile.read()
    else:
        print(f"Validating JSON format and content from stdin")
        for line in sys.stdin:
            json_data += line

    json.loads(json_data)
    print("JSON format appears valid.")  # only prints if valid.

    ugs = UsersAndGroups()
    ugs.load_from_json(json_str=json_data)
    if ugs.is_valid()[0]:
        print(
            f"JSON content appears valid.  There are {ugs.number_groups()} groups and {ugs.number_users()} users."
        )
    else:
        print(f"JSON content does not appear valid.")
예제 #2
0
    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)
예제 #3
0
    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="******"
        )
예제 #4
0
    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_from_json(self):
        json_str = """
[
  {
    "principalTypeEnum": "LOCAL_GROUP", 
    "name": "test", 
    "displayName": "Test Group", 
    "description": "Test group for validation.", 
    "visibility": "NON_SHARABLE"
  }, 
  {
  "principalTypeEnum": "LOCAL_USER", 
  "name": "user_1",
  "displayName": "User 1", 
  "password": "******",
  "mail": "*****@*****.**", 
  "groupNames": ["test"], 
  "visibility": "NON_SHARABLE"
  }
]
"""
        ugs = UsersAndGroups()
        ugs.load_from_json(json_str=json_str.replace("\n", ""))
        self.assertTrue(ugs.is_valid())
예제 #6
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)
예제 #7
0
    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)
예제 #8
0
    def test_adding_and_removing_groups(self):
        """Tests adding and removing groups."""
        auag = UsersAndGroups()

        auag.add_group(Group("Group1"))
        auag.add_group(Group("Group2"))
        auag.add_group(Group("Group3"))

        self.assertTrue(auag.has_group("Group1"))
        self.assertTrue(auag.has_group("Group2"))
        self.assertTrue(auag.has_group("Group3"))
        self.assertEqual(auag.number_groups(), 3)

        auag.remove_group("Group1")
        self.assertFalse(auag.has_group("Group1"))
        self.assertEqual(auag.number_groups(), 2)

        self.assertTrue(auag.has_group("Group2"))
        u = auag.get_group("Group2")
        self.assertTrue(u.name, "Group2")

        self.assertIsNone(auag.get_group("noone"))
예제 #9
0
    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 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)
예제 #11
0
    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()
예제 #12
0
    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)
예제 #13
0
    def test_duplicate_groups(self):
        """Tests creating duplicate groups with different flags."""

        auag = UsersAndGroups()

        # create a duplicate with default flag to raise an error.
        auag.add_group(Group(name="group1"))
        with self.assertRaises(Exception):
            auag.add_group(Group(name="group1"))

        # create with overwrite.
        auag.add_group(
            Group(name="group2", group_names=["group2"]),
            duplicate=UsersAndGroups.OVERWRITE_ON_DUPLICATE,
        )
        u = auag.get_group("group2")
        self.assertEqual(u.name, "group2")
        self.assertEqual(u.groupNames, ["group2"])

        auag.add_group(
            Group(name="group2", group_names=["group3"]),
            duplicate=UsersAndGroups.OVERWRITE_ON_DUPLICATE,
        )
        u = auag.get_group("group2")
        self.assertEqual(u.name, "group2")
        self.assertEqual(u.groupNames, ["group3"])

        # create with update.
        auag.add_group(
            Group(name="group3", group_names=["group2"]),
            duplicate=UsersAndGroups.OVERWRITE_ON_DUPLICATE,
        )
        u = auag.get_group("group3")
        self.assertEqual(u.name, "group3")
        self.assertEqual(u.groupNames, ["group2"])

        auag.add_group(
            Group(name="group3", group_names=["group3"]),
            duplicate=UsersAndGroups.UPDATE_ON_DUPLICATE,
        )
        u = auag.get_group("group3")
        self.assertEqual(u.groupNames, ["group2", "group3"])
예제 #14
0
    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_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)