2017-02-28 21:18:19 +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
|
2017-02-28 21:18:19 +00:00
|
|
|
from oauth.oidc import OIDC_WELLKNOWN
|
2018-05-29 17:50:51 +00:00
|
|
|
from util.config.validator import ValidatorContext
|
2017-02-28 21:18:19 +00:00
|
|
|
from util.config.validators import ConfigValidationException
|
|
|
|
from util.config.validators.validate_oidc import OIDCLoginValidator
|
|
|
|
|
2017-04-24 18:52:30 +00:00
|
|
|
from test.fixtures import *
|
|
|
|
|
2017-02-28 21:18:19 +00:00
|
|
|
@pytest.mark.parametrize('unvalidated_config', [
|
|
|
|
({'SOMETHING_LOGIN_CONFIG': {}}),
|
2017-04-07 15:33:02 +00:00
|
|
|
({'SOMETHING_LOGIN_CONFIG': {'OIDC_SERVER': 'foo'}}),
|
|
|
|
({'SOMETHING_LOGIN_CONFIG': {'OIDC_SERVER': 'foo', 'CLIENT_ID': 'foobar'}}),
|
|
|
|
({'SOMETHING_LOGIN_CONFIG': {'OIDC_SERVER': 'foo', 'CLIENT_SECRET': 'foobar'}}),
|
2017-02-28 21:18:19 +00:00
|
|
|
])
|
2017-04-24 18:52:30 +00:00
|
|
|
def test_validate_invalid_oidc_login_config(unvalidated_config, app):
|
2017-02-28 21:18:19 +00:00
|
|
|
validator = OIDCLoginValidator()
|
|
|
|
|
|
|
|
with pytest.raises(ConfigValidationException):
|
2018-05-29 17:50:51 +00:00
|
|
|
validator.validate(ValidatorContext(unvalidated_config))
|
2017-02-28 21:18:19 +00:00
|
|
|
|
2017-04-24 18:52:30 +00:00
|
|
|
def test_validate_oidc_login(app):
|
2017-02-28 21:18:19 +00:00
|
|
|
url_hit = [False]
|
|
|
|
@urlmatch(netloc=r'someserver', path=r'/\.well-known/openid-configuration')
|
|
|
|
def handler(_, __):
|
|
|
|
url_hit[0] = True
|
|
|
|
data = {
|
2017-07-21 19:56:46 +00:00
|
|
|
'token_endpoint': 'foobar',
|
2017-02-28 21:18:19 +00:00
|
|
|
}
|
|
|
|
return {'status_code': 200, 'content': json.dumps(data)}
|
|
|
|
|
|
|
|
with HTTMock(handler):
|
|
|
|
validator = OIDCLoginValidator()
|
2018-05-29 17:50:51 +00:00
|
|
|
unvalidated_config = ValidatorContext({
|
2017-02-28 21:18:19 +00:00
|
|
|
'SOMETHING_LOGIN_CONFIG': {
|
2017-04-07 15:33:02 +00:00
|
|
|
'CLIENT_ID': 'foo',
|
|
|
|
'CLIENT_SECRET': 'bar',
|
2017-02-28 21:18:19 +00:00
|
|
|
'OIDC_SERVER': 'http://someserver',
|
|
|
|
'DEBUGGING': True, # Allows for HTTP.
|
|
|
|
},
|
2018-05-29 17:50:51 +00:00
|
|
|
})
|
|
|
|
unvalidated_config.http_client = build_requests_session()
|
|
|
|
|
|
|
|
validator.validate(unvalidated_config)
|
2017-02-28 21:18:19 +00:00
|
|
|
|
|
|
|
assert url_hit[0]
|