2019-08-23 21:16:30 +07:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import re
|
|
|
|
|
2019-10-10 14:22:12 +07:00
|
|
|
import pytest
|
2022-07-19 20:56:24 +07:00
|
|
|
import requests
|
2019-08-23 21:16:30 +07:00
|
|
|
|
|
|
|
# public_repo — necessary to leave comments
|
|
|
|
# admin:repo_hook — to set up hooks (duh)
|
|
|
|
# delete_repo — to cleanup repos created under a user
|
|
|
|
# user:email — fetch token/user's email addresses
|
|
|
|
TOKEN_SCOPES = {
|
|
|
|
'github': {'admin:repo_hook', 'delete_repo', 'public_repo', 'user:email'},
|
|
|
|
# TODO: user:email so they can fetch the user's email?
|
|
|
|
'role_reviewer': {'public_repo'},# 'delete_repo'},
|
|
|
|
'role_self_reviewer': {'public_repo'},# 'delete_repo'},
|
|
|
|
'role_other': {'public_repo'},# 'delete_repo'},
|
|
|
|
}
|
|
|
|
@pytest.fixture(autouse=True, scope='session')
|
|
|
|
def _check_scopes(config):
|
|
|
|
for section, vals in config.items():
|
|
|
|
required_scopes = TOKEN_SCOPES.get(section)
|
|
|
|
if required_scopes is None:
|
|
|
|
continue
|
|
|
|
|
|
|
|
response = requests.get('https://api.github.com/rate_limit', headers={
|
|
|
|
'Authorization': 'token %s' % vals['token']
|
|
|
|
})
|
|
|
|
assert response.status_code == 200
|
|
|
|
x_oauth_scopes = response.headers['X-OAuth-Scopes']
|
|
|
|
token_scopes = set(re.split(r',\s+', x_oauth_scopes))
|
|
|
|
assert token_scopes >= required_scopes, \
|
|
|
|
"%s should have scopes %s, found %s" % (section, token_scopes, required_scopes)
|
|
|
|
|
2020-01-21 20:56:57 +07:00
|
|
|
@pytest.fixture()
|
2019-08-23 21:16:30 +07:00
|
|
|
def module():
|
|
|
|
""" When a test function is (going to be) run, selects the containing
|
|
|
|
module (as needing to be installed)
|
|
|
|
"""
|
|
|
|
# NOTE: no request.fspath (because no request.function) in session-scoped fixture so can't put module() at the toplevel
|
|
|
|
return 'forwardport'
|