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.")
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_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())