From 59e2cf55b5bc67721dfe4db07f12239505b4907b Mon Sep 17 00:00:00 2001 From: Miguel Ballesteros Date: Wed, 28 Dec 2022 19:51:08 +0100 Subject: [PATCH] Reorganized tests --- test/auth/test_oauthuser.py | 69 ++++++++++++ test/{ => auth}/test_userkey.py | 106 ++++++------------ .../{test_analytics.py => test_analytics.txt} | 0 test/{test_company.py => test_company.txt} | 0 ..._dictionaries.py => test_dictionaries.txt} | 0 test/{test_explain.py => test_explain.txt} | 0 ...test_extraction.py => test_extraction.txt} | 0 ...napshot_init.py => test_snapshot_init.txt} | 0 ...napshotquery.py => test_snapshotquery.txt} | 0 ...st_stream_init.py => test_stream_init.txt} | 0 test/test_unit_userkey.py | 48 -------- test/{test_updates.py => test_updates.txt} | 0 12 files changed, 105 insertions(+), 118 deletions(-) create mode 100755 test/auth/test_oauthuser.py rename test/{ => auth}/test_userkey.py (54%) rename test/{test_analytics.py => test_analytics.txt} (100%) rename test/{test_company.py => test_company.txt} (100%) rename test/{test_dictionaries.py => test_dictionaries.txt} (100%) rename test/{test_explain.py => test_explain.txt} (100%) rename test/{test_extraction.py => test_extraction.txt} (100%) rename test/{test_snapshot_init.py => test_snapshot_init.txt} (100%) rename test/{test_snapshotquery.py => test_snapshotquery.txt} (100%) rename test/{test_stream_init.py => test_stream_init.txt} (100%) delete mode 100644 test/test_unit_userkey.py rename test/{test_updates.py => test_updates.txt} (100%) diff --git a/test/auth/test_oauthuser.py b/test/auth/test_oauthuser.py new file mode 100755 index 0000000..0cfaeae --- /dev/null +++ b/test/auth/test_oauthuser.py @@ -0,0 +1,69 @@ +""" + Tests for the UserKey class +""" +import pytest +from factiva.analytics import OAuthUser +from factiva.analytics.common import config + +FACTIVA_CLIENTID = config.load_environment_value("FACTIVA_CLIENTID") +FACTIVA_USERNAME = config.load_environment_value("FACTIVA_USERNAME") +FACTIVA_PASSWORD = config.load_environment_value("FACTIVA_PASSWORD") + + +def _test_oauthuser_types(usr): + """" + Checks the correct types were returned. + """ + assert isinstance(usr.current_jwt_token, str) + + +def _test_oauthuser_values(usr): + """ + Checks if values within the expected lengths and ranges + were returned + """ + assert len(usr.current_jwt_token.split('.')) == 3 + + +def test_oauthuser_create_from_env(): + """" + Creates the object using the ENV variables + """ + usr = OAuthUser() + _test_oauthuser_types(usr) + _test_oauthuser_values(usr) + + +def test_oauthuser_create_from_params(): + """ + Creates an empty object from the passed params + """ + usr = OAuthUser(client_id=FACTIVA_CLIENTID, + username=FACTIVA_USERNAME, + password=FACTIVA_PASSWORD) + _test_oauthuser_types(usr) + _test_oauthuser_values(usr) + + +def test_wrong_credentials(): + """ + Creates an object from the provided string + The key is invalid and this should validate how the error is processed + """ + with pytest.raises(PermissionError, match=r'Invalid user credentials'): + o = OAuthUser(client_id='client_id_value', + username='username_value', + password='password_value') + o.get_id_token() + + +def test_invald_param_types(): + """ + Attempts to create an object with wrong parameter types + """ + with pytest.raises(ValueError, match=r'The client_id param must be a string'): + OAuthUser({}, 'abc', 'abc') + with pytest.raises(ValueError, match=r'The username param must be a string'): + OAuthUser('abc', 44.5, 'abc') + with pytest.raises(ValueError, match=r'The password param must be a string'): + OAuthUser('abc', 'abc', 123) diff --git a/test/test_userkey.py b/test/auth/test_userkey.py similarity index 54% rename from test/test_userkey.py rename to test/auth/test_userkey.py index bec2ffb..910c139 100755 --- a/test/test_userkey.py +++ b/test/auth/test_userkey.py @@ -3,9 +3,9 @@ """ import pytest from factiva.analytics import UserKey -from factiva.analytics.common.tools import load_environment_value +from factiva.analytics.common import config -FACTIVA_USERKEY = load_environment_value("FACTIVA_USERKEY") +FACTIVA_USERKEY = config.load_environment_value("FACTIVA_USERKEY") DUMMY_KEY = 'abcd1234abcd1234abcd1234abcd1234' # API Response sample with the most complete set of attributes @@ -54,11 +54,12 @@ # } -def check_userkey_types(usr): +def _test_userkey_types(usr): """" Checks the correct types were returned. """ - usr = UserKey(stats=True) + if isinstance(usr, str): + usr = UserKey(stats=True) assert isinstance(usr.key, str) assert isinstance(usr.cloud_token, dict) assert isinstance(usr.account_name, str) @@ -76,84 +77,51 @@ def check_userkey_types(usr): assert isinstance(usr.enabled_company_identifiers, list) +def _test_userkey_values(usr): + """ + Checks if values within the expected lengths and ranges + were returned + """ + if isinstance(usr, str): + usr = UserKey(stats=True) + assert usr.key == FACTIVA_USERKEY + assert len(usr.account_name) >= 0 + assert len(usr.active_products) >= 0 + assert usr.max_allowed_concurrent_extractions >= 0 + assert usr.max_allowed_extracted_documents >= 0 + assert usr.max_allowed_extractions >= 0 + assert usr.total_downloaded_bytes >= 0 + assert usr.total_extracted_documents >= 0 + assert usr.total_extractions >= 0 + assert usr.total_stream_instances >= 0 + assert usr.total_stream_subscriptions >= 0 + assert len(usr.enabled_company_identifiers) >= 0 + def test_userkey_with_stats(): """" Creates the object using the ENV variable and request the usage details to the API service """ - aku = UserKey(stats=True) - check_userkey_types(aku) - assert aku.key == FACTIVA_USERKEY - assert len(aku.account_name) > 0 - assert len(aku.active_products) > 0 - assert aku.max_allowed_concurrent_extractions >= 0 - assert aku.max_allowed_extracted_documents >= 0 - assert aku.max_allowed_extractions >= 0 - assert aku.total_downloaded_bytes >= 0 - assert aku.total_extracted_documents >= 0 - assert aku.total_extractions >= 0 - assert aku.total_stream_instances >= 0 - assert aku.total_stream_subscriptions >= 0 - assert len(aku.enabled_company_identifiers) > 0 + usr = UserKey(stats=True) + _test_userkey_types(usr) + _test_userkey_values(usr) def test_userkey_without_stats(): """ Creates an empty object from the ENV variable with a value only for the key property """ - aku = UserKey() - check_userkey_types(aku) - assert aku.key == FACTIVA_USERKEY - assert len(aku.account_name) == 0 - assert len(aku.active_products) == 0 - assert aku.max_allowed_concurrent_extractions == 0 - assert aku.max_allowed_extracted_documents == 0 - assert aku.max_allowed_extractions == 0 - assert aku.total_downloaded_bytes == 0 - assert aku.total_extracted_documents == 0 - assert aku.total_extractions == 0 - assert aku.total_stream_instances == 0 - assert aku.total_stream_subscriptions == 0 - assert len(aku.enabled_company_identifiers) == 0 + usr = UserKey() + _test_userkey_types(usr) + _test_userkey_values(usr) def test_user_with_parameter_and_stats(): """ API Key is passed as a string and stats=True """ - aku = UserKey(key=FACTIVA_USERKEY, stats=True) - check_userkey_types(aku) - assert aku.key == FACTIVA_USERKEY - assert len(aku.account_name) > 0 - assert len(aku.active_products) > 0 - assert aku.max_allowed_concurrent_extractions >= 0 - assert aku.max_allowed_extracted_documents >= 0 - assert aku.max_allowed_extractions >= 0 - assert aku.total_downloaded_bytes >= 0 - assert aku.total_extracted_documents >= 0 - assert aku.total_extractions >= 0 - assert aku.total_stream_instances >= 0 - assert aku.total_stream_subscriptions >= 0 - assert len(aku.enabled_company_identifiers) > 0 - - -def test_user_with_parameter_without_stats(): - """ - Creates an empty object from the provided string with a value only for the key property - """ - usr = UserKey(DUMMY_KEY) - check_userkey_types(usr) - assert usr.key == DUMMY_KEY - assert usr.account_name == '' - assert usr.active_products == '' - assert usr.max_allowed_concurrent_extractions == 0 - assert usr.max_allowed_extracted_documents == 0 - assert usr.max_allowed_extractions == 0 - assert usr.total_downloaded_bytes == 0 - assert usr.total_extracted_documents == 0 - assert usr.total_extractions == 0 - assert usr.total_stream_instances == 0 - assert usr.total_stream_subscriptions == 0 - assert len(usr.enabled_company_identifiers) == 0 + usr = UserKey(key=FACTIVA_USERKEY, stats=True) + _test_userkey_types(usr) + _test_userkey_values(usr) def test_invalid_key(): @@ -162,8 +130,7 @@ def test_invalid_key(): The key is invalid and this should validate how the error is processed """ with pytest.raises(ValueError, match=r'Factiva User-Key does not exist or inactive.'): - usr = UserKey(DUMMY_KEY, stats=True) - assert usr.account_name != '' + UserKey(DUMMY_KEY, stats=True) def test_invald_lenght_key(): @@ -171,5 +138,4 @@ def test_invald_lenght_key(): Attempts to create an object with malformed keys. This requires assert the raised exception. """ with pytest.raises(ValueError, match=r'Factiva User-Key has the wrong length'): - usr = UserKey('abc') - assert usr.account_name != '' + UserKey('abc') diff --git a/test/test_analytics.py b/test/test_analytics.txt similarity index 100% rename from test/test_analytics.py rename to test/test_analytics.txt diff --git a/test/test_company.py b/test/test_company.txt similarity index 100% rename from test/test_company.py rename to test/test_company.txt diff --git a/test/test_dictionaries.py b/test/test_dictionaries.txt similarity index 100% rename from test/test_dictionaries.py rename to test/test_dictionaries.txt diff --git a/test/test_explain.py b/test/test_explain.txt similarity index 100% rename from test/test_explain.py rename to test/test_explain.txt diff --git a/test/test_extraction.py b/test/test_extraction.txt similarity index 100% rename from test/test_extraction.py rename to test/test_extraction.txt diff --git a/test/test_snapshot_init.py b/test/test_snapshot_init.txt similarity index 100% rename from test/test_snapshot_init.py rename to test/test_snapshot_init.txt diff --git a/test/test_snapshotquery.py b/test/test_snapshotquery.txt similarity index 100% rename from test/test_snapshotquery.py rename to test/test_snapshotquery.txt diff --git a/test/test_stream_init.py b/test/test_stream_init.txt similarity index 100% rename from test/test_stream_init.py rename to test/test_stream_init.txt diff --git a/test/test_unit_userkey.py b/test/test_unit_userkey.py deleted file mode 100644 index cd30abb..0000000 --- a/test/test_unit_userkey.py +++ /dev/null @@ -1,48 +0,0 @@ -""" - Unit tests for the UseKey class -""" -import unittest -from factiva.analytics import UserKey -from factiva.analytics.common.tools import load_environment_value - -FACTIVA_USERKEY = load_environment_value("FACTIVA_USERKEY") -DUMMY_KEY = 'abcd1234abcd1234abcd1234abcd1234' - - -class TestUserKey(unittest.TestCase): - """ - Class for the UserKey unit tests - """ - - def test_create_user_with_env_key(self): - """ - Tests a UserKey instance creation from a valid key - """ - usr = UserKey(stats=True) - self.assertEqual(usr.key, FACTIVA_USERKEY) - - - def test_create_user_with_key_dummy(self): - """ - Tests a UserKey instance creation from a dummy key. - No API request is sent, so, no error is expected. - """ - usr = UserKey(key=DUMMY_KEY) - self.assertNotEqual(usr.key, FACTIVA_USERKEY) - - - def test_create_user_with_invalid_key(self): - """ - Tests a UserKey instance creation from a key with the wrong length - """ - with self.assertRaises(ValueError): - UserKey(key='aabbcc') - - def test_fetch_cloud_token(self): - """ - Tests a UserKey instance creation from a valid key (env), and - checks the returned cloud token is valid. - """ - usr = UserKey() - usr.get_cloud_token() - self.assertNotEqual(usr.cloud_token, {}) diff --git a/test/test_updates.py b/test/test_updates.txt similarity index 100% rename from test/test_updates.py rename to test/test_updates.txt