Require api keys

This commit is contained in:
Madison May
2015-03-26 21:01:11 -04:00
parent 7cde7a7fdb
commit f7c263dc9c
16 changed files with 182 additions and 253 deletions
+19 -33
View File
@@ -27,11 +27,9 @@ class TestConfigureEnv(unittest.TestCase):
"""
Ensure cloud authentication credentials are read in from environment variables
"""
username = "test"
password = "password"
os.environ["INDICO_USERNAME"] = username
os.environ["INDICO_PASSWORD"] = password
assert config.SETTINGS.auth() == (username, password)
api_key = "text"
os.environ["INDICO_API_KEY"] = api_key
assert config.SETTINGS.api_key() == api_key
class TestConfigurationFile(unittest.TestCase):
@@ -40,17 +38,15 @@ class TestConfigurationFile(unittest.TestCase):
"""
def setUp(self):
self.username = "test"
self.password = "password"
self.api_key = "test"
self.cloud = "localhost"
config = """
[auth]
username = %s
password = %s
api_key = %s
[private_cloud]
cloud = %s
""" % (self.username, self.password, self.cloud)
""" % (self.api_key, self.cloud)
config_file = StringIO(textwrap.dedent(config))
self.settings = Settings(files=[config_file])
@@ -66,7 +62,7 @@ class TestConfigurationFile(unittest.TestCase):
"""
Ensure cloud authentication credentials are read in from file
"""
assert self.settings.auth() == (self.username, self.password)
assert self.settings.api_key() == self.api_key
class TestPrecedence(unittest.TestCase):
@@ -75,27 +71,23 @@ class TestPrecedence(unittest.TestCase):
"""
def setUp(self):
self.file_username = "file-username"
self.file_password = "file-password"
self.file_api_key = "file-api-key"
self.file_cloud = "file-cloud"
self.env_username = "env-username"
self.env_password = "env-password"
self.env_api_key = "env-api-key"
self.env_cloud = "env-cloud"
config = """
[auth]
username = %s
password = %s
api_key = %s
[private_cloud]
cloud = %s
""" % (self.file_username, self.file_password, self.file_cloud)
""" % (self.file_api_key, self.file_cloud)
config_file = StringIO(textwrap.dedent(config))
os.environ = {
'INDICO_CLOUD': self.env_cloud,
'INDICO_USERNAME': self.env_username,
'INDICO_PASSWORD': self.env_password
'INDICO_API_KEY': self.env_api_key
}
self.settings = Settings(files=[config_file])
@@ -110,7 +102,7 @@ class TestPrecedence(unittest.TestCase):
Ensure cloud authentication credentials set in environment variables
are used over those in config files
"""
assert self.settings.auth() == (self.env_username, self.env_password)
assert self.settings.api_key() == self.env_api_key
class TestConfigFilePrecedence(unittest.TestCase):
@@ -119,37 +111,31 @@ class TestConfigFilePrecedence(unittest.TestCase):
"""
def setUp(self):
self.high_priority_username = "high-priority-username"
self.high_priority_password = "high-priority-password"
self.high_priority_api_key = "high-priority-api-key"
self.high_priority_cloud = "high-priority-cloud"
self.low_priority_username = "low-priority-username"
self.low_priority_password = "low-priority-password"
self.low_priority_api_key = "low-priority-api-key"
self.low_priority_cloud = "low-priority-cloud"
high_priority_config = """
[auth]
username = %s
password = %s
api_key = %s
[private_cloud]
cloud = %s
""" % (
self.high_priority_username,
self.high_priority_password,
self.high_priority_api_key,
self.high_priority_cloud
)
low_priority_config = """
[auth]
username = %s
password = %s
[private_cloud]
cloud = %s
""" % (
self.low_priority_username,
self.low_priority_password,
self.low_priority_api_key,
self.low_priority_cloud
)
@@ -172,4 +158,4 @@ class TestConfigFilePrecedence(unittest.TestCase):
"""
Ensure the cloud auth priority is handled properly
"""
assert self.settings.auth() == (self.high_priority_username, self.high_priority_password)
assert self.settings.api_key() == self.high_priority_api_key
+50 -13
View File
@@ -16,36 +16,40 @@ DIR = os.path.dirname(os.path.realpath(__file__))
class BatchAPIRun(unittest.TestCase):
def setUp(self):
self.auth = config.AUTH
self.api_key = config.api_key
config.url_protocol = "http:"
if not all(self.auth):
if not all(self.api_key):
raise SkipTest
def tearDown(self):
config.url_protocol = "https:"
def test_batch_texttags(self):
test_data = ["On Monday, president Barack Obama will be..."]
response = batch_text_tags(test_data, auth=self.auth)
response = batch_text_tags(test_data, api_key=self.api_key)
self.assertTrue(isinstance(response, list))
def test_batch_posneg(self):
test_data = ['Worst song ever', 'Best song ever']
response = batch_sentiment(test_data, auth=self.auth)
response = batch_sentiment(test_data, api_key=self.api_key)
self.assertTrue(isinstance(response, list))
self.assertTrue(response[0] < 0.5)
def test_batch_political(self):
test_data = ["Guns don't kill people, people kill people."]
response = batch_political(test_data, auth=self.auth)
response = batch_political(test_data, api_key=self.api_key)
self.assertTrue(isinstance(response, list))
def test_batch_fer(self):
test_data = [np.random.rand(48, 48).tolist()]
response = batch_fer(test_data, auth=self.auth)
response = batch_fer(test_data, api_key=self.api_key)
self.assertTrue(isinstance(response, list))
self.assertTrue(isinstance(response[0], dict))
def test_batch_facial_features(self):
test_data = [np.random.rand(48, 48).tolist()]
response = batch_facial_features(test_data, auth=self.auth)
response = batch_facial_features(test_data, api_key=self.api_key)
self.assertTrue(isinstance(response, list))
self.assertTrue(isinstance(response[0], list))
self.assertEqual(len(response[0]), 48)
@@ -65,21 +69,21 @@ class BatchAPIRun(unittest.TestCase):
def test_batch_image_features_greyscale(self):
test_data = [np.random.rand(64, 64).tolist()]
response = batch_image_features(test_data, auth=self.auth)
response = batch_image_features(test_data, api_key=self.api_key)
self.assertTrue(isinstance(response, list))
self.assertTrue(isinstance(response[0], list))
self.assertEqual(len(response[0]), 2048)
def test_batch_image_features_rgb(self):
test_data = [np.random.rand(64, 64, 3).tolist()]
response = batch_image_features(test_data, auth=self.auth)
response = batch_image_features(test_data, api_key=self.api_key)
self.assertTrue(isinstance(response, list))
self.assertTrue(isinstance(response[0], list))
self.assertEqual(len(response[0]), 2048)
def test_batch_language(self):
test_data = ['clearly an english sentence']
response = batch_language(test_data, auth=self.auth)
response = batch_language(test_data, api_key=self.api_key)
self.assertTrue(isinstance(response, list))
self.assertTrue(response[0]['English'] > 0.25)
@@ -88,7 +92,7 @@ class BatchAPIRun(unittest.TestCase):
self.assertRaises(ConnectionError,
batch_language,
test_data,
auth=self.auth,
api_key=self.api_key,
cloud='invalid/cloud')
@@ -124,11 +128,11 @@ class FullAPIRun(unittest.TestCase):
self.assertTrue(isinstance(response, dict))
self.assertEqual(political_set, set(response.keys()))
test_string = "Save the whales"
test_string = "pro-choice"
response = political(test_string)
self.assertTrue(isinstance(response, dict))
assert response['Green'] > 0.5
assert response['Liberal'] > 0.25
def test_posneg(self):
test_string = "Worst song ever."
@@ -252,6 +256,39 @@ class FullAPIRun(unittest.TestCase):
test_data,
cloud='invalid/cloud')
temp_cloud = config.cloud
config.cloud = 'invalid/cloud'
self.assertEqual(config.cloud, 'invalid/cloud')
self.assertRaises(ConnectionError,
language,
test_data)
config.cloud = temp_cloud
self.assertRaises(ConnectionError,
language,
test_data,
cloud='indico-test')
def test_set_api_key(self):
test_data = 'clearly an english sentence'
self.assertRaises(ValueError,
language,
test_data,
api_key ='invalid_api_key')
temp_api_key = config.api_key
config.api_key = 'invalid_api_key'
self.assertEqual(config.api_key, 'invalid_api_key')
self.assertRaises(ValueError,
language,
test_data)
config.api_key = temp_api_key
if __name__ == "__main__":
unittest.main()