From 7a260d81d32376207919a7f11af1dad54843704e Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Wed, 15 Feb 2017 15:30:46 -0500 Subject: [PATCH] Pull out bitbucket trigger validation into validator class --- util/config/validator.py | 26 +----------- .../test/test_validate_bitbucket_trigger.py | 40 +++++++++++++++++++ .../validators/validate_bitbucket_trigger.py | 29 ++++++++++++++ 3 files changed, 71 insertions(+), 24 deletions(-) create mode 100644 util/config/validators/test/test_validate_bitbucket_trigger.py create mode 100644 util/config/validators/validate_bitbucket_trigger.py diff --git a/util/config/validator.py b/util/config/validator.py index 1be17b614..5835d1267 100644 --- a/util/config/validator.py +++ b/util/config/validator.py @@ -4,7 +4,6 @@ import peewee from app import app, get_app_url from auth.auth_context import get_authenticated_user -from bitbucket import BitBucket from data.database import validate_database_url from data.users import LDAP_CERT_FILENAME from oauth.services.github import GithubOAuthService @@ -22,6 +21,7 @@ from util.config.validators.validate_signer import SignerValidator from util.config.validators.validate_torrent import BittorrentValidator from util.config.validators.validate_ssl import SSLValidator, SSL_FILENAMES from util.config.validators.validate_google_login import GoogleLoginValidator +from util.config.validators.validate_bitbucket_trigger import BitbucketTriggerValidator logger = logging.getLogger(__name__) @@ -136,28 +136,6 @@ def _validate_github_with_key(config_key, config): raise ConfigValidationException('Invalid organization: %s' % org_id) -def _validate_bitbucket(config, user_obj, _): - """ Validates the config for BitBucket. """ - trigger_config = config.get('BITBUCKET_TRIGGER_CONFIG') - if not trigger_config: - raise ConfigValidationException('Missing client ID and client secret') - - if not trigger_config.get('CONSUMER_KEY'): - raise ConfigValidationException('Missing Consumer Key') - - if not trigger_config.get('CONSUMER_SECRET'): - raise ConfigValidationException('Missing Consumer Secret') - - key = trigger_config['CONSUMER_KEY'] - secret = trigger_config['CONSUMER_SECRET'] - callback_url = '%s/oauth1/bitbucket/callback/trigger/' % (get_app_url()) - - bitbucket_client = BitBucket(key, secret, callback_url) - (result, _, _) = bitbucket_client.get_authorization_url() - if not result: - raise ConfigValidationException('Invalid consumer key or secret') - - VALIDATORS = { DatabaseValidator.name: DatabaseValidator.validate, RedisValidator.name: RedisValidator.validate, @@ -166,7 +144,7 @@ VALIDATORS = { 'github-login': _validate_github('GITHUB_LOGIN_CONFIG'), 'github-trigger': _validate_github('GITHUB_TRIGGER_CONFIG'), 'gitlab-trigger': _validate_gitlab, - 'bitbucket-trigger': _validate_bitbucket, + BitbucketTriggerValidator.name: BittorrentValidator.validate, GoogleLoginValidator.name: GoogleLoginValidator.validate, SSLValidator.name: SSLValidator.validate, LDAPValidator.name: LDAPValidator.validate, diff --git a/util/config/validators/test/test_validate_bitbucket_trigger.py b/util/config/validators/test/test_validate_bitbucket_trigger.py new file mode 100644 index 000000000..234843561 --- /dev/null +++ b/util/config/validators/test/test_validate_bitbucket_trigger.py @@ -0,0 +1,40 @@ +import pytest + +from httmock import urlmatch, HTTMock + +from util.config.validators import ConfigValidationException +from util.config.validators.validate_bitbucket_trigger import BitbucketTriggerValidator + +@pytest.mark.parametrize('unvalidated_config', [ + ({}), + ({'BITBUCKET_TRIGGER_CONFIG': {}}), + ({'BITBUCKET_TRIGGER_CONFIG': {'CONSUMER_KEY': 'foo'}}), + ({'BITBUCKET_TRIGGER_CONFIG': {'CONSUMER_SECRET': 'foo'}}), +]) +def test_validate_invalid_bitbucket_trigger_config(unvalidated_config): + validator = BitbucketTriggerValidator() + + with pytest.raises(ConfigValidationException): + validator.validate(unvalidated_config, None, None) + +def test_validate_bitbucket_trigger(): + url_hit = [False] + + @urlmatch(netloc=r'bitbucket.org') + def handler(url, request): + url_hit[0] = True + return { + 'status_code': 200, + 'content': 'oauth_token=foo&oauth_token_secret=bar', + } + + with HTTMock(handler): + validator = BitbucketTriggerValidator() + validator.validate({ + 'BITBUCKET_TRIGGER_CONFIG': { + 'CONSUMER_KEY': 'foo', + 'CONSUMER_SECRET': 'bar', + }, + }, None, None) + + assert url_hit[0] diff --git a/util/config/validators/validate_bitbucket_trigger.py b/util/config/validators/validate_bitbucket_trigger.py new file mode 100644 index 000000000..15378c1b4 --- /dev/null +++ b/util/config/validators/validate_bitbucket_trigger.py @@ -0,0 +1,29 @@ +from bitbucket import BitBucket + +from app import get_app_url +from util.config.validators import BaseValidator, ConfigValidationException + +class BitbucketTriggerValidator(BaseValidator): + name = "bitbucket-trigger" + + @classmethod + def validate(cls, config, user, user_password): + """ Validates the config for BitBucket. """ + trigger_config = config.get('BITBUCKET_TRIGGER_CONFIG') + if not trigger_config: + raise ConfigValidationException('Missing client ID and client secret') + + if not trigger_config.get('CONSUMER_KEY'): + raise ConfigValidationException('Missing Consumer Key') + + if not trigger_config.get('CONSUMER_SECRET'): + raise ConfigValidationException('Missing Consumer Secret') + + key = trigger_config['CONSUMER_KEY'] + secret = trigger_config['CONSUMER_SECRET'] + callback_url = '%s/oauth1/bitbucket/callback/trigger/' % (get_app_url()) + + bitbucket_client = BitBucket(key, secret, callback_url) + (result, _, _) = bitbucket_client.get_authorization_url() + if not result: + raise ConfigValidationException('Invalid consumer key or secret')