From 9558c0e9374e194e707b1936ef4aa3e849aba9f3 Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Thu, 30 Jun 2016 14:10:22 -0400 Subject: [PATCH] Fix handling of Github API paths and add tests --- test/test_github.py | 48 ++++++++++++++++++++++++++++++++++++++++++++ test/test_util.py | 7 +++++++ util/config/oauth.py | 6 +++++- 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 test/test_github.py diff --git a/test/test_github.py b/test/test_github.py new file mode 100644 index 000000000..4b2fc5d2c --- /dev/null +++ b/test/test_github.py @@ -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() diff --git a/test/test_util.py b/test/test_util.py index 8b1022940..47974bfb2 100644 --- a/test/test_util.py +++ b/test/test_util.py @@ -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: diff --git a/util/config/oauth.py b/util/config/oauth.py index e28ca73f5..6b1ef9b9b 100644 --- a/util/config/oauth.py +++ b/util/config/oauth.py @@ -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')