import unittest import logging import requests import json import urllib.parse from apidirectory import ApiDirectory logger = logging.getLogger(__name__) # This test class test Create, Read, Update, and Delete (CRUD) oeprations. # The first test has a one because when I run the test they run in alphbetic order # which may be different in your IDE, or command line. class IamGroupCRUDTests(unittest.TestCase): client_id = "some-guid-goes-here" secret = "secret-string-goes-here" scope = "iamgroups" token_url = "https://apigw-tst.it.umich.edu/um/inst/oauth2/token" url_base = "https://apigw-tst.it.umich.edu/um/iamGroups" # Test a create operation. Notice that the data structure needs to be converted to json. # The url_endpoint is obtain from the API directory # The api.build_headers builds the header needed for two factor oauth. def test_1_create(self): api = ApiDirectory(self.client_id, self.secret, self.scope, self.token_url) url_endpoint = 'create' url = IamGroupCRUDTests.url_base + '/' + url_endpoint data = {'name': 'iamGroupTestJB'} response = requests.post( url=url, data=json.dumps(data), headers=api.build_headers(), timeout=10 ) print('Response: {}'.format(response)) print('JSON: {}'.format(response.json())) self.assertEqual(response.status_code, requests.codes.ok) # The Read operation takes a dn on the url. Because a dn contains spaces, commas and equals it # needs to url encode. Note the test at the bottom of the unit test. def test_2_read(self): api = ApiDirectory(self.client_id, self.secret, self.scope, self.token_url) dn = "cn=TestGroupName,ou=user groups,ou=groups,dc=umich,dc=edu" encoded = urllib.parse.quote(dn) url_endpoint = 'profile/dn' url = IamGroupCRUDTests.url_base + '/' + url_endpoint + "/" + encoded response = requests.get( url=url, headers=api.build_headers(), timeout=10 ) print('Response: {}'.format(response)) print('JSON: {}'.format(response.json())) self.assertEqual(response.status_code, requests.codes.ok) # note that the returned json is a dictionary object with a key of group. # the value is a list of groups and each group is a dictionary. map = response.json() list = map['group'] profile = list[0] self.assertEqual(profile['dn'], dn) # update. There are many update operations. I am showing how to modify a moderator. def test_3_update(self): api = ApiDirectory(self.client_id, self.secret, self.scope, self.token_url) url_endpoint = 'update/moderator' url = IamGroupCRUDTests.url_base + '/' + url_endpoint data = {'dn': 'cn=TestGroupName,ou=user groups,ou=groups,dc=umich,dc=edu', 'moderator': [{'email': 'bjensen@umich.edu'}, {'name': 'Barbara Jensen', 'email': 'bjensen@umich.edu'}]} response = requests.post( url=url, data=json.dumps(data), headers=api.build_headers(), timeout=10 ) print('Response: {}'.format(response)) print('JSON: {}'.format(response.json())) self.assertEqual(response.status_code, requests.codes.ok) # delete an existing group. Again the dn is part of the url so it should be encoded. def test_4_delete(self): api = ApiDirectory(self.client_id, self.secret, self.scope, self.token_url) dn = "cn=TestGroupName,ou=user groups,ou=groups,dc=umich,dc=edu" encoded = urllib.parse.quote(dn) url_endpoint = 'delete' url = IamGroupCRUDTests.url_base + '/' + url_endpoint + "/" + encoded response = requests.get( url=url, headers=api.build_headers(), timeout=10 ) print('Response: {}'.format(response)) print('JSON: {}'.format(response.json())) self.assertEqual(response.status_code, requests.codes.ok) # Note that I used urllib.parse.quote which converts spaces to %20. # the urllib.parse.quote_plus converts spaces to a plus sign. Which fails in this api. def test_urlencoding(self): expected = "cn%3DTestGroupName%2Cou%3Duser%20groups%2Cou%3Dgroups%2Cdc%3Dumich%2Cdc%3Dedu" pre = "cn=TestGroupName,ou=user groups,ou=groups,dc=umich,dc=edu" post = urllib.parse.quote(pre) print(post) self.assertEqual(post, expected) # Test check if the group name is avaliable and follows all the rules. # Make sure to encode the name because the name can contain spaces and other specail chars. def test_isValid(self): api = ApiDirectory(self.client_id, self.secret, self.scope, self.token_url) name = "TestGroupName" encoded = urllib.parse.quote(name) url_endpoint = 'isValidName' url = IamGroupCRUDTests.url_base + '/' + url_endpoint + "/" + encoded response = requests.get( url=url, headers=api.build_headers(), timeout=10 ) print('Response: {}'.format(response)) print('JSON: {}'.format(response.json())) self.assertEqual(response.status_code, requests.codes.ok) map = response.json() self.assertTrue(map['valid']) # Test check if the group name is avaliable and follows all the rules. fails # Make sure to encode the name because the name can contain spaces and other specail chars. def test_isValid_failed(self): api = ApiDirectory(self.client_id, self.secret, self.scope, self.token_url) name = "bjensen" encoded = urllib.parse.quote(name) url_endpoint = 'isValidName' url = IamGroupCRUDTests.url_base + '/' + url_endpoint + "/" + encoded response = requests.get( url=url, headers=api.build_headers(), timeout=10 ) print('Response: {}'.format(response)) print('JSON: {}'.format(response.json())) self.assertEqual(response.status_code, requests.codes.ok) map = response.json() self.assertFalse(map['valid'])