def test_connection_pooled(self): configuration.base_url = 'http://localhost:65521' run_server() request = CoreSendRequest() data = {'event': '$login.authenticate', 'user_id': '12345'} response = request.build_query('post', 'authenticate', data) num_pools = len(response.connection.poolmanager.pools.keys()) configuration.base_url = 'https://api.castle.io/v1' self.assertEqual(num_pools, 1)
def test_build_query(self): data = {'event': '$login.authenticate', 'user_id': '12345'} configuration.api_secret = 'api_secret' # JSON requires double quotes for its strings response_text = {"action": "allow", "user_id": "12345"} responses.add( responses.POST, 'https://api.castle.io/v1/authenticate', json=response_text, status=200 ) res = CoreSendRequest().build_query('post', 'authenticate', data) self.assertIsInstance(res, Response) self.assertEqual(res.status_code, 200) self.assertEqual(res.json(), response_text) configuration.api_secret = None
class APIRequest(object): def __init__(self, config=configuration): self.req = CoreSendRequest({'Content-Type': 'application/json'}) self.config = config def request(self, command): if not self.config.isValid(): raise ConfigurationError return self.req.build_query(command.method, command.path, command.data, self.config) def call(self, command): return CoreProcessResponse(self.request(command)).call()
def test_verify_false(self): configuration.base_url = 'http://api.castle.io' self.assertEqual(CoreSendRequest().verify(), False) configuration.base_url = 'https://api.castle.io/v1'
def test_verify_true(self): self.assertEqual(CoreSendRequest().verify(), True)
def test_build_url_with_port(self): configuration.base_url = 'http://api.castle.local:3001' self.assertEqual( CoreSendRequest().build_url('test'), 'http://api.castle.local:3001/test' )
def test_build_url(self): self.assertEqual( CoreSendRequest().build_url('authenticate'), 'https://api.castle.io/v1/authenticate' )
def test_init_base_url(self): self.assertEqual(CoreSendRequest().base_url, 'https://api.castle.io/v1')
def test_init_headers(self): headers = {'X-Castle-Client-Id': '1234'} self.assertEqual(CoreSendRequest(headers).headers, headers)
def __init__(self, config=configuration): self.req = CoreSendRequest({'Content-Type': 'application/json'}) self.config = config