From 1f7655dafedaebbc1f5f6c993e85846cd6e01cf7 Mon Sep 17 00:00:00 2001 From: Joshua Hiller Date: Wed, 6 Sep 2023 12:56:09 -0400 Subject: [PATCH] Expand unit testing to complete code coverage --- tests/test_authentications.py | 12 +++++++++--- tests/test_falcon_container.py | 9 ++++++++- tests/test_service_class.py | 9 ++++++--- tests/test_uber.py | 23 +++++++++++++++-------- tests/test_uber_api_complete.py | 20 ++++++++++++++++++-- 5 files changed, 56 insertions(+), 17 deletions(-) diff --git a/tests/test_authentications.py b/tests/test_authentications.py index 2a378a322..d218f10cd 100644 --- a/tests/test_authentications.py +++ b/tests/test_authentications.py @@ -86,15 +86,21 @@ def serviceAny_forceCrossCloudResponseFailure(self): return False def serviceAny_forceCrossCloudResponseGovFailure(self): + _success = False falcon = OAuth2(client_id=os.environ["CROSS_DEBUG_KEY"], client_secret=os.environ["CROSS_DEBUG_SECRET"], base_url="us1", debug=_DEBUG ) result = falcon.token() if result["status_code"] == 403: - return True - else: - return False + falcon = APIHarness(client_id=os.environ["CROSS_DEBUG_KEY"], + client_secret=os.environ["CROSS_DEBUG_SECRET"], + base_url="us1", debug=_DEBUG + ) + result = falcon.authenticate() + if falcon.token_status == 403: + _success = True + return _success def serviceAny_checkRegionNameLookups(self): falcon = OAuth2(client_id=auth.config["falcon_client_id"], diff --git a/tests/test_falcon_container.py b/tests/test_falcon_container.py index c255f08a9..892530625 100644 --- a/tests/test_falcon_container.py +++ b/tests/test_falcon_container.py @@ -9,7 +9,7 @@ # Import our sibling src folder into the path sys.path.append(os.path.abspath('src')) # Classes to test - manually imported from sibling folder -from falconpy import FalconContainer, APIHarness +from falconpy import FalconContainer, APIHarness, APIHarnessV2 auth = Authorization.TestAuthorization() config = auth.getConfigObject() @@ -18,6 +18,10 @@ client_secret=falcon.auth_object.creds["client_secret"], base_url=falcon.auth_object.base_url ) +uber2 = APIHarnessV2(client_id=falcon.auth_object.creds["client_id"], + client_secret=falcon.auth_object.creds["client_secret"], + base_url=falcon.auth_object.base_url + ) AllowedResponses = [200, 201, 204, 400, 403, 404, 429, 500, 502] # Allowing no content returned as code paths are confirmed @@ -33,6 +37,9 @@ def run_tests(self): "GetAssessmentUber": uber.command("GetImageAssessmentReport", repository="misp", tag="latest"), "DeleteImageDetailsUber": uber.command("DeleteImageDetails", image_id="12345678"), "ImageMatchesPolicyUber": uber.command("ImageMatchesPolicy", repository="whatever", tag="whatever"), + "GetAssessmentUber2": uber2.command("GetImageAssessmentReport", repository="misp", tag="latest"), + "DeleteImageDetailsUber2": uber2.command("DeleteImageDetails", image_id="12345678"), + "ImageMatchesPolicyUber2": uber2.command("ImageMatchesPolicy", repository="whatever", tag="whatever"), "read_image_vulnerabilities": falcon.read_image_vulnerabilities(osversion="Windows", packages={"LayerIndex": 1}), "ReadRegistryEntities": falcon.read_registry_entities(), "ReadRegistryEntitiesByUUID": falcon.read_registry_entities_by_uuid(ids="12345678"), diff --git a/tests/test_service_class.py b/tests/test_service_class.py index c360444b1..3fd946382 100644 --- a/tests/test_service_class.py +++ b/tests/test_service_class.py @@ -300,8 +300,10 @@ def test_auth_object_invalid_config(self): @not_supported def test_log_facility_shutdown(self): _thing = OAuth2(creds=config.creds, debug=True) - if _thing.log_facility.active: - _thing.log_facility.deactivate_log() + _thing.login() + if _thing.authenticated() and not _thing.token_expired(): # Duplicative, testing methods + if _thing.log_facility.active: + _thing.log_facility.deactivate_log() assert not _thing.log @@ -364,7 +366,8 @@ def test_list_response_component_get_property_fail(self): with pytest.warns(SSLDisabledWarning): _no_ssl = Hosts(creds=config.creds, pythonic=True, debug=_DEBUG, ssl_verify=False) try: - _thing: Result = _no_ssl.query_devices(limit=3) + if _no_ssl.token_valid and not _no_ssl.token_stale: # Duplicative, just testing the properties + _thing: Result = _no_ssl.query_devices(limit=3) except APIError: pytest.skip("SSL required for GovCloud testing.") diff --git a/tests/test_uber.py b/tests/test_uber.py index b2c0820d3..d6d9f4055 100644 --- a/tests/test_uber.py +++ b/tests/test_uber.py @@ -12,7 +12,7 @@ # flake8: noqa=E402 # pylint: disable=C0103 # Classes to test - manually imported from our sibling folder -from falconpy import APIAdvanced, APIError +from falconpy import APIHarnessV2, APIError # Import perform_request from _util so we can test generating 405's directly from falconpy._util import perform_request, force_default @@ -37,7 +37,7 @@ else: sys.exit(1) -falcon = APIAdvanced( +falcon = APIHarnessV2( client_id=config["falcon_client_id"], client_secret=config["falcon_client_secret"], base_url=config["falcon_base_url"], debug=_DEBUG @@ -46,10 +46,14 @@ class TestUber: def uberCCAWS_GetAWSSettings(self): + returned = False + authenticated = falcon.authenticated() + expired = falcon.token_expired() if falcon.command("GetAWSSettings")["status_code"] in AllowedResponses: - return True + returned = True else: - return False + returned = False + return returned def uberCCAWS_QueryAWSAccounts(self): if falcon.command("QueryAWSAccounts", parameters={"limit": 1})["status_code"] in AllowedResponses: @@ -235,14 +239,14 @@ def uberCCAWS_GenerateTokenError(self): return returned def uberCCAWS_BadAuthentication(self): - falcon = APIAdvanced(debug=_DEBUG) + falcon = APIHarnessV2(debug=_DEBUG) if falcon.command("QueryAWSAccounts", parameters={"limit": 1})["status_code"] in AllowedResponses: return True else: return False def uberCCAWS_DisableSSLVerify(self): - falcon = APIAdvanced( + falcon = APIHarnessV2( creds={ "client_id": config["falcon_client_id"], "client_secret": config["falcon_client_secret"] @@ -354,13 +358,16 @@ def test_uber_deprecated_attributes(self): def test_uber_properties(self): # Force a new object so we can flip the debug flag - temp_falcon = APIAdvanced(access_token=falcon.token_value, + temp_falcon = APIHarnessV2(access_token=falcon.token_value, base_url=config["falcon_base_url"], debug=True ) assert bool(temp_falcon.debug) + def test_uber_token_generate(self): + assert(bool(falcon.command("oauth2AccessToken")["status_code"] == 201)) + def test_uber_revoke_failure(self): assert bool(falcon.command("oauth2RevokeToken")["status_code"] == 400) @@ -371,7 +378,7 @@ def test_uber_revoke_success(self): def test_pythonic_failure(self): _success = False - new_falcon = APIAdvanced(access_token=falcon.token_value, + new_falcon = APIHarnessV2(access_token=falcon.token_value, base_url=config["falcon_base_url"], debug=_DEBUG, pythonic=True diff --git a/tests/test_uber_api_complete.py b/tests/test_uber_api_complete.py index 85bd8bd0a..90f7c2cb9 100644 --- a/tests/test_uber_api_complete.py +++ b/tests/test_uber_api_complete.py @@ -166,8 +166,9 @@ def uberCCAWS_OverrideAndHeader(self): def uberContainer_TestBodyIDsPayload(self): successful = False - if falcon.command("GetDeviceDetails", ids="12345678")["status_code"] in AllowedResponses: - successful = True + if falcon.authenticated and not falcon.token_expired(): + if falcon.command("GetDeviceDetails", ids="12345678")["status_code"] in AllowedResponses: + successful = True return successful @@ -339,6 +340,21 @@ def test_BadAuthentication(self): def test_DisableSSLVerify(self): assert self.uberCCAWS_DisableSSLVerify() is True + def test_crossover_properties(self): + _success = True + try: + _ = falcon.token_value + debug_setting = falcon.debug + pythonic_setting = falcon.pythonic + falcon.pythonic = False + debug_rec_count = falcon.debug_record_count + falcon.debug_record_count = 101 + sanitize = falcon.sanitize_log + falcon.sanitize_log = True + except: + _success = False + assert _success + # def test_uber_deprecated_methods(self): # assert bool(falcon.valid_cred_format() # and falcon.headers()