示例#1
0
 def test_good_init(self, mock):
     '''Test for a successful init'''
     token = '1234-1234-1234-1234'
     mock.register_uri('POST',
                       '{0}/{1}/oauth2/token'.format(
                           constants.OAUTH2_ENDPOINT, self.tenant_id),
                       json={'access_token': token},
                       status_code=httplib.OK)
     connection.AzureConnection(logger=self.log)
示例#2
0
 def __init__(self, name, endpoint,
              api_version=None, logger=None,
              _ctx=ctx):
     # Set the active context
     self.ctx = _ctx
     # Configure logger
     self.log = utils.create_child_logger(
         'resources.{0}'.format(name.replace(' ', '')),
         plogger=logger)
     # Set up labeling
     self.name = name
     # Build the partial endpoint
     self.endpoint = endpoint
     # Get a connection
     self.client = connection.AzureConnection(
         api_version=api_version,
         logger=self.log,
         _ctx=self.ctx)
示例#3
0
 def test_good_request(self, mock):
     '''Test for a successful request'''
     token = '1234-1234-1234-1234'
     mock.register_uri('POST',
                       '{0}/{1}/oauth2/token'.format(
                           constants.OAUTH2_ENDPOINT, self.tenant_id),
                       json={'access_token': token},
                       status_code=httplib.OK)
     mock.register_uri('GET',
                       '{0}/subscriptions/{1}{2}'.format(
                           constants.CONN_API_ENDPOINT,
                           self.subscription_id, '/some/resource'),
                       json={'data': 'stuff'},
                       status_code=httplib.OK)
     conn = connection.AzureConnection(logger=self.log)
     res = conn.request(url='/some/resource', method='get')
     self.assertEqual(res.status_code, httplib.OK)
     self.assertEqual(res.json().get('data'), 'stuff')
示例#4
0
 def test_bad_credentials_request(self, mock):
     '''Test for a request using bad credentials'''
     token = '1234-1234-1234-1234'
     mock.register_uri('POST',
                       '{0}/{1}/oauth2/token'.format(
                           constants.OAUTH2_ENDPOINT, self.tenant_id),
                       json={'access_token': token},
                       status_code=httplib.OK)
     mock.register_uri('GET',
                       '{0}/subscriptions/{1}{2}'.format(
                           constants.CONN_API_ENDPOINT,
                           self.subscription_id, '/some/resource'),
                       json={'error': 'invalid_client'},
                       status_code=httplib.UNAUTHORIZED)
     conn = connection.AzureConnection(logger=self.log)
     self.assertRaises(exceptions.InvalidCredentials,
                       conn.request,
                       url='/some/resource',
                       method='get')
示例#5
0
 def test_unauthorized_request(self, mock):
     '''Test for an unauthorized request'''
     token = '1234-1234-1234-1234'
     mock.register_uri('POST',
                       '{0}/{1}/oauth2/token'.format(
                           constants.OAUTH2_ENDPOINT, self.tenant_id),
                       json={'access_token': token},
                       status_code=httplib.OK)
     mock.register_uri('GET',
                       '{0}/subscriptions/{1}{2}'.format(
                           constants.CONN_API_ENDPOINT,
                           self.subscription_id, '/some/resource'),
                       json={'error': 'unknown_code'},
                       status_code=httplib.UNAUTHORIZED)
     conn = connection.AzureConnection(logger=self.log)
     self.assertRaises(exceptions.UnauthorizedRequest,
                       conn.request,
                       url='/some/resource',
                       method='get')