class TestNodeConnector(unittest.TestCase): def setUp(self): self.client=Resource('http://127.0.0.1:8000') self.client.post('/environments',headers={'Content-Type': 'application/json'},payload='{"name":"test"}') self.client.post('/environments/0/nodes',headers={'Content-Type': 'application/json'},payload='{"name":"testNode","type":"reprap"}') def test_ConnectorCreate(self): self.client.put('/environments/0/nodes/0/connector',headers={'Content-Type': 'application/json'},payload='{"driverType":"teacup","driverParams":{"speed":115200}}') self.failUnlessEqual(200, self.client.status) def test_ConnectorRead(self): self.client.get('/environments/0/nodes/0/connector',headers={'Content-Type': 'application/json'}) self.failUnlessEqual(200, self.client.status) def test_ConnectorUpdate(self): self.client.put('/environments/0/nodes/0/connector',headers={'Content-Type': 'application/json'},payload='{"driverType":"teacup","driverParams":{"speed":115200}}') self.failUnlessEqual(200, self.client.status) def test_ConnectorDelete(self): self.client.delete('/environments/0/nodes/0/connector',headers={'Content-Type': 'application/json'}) self.failUnlessEqual(200, self.client.status) def test_ConnectorStatusUpdate(self): self.client.put('/environments/0/nodes/0/connector',headers={'Content-Type': 'application/json'},payload='{"driverType":"teacup","driverParams":{"speed":115200}}') self.failUnlessEqual(200, self.client.status) self.client.post('/environments/0/nodes/0/connector/status',headers={'Content-Type': 'application/json'},payload='{"connected":"True"}') self.failUnlessEqual(200, self.client.status) def tearDown(self): self.client.delete('/environments',headers={'Content-Type': 'application/json'})
class TestNodeTasks(unittest.TestCase): def setUp(self): self.client=Resource('http://127.0.0.1:8000') self.client.post('/environments',headers={'Content-Type': 'application/json'},payload='{"name":"test"}') self.client.post('/environments/0/nodes',headers={'Content-Type': 'application/json'},payload='{"name":"testNode","type":"reprap"}') def test_TaskCreate(self): response=self.client.post('/environments/0/nodes/0/tasks',headers={'Content-Type': 'application/json'},payload='{"name":"testTask","type":"print"}') self.failUnlessEqual(200, self.client.status) def test_TaskRead(self): self.client.get('/environments/0/nodes/0/connector',headers={'Content-Type': 'application/json'}) self.failUnlessEqual(200, self.client.status) def test_ConnectorUpdate(self): self.client.put('/environments/0/nodes/0/connector',headers={'Content-Type': 'application/json'},payload='{"driverType":"teacup","driverParams":{"speed":115200}}') self.failUnlessEqual(200, self.client.status) def test_ConnectorDelete(self): self.client.delete('/environments/0/nodes/0/connector',headers={'Content-Type': 'application/json'}) self.failUnlessEqual(200, self.client.status) def test_ConnectorStatusUpdate(self): self.client.put('/environments/0/nodes/0/connector',headers={'Content-Type': 'application/json'},payload='{"driverType":"teacup","driverParams":{"speed":115200}}') self.failUnlessEqual(200, self.client.status) self.client.post('/environments/0/nodes/0/connector/status',headers={'Content-Type': 'application/json'},payload='{"connected":"True"}') self.failUnlessEqual(200, self.client.status) def tearDown(self): self.client.delete('/environments',headers={'Content-Type': 'application/json'})
class TestEnvironments(unittest.TestCase): def setUp(self): self.client=Resource('http://127.0.0.1:8000') def test_EnvironmentCreate(self): response=self.client.put('/environments',headers={'Content-Type': 'application/json'},payload='{"name":"test"}') self.failUnlessEqual(200, self.client.status) def test_EnvironmenstRead(self): self.client.get('/environments',headers={'Content-Type': 'application/json'}) self.failUnlessEqual(200, self.client.status) try: self.client.get('/environments',headers={'Content-Type': 'application/xml'}) except: pass self.failUnlessEqual(500, self.client.status) self.client.put('/environments',headers={'Content-Type': 'application/json'},payload='{"name":"test"}') response=self.client.get('/environments',headers={'Content-Type': 'application/json'}) self.failUnlessEqual('{"Environments":["test"]}',response) def test_EnvironmentUpdate(self): pass def test_EnvironmentsDelete(self): self.client.delete('/environments',headers={'Content-Type': 'application/json'}) self.failUnlessEqual(200, self.client.status) def tearDown(self): self.client.delete('/environments',headers={'Content-Type': 'application/json'})
class TestEnvironments(unittest.TestCase): def setUp(self): self.client = Resource("http://127.0.0.1:8000") def test_EnvironmentCreate(self): response = self.client.put( "/environments", headers={"Content-Type": "application/json"}, payload='{"name":"test"}' ) self.failUnlessEqual(200, self.client.status) def test_EnvironmenstRead(self): self.client.get("/environments", headers={"Content-Type": "application/json"}) self.failUnlessEqual(200, self.client.status) try: self.client.get("/environments", headers={"Content-Type": "application/xml"}) except: pass self.failUnlessEqual(500, self.client.status) self.client.put("/environments", headers={"Content-Type": "application/json"}, payload='{"name":"test"}') response = self.client.get("/environments", headers={"Content-Type": "application/json"}) self.failUnlessEqual('{"Environments":["test"]}', response) def test_EnvironmentUpdate(self): pass def test_EnvironmentsDelete(self): self.client.delete("/environments", headers={"Content-Type": "application/json"}) self.failUnlessEqual(200, self.client.status) def tearDown(self): self.client.delete("/environments", headers={"Content-Type": "application/json"})
class GetAuth(unittest.TestCase): def setUp(self): self.res = Resource("http://"+utils.sso_server) # set logging level # Need to be moved to somewhere else for global configuration debug = logging.DEBUG logging.basicConfig(level = debug, stream = sys.stderr, format = '%(levelname)s %(asctime)s [%(message)s]') logging.info("") def test_postive_get_auth_horizon_check_keys(self): """ Check the response contains correct keys. """ r = self.res.get('/auth/config/'+utils.partner, headers=utils.headers) logging.info("Return response is '%s'" % r) # convert string to dictionary rd = ast.literal_eval(r) logging.info("Return response in dictionary format is '%s'" % rd) self.assertEqual(self.res.response.status, 200) keys = ['type', 'web_endpoint', 'client_endpoint', 'org_name'] self.assertTrue(utils.is_same_array(keys, rd.keys()), "Keys are not correct!") def test_positive_get_auth_horizon_check_values(self): """ Check the response contains correct values. """ r = self.res.get('/auth/config/'+(utils.partner), headers=utils.headers) logging.info("Return response is '%s'" % r) # convert string to dictionary rd = ast.literal_eval(r) logging.info("Return response in dictionary format is '%s'" % rd) self.assertEqual(self.res.response.status, 200) types = ['mozy', 'cbeyond', 'horizon'] # assert 'type' is in the 3 values self.assertTrue(rd['type'] in types, "The 'type' is not in '%s'" % types) # assert 'web_endpoint' is the url format p = re.compile("(https:\/\/)*[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?") self.assertTrue(p.match(rd['web_endpoint']), "The 'web_endpoint' does not match URL format") # assert 'client_endpoint' is the url format self.assertTrue(p.match(rd['client_endpoint']), "The 'client_endpoint' does not match URL format") # assert 'horizon_org_name' is at least a string type self.assertEqual(type(rd['org_name']), type(str("")), "The 'org_name' is not string type") def test_negative_get_auth_horizon_partner_not_exist(self): """ Check exception raised while getting auth for not existent partner, and verify the exception contains correct error messages. """ nosub = utils.random_str() logging.info("The not existent subdomain to be tested is '%s'" % nosub) with self.assertRaises(rest.RequestFailed) as e: self.res.get('/auth/config/' + (nosub), headers=utils.headers) self.assertEqual(self.res.response.status, 400) # verify the exception is expected utils.verify_rest_requetfailed_exception(e,utils.get_exception('UnknownSubdomain', 'GetAuthExceptions'), self)
class ReviewRequestManager(object): def __init__(self, config): self.url = config['url'] self.last_seen_rr = config['last_seen_rr'] self.conn = Resource(self.url) def __str__(self): return "URL: %s\nLast Seen Review Request: %s\n" % (self.url, self.last_seen_rr) def get_unseen(self): unseen = [] open_reqs = self.get_open_reqs() unseen_highest_id = 0 for request in open_reqs: request_id = int(request['id']) if request_id > self.last_seen_rr: unseen.append(ReviewRequest(request)) if request_id > unseen_highest_id: unseen_highest_id = request_id self.last_seen_rr = unseen_highest_id return unseen def get_open_reqs(self): # Query the RB WebAPI, and find all of the open review requests. response = self.conn.get("/review-requests", params={'status':'pending'}) response_data = json.loads(response) return response_data['review_requests']
class TestNodes(unittest.TestCase): def setUp(self): self.client=Resource('http://127.0.0.1:8000') response=self.client.put('/environments',headers={'Content-Type': 'application/json'},payload='{"name":"test"}') self.failUnlessEqual(200, self.client.status) def test_NodeCreate(self): response=self.client.post('/environments/0/nodes',headers={'Content-Type': 'application/json'},payload='{"name":"testNode","type":"reprap"}') self.failUnlessEqual(200, self.client.status) def test_NodesRead(self): self.client.get('/environments/0/nodes',headers={'Content-Type': 'application/json'}) self.failUnlessEqual(200, self.client.status) def test_NodeUpdate(self): pass def test_NodesDelete(self): self.client.delete('/environments/0/nodes',headers={'Content-Type': 'application/json'}) self.failUnlessEqual(200, self.client.status) def tearDown(self): self.client.delete('/environments',headers={'Content-Type': 'application/json'})
class DeleteTokens(unittest.TestCase): def setUp(self): self.res = Resource("http://"+utils.sso_server) # set logging level # Need to be moved to somewhere else for global configuration debug = logging.DEBUG logging.basicConfig(level = debug, stream = sys.stderr, format = '%(levelname)s %(asctime)s [%(message)s]') logging.info("") self.token = utils.retrieve_token() def test_positive_delete_tokens_check_keys(self): """ Check the response contains correct keys. """ r = self.res.delete('/tokens/'+self.token['access_token'], headers=utils.headers) logging.info("Return response is '%s'" % r) # convert string to dictionary rd = ast.literal_eval(r) logging.info("Return response in dictionary format is '%s'" % rd) self.assertEqual(self.res.response.status, 200) keys = ['revoked_token_num'] self.assertTrue(utils.is_same_array(keys, rd.keys()), "Keys are not correct!") def test_positive_delete_tokens_exist(self): """ Delete tokens exist and check the 'revoked_token_num' is '1' """ token = utils.retrieve_token(another=True) r = self.res.delete('/tokens/'+token['access_token'], headers=utils.headers) logging.info("Return response is '%s'" % r) self.assertEqual(self.res.response.status, 200) # convert string to dictionary rd = ast.literal_eval(r) logging.info("Return response in dictionary format is '%s'" % rd) # assert 'revoked_token_num' is 1 self.assertEqual(rd['revoked_token_num'], 1, "The 'revoked_token_num' is not equal to 1") def test_negative_delete_tokens_not_exist(self): """ Delete tokens not exist and check the 'revoked_token_num' is '0' """ r = self.res.delete('/tokens/'+utils.random_str(), headers=utils.headers) logging.info("Return response is '%s'" % r) self.assertEqual(self.res.response.status, 200) # convert string to dictionary rd = ast.literal_eval(r) logging.info("Return response in dictionary format is '%s'" % rd) # assert 'revoked_token_num' is 1 self.assertEqual(rd['revoked_token_num'], 0, "The 'revoked_token_num' is not equal to 0") def test_negative_delete_tokens_parameter_missing(self): """ Check exception raised while deleting tokens without giving parameter, and verify the exception contains correct error messages. """ malpayloads = [''] for mp in malpayloads: logging.info("The malpayload acting unauthorized client is '%s'" % mp) with self.assertRaises(rest.RequestFailed) as e: self.res.delete('/tokens/'+mp, headers=utils.headers) self.assertEqual(self.res.response.status, 400) # verify the retrieved exception is expected utils.verify_rest_requetfailed_exception(e, utils.get_exception('ParameterMissing', 'DeleteTokensExceptions'), self) def test_positive_delete_tokens_condition_exist(self): """ Delete tokens with existing user_id and check the 'revoked_token_num' is >= '1' """ token = utils.retrieve_token(another=True) r = self.res.get('/tokens/'+token['access_token'], headers=utils.headers) token = ast.literal_eval(r) r = self.res.delete('/tokens/?user_id='+str(token['user_id']), headers=utils.headers) logging.info("Return response is '%s'" % r) self.assertEqual(self.res.response.status, 200) # convert string to dictionary rd = ast.literal_eval(r) logging.info("Return response in dictionary format is '%s'" % rd) # assert 'revoked_token_num' is >= 1 self.assertTrue((rd['revoked_token_num'] >= 1), "The 'revoked_token_num' is not >= 1") def test_negative_delete_tokens_condition_user_not_exist(self): """ Delete tokens with not existing user_id and check the 'revoked_token_num' '0' """ malpayloads = [str(random.randint(0,99)), utils.random_str()] for mp in malpayloads: logging.info("The malpayload acting not existent user_id is '%s'" % mp) r = self.res.delete('/tokens/?user_id='+mp, headers=utils.headers) self.assertEqual(self.res.response.status, 200) # convert string to dictionary rd = ast.literal_eval(r) logging.info("Return response in dictionary format is '%s'" % rd) # assert 'revoked_token_num' is 0 self.assertEqual(rd['revoked_token_num'], 0, "The 'revoked_token_num' is not 0") def test_negative_delete_tokens_condition_parameter_missing(self): """ Check exception raised while deleting tokens without giving client_id, and verify the exception contains correct error messages. """ malpayloads = ['', 'client_id='] for mp in malpayloads: logging.info("The malpayload acting unauthorized client is '%s'" % mp) with self.assertRaises(rest.RequestFailed) as e: self.res.delete('/tokens/?'+mp, headers=utils.headers) self.assertEqual(self.res.response.status, 400) # verify the retrieved exception is expected utils.verify_rest_requetfailed_exception(e, utils.get_exception('ParameterMissing', 'DeleteTokensExceptions'), self) def test_negative_delete_tokens_condition_invalid_user(self): """ Check exception raised while deleting tokens with invalid user_id, and verify the exception contains correct error messages. """ malpayloads = ['', ' '] for mp in malpayloads: logging.info("The malpayload acting invalid user_id is '%s'" % mp) with self.assertRaises(rest.RequestFailed) as e: url = '/tokens/?user_id='+mp logging.debug("The requested url is '%s'" % url) self.res.delete(url, headers=utils.headers) # verify the retrieved exception is expected self.assertEqual(self.res.response.status, 400)
class GetTokens(unittest.TestCase): def setUp(self): self.res = Resource("http://"+utils.sso_server) # set logging level # Need to be moved to somewhere else for global configuration debug = logging.DEBUG logging.basicConfig(level = debug, stream = sys.stderr, format = '%(levelname)s %(asctime)s [%(message)s]') logging.info("") # post saml grant self.token = utils.retrieve_token() def test_positive_get_tokens_check_keys(self): """ Check the response contains correct keys. """ r = self.res.get('/tokens/'+self.token['access_token'], headers=utils.headers) logging.info("Return response is '%s'" % r) # convert string to dictionary rd = ast.literal_eval(r) logging.info("Return response in dictionary format is '%s'" % rd) self.assertEqual(self.res.response.status, 200) keys = ['access_token_expires_at', 'user_id', 'access_token', 'user', 'client_id', 'access_token_secret', 'permissions'] self.assertTrue(utils.is_same_array(keys, rd.keys()), "Keys are not correct!") def test_positive_get_tokens_check_values(self): """ Check the response contains correct values. """ r = self.res.get('/tokens/'+self.token['access_token'], headers=utils.headers) logging.info("Return response is '%s'" % r) # convert string to dictionary rd = ast.literal_eval(r) logging.info("Return response in dictionary format is '%s'" % rd) self.assertEqual(self.res.response.status, 200) # assert 'access_token_expires_at' is in the given values p = re.compile("\d{4,4}(\-\d\d){2,2}T(\d\d:){2,2}\d\dZ") self.assertTrue(p.match(rd['access_token_expires_at']), "The 'access_token_expires_at' does not match '2011-10-21T11:28:39Z'") # assert 'user_id' is integer self.assertEqual(type(rd['client_id']), type(1), "The 'client_id' is not integer") # assert 'access_token' is equal to the one given in token self.assertEqual(rd['access_token'], self.token['access_token'], "The 'access_token' is not equal to the one given in token") # assert 'user' is a dictionary object self.assertEqual(type(rd['user']), type({}), "The 'user' is not dictionary") # assert 'user' has the 'name', 'custom_permission', 'email' and 'id' fields. keys = ['name', 'custom_permission', 'email', 'id'] self.assertTrue(utils.is_same_array(keys, rd['user'].keys()), "The 'user' keys are not correct!") # assert 'user:name' is string self.assertEqual(type(rd['user']['name']), type(""), "The 'user:name' is not string") # assert 'user:custom_permission' is list self.assertEqual(type(rd['user']['custom_permission']), type([]), "The 'user:custom_permission' is not list") # assert 'user:email' is correct format pe = re.compile("[\w\-\._]+@[\w]+\.[\w]+") self.assertTrue(pe.match(rd['user']['email']), "The 'user:email' does not match email address") # assert 'user:id' is integer and equal to the 'user_id' self.assertEqual(rd['user']['id'], rd['user_id'], "The 'user:id' does not equal to 'user_id'") # assert 'client_id' is integer self.assertEqual(type(rd['client_id']), type(1), "The 'client_id' is not integer") # assert 'access_token_secret' is 32 character long self.assertEqual(len(rd['access_token_secret']), 32, "The 'access_token_secret' is not 32 character-long") # assert 'permissions' is a list and values are in the given range self.assertEqual(type(rd['permissions']), type([]), "The 'permissions' is not a list") permissions = ['triton_manifest', 'triton_read', 'triton_write', 'mip_fs_read', 'mip_fs_write', 'mip_photos_read', 'mip_photos_write'] for p in rd['permissions']: assertTrue( p in permissions, "The 'permissions' - '%s' is not in the permission list" % p) def test_negative_get_tokens_invalid_token(self): """ Check exception raised while getting tokens information with invalid tokens, and verify the exception contains correct error messages. """ malpayloads = [utils.random_str()] for mp in malpayloads: logging.info("The malpayload acting unauthorized client is '%s'" % mp) with self.assertRaises(Exception) as e: self.res.get('/tokens/'+mp, headers=utils.headers) self.assertEqual(self.res.response.status, 404) # verify the retrieved exception is expected utils.verify_rest_requetfailed_exception(e, utils.get_exception('InvalidToken', 'GetTokensExceptions'), self) def test_negative_get_tokens_parameter_missing(self): """ Check exception raised while getting tokens information without tokens given, and verify the exception contains correct error messages. """ malpayloads = [''] for mp in malpayloads: logging.info("The malpayload acting unauthorized client is '%s'" % mp) with self.assertRaises(Exception) as e: self.res.get('/tokens/'+mp, headers=utils.headers) self.assertEqual(self.res.response.status, 405)
from restclient import Resource import random import json s = 'http://localhost:8089/' u0 = str(random.randint(0, 10000)) u1 = str(random.randint(0, 10000)) print(u0, u1) res = Resource(s) print res.post('api/%s/' % u0) print res.post('api/%s/msg' % u0, payload='Immmmm') msg2_id = json.loads(res.post('api/%s/msg' % u0, payload='sailing'))['msg_id'] print res.post('api/%s/msg' % u0, payload='away') print res.get('api/%s/' % u0) print res.delete('api/%s/msg/%s/' % (u0, msg2_id)) print res.post('api/%s/' % u1) r = res.get() print(r) print res.delete('api/%s/' % u0) r = res.get() print(r)
from restclient import Resource import random import json s = 'http://localhost:8089/' u0 = str(random.randint(0, 10000)) u1 = str(random.randint(0, 10000)) print (u0, u1) res = Resource(s) print res.post('api/%s/' % u0) print res.post('api/%s/msg' % u0, payload='Immmmm') msg2_id = json.loads(res.post('api/%s/msg' % u0, payload='sailing'))['msg_id'] print res.post('api/%s/msg' % u0, payload='away') print res.get('api/%s/' % u0) print res.delete('api/%s/msg/%s/' % (u0, msg2_id)) print res.post('api/%s/' % u1) r = res.get() print(r) print res.delete('api/%s/' % u0) r = res.get() print(r)