Fix handling of Github API paths and add tests
This commit is contained in:
parent
cf947962dc
commit
9558c0e937
3 changed files with 60 additions and 1 deletions
48
test/test_github.py
Normal file
48
test/test_github.py
Normal file
|
@ -0,0 +1,48 @@
|
|||
import unittest
|
||||
|
||||
from util.config.oauth import GithubOAuthConfig
|
||||
|
||||
class TestGithub(unittest.TestCase):
|
||||
def test_basic_enterprise_config(self):
|
||||
config = {
|
||||
'GITHUB_TRIGGER_CONFIG': {
|
||||
'GITHUB_ENDPOINT': 'https://github.somedomain.com/',
|
||||
'CLIENT_ID': 'someclientid',
|
||||
'CLIENT_SECRET': 'someclientsecret',
|
||||
}
|
||||
}
|
||||
|
||||
github_trigger = GithubOAuthConfig(config, 'GITHUB_TRIGGER_CONFIG')
|
||||
self.assertTrue(github_trigger.is_enterprise())
|
||||
self.assertEquals('https://github.somedomain.com/login/oauth/authorize?', github_trigger.authorize_endpoint())
|
||||
self.assertEquals('https://github.somedomain.com/login/oauth/access_token', github_trigger.token_endpoint())
|
||||
|
||||
self.assertEquals('https://github.somedomain.com/api/v3', github_trigger.api_endpoint())
|
||||
|
||||
self.assertEquals('https://github.somedomain.com/api/v3/user', github_trigger.user_endpoint())
|
||||
self.assertEquals('https://github.somedomain.com/api/v3/user/emails', github_trigger.email_endpoint())
|
||||
self.assertEquals('https://github.somedomain.com/api/v3/user/orgs', github_trigger.orgs_endpoint())
|
||||
|
||||
def test_custom_enterprise_config(self):
|
||||
config = {
|
||||
'GITHUB_TRIGGER_CONFIG': {
|
||||
'GITHUB_ENDPOINT': 'https://github.somedomain.com/',
|
||||
'API_ENDPOINT': 'http://somedomain.com/api',
|
||||
'CLIENT_ID': 'someclientid',
|
||||
'CLIENT_SECRET': 'someclientsecret',
|
||||
}
|
||||
}
|
||||
|
||||
github_trigger = GithubOAuthConfig(config, 'GITHUB_TRIGGER_CONFIG')
|
||||
self.assertTrue(github_trigger.is_enterprise())
|
||||
self.assertEquals('https://github.somedomain.com/login/oauth/authorize?', github_trigger.authorize_endpoint())
|
||||
self.assertEquals('https://github.somedomain.com/login/oauth/access_token', github_trigger.token_endpoint())
|
||||
|
||||
self.assertEquals('http://somedomain.com/api', github_trigger.api_endpoint())
|
||||
|
||||
self.assertEquals('http://somedomain.com/api/user', github_trigger.user_endpoint())
|
||||
self.assertEquals('http://somedomain.com/api/user/emails', github_trigger.email_endpoint())
|
||||
self.assertEquals('http://somedomain.com/api/user/orgs', github_trigger.orgs_endpoint())
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
|
@ -209,8 +209,15 @@ class TestSlashJoining(unittest.TestCase):
|
|||
test_cases = [
|
||||
(['https://github.com', '/coreos-inc/' 'quay/pull/1092/files'],
|
||||
'https://github.com/coreos-inc/quay/pull/1092/files'),
|
||||
|
||||
(['https://', 'github.com/', '/coreos-inc', '/quay/pull/1092/files/'],
|
||||
'https://github.com/coreos-inc/quay/pull/1092/files'),
|
||||
|
||||
(['https://somegithub.com/', '/api/v3/'],
|
||||
'https://somegithub.com/api/v3'),
|
||||
|
||||
(['https://github.somedomain.com/', '/api/v3/'],
|
||||
'https://github.somedomain.com/api/v3'),
|
||||
]
|
||||
|
||||
for args, url in test_cases:
|
||||
|
|
|
@ -108,7 +108,11 @@ class GithubOAuthConfig(OAuthConfig):
|
|||
return self.config.get('API_ENDPOINT', slash_join(self._endpoint(), '/api/v3/'))
|
||||
|
||||
def api_endpoint(self):
|
||||
return self._api_endpoint()[0:-1]
|
||||
endpoint = self._api_endpoint()
|
||||
if endpoint.endswith('/'):
|
||||
return endpoint[0:-1]
|
||||
|
||||
return endpoint
|
||||
|
||||
def user_endpoint(self):
|
||||
return slash_join(self._api_endpoint(), 'user')
|
||||
|
|
Reference in a new issue