[Python] Conditionally set auth attributes (#4988)

* Enhance python API keys

* Run python scripts under ./bin/openapi3

* fix unit test issue

* Fix unit tests

* Fix unit tests

* Invoke bin scripts
This commit is contained in:
Sebastien Rosset
2020-01-16 01:42:23 -08:00
committed by William Cheng
parent b94fe7a40f
commit fa0ef2be25
16 changed files with 440 additions and 235 deletions

View File

@@ -55,6 +55,29 @@ class ApiClientTests(unittest.TestCase):
self.assertEqual('test_username', client.configuration.username)
self.assertEqual('test_password', client.configuration.password)
# test api key without prefix
config.api_key['api_key'] = '123456'
config.api_key_prefix['api_key'] = None
# update parameters based on auth setting
client.update_params_for_auth(header_params, query_params, auth_settings)
self.assertEqual(header_params['api_key'], '123456')
# test api key with empty prefix
config.api_key['api_key'] = '123456'
config.api_key_prefix['api_key'] = ''
# update parameters based on auth setting
client.update_params_for_auth(header_params, query_params, auth_settings)
self.assertEqual(header_params['api_key'], '123456')
# test api key with prefix specified in the api_key, useful when the prefix
# must include '=' sign followed by the API key secret without space.
config.api_key['api_key'] = 'PREFIX=123456'
config.api_key_prefix['api_key'] = None
# update parameters based on auth setting
client.update_params_for_auth(header_params, query_params, auth_settings)
self.assertEqual(header_params['api_key'], 'PREFIX=123456')
def test_select_header_accept(self):
accepts = ['APPLICATION/JSON', 'APPLICATION/XML']
accept = self.api_client.select_header_accept(accepts)

View File

@@ -73,6 +73,7 @@ class PetApiTests(unittest.TestCase):
def setUp(self):
config = Configuration()
config.host = HOST
config.access_token = 'ACCESS_TOKEN'
self.api_client = petstore_api.ApiClient(config)
self.pet_api = petstore_api.PetApi(self.api_client)
self.setUpModels()
@@ -115,6 +116,26 @@ class PetApiTests(unittest.TestCase):
resp.close()
resp.release_conn()
def test_config(self):
config = Configuration(host=HOST)
self.assertIsNotNone(config.get_host_settings())
self.assertEquals(config.get_basic_auth_token(),
urllib3.util.make_headers(basic_auth=":").get('authorization'))
self.assertEquals(len(config.auth_settings()), 1)
self.assertIn("petstore_auth", config.auth_settings().keys())
config.username = "user"
config.password = "password"
self.assertEquals(
config.get_basic_auth_token(),
urllib3.util.make_headers(basic_auth="user:password").get('authorization'))
self.assertEquals(len(config.auth_settings()), 2)
self.assertIn("petstore_auth", config.auth_settings().keys())
self.assertIn("http_basic_test", config.auth_settings().keys())
config.username = None
config.password = None
self.assertEquals(len(config.auth_settings()), 1)
self.assertIn("petstore_auth", config.auth_settings().keys())
def test_timeout(self):
mock_pool = MockPoolManager(self)
self.api_client.rest_client.pool_manager = mock_pool
@@ -122,13 +143,13 @@ class PetApiTests(unittest.TestCase):
mock_pool.expect_request('POST', 'http://localhost/v2/pet',
body=json.dumps(self.api_client.sanitize_for_serialization(self.pet)),
headers={'Content-Type': 'application/json',
'Authorization': 'Bearer ',
'Authorization': 'Bearer ACCESS_TOKEN',
'User-Agent': 'OpenAPI-Generator/1.0.0/python'},
preload_content=True, timeout=TimeoutWithEqual(total=5))
mock_pool.expect_request('POST', 'http://localhost/v2/pet',
body=json.dumps(self.api_client.sanitize_for_serialization(self.pet)),
headers={'Content-Type': 'application/json',
'Authorization': 'Bearer ',
'Authorization': 'Bearer ACCESS_TOKEN',
'User-Agent': 'OpenAPI-Generator/1.0.0/python'},
preload_content=True, timeout=TimeoutWithEqual(connect=1, read=2))
@@ -325,7 +346,7 @@ class PetApiTests(unittest.TestCase):
'Accept': 'application/json',
'Content-Type': 'multipart/form-data',
'User-Agent': 'OpenAPI-Generator/1.0.0/python',
'Authorization': 'Bearer '
'Authorization': 'Bearer ACCESS_TOKEN'
},
post_params=[
('files', ('1px_pic1.png', b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x00\x00\x00\x00:~\x9bU\x00\x00\x00\nIDATx\x9cc\xfa\x0f\x00\x01\x05\x01\x02\xcf\xa0.\xcd\x00\x00\x00\x00IEND\xaeB`\x82', 'image/png')),