Note: The default ITS GitLab runner is a shared resource and is subject to slowdowns during heavy usage.
You can run your own GitLab runner that is dedicated just to your group if you need to avoid processing delays.

Commit 83ddcbff authored by John C Boufford's avatar John C Boufford
Browse files

Completed crud operations, add find my groups by role. Started Searching unit test.

parent 744893b4
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 = "e06fb405-c58e-4334-b746-dd5969575bf2"
secret = "G5oT3vL1uK8xS7rO5sJ4nH7bK5iB6hN5jN5dI0nL8tO5bG4tT5"
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()))
map = response.json();
self.assertEqual(response.status_code, requests.codes.ok)
# The Read operation take 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=iamGroupTestJB,ou=user groups,ou=groups,dc=umich,dc=edu"
encoded_dn = urllib.parse.quote(dn)
url_endpoint = 'profile/dn'
url = IamGroupCRUDTests.url_base + '/' + url_endpoint + "/" + encoded_dn
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 group object which is a list.
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 modifiy 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=iamGroupTestJB,ou=user groups,ou=groups,dc=umich,dc=edu',
'moderator' : [{'email': 'john.boufford@yahoo.com'},
{'name':'John Boufford','email':'jbouffor@yahoo.com'}]}
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=iamGroupTestJB,ou=user groups,ou=groups,dc=umich,dc=edu"
encoded_dn = urllib.parse.quote(dn)
url_endpoint = 'delete'
url = IamGroupCRUDTests.url_base + '/' + url_endpoint + "/" + encoded_dn
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%3DiamGroupTestJB%2Cou%3Duser%20groups%2Cou%3Dgroups%2Cdc%3Dumich%2Cdc%3Dedu"
pre = "cn=iamGroupTestJB,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 = "iamGroupTestJB"
encoded_name = urllib.parse.quote(name)
url_endpoint = 'mygroup/isValidName'
url = IamGroupCRUDTests.url_base + '/' + url_endpoint + "/" + encoded_name
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 = "jbouffor"
encoded_name = urllib.parse.quote(name)
url_endpoint = 'mygroup/isValidName'
url = IamGroupCRUDTests.url_base + '/' + url_endpoint + "/" + encoded_name
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'])
import unittest
import logging
import requests
import json
import urllib.parse
from apidirectory import ApiDirectory
logger = logging.getLogger(__name__)
#This is a test of find groups that I play some role in. Owner, Member, Moderator.
class IamGroupMyGroupsTests(unittest.TestCase):
client_id = "e06fb405-c58e-4334-b746-dd5969575bf2"
secret = "G5oT3vL1uK8xS7rO5sJ4nH7bK5iB6hN5jN5dI0nL8tO5bG4tT5"
scope = "iamgroups"
token_url = "https://apigw-tst.it.umich.edu/um/inst/oauth2/token"
url_base = "https://apigw-tst.it.umich.edu/um/iamGroups"
# Find all my groups, Groups that I am a ower, moderator, member or have any role in.
def test_allmygroups(self):
api = ApiDirectory(self.client_id, self.secret, self.scope, self.token_url)
url_endpoint = 'mygroup/allmygroups'
url = IamGroupMyGroupsTests.url_base + '/' + url_endpoint
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)
# Finds all the groups Im a member of.
def test_membership(self):
api = ApiDirectory(self.client_id, self.secret, self.scope, self.token_url)
url_endpoint = 'mygroup/membership'
url = IamGroupMyGroupsTests.url_base + '/' + url_endpoint
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)
# Finds all the groups Im a direct owner of. This is where the group is listed as an owner.
def test_owner(self):
api = ApiDirectory(self.client_id, self.secret, self.scope, self.token_url)
url_endpoint = 'mygroup/ownership'
url = IamGroupMyGroupsTests.url_base + '/' + url_endpoint
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)
# finds all the groups I am an indirect owner, because one of the groups Im a member
# is listed as an owner of another group.
def test_ownershipByGroups(self):
api = ApiDirectory(self.client_id, self.secret, self.scope, self.token_url)
url_endpoint = 'mygroup/owershipbygroups'
url = IamGroupMyGroupsTests.url_base + '/' + url_endpoint
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)
#Finds the groups that I am both a direct owner and an indirect owner.
def test_ownership(self):
api = ApiDirectory(self.client_id, self.secret, self.scope, self.token_url)
url_endpoint = 'mygroup/allownership'
url = IamGroupMyGroupsTests.url_base + '/' + url_endpoint
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)
# Finds groups that have been set up with an email owner @ nonumich.
def test_moderator(self):
api = ApiDirectory(self.client_id, self.secret, self.scope, self.token_url)
url_endpoint = 'mygroup/moderated'
url = IamGroupMyGroupsTests.url_base + '/' + url_endpoint
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)
import unittest
import logging
import requests
from apidirectory import ApiDirectory
logger = logging.getLogger(__name__)
class IamGroupTests(unittest.TestCase):
client_id = "e06fb405-c58e-4334-b746-dd5969575bf2"
secret = "G5oT3vL1uK8xS7rO5sJ4nH7bK5iB6hN5jN5dI0nL8tO5bG4tT5"
scope = "iamgroups"
token_url = "https://apigw-tst.it.umich.edu/um/inst/oauth2/token"
url_base = "https://apigw-tst.it.umich.edu/um/iamGroups"
def test_find_person(self):
api = ApiDirectory(self.client_id, self.secret, self.scope, self.token_url)
uid = "jbouffor"
url_endpoint = 'find/person'
url = IamGroupTests.url_base + '/' + url_endpoint + "/" + uid
response = requests.get(
url=url,
headers = api.build_headers(),
timeout = 10
)
print(api.build_headers())
print('Response: {}'.format(response))
print('JSON: {}'.format(response.json()))
self.assertEqual(response.status_code, requests.codes.ok)
map = response.json()
self.assertEqual(map['naming'], "jbouffor")
'''
Sample json for the advance search
{ "searchText" : "somegroup" }
or
{ "owner" : "uid=uniqname,ou=people,dc=umich,dc=umich"}
or
{ "cn" : "somegroup" }
or
{ "cn" : "somegroup", "cnSearchType":"contains" }
When searching by common name, cn, search types are
starts with ( default )
matches exactly
contains
ends with
'''
import unittest
import logging
import requests
import json
import urllib.parse
from apidirectory import ApiDirectory
logger = logging.getLogger(__name__)
class IamGroupTests(unittest.TestCase):
class IamGroupSearchTests(unittest.TestCase):
client_id = "e06fb405-c58e-4334-b746-dd5969575bf2"
secret = "G5oT3vL1uK8xS7rO5sJ4nH7bK5iB6hN5jN5dI0nL8tO5bG4tT5"
......@@ -16,67 +34,66 @@ class IamGroupTests(unittest.TestCase):
token_url = "https://apigw-tst.it.umich.edu/um/inst/oauth2/token"
url_base = "https://apigw-tst.it.umich.edu/um/iamGroups"
def test_create(self):
# Search for a group that contains ITS somewhere in the name.
def test_search(self):
api = ApiDirectory(self.client_id, self.secret, self.scope, self.token_url)
url_endpoint = 'search'
url = IamGroupSearchTests.url_base + '/' + url_endpoint
url_endpoint = 'create'
url = IamGroupTests.url_base + '/' + url_endpoint
data = 'iamGroupTestJB'
data = {'cn': 'ITS', "cnSearchType":"contains" }
response = requests.post(
url=url,
data=data,
headers = api.build_headers(),
timeout = 10
data=json.dumps(data),
headers=api.build_headers(),
timeout=10
)
print(api.build_headers())
print('Response: {}'.format(response))
print('JSON: {}'.format(response.json()))
map = response.json();
self.assertEqual(response.status_code, requests.codes.ok)
def test_read(self):
# Search for a group ITS. Same behavior if you type a name in the quick search box.
def test_simple_search(self):
api = ApiDirectory(self.client_id, self.secret, self.scope, self.token_url)
dn = "cn=iamGroupTestJB,ou=user groups,ou=groups,dc=umich,dc=edu"
url_endpoint = 'profile'
url = IamGroupTests.url_base + '/' + url_endpoint + "/" + dn
url_endpoint = 'search'
url = IamGroupSearchTests.url_base + '/' + url_endpoint
response = requests.get(
data = {'searchText': 'ITS'}
response = requests.post(
url=url,
headers = api.build_headers(),
timeout = 10
data=json.dumps(data),
headers=api.build_headers(),
timeout=10
)
print(api.build_headers())
print('Response: {}'.format(response))
print('JSON: {}'.format(response.json()))
map = response.json();self.assertEqual(response.status_code, requests.codes.ok)
self.assertEqual(response.status_code, requests.codes.ok)
self.assertEqual[map['dn'], "jbouffor"]
def test_update(self):
# Search for a member in the groups Im an owner.
def test_search_mygroups_for_member(self):
api = ApiDirectory(self.client_id, self.secret, self.scope, self.token_url)
url_endpoint = 'searchForItem'
name = "uid=jbouffor,ou=people,dc=umich,dc=edu"
# name = "cn=group name,ou=user groups,ou=groups,dc=umich,dc=edu"
# name = "someone@yahoo.com"
encoded = urllib.parse.quote(name)
url = IamGroupSearchTests.url_base + '/' + url_endpoint + '/' + name
url_endpoint = 'update/moderator'
url = IamGroupTests.url_base + '/' + url_endpoint
data = { 'dn' : 'cn=mye2eapitest,ou=user groups,ou=groups,dc=umich,dc=edu',
'moderator' : [{'email': 'john.boufford@yahoo.com'},
{'name':'John Boufford','email':'jbouffor@yahoo.com'}]}
response = requests.post(
response = requests.get(
url=url,
data=json.dumps(data),
headers=api.build_headers(),
timeout=10
)
print(api.build_headers())
print('Response: {}'.format(response))
print('JSON: {}'.format(response.json()))
map = response.json();
self.assertEqual(response.status_code, requests.codes.ok)
self.assertEqual(response.status_code, requests.codes.ok)
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment