From 85d5782c5d477934497a12d0664e4f070508dc86 Mon Sep 17 00:00:00 2001 From: jshcodes <74007258+jshcodes@users.noreply.github.com> Date: Thu, 24 Dec 2020 17:27:55 -0500 Subject: [PATCH] Fix to reduce test flakiness in test_authorization.py (#28) * Package layout updates * Package updates * Cleaning up tabs * README and LICENSE updates * More package testing * Broken link fixes * Uber class custom headers, Content-Type retained * v0.1.8 - Uber class custom headers * Uber class fix for octet-stream file uploads * README.md updates * Package development status alignment * Typo fix in README.md * Minor README.md text edits * Initial unit tests: Service and Uber Auth / Revoke * Initial unit tests: CCAWS - GetAWSSettings * Uber class fix for non-JSON API responses * Updated to support GitHub workflow execution * Working directory fix * Fixed authorization unit test 500 error * Adjusted workflow directory * Added working directory * Changed working directory * Working directory debugging * Debugging workflows * Lessee if this werks... * Reverted linting.yml change * Now there's a test package * Pytest debugging * Trying it another way * Another variation * Fix to reduce flakiness in test_authorization.py * Comment typo Co-authored-by: Shawn Wells --- tests/test_authorization.py | 71 +++++++++++++++++++++++-------------- 1 file changed, 45 insertions(+), 26 deletions(-) diff --git a/tests/test_authorization.py b/tests/test_authorization.py index f72bc9444..6ef7f8525 100644 --- a/tests/test_authorization.py +++ b/tests/test_authorization.py @@ -1,9 +1,17 @@ # A valid CrowdStrike Falcon API key is required to run these tests. +# You can store these values in your environment (this is the preferred method). +# Example: +# export DEBUG_API_ID=CLIENT_ID_GOES_HERE +# export DEBUG_API_SECRET=CLIENT_SECRET_GOES_HERE +# +# You may also store these values locally in a configuration file. +# DO NOT SUBMIT A COMMIT OR A PR THAT INCLUDES YOUR CONFIGURATION FILE. # API client ID & secret should be stored in tests/test.config in JSON format. # { # "falcon_client_id": "CLIENT_ID_GOES_HERE", # "falcon_client_secret": "CLIENT_SECRET_GOES_HERE" # } + import json import os import sys @@ -13,30 +21,38 @@ from falconpy import api_complete as FalconSDK from falconpy import oauth2 as FalconAuth - # The TestAuthorization class tests authentication and deauthentication # for both the Uber and Service classes. class TestAuthorization(): def getConfig(self): #Grab our config parameters - try: + if "DEBUG_API_ID" in os.environ and "DEBUG_API_SECRET" in os.environ: self.config = {} self.config["falcon_client_id"] = os.getenv("DEBUG_API_ID") self.config["falcon_client_secret"] = os.getenv("DEBUG_API_SECRET") - except: - with open('%s/test.config' % os.path.dirname(os.path.abspath(__file__)), 'r') as file_config: - self.config = json.loads(file_config.read()) + return True + else: + cur_path = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists('%s/test.config' % cur_path): + with open('%s/test.config' % cur_path, 'r') as file_config: + self.config = json.loads(file_config.read()) + return True + else: + return False def uberAuth(self): - self.getConfig() - self.falcon = FalconSDK.APIHarness(creds={ - "client_id": self.config["falcon_client_id"], - "client_secret": self.config["falcon_client_secret"] - } - ) - self.falcon.authenticate() - if self.falcon.authenticated: - return True + status = self.getConfig() + if status: + self.falcon = FalconSDK.APIHarness(creds={ + "client_id": self.config["falcon_client_id"], + "client_secret": self.config["falcon_client_secret"] + } + ) + self.falcon.authenticate() + if self.falcon.authenticated: + return True + else: + return False else: return False @@ -44,20 +60,23 @@ def uberRevoke(self): return self.falcon.deauthenticate() def serviceAuth(self): - self.getConfig() - self.authorization = FalconAuth.OAuth2(creds={ - 'client_id': self.config["falcon_client_id"], - 'client_secret': self.config["falcon_client_secret"] - }) + status = self.getConfig() + if status: + self.authorization = FalconAuth.OAuth2(creds={ + 'client_id': self.config["falcon_client_id"], + 'client_secret': self.config["falcon_client_secret"] + }) - try: - self.token = self.authorization.token()['body']['access_token'] + try: + self.token = self.authorization.token()['body']['access_token'] + + except: + self.token = False - except: - self.token = False - - if self.token: - return True + if self.token: + return True + else: + return False else: return False