Commit 35768ff8 authored by John C Boufford's avatar John C Boufford
Browse files

Create End 2 End Test for the iamGroups web services hosted on the directory of API.

parents
.idea
*.iml
\ No newline at end of file
import requests
import json
import logging
logger = logging.getLogger(__name__)
class ApiDirectory:
def __init__(self, client_id, secret, scope, token_url, timeout=10):
self.client_id = client_id
self.secret = secret
self.scope = scope
self.token_url = token_url
self.timeout = timeout
def _validate_initialization(self):
if (self.client_id == None
or self.secret == None
or self.scope == None
or self.token_url == None):
raise Exception("Invalid class configuration")
def _find_token(self):
data = {
'grant_type': 'client_credentials',
'scope': 'constituents'
}
headers = {
'Content-type': 'application/x-www-form-urlencoded',
'accept': 'application/json'
}
url = self.token_url + "?grant_type=client_credentials&scope={}".format(self.scope)
response = requests.post(
url,
data=json.dumps(data),
headers=headers,
auth=(self.client_id, self.secret),
timeout=self.timeout,
)
logger.debug('response={} json={}'.format(response, response.json()))
return response.json()
# gets access token from find_token
def _find_access_token(self):
token = self._find_token()
if ('access_token' in token):
access_token = token['access_token']
else:
print(token)
if ('error' in token):
err = token['error']
if ('error_description' in token):
err += ":" + token['error_description']
if (err is None):
err = "unknown error"
raise Exception(err)
return access_token
def build_headers(self):
self._validate_initialization()
bearer = self._find_access_token()
return {
'x-ibm-client-id': '{}'.format(self.client_id),
'authorization': 'Bearer ' + bearer,
'accept': 'application/json'
}
requests==2.20.0
\ No newline at end of file
import unittest
from apidirectory import ApiDirectory
class ApiDirectoryTest(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"
def test_constructor(self):
api = ApiDirectory(self.client_id, self.secret, self.scope, self.token_url);
self.assertEquals(api.client_id, self.client_id)
self.assertEquals(api.secret, self.secret)
self.assertEquals(api.scope, self.scope)
self.assertEquals(api.token_url, self.token_url)
self.assertEquals(api.timeout, 10)
def test_build_header(self):
api = ApiDirectory(self.client_id, self.secret, self.scope, self.token_url);
header = api.build_headers()
map = header
self.assertEquals(map['x-ibm-client-id'], self.client_id)
self.assertTrue('authorization' in map)
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_create(self):
api = ApiDirectory(self.client_id, self.secret, self.scope, self.token_url)
url_endpoint = 'create'
url = IamGroupTests.url_base + '/' + url_endpoint
data = 'iamGroupTestJB'
response = requests.post(
url=url,
data=data,
headers = api.build_headers(),
timeout = 10
)
print('Response: {}'.format(response))
print('JSON: {}'.format(response.json()))
map = response.json();
self.assertEquals(map['status'], 200)
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