def test_event_and_action_functions_called(app, mocker): github_app = GitHubApp(app) event_function = MagicMock() event_action_function = MagicMock() other_event_function = MagicMock() github_app._hook_mappings = { 'foo': [event_function], 'foo.bar': [event_action_function], 'bar': [other_event_function] } mocker.patch('flask_githubapp.core.GitHubApp._verify_webhook') with app.test_client() as client: resp = client.post('/', data=json.dumps({ 'installation': { 'id': 2 }, 'action': 'bar' }), headers={ 'X-GitHub-Event': 'foo', 'Content-Type': 'application/json' }) assert resp.status_code == 200 event_function.assert_called_once_with() event_action_function.assert_called_once_with() other_event_function.assert_not_called()
def test_function_exception_raise_500_error(app, mocker): github_app = GitHubApp(app) function_to_call = MagicMock() function_to_call.__name__ = 'foo' # used to generate response function_to_call.side_effect = Exception('foo exception') github_app._hook_mappings['foo'] = [function_to_call] mocker.patch('flask_githubapp.core.GitHubApp._verify_webhook') with app.test_client() as client: resp = client.post('/', data=json.dumps({'installation': { 'id': 2 }}), headers={ 'X-GitHub-Event': 'foo', 'Content-Type': 'application/json' }) assert resp.status_code == 500 function_to_call.assert_called_once_with()
def test_no_target_functions(app, mocker): github_app = GitHubApp(app) function_to_miss = MagicMock() function_to_miss.__name__ = 'foo' # used to generate response github_app._hook_mappings['foo'] = [function_to_miss] mocker.patch('flask_githubapp.core.GitHubApp._verify_webhook') with app.test_client() as client: resp = client.post('/', data=json.dumps({'installation': { 'id': 2 }}), headers={ 'X-GitHub-Event': 'bar', 'Content-Type': 'application/json' }) assert resp.status_code == 200 function_to_miss.assert_not_called() assert resp.json['status'] == STATUS_NO_FUNC_CALLED assert resp.json['calls'] == {}
def test_view_returns_map_of_called_functions_and_returned_data(app, mocker): github_app = GitHubApp(app) def event_function(): return 'foo' def event_action_function(): return 'bar' def other_event_function(): return 'baz' github_app._hook_mappings = { 'foo': [event_function], 'foo.bar': [event_action_function], 'bar': [other_event_function] } mocker.patch('flask_githubapp.core.GitHubApp._verify_webhook') with app.test_client() as client: resp = client.post('/', data=json.dumps({ 'installation': { 'id': 2 }, 'action': 'bar' }), headers={ 'X-GitHub-Event': 'foo', 'Content-Type': 'application/json' }) assert resp.status_code == 200 assert resp.json == { 'status': 'HIT', 'calls': { 'event_function': 'foo', 'event_action_function': 'bar', } }
def test_issues_hook_invalid_signature(app): with open(os.path.join(FIXURES_DIR, 'issues_hook.json'), 'rb') as hook: issues_data = hook.read() GitHubApp(app) with app.test_client() as client: resp = client.post('/', data=issues_data, headers={ 'Content-Type': 'application/json', 'X-GitHub-Event': 'issues', 'X-Hub-Signature': 'sha1=badhash' }) assert resp.status_code == 400
def test_github_installation_client_is_lazy(app, mocker): github_app = GitHubApp(app) installation_id = 2 mocker.patch('flask_githubapp.core.GitHubApp._verify_webhook') mock_client = mocker.patch('flask_githubapp.core.GitHubApp.client') with app.test_client() as client: resp = client.post('/', data=json.dumps( {'installation': { 'id': installation_id }}), headers={ 'X-GitHub-Event': 'foo', 'Content-Type': 'application/json' }) assert resp.status_code == 200 mock_client.login_as_app_installation.assert_not_called()
def test_issues_hook_valid_signature(app): with open(os.path.join(FIXURES_DIR, 'issues_hook.json'), 'rb') as hook: issues_data = hook.read() GitHubApp(app) with app.test_client() as client: resp = client.post('/', data=issues_data, headers={ 'Content-Type': 'application/json', 'X-GitHub-Event': 'issues', 'X-Hub-Signature': 'sha1=85d20eda7a4e518f956b99f432b1225de8516e56' }) assert resp.status_code == 200
def test_events_with_actions_mapped_to_functions(app, mocker): github_app = GitHubApp(app) function_to_call = MagicMock() function_to_call.__name__ = 'foo' # used to generate response function_to_call.return_value = 'foo' # return data must be serializable github_app._hook_mappings['foo.bar'] = [function_to_call] mocker.patch('flask_githubapp.core.GitHubApp._verify_webhook') with app.test_client() as client: resp = client.post('/', data=json.dumps({ 'installation': { 'id': 2 }, 'action': 'bar' }), headers={ 'X-GitHub-Event': 'foo', 'Content-Type': 'application/json' }) assert resp.status_code == 200 function_to_call.assert_called_once_with()
def test_init_app(app): github_app = GitHubApp() github_app.init_app(app) assert 'GITHUBAPP_URL' not in app.config
from flask_githubapp import GitHubApp # type: ignore from ..common import github_utils from ..common.typ import AppInstallationTokenAuth, CorregirJob, Repo from ..corrector.tasks import corregir_entrega from .queue import task_queue from .reposdb import make_reposdb from .settings import load_config __all__ = [ "repos_hook", ] reposdb = make_reposdb() repos_hook = GitHubApp() def app_installation_token_auth() -> AppInstallationTokenAuth: """Obtiene el token de autenticación del objeto GitHubApp. """ client = repos_hook.installation_client session_auth = client.session.auth # Para poder usar github3.py en el worker, se necesita tanto el token # como su fecha de expiración. Para PyGithub haría falta solamente el # token. return AppInstallationTokenAuth( token=session_auth.token, expires_at=session_auth.expires_at_str, )