2017-02-15 20:39:03 +00:00
|
|
|
import json
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from httmock import urlmatch, HTTMock
|
|
|
|
|
2018-05-29 17:50:51 +00:00
|
|
|
from config import build_requests_session
|
|
|
|
from util.config import URLSchemeAndHostname
|
|
|
|
from util.config.validator import ValidatorContext
|
2017-02-15 20:39:03 +00:00
|
|
|
from util.config.validators import ConfigValidationException
|
|
|
|
from util.config.validators.validate_gitlab_trigger import GitLabTriggerValidator
|
|
|
|
|
2017-04-24 18:52:30 +00:00
|
|
|
from test.fixtures import *
|
|
|
|
|
2017-02-15 20:39:03 +00:00
|
|
|
@pytest.mark.parametrize('unvalidated_config', [
|
|
|
|
({}),
|
|
|
|
({'GITLAB_TRIGGER_CONFIG': {'GITLAB_ENDPOINT': 'foo'}}),
|
|
|
|
({'GITLAB_TRIGGER_CONFIG': {'GITLAB_ENDPOINT': 'http://someendpoint', 'CLIENT_ID': 'foo'}}),
|
|
|
|
({'GITLAB_TRIGGER_CONFIG': {'GITLAB_ENDPOINT': 'http://someendpoint', 'CLIENT_SECRET': 'foo'}}),
|
|
|
|
])
|
2017-04-24 18:52:30 +00:00
|
|
|
def test_validate_invalid_gitlab_trigger_config(unvalidated_config, app):
|
2017-02-15 20:39:03 +00:00
|
|
|
validator = GitLabTriggerValidator()
|
|
|
|
|
|
|
|
with pytest.raises(ConfigValidationException):
|
2018-05-29 17:50:51 +00:00
|
|
|
validator.validate(ValidatorContext(unvalidated_config))
|
2017-02-15 20:39:03 +00:00
|
|
|
|
2017-04-24 18:52:30 +00:00
|
|
|
def test_validate_gitlab_enterprise_trigger(app):
|
2017-02-15 20:39:03 +00:00
|
|
|
url_hit = [False]
|
|
|
|
|
|
|
|
@urlmatch(netloc=r'somegitlab', path='/oauth/token')
|
|
|
|
def handler(_, __):
|
|
|
|
url_hit[0] = True
|
|
|
|
return {'status_code': 400, 'content': json.dumps({'error': 'invalid code'})}
|
|
|
|
|
|
|
|
with HTTMock(handler):
|
|
|
|
validator = GitLabTriggerValidator()
|
2018-06-01 15:31:19 +00:00
|
|
|
|
|
|
|
url_scheme_and_hostname = URLSchemeAndHostname('http', 'localhost:5000')
|
|
|
|
|
2018-05-29 17:50:51 +00:00
|
|
|
unvalidated_config = ValidatorContext({
|
2017-02-15 20:39:03 +00:00
|
|
|
'GITLAB_TRIGGER_CONFIG': {
|
|
|
|
'GITLAB_ENDPOINT': 'http://somegitlab',
|
|
|
|
'CLIENT_ID': 'foo',
|
|
|
|
'CLIENT_SECRET': 'bar',
|
|
|
|
},
|
2018-06-01 15:31:19 +00:00
|
|
|
}, http_client=build_requests_session(), url_scheme_and_hostname=url_scheme_and_hostname)
|
2018-05-29 17:50:51 +00:00
|
|
|
|
|
|
|
validator.validate(unvalidated_config)
|
2017-02-15 20:39:03 +00:00
|
|
|
|
|
|
|
assert url_hit[0]
|