def test_fetch_protected_branches(mocker): cached_arequest = mocker.patch('jenkins_epo.repository.cached_arequest') from jenkins_epo.repository import Repository yield from Repository('owner', 'name').fetch_protected_branches() assert cached_arequest.mock_calls
def test_process_protected_branches(): from jenkins_epo.repository import Repository repo = Repository('owner', 'name') repo.heads_filter = ['*', '-*/skip'] branches = list( repo.process_protected_branches([ { 'name': 'master', 'commit': { 'sha': 'd0d0cafec0041edeadbeef' }, }, { 'name': 'skip', 'commit': { 'sha': 'pouetpouet' }, }, ])) assert 1 == len(branches) head = branches[0] assert 'refs/heads/master' == head.fullref assert 'master' == head.ref assert head.url.startswith('https://github.com')
def test_fetch_commit(mocker): cached_arequest = mocker.patch('jenkins_epo.repository.cached_arequest') from jenkins_epo.repository import Repository cached_arequest.return_value = [] yield from Repository('owner', 'name').fetch_commit('cafedodo')
def test_process_jenkins_yml_settings(): from jenkins_epo.repository import Repository repo = Repository('owner', 'repo1') repo.process_settings(jenkins_yml=repr( dict(settings=dict(collaborators=['bdfl', 'hacker']))), ) assert ['bdfl', 'hacker'] == repo.SETTINGS.COLLABORATORS
def test_process_jenkins_yml_settings_reviewers_compat(): from jenkins_epo.repository import Repository repo = Repository('owner', 'repo1') repo.process_settings(jenkins_yml=repr( dict(settings=dict(reviewers=['bdfl']))), ) assert ['bdfl'] == repo.SETTINGS.COLLABORATORS
def test_process_pulls(): from jenkins_epo.repository import Repository repo = Repository('owner', 'name') repo.heads_filter = ['*2'] heads = list( repo.process_pull_requests([ dict( html_url='https://github.com/owner/repo/pull/2', number=2, head=dict( repo=dict(html_url='https://.../owner/repo'), ref='feature', sha='d0d0', ), ), dict( html_url='https://github.com/owner/repo/pull/3', number=3, head=dict( repo=dict(html_url='https://.../owner/repo'), ref='hotfix', sha='cafe', ), ), ])) assert 1 == len(heads) head = heads[0] assert 'refs/heads/feature' == head.fullref assert 'feature' == head.ref assert head.url.startswith('https://github.com')
def test_set_hook(mocker): GITHUB = mocker.patch('jenkins_epo.repository.GITHUB') from jenkins_epo.repository import Repository repo = Repository('owner', 'name') yield from repo.set_hook(dict()) assert GITHUB.repos().hooks.apost.mock_calls yield from repo.set_hook(dict(), hookid='1231') assert GITHUB.repos().hooks().apatch.mock_calls
def test_process_hooks(): from jenkins_epo.repository import Repository repo = Repository('owner', 'name') hooks = list( repo.process_hooks([ dict(name='jenkins'), dict(name='web', config=dict(url='http://other')), dict(name='web', config=dict(url='http://me')), ], webhook_url='http://me')) assert 1 == len(hooks)
def test_load_settings_collaborators_override(mocker): cached_arequest = mocker.patch('jenkins_epo.repository.cached_arequest', CoroutineMock()) GITHUB = mocker.patch('jenkins_epo.repository.GITHUB') from jenkins_epo.repository import Repository GITHUB.fetch_file_contents.side_effect = [ repr(dict(settings=dict(branches=['master'], collaborators=['octo']))), ] repo = Repository('owner', 'repo1') repo.load_settings() assert not cached_arequest.mock_calls
def test_report_issue(mocker): GITHUB = mocker.patch('jenkins_epo.repository.GITHUB') GITHUB.repos().issues.apost = CoroutineMock() from jenkins_epo.repository import Repository repo = Repository('owner', 'name') GITHUB.dry = 1 payload = yield from repo.report_issue('title', 'body') assert not GITHUB.repos().issues.apost.mock_calls assert 0 == payload['number'] GITHUB.dry = 0 yield from repo.report_issue('title', 'body') assert GITHUB.repos().issues.apost.mock_calls
def test_load_settings_no_yml(mocker): GITHUB = mocker.patch('jenkins_epo.repository.GITHUB') unpaginate = mocker.patch( 'jenkins_epo.repository.unpaginate', CoroutineMock(return_value=[]), ) from jenkins_epo.repository import ApiNotFoundError, Repository GITHUB.fetch_file_contents = CoroutineMock( side_effect=ApiNotFoundError('url', Mock(), Mock())) repo = Repository('owner', 'repo1') yield from repo.load_settings() assert GITHUB.fetch_file_contents.mock_calls assert unpaginate.mock_calls
def test_collaborators(): from jenkins_epo.repository import Repository repo = Repository('owner', 'repository') repo.process_settings(collaborators=[ { 'login': '******', 'site_admin': True }, { 'login': '******', 'permissions': { 'admin': False, 'pull': False, 'push': False }, 'site_admin': False, }, { 'login': '******', 'permissions': { 'admin': False, 'pull': True, 'push': True }, 'site_admin': False, }, { 'login': '******', 'permissions': { 'admin': True, 'pull': True, 'push': True }, 'site_admin': False, }, ]) collaborators = repo.SETTINGS.COLLABORATORS assert 'siteadmin' in collaborators assert 'contributor' not in collaborators assert 'pusher' in collaborators assert 'owner' in collaborators
def test_load_settings_collaborators_denied(mocker): GITHUB = mocker.patch('jenkins_epo.repository.GITHUB') unpaginate = mocker.patch( 'jenkins_epo.repository.unpaginate', CoroutineMock(), ) from jenkins_epo.repository import ( Repository, ApiNotFoundError, UnauthorizedRepository, ) GITHUB.fetch_file_contents = CoroutineMock( return_value=repr(dict(settings=dict(branches=['master'])))) unpaginate.side_effect = ApiNotFoundError('u', Mock(), Mock()) repo = Repository('owner', 'repo1') with pytest.raises(UnauthorizedRepository): yield from repo.load_settings() assert GITHUB.fetch_file_contents.mock_calls assert unpaginate.mock_calls
def test_process_commits(): from jenkins_epo.repository import Repository repo = Repository('owner', 'name') items = list(repo.process_commits([dict(sha='cafed0d0')])) assert 1 == len(items)
def test_load_settings_already_loaded(): from jenkins_epo.repository import Repository repo = Repository('owner', 'repo1') repo.SETTINGS.update({'COLLABORATORS': ['bdfl']}) repo.load_settings()