Skip to content

Commit

Permalink
Reorganized tests
Browse files Browse the repository at this point in the history
  • Loading branch information
miballe committed Dec 28, 2022
1 parent 3d43bea commit 59e2cf5
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 118 deletions.
69 changes: 69 additions & 0 deletions test/auth/test_oauthuser.py
Original file line number Diff line number Diff line change
@@ -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)
106 changes: 36 additions & 70 deletions test/test_userkey.py → test/auth/test_userkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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():
Expand All @@ -162,14 +130,12 @@ 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():
"""
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')
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
48 changes: 0 additions & 48 deletions test/test_unit_userkey.py

This file was deleted.

File renamed without changes.

0 comments on commit 59e2cf5

Please sign in to comment.