Skip to content

Commit

Permalink
Added token refersh call when creating GiteeClient
Browse files Browse the repository at this point in the history
Signed-off-by: Willem Jiang <willem.jiang@gmail.com>
  • Loading branch information
WillemJiang committed Apr 7, 2020
1 parent f311b8d commit 0e9beb1
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 14 deletions.
13 changes: 11 additions & 2 deletions perceval/backends/core/gitee.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

GITEE_URL = "https://gitee.com/"
GITEE_API_URL = "https://gitee.com/api/v5"

GITEE_REFRESH_TOKEN_URL = "https://gitee.com/oauth/token"

# Range before sleeping until rate limit reset
MIN_RATE_LIMIT = 10
Expand Down Expand Up @@ -456,7 +456,7 @@ class GiteeClient(HttpClient, RateLimitHandler):
"""
EXTRA_STATUS_FORCELIST = [403, 500, 502, 503]

_users = {} # users cache
_users = {} # users cache
_users_orgs = {} # users orgs cache

def __init__(self, owner, repository, tokens,
Expand All @@ -481,6 +481,8 @@ def __init__(self, owner, repository, tokens,
extra_headers=self._set_extra_headers(),
extra_status_forcelist=self.EXTRA_STATUS_FORCELIST,
archive=archive, from_archive=from_archive, ssl_verify=ssl_verify)
# refresh the access token
self._refresh_access_token()

def issue_comments(self, issue_number):
"""Get the issue comments """
Expand Down Expand Up @@ -676,6 +678,13 @@ def _set_extra_headers(self):
headers.update({'Content-Type': 'application/json;charset=UTF-8'})
return headers

def _refresh_access_token(self):
"""Send a refresh post access to the Gitee Server"""
if self.access_token:
url = GITEE_REFRESH_TOKEN_URL + "?grant_type=refresh_token&refresh_token=" + self.access_token
logger.info("Refresh the access_token for Gitee API")
self.session.post(url, data=None, headers=None, stream=False, auth=None)


class GiteeCommand(BackendCommand):
"""Class to run GitHub backend from the command line."""
Expand Down
64 changes: 52 additions & 12 deletions tests/test_gitee.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

from perceval.backend import BackendCommandArgumentParser
from perceval.backends.core.gitee import (Gitee, GiteeClient,
CATEGORY_PULL_REQUEST, GiteeCommand)
CATEGORY_PULL_REQUEST, GiteeCommand, GITEE_REFRESH_TOKEN_URL)

from base import TestCaseBackendArchive
from perceval.utils import DEFAULT_DATETIME, DEFAULT_LAST_DATETIME
Expand Down Expand Up @@ -61,6 +61,7 @@ def setup_gitee_pull_request_services():


def setup_gitee_basic_services():
setup_refresh_access_token_service()
orgs = read_file('data/gitee/gitee_user_orgs')
httpretty.register_uri(httpretty.GET, GITEE_USER_ORGS_URL, body=orgs, status=200)

Expand All @@ -76,15 +77,15 @@ def __setup_gitee_issue_services():
issues2 = read_file('data/gitee/gitee_issues2')

pagination_issue_header_1 = {'Link': '<' + GITEE_ISSUES_URL +
'/?&page=2>; rel="next", <' + GITEE_ISSUES_URL +
'/?&page=2>; rel="last"',
'/?&page=2>; rel="next", <' + GITEE_ISSUES_URL +
'/?&page=2>; rel="last"',
'total_count': '2',
'total_page': '2'
}

pagination_issue_header_2 = {'Link': '<' + GITEE_ISSUES_URL +
'/?&page=1>; rel="prev", <' + GITEE_ISSUES_URL +
'/?&page=1>; rel="first"',
'/?&page=1>; rel="prev", <' + GITEE_ISSUES_URL +
'/?&page=1>; rel="first"',
'total_count': '2',
'total_page': '2'
}
Expand Down Expand Up @@ -136,6 +137,10 @@ def __setup_gitee_pull_request_services():
})


def setup_refresh_access_token_service():
httpretty.register_uri(httpretty.POST, GITEE_REFRESH_TOKEN_URL, body="", status=200)


class TestGiteeBackend(unittest.TestCase):
"""Gitee Backend tests"""

Expand All @@ -150,6 +155,7 @@ def test_init(self):
@httpretty.activate
def test_fetch_empty(self):
""" Test when get a empty issues API call """
setup_gitee_issue_services()
empty_issue = '[]'
httpretty.register_uri(httpretty.GET, GITEE_ISSUES_URL,
body=empty_issue, status=200,
Expand Down Expand Up @@ -281,8 +287,10 @@ def test_fetch_from_empty_archive(self):
class TestGiteeClient(unittest.TestCase):
"""Gitee API client tests"""

@httpretty.activate
def test_init(self):
""" Test for the initialization of GiteeClient """
setup_refresh_access_token_service()
client = GiteeClient('gitee_example', 'repo', ['aaa'])
self.assertEqual(client.owner, 'gitee_example')
self.assertEqual(client.repository, 'repo')
Expand All @@ -295,7 +303,33 @@ def test_init(self):
@httpretty.activate
def test_get_empty_issues(self):
""" Test when issue is empty API call """
setup_refresh_access_token_service()
empty_issue = '[]'
httpretty.register_uri(httpretty.GET, GITEE_ISSUES_URL,
body=empty_issue, status=200,
forcing_headers={
"total_count": "0",
"total_page": "0"
})

client = GiteeClient('gitee_example', 'repo', ['aaa'])
raw_issues = [issues for issues in client.issues()]
self.assertEqual(raw_issues[0], empty_issue)

# Check requests parameter
expected = {
'per_page': ['100'],
'state': ['all'],
'direction': ['asc'],
'sort': ['updated'],
'access_token': ['aaa']
}
self.assertDictEqual(httpretty.last_request().querystring, expected)

@httpretty.activate
def test_get_refresh_token(self):
""" Test when issue is empty API call """
setup_refresh_access_token_service()
empty_issue = '[]'
httpretty.register_uri(httpretty.GET, GITEE_ISSUES_URL,
body=empty_issue, status=200,
Expand All @@ -304,6 +338,8 @@ def test_get_empty_issues(self):
"total_page": "0"
})

httpretty.register_uri(httpretty.POST, GITEE_REFRESH_TOKEN_URL, body=empty_issue, status=200)

client = GiteeClient('gitee_example', 'repo', ['aaa'])
raw_issues = [issues for issues in client.issues()]
self.assertEqual(raw_issues[0], empty_issue)
Expand All @@ -321,7 +357,7 @@ def test_get_empty_issues(self):
@httpretty.activate
def test_get_issues(self):
"""Test Gitee issues API """

setup_refresh_access_token_service()
issues = read_file('data/gitee/gitee_issues1')
httpretty.register_uri(httpretty.GET, GITEE_ISSUES_URL,
body=issues, status=200,
Expand All @@ -347,19 +383,19 @@ def test_get_issues(self):
@httpretty.activate
def test_get_two_pages_issues(self):
"""Test Gitee issues API """

setup_refresh_access_token_service()
issues_1 = read_file('data/gitee/gitee_issues1')
issues_2 = read_file('data/gitee/gitee_issues2')
pagination_issue_header_1 = {'Link': '<' + GITEE_ISSUES_URL +
'/?&page=2>; rel="next", <' + GITEE_ISSUES_URL +
'/?&page=2>; rel="last"',
'/?&page=2>; rel="next", <' + GITEE_ISSUES_URL +
'/?&page=2>; rel="last"',
'total_count': '2',
'total_page': '2'
}

pagination_issue_header_2 = {'Link': '<' + GITEE_ISSUES_URL +
'/?&page=1>; rel="prev", <' + GITEE_ISSUES_URL +
'/?&page=1>; rel="first"',
'/?&page=1>; rel="prev", <' + GITEE_ISSUES_URL +
'/?&page=1>; rel="first"',
'total_count': '2',
'total_page': '2'
}
Expand All @@ -380,7 +416,7 @@ def test_get_two_pages_issues(self):
@httpretty.activate
def test_issue_comments(self):
"""Test Gitee issue comments API """

setup_refresh_access_token_service()
issue_comments = read_file('data/gitee/gitee_issue1_comments')
httpretty.register_uri(httpretty.GET, GITEE_ISSUE_COMMENTS_URL_1,
body=issue_comments, status=200,
Expand All @@ -402,6 +438,7 @@ def test_issue_comments(self):

@httpretty.activate
def test_pulls(self):
setup_refresh_access_token_service()
pull_request = read_file('data/gitee/gitee_pull_request1')
httpretty.register_uri(httpretty.GET, GITEE_PULL_REQUEST_URL,
body=pull_request, status=200,
Expand All @@ -425,6 +462,7 @@ def test_pulls(self):

@httpretty.activate
def test_repo(self):
setup_refresh_access_token_service()
repo = read_file('data/gitee/gitee_repo')
httpretty.register_uri(httpretty.GET, GITEE_REPO_URL, body=repo, status=200)
client = GiteeClient("gitee_example", "repo", ['aaa'], None)
Expand All @@ -433,6 +471,7 @@ def test_repo(self):

@httpretty.activate
def test_user_orgs(self):
setup_refresh_access_token_service()
orgs = read_file('data/gitee/gitee_user_orgs')
httpretty.register_uri(httpretty.GET, GITEE_USER_ORGS_URL, body=orgs, status=200)
client = GiteeClient("gitee_example", "repo", ['aaa'], None)
Expand All @@ -441,6 +480,7 @@ def test_user_orgs(self):

@httpretty.activate
def test_get_user(self):
setup_refresh_access_token_service()
user = read_file('data/gitee/gitee_login')
httpretty.register_uri(httpretty.GET, GITEE_USER_URL, body=user, status=200)
client = GiteeClient("gitee_example", "repo", ['aaa'], None)
Expand Down

0 comments on commit 0e9beb1

Please sign in to comment.