示例#1
0
class RestClientTest(TestCase):
    """ small test for The RestClient
    This should not be to much, since there is an hourly limit of requests for the github api
    """

    def setUp(self):
        """setup"""
        super(RestClientTest, self).setUp()
        self.client = RestClient('https://api.github.com', username=GITHUB_LOGIN, token=GITHUB_TOKEN)

    def test_client(self):
        """Do a test api call"""
        status, body = self.client.repos[GITHUB_USER][GITHUB_REPO].contents.a_directory['a_file.txt'].get()
        self.assertEqual(status, 200)
        # dGhpcyBpcyBhIGxpbmUgb2YgdGV4dAo= == 'this is a line of text' in base64 encoding
        self.assertEqual(body['content'].strip(), u"dGhpcyBpcyBhIGxpbmUgb2YgdGV4dAo=")

        status, body = self.client.repos['hpcugent']['easybuild-framework'].pulls[1].get()
        self.assertEqual(status, 200)
        self.assertEqual(body['merge_commit_sha'], u'fba3e13815f3d2a9dfbd2f89f1cf678dd58bb1f1')

    def test_request_methods(self):
        """Test all request methods"""
        status, body = self.client.head()
        self.assertEqual(status, 200)
        try:
            status, body = self.client.user.emails.post(body='*****@*****.**')
            self.assertTrue(False, 'posting to unauthorized endpoint did not trhow a http error')
        except HTTPError:
            pass
        try:
            status, body = self.client.user.emails.delete(body='*****@*****.**')
            self.assertTrue(False, 'deleting to unauthorized endpoint did not trhow a http error')
        except HTTPError:
            pass
示例#2
0
class RestClientTest(TestCase):
    """ small test for The RestClient
    This should not be to much, since there is an hourly limit of requests for the github api
    """
    def setUp(self):
        """setup"""
        super(RestClientTest, self).setUp()
        self.client = RestClient('https://api.github.com',
                                 username=GITHUB_LOGIN,
                                 token=GITHUB_TOKEN)

    def test_client(self):
        """Do a test api call"""
        if GITHUB_TOKEN is None:
            print("Skipping test_client, since no GitHub token is available")
            return

        status, body = self.client.repos[GITHUB_USER][
            GITHUB_REPO].contents.a_directory['a_file.txt'].get()
        self.assertEqual(status, 200)
        # dGhpcyBpcyBhIGxpbmUgb2YgdGV4dAo= == 'this is a line of text' in base64 encoding
        self.assertEqual(body['content'].strip(),
                         u"dGhpcyBpcyBhIGxpbmUgb2YgdGV4dAo=")

        status, body = self.client.repos['hpcugent'][
            'easybuild-framework'].pulls[1].get()
        self.assertEqual(status, 200)
        self.assertEqual(body['merge_commit_sha'],
                         u'fba3e13815f3d2a9dfbd2f89f1cf678dd58bb1f1')

    def test_request_methods(self):
        """Test all request methods"""
        if GITHUB_TOKEN is None:
            print(
                "Skipping test_request_methods, since no GitHub token is available"
            )
            return

        status, headers = self.client.head()
        self.assertEqual(status, 200)
        self.assertTrue(headers)
        self.assertTrue('X-GitHub-Media-Type' in headers)
        try:
            status, body = self.client.user.emails.post(
                body='*****@*****.**')
            self.assertTrue(
                False,
                'posting to unauthorized endpoint did not trhow a http error')
        except HTTPError:
            pass
        try:
            status, body = self.client.user.emails.delete(
                body='*****@*****.**')
            self.assertTrue(
                False,
                'deleting to unauthorized endpoint did not trhow a http error')
        except HTTPError:
            pass

    def test_get_method(self):
        """A quick test of a GET to the github API"""

        status, body = self.client.users['hpcugent'].get()
        self.assertEqual(status, 200)
        self.assertEqual(body['login'], 'hpcugent')
        self.assertEqual(body['id'], 1515263)

    def test_get_connection(self):
        """Test for Client.get_connection."""

        client = Client('https://api.github.com')

        url = '/repos/hpcugent/vsc-base/pulls/296/comments'
        try:
            client.get_connection(Client.POST,
                                  url,
                                  body="{'body': 'test'}",
                                  headers={})
            self.assertTrue(
                False,
                "Trying to post a comment unauthorized should result in HTTPError"
            )
        except HTTPError:
            pass