class GitHub: def __init__(self): self.api = Api('https://api.github.com', headers={'Authorization': 'token {}'.format(s.token)}, user_agent='AutoGitHubBot by github.com/naiyt') def repo(self, username, repo): info = self.api.get('/repos/{}/{}'.format(username, repo)) if info.status_code != 200: raise BadRepo('Repo {} does not exist'.format(repo)) return Repo(info.json()) def user(self, username): info = self.api.get('/users/{}'.format(username)) if info.status_code != 200: raise BadUser('User {} does not exist'.format(username)) return User(info.json())
def setUp(self): self.url = url self.test_api = Api(self.url)
def __init__(self): self.api = Api('https://api.github.com', headers={'Authorization': 'token {}'.format(s.token)}, user_agent='AutoGitHubBot by github.com/naiyt')
class TestApi(unittest.TestCase): def setUp(self): self.url = url self.test_api = Api(self.url) def test_that_base_url_is_set(self): self.assertEqual(self.test_api.base_url, self.url) def test_that_headers_are_set(self): headers = {'Authorization': 'Super secret OAuth'} api_with_headers = Api(self.url, headers=headers) self.assertDictEqual(api_with_headers.headers, { 'ACCEPT': 'application/json', 'User-Agent': 'ApiHelper v{}'.format(api_with_headers._version), 'Authorization': 'Super secret OAuth' }) def test_that_user_agent_is_set(self): user_agent = 'Super cool user agent' api_with_agent = Api(self.url, user_agent=user_agent) self.assertEqual(api_with_agent.headers['User-Agent'], user_agent) def test_route_with_leading_slash(self): with HTTMock(response_content): res = self.test_api.get('/url') self.assertEqual(res.url, '{}/url'.format(self.url)) def test_route_without_leading_slash(self): with HTTMock(response_content): res = self.test_api.get('url') self.assertEqual(res.url, '{}/url'.format(self.url)) def test_that_get_request_succeeds(self): with HTTMock(response_content): res = self.test_api.get('/', {'user': '******'}) self.assertEqual(res.status_code, 200) self.assertEqual(res.request.method, 'GET') self.assertDictEqual(res.json(), { 'success': True, 'message': get_message }) def test_that_post_request_succeeds(self): with HTTMock(response_content): res = self.test_api.post('/', {'password': '******'}) self.assertEqual(res.status_code, 200) self.assertEqual(res.request.method, 'POST') self.assertDictEqual(res.json(), { 'success': True, 'message': post_message }) def test_that_head_request_succeeds(self): with HTTMock(response_content): res = self.test_api.head('/', {'person': 'john'}) self.assertEqual(res.status_code, 200) self.assertEqual(res.request.method, 'HEAD') self.assertDictEqual(res.json(), { 'success': True, 'message': head_message }) def test_that_put_request_succeeds(self): with HTTMock(response_content): res = self.test_api.put('/', {'new_user': '******'}) self.assertEqual(res.status_code, 200) self.assertEqual(res.request.method, 'PUT') self.assertDictEqual(res.json(), { 'success': True, 'message': put_message }) def test_that_delete_request_succeeds(self): with HTTMock(response_content): res = self.test_api.delete('/', {'user': '******'}) self.assertEqual(res.status_code, 200) self.assertEqual(res.request.method, 'DELETE') self.assertDictEqual(res.json(), { 'success': True, 'message': delete_message })