import pytest

from httmock import urlmatch, HTTMock

from util.config import URLSchemeAndHostname
from util.config.validator import ValidatorContext
from util.config.validators import ConfigValidationException
from util.config.validators.validate_bitbucket_trigger import BitbucketTriggerValidator

from test.fixtures import *


@pytest.mark.parametrize(
    "unvalidated_config",
    [
        (ValidatorContext({})),
        (ValidatorContext({"BITBUCKET_TRIGGER_CONFIG": {}})),
        (ValidatorContext({"BITBUCKET_TRIGGER_CONFIG": {"CONSUMER_KEY": "foo"}})),
        (ValidatorContext({"BITBUCKET_TRIGGER_CONFIG": {"CONSUMER_SECRET": "foo"}})),
    ],
)
def test_validate_invalid_bitbucket_trigger_config(unvalidated_config, app):
    validator = BitbucketTriggerValidator()

    with pytest.raises(ConfigValidationException):
        validator.validate(unvalidated_config)


def test_validate_bitbucket_trigger(app):
    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()

        url_scheme_and_hostname = URLSchemeAndHostname("http", "localhost:5000")
        unvalidated_config = ValidatorContext(
            {
                "BITBUCKET_TRIGGER_CONFIG": {
                    "CONSUMER_KEY": "foo",
                    "CONSUMER_SECRET": "bar",
                },
            },
            url_scheme_and_hostname=url_scheme_and_hostname,
        )

        validator.validate(unvalidated_config)

        assert url_hit[0]

[((23, 16, 23, 43), 'util.config.validators.validate_bitbucket_trigger.BitbucketTriggerValidator', 'BitbucketTriggerValidator', ({}, {}), '()', False, 'from util.config.validators.validate_bitbucket_trigger import BitbucketTriggerValidator\n'), ((32, 5, 32, 38), 'httmock.urlmatch', 'urlmatch', (), '', False, 'from httmock import urlmatch, HTTMock\n'), ((25, 9, 25, 49), 'pytest.raises', 'pytest.raises', ({(25, 23, 25, 48): 'ConfigValidationException'}, {}), '(ConfigValidationException)', False, 'import pytest\n'), ((16, 9, 16, 29), 'util.config.validator.ValidatorContext', 'ValidatorContext', ({(16, 26, 16, 28): '{}'}, {}), '({})', False, 'from util.config.validator import ValidatorContext\n'), ((17, 9, 17, 59), 'util.config.validator.ValidatorContext', 'ValidatorContext', ({(17, 26, 17, 58): "{'BITBUCKET_TRIGGER_CONFIG': {}}"}, {}), "({'BITBUCKET_TRIGGER_CONFIG': {}})", False, 'from util.config.validator import ValidatorContext\n'), ((18, 9, 18, 80), 'util.config.validator.ValidatorContext', 'ValidatorContext', ({(18, 26, 18, 79): "{'BITBUCKET_TRIGGER_CONFIG': {'CONSUMER_KEY': 'foo'}}"}, {}), "({'BITBUCKET_TRIGGER_CONFIG': {'CONSUMER_KEY': 'foo'}})", False, 'from util.config.validator import ValidatorContext\n'), ((19, 9, 19, 83), 'util.config.validator.ValidatorContext', 'ValidatorContext', ({(19, 26, 19, 82): "{'BITBUCKET_TRIGGER_CONFIG': {'CONSUMER_SECRET': 'foo'}}"}, {}), "({'BITBUCKET_TRIGGER_CONFIG': {'CONSUMER_SECRET': 'foo'}})", False, 'from util.config.validator import ValidatorContext\n'), ((40, 9, 40, 25), 'httmock.HTTMock', 'HTTMock', ({(40, 17, 40, 24): 'handler'}, {}), '(handler)', False, 'from httmock import urlmatch, HTTMock\n'), ((41, 20, 41, 47), 'util.config.validators.validate_bitbucket_trigger.BitbucketTriggerValidator', 'BitbucketTriggerValidator', ({}, {}), '()', False, 'from util.config.validators.validate_bitbucket_trigger import BitbucketTriggerValidator\n'), ((43, 34, 43, 80), 'util.config.URLSchemeAndHostname', 'URLSchemeAndHostname', ({(43, 55, 43, 61): '"""http"""', (43, 63, 43, 79): '"""localhost:5000"""'}, {}), "('http', 'localhost:5000')", False, 'from util.config import URLSchemeAndHostname\n'), ((44, 29, 52, 9), 'util.config.validator.ValidatorContext', 'ValidatorContext', (), '', False, 'from util.config.validator import ValidatorContext\n')]