From 93ea60cc86c048b527373cc3260e2c4d9897b50c Mon Sep 17 00:00:00 2001 From: HugoPBrito Date: Tue, 26 Nov 2024 17:18:57 +0100 Subject: [PATCH] feat: add testing for new auth methods --- tests/providers/github/github_fixtures.py | 7 +- .../providers/github/github_provider_test.py | 125 +++++++++++++++--- 2 files changed, 110 insertions(+), 22 deletions(-) diff --git a/tests/providers/github/github_fixtures.py b/tests/providers/github/github_fixtures.py index cd57c6f002..a848c9ed17 100644 --- a/tests/providers/github/github_fixtures.py +++ b/tests/providers/github/github_fixtures.py @@ -9,13 +9,16 @@ ACCOUNT_ID = "account-id" ACCOUNT_URL = "/user" # GitHub Credentials -TOKEN = "github-token" +PAT_TOKEN = "github-token" +OAUTH_TOKEN = "oauth-token" +APP_ID = "app-id" +APP_KEY = "app-key" # Mocked GitHub Provider def set_mocked_github_provider( auth_method: str = "personal_access", - credentials: GithubSession = GithubSession(token=TOKEN), + credentials: GithubSession = GithubSession(token=PAT_TOKEN, id=APP_ID, key=APP_KEY), identity: GithubIdentityInfo = GithubIdentityInfo( account_name=ACCOUNT_NAME, account_id=ACCOUNT_ID, diff --git a/tests/providers/github/github_provider_test.py b/tests/providers/github/github_provider_test.py index 1f3ab192b9..30d770ef3b 100644 --- a/tests/providers/github/github_provider_test.py +++ b/tests/providers/github/github_provider_test.py @@ -5,44 +5,55 @@ from prowler.config.config import ( load_and_validate_config_file, ) from prowler.providers.github.github_provider import GithubProvider -from prowler.providers.github.models import GithubIdentityInfo, GithubSession +from prowler.providers.github.models import ( + GithubAppIdentityInfo, + GithubIdentityInfo, + GithubSession, +) from tests.providers.github.github_fixtures import ( ACCOUNT_ID, ACCOUNT_NAME, ACCOUNT_URL, - TOKEN, + APP_ID, + APP_KEY, + OAUTH_TOKEN, + PAT_TOKEN, ) class TestGitHubProvider: - def test_github_provider(self): - # We need to set exactly one auth method - personal_access = True - oauth_app = False - github_app = False + def test_github_provider_PAT(self): + personal_access_token = PAT_TOKEN + oauth_app_token = None + github_app_id = None + github_app_key = None fixer_config = load_and_validate_config_file( "github", default_fixer_config_file_path ) - with patch( - "prowler.providers.github.github_provider.GithubProvider.setup_session", - return_value=GithubSession(token=TOKEN), - ), patch( - "prowler.providers.github.github_provider.GithubProvider.setup_identity", - return_value=GithubIdentityInfo( - account_id=ACCOUNT_ID, - account_name=ACCOUNT_NAME, - account_url=ACCOUNT_URL, + with ( + patch( + "prowler.providers.github.github_provider.GithubProvider.setup_session", + return_value=GithubSession(token=PAT_TOKEN, id="", key=""), + ), + patch( + "prowler.providers.github.github_provider.GithubProvider.setup_identity", + return_value=GithubIdentityInfo( + account_id=ACCOUNT_ID, + account_name=ACCOUNT_NAME, + account_url=ACCOUNT_URL, + ), ), ): provider = GithubProvider( - personal_access, - github_app, - oauth_app, + personal_access_token, + oauth_app_token, + github_app_id, + github_app_key, ) assert provider._type == "github" - assert provider.session == GithubSession(token=TOKEN) + assert provider.session == GithubSession(token=PAT_TOKEN, id="", key="") assert provider.identity == GithubIdentityInfo( account_name=ACCOUNT_NAME, account_id=ACCOUNT_ID, @@ -50,3 +61,77 @@ class TestGitHubProvider: ) assert provider._audit_config == {} assert provider._fixer_config == fixer_config + + def test_github_provider_OAuth(self): + personal_access_token = None + oauth_app_token = OAUTH_TOKEN + github_app_id = None + github_app_key = None + fixer_config = load_and_validate_config_file( + "github", default_fixer_config_file_path + ) + + with ( + patch( + "prowler.providers.github.github_provider.GithubProvider.setup_session", + return_value=GithubSession(token=OAUTH_TOKEN, id="", key=""), + ), + patch( + "prowler.providers.github.github_provider.GithubProvider.setup_identity", + return_value=GithubIdentityInfo( + account_id=ACCOUNT_ID, + account_name=ACCOUNT_NAME, + account_url=ACCOUNT_URL, + ), + ), + ): + provider = GithubProvider( + personal_access_token, + oauth_app_token, + github_app_id, + github_app_key, + ) + + assert provider._type == "github" + assert provider.session == GithubSession(token=OAUTH_TOKEN, id="", key="") + assert provider.identity == GithubIdentityInfo( + account_name=ACCOUNT_NAME, + account_id=ACCOUNT_ID, + account_url=ACCOUNT_URL, + ) + assert provider._audit_config == {} + assert provider._fixer_config == fixer_config + + def test_github_provider_App(self): + personal_access_token = None + oauth_app_token = None + github_app_id = APP_ID + github_app_key = APP_KEY + fixer_config = load_and_validate_config_file( + "github", default_fixer_config_file_path + ) + + with ( + patch( + "prowler.providers.github.github_provider.GithubProvider.setup_session", + return_value=GithubSession(token="", id=APP_ID, key=APP_KEY), + ), + patch( + "prowler.providers.github.github_provider.GithubProvider.setup_identity", + return_value=GithubAppIdentityInfo( + app_id=APP_ID, + ), + ), + ): + provider = GithubProvider( + personal_access_token, + oauth_app_token, + github_app_id, + github_app_key, + ) + + assert provider._type == "github" + assert provider.session == GithubSession(token="", id=APP_ID, key=APP_KEY) + assert provider.identity == GithubAppIdentityInfo(app_id=APP_ID) + assert provider._audit_config == {} + assert provider._fixer_config == fixer_config