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