2017-02-15 21:07:25 +00:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from httmock import urlmatch, HTTMock
|
|
|
|
|
2018-05-29 17:50:51 +00:00
|
|
|
from config import build_requests_session
|
|
|
|
from util.config.validator import ValidatorContext
|
2017-02-15 21:07:25 +00:00
|
|
|
from util.config.validators import ConfigValidationException
|
|
|
|
from util.config.validators.validate_github import GitHubLoginValidator, GitHubTriggerValidator
|
|
|
|
|
2017-04-24 18:52:30 +00:00
|
|
|
from test.fixtures import *
|
|
|
|
|
2017-02-15 21:07:25 +00:00
|
|
|
@pytest.fixture(params=[GitHubLoginValidator, GitHubTriggerValidator])
|
|
|
|
def github_validator(request):
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('github_config', [
|
|
|
|
({}),
|
|
|
|
({'GITHUB_ENDPOINT': 'foo'}),
|
|
|
|
({'GITHUB_ENDPOINT': 'http://github.com'}),
|
|
|
|
({'GITHUB_ENDPOINT': 'http://github.com', 'CLIENT_ID': 'foo'}),
|
|
|
|
({'GITHUB_ENDPOINT': 'http://github.com', 'CLIENT_SECRET': 'foo'}),
|
|
|
|
({
|
|
|
|
'GITHUB_ENDPOINT': 'http://github.com',
|
|
|
|
'CLIENT_ID': 'foo',
|
|
|
|
'CLIENT_SECRET': 'foo',
|
|
|
|
'ORG_RESTRICT': True
|
|
|
|
}),
|
|
|
|
({
|
|
|
|
'GITHUB_ENDPOINT': 'http://github.com',
|
|
|
|
'CLIENT_ID': 'foo',
|
|
|
|
'CLIENT_SECRET': 'foo',
|
|
|
|
'ORG_RESTRICT': True,
|
|
|
|
'ALLOWED_ORGANIZATIONS': [],
|
|
|
|
}),
|
|
|
|
])
|
2017-04-24 18:52:30 +00:00
|
|
|
def test_validate_invalid_github_config(github_config, github_validator, app):
|
2017-02-15 21:07:25 +00:00
|
|
|
with pytest.raises(ConfigValidationException):
|
|
|
|
unvalidated_config = {}
|
|
|
|
unvalidated_config[github_validator.config_key] = github_config
|
2018-05-29 17:50:51 +00:00
|
|
|
github_validator.validate(ValidatorContext(unvalidated_config))
|
2017-02-15 21:07:25 +00:00
|
|
|
|
2017-04-24 18:52:30 +00:00
|
|
|
def test_validate_github(github_validator, app):
|
2017-02-15 21:07:25 +00:00
|
|
|
url_hit = [False, False]
|
|
|
|
|
|
|
|
@urlmatch(netloc=r'somehost')
|
|
|
|
def handler(url, request):
|
|
|
|
url_hit[0] = True
|
|
|
|
return {'status_code': 200, 'content': '', 'headers': {'X-GitHub-Request-Id': 'foo'}}
|
|
|
|
|
|
|
|
@urlmatch(netloc=r'somehost', path=r'/api/v3/applications/foo/tokens/foo')
|
|
|
|
def app_handler(url, request):
|
|
|
|
url_hit[1] = True
|
|
|
|
return {'status_code': 404, 'content': '', 'headers': {'X-GitHub-Request-Id': 'foo'}}
|
|
|
|
|
|
|
|
with HTTMock(app_handler, handler):
|
2018-05-29 17:50:51 +00:00
|
|
|
unvalidated_config = ValidatorContext({
|
2017-02-15 21:07:25 +00:00
|
|
|
github_validator.config_key: {
|
|
|
|
'GITHUB_ENDPOINT': 'http://somehost',
|
|
|
|
'CLIENT_ID': 'foo',
|
|
|
|
'CLIENT_SECRET': 'bar',
|
|
|
|
},
|
2018-05-29 17:50:51 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
unvalidated_config.http_client = build_requests_session()
|
|
|
|
github_validator.validate(unvalidated_config)
|
2017-02-15 21:07:25 +00:00
|
|
|
|
|
|
|
assert url_hit[0]
|
|
|
|
assert url_hit[1]
|