async def test_success(): event_data = { 'action': 'opened', 'number': 2248, 'pull_request': { 'title': '[3.6] Backport this …', 'body': '…(GH-1234)', }, 'repository': { 'issues_url': 'https://api.github.com/repos/python/cpython/issues{/number}', }, } event = sansio.Event(event_data, event='pull_request', delivery_id='1') issue_data = { 'labels': [{ 'name': 'needs backport to 3.6' }], 'labels_url': 'https://api.github.com/repos/python/cpython/issues/1234/labels{/name}', 'comments_url': 'https://api.github.com/repos/python/cpython/issues/1234/comments', } gh = FakeGH(getitem=issue_data) await backport.router.dispatch(event, gh) assert gh.getitem_url == sansio.format_url( event_data['repository']['issues_url'], {'number': '1234'}) assert gh.delete_url == sansio.format_url( issue_data['labels_url'], {'name': 'needs backport to 3.6'}) assert gh.post_url == issue_data['comments_url'] message = gh.post_data['body'] assert message == backport.MESSAGE_TEMPLATE.format(branch='3.6', pr='2248')
def test_template(self): template_url = "https://api.github.com/users/octocat/gists{/gist_id}" template_data = {"gist_id": "1234"} # Substituting an absolute URL. url = sansio.format_url(template_url, template_data) assert url == "https://api.github.com/users/octocat/gists/1234" # No substituting an absolute URL. url = sansio.format_url(template_url, {}) assert url == "https://api.github.com/users/octocat/gists" # Substituting a relative URL. url = sansio.format_url("/users/octocat/gists{/gist_id}", template_data) assert url == "https://api.github.com/users/octocat/gists/1234"
def test_different_base_and_absolute_url(self): url = sansio.format_url( "https://api.github.com/notifications", {}, base_url="https://my.host.com/notifications", ) assert url == "https://api.github.com/notifications"
async def getitem(self, url, url_vars={}): self.getitem_url = sansio.format_url(url, url_vars) to_return = self._getitem_return[self.getitem_url] if isinstance(to_return, Exception): raise to_return else: return to_return
def test_template_with_base_url(self): template_url = "https://my.host.com/users/octocat/gists{/gist_id}" template_data = {"gist_id": "1234"} # Substituting an absolute URL. url = sansio.format_url( template_url, template_data, base_url="https://my.host.com" ) assert url == "https://my.host.com/users/octocat/gists/1234" # No substituting an absolute URL. url = sansio.format_url(template_url, {}, base_url="https://my.host.com") assert url == "https://my.host.com/users/octocat/gists" # Substituting a relative URL. url = sansio.format_url( "/users/octocat/gists{/gist_id}", template_data, base_url="https://my.host.com", ) assert url == "https://my.host.com/users/octocat/gists/1234"
async def test_backport_label_removal_success(): event_data = { 'action': 'opened', 'number': 2248, 'pull_request': { 'title': '[3.6] Backport this …', 'body': '…(GH-1234)', 'issue_url': 'https://api.github.com/issue/2248', 'base': { 'ref': '3.6', }, 'statuses_url': 'https://api.github.com/repos/python/cpython/statuses/somehash', }, 'repository': { 'issues_url': 'https://api.github.com/issue{/number}', }, } event = sansio.Event(event_data, event='pull_request', delivery_id='1') getitem_data = { 'https://api.github.com/issue/1234': { 'labels': [{ 'name': 'needs backport to 3.6' }], 'labels_url': 'https://api.github.com/issue/1234/labels{/name}', 'comments_url': 'https://api.github.com/issue/1234/comments', }, 'https://api.github.com/issue/2248': {}, } gh = FakeGH(getitem=getitem_data) await backport.router.dispatch(event, gh) issue_data = getitem_data['https://api.github.com/issue/1234'] assert gh.delete_url == sansio.format_url( issue_data['labels_url'], {'name': 'needs backport to 3.6'}) assert len(gh.post_) > 0 expected_post = None for post in gh.post_: if post[0] == issue_data['comments_url']: expected_post = post message = post[1]['body'] assert message == backport.MESSAGE_TEMPLATE.format(branch='3.6', pr='2248') assert expected_post is not None
async def test_missing_label(): title = '[3.6] Backport this (GH-1234)' data = { 'number': 2248, 'pull_request': { 'title': title, 'body': '', }, 'repository': { 'issues_url': 'https://api.github.com/repos/python/cpython/issues{/number}' } } event = sansio.Event(data, event='pull_request', delivery_id='1') gh = FakeGH(getitem={'labels': [{'name': 'CLA signed'}]}) await backport.remove_backport_label(event, gh) assert gh.getitem_url == sansio.format_url( data['repository']['issues_url'], {'number': '1234'}) assert gh.delete_url is None
async def test_backport_label_removal_with_parentheses_in_title(): event_data = { 'action': 'opened', 'number': 2248, 'pull_request': { 'title': '[3.6] Backport (0.9.6) this (more bpo-1234) (GH-1234)', 'body': '…', 'issue_url': 'https://api.github.com/issue/2248', 'base': { 'ref': '3.6', }, 'statuses_url': 'https://api.github.com/repos/python/cpython/statuses/somehash', }, 'repository': { 'issues_url': 'https://api.github.com/issue{/number}', }, } event = sansio.Event(event_data, event='pull_request', delivery_id='1') getitem_data = { 'https://api.github.com/issue/1234': { 'labels': [{ 'name': 'needs backport to 3.6' }], 'labels_url': 'https://api.github.com/issue/1234/labels{/name}', 'comments_url': 'https://api.github.com/issue/1234/comments', }, 'https://api.github.com/issue/2248': {}, } gh = FakeGH(getitem=getitem_data) await backport.router.dispatch(event, gh) issue_data = getitem_data['https://api.github.com/issue/1234'] assert gh.delete_url == sansio.format_url( issue_data['labels_url'], {'name': 'needs backport to 3.6'})
def test_quoting(self, base_url): template_url = "https://api.github.com/repos/python/cpython/labels{/name}" label = {"name": "CLA signed"} url = sansio.format_url(template_url, label, base_url=base_url) assert url == "https://api.github.com/repos/python/cpython/labels/CLA%20signed"
async def delete(self, url, url_vars): self.delete_url = sansio.format_url(url, url_vars)
async def getitem(self, url, url_vars): self.getitem_url = sansio.format_url(url, url_vars) return self._getitem_return
async def post(self, url, url_vars={}, *, data): self.post_url = sansio.format_url(url, url_vars) self.post_data = data
async def post(self, url, url_vars={}, *, data): post_url = sansio.format_url(url, url_vars) self.post_.append((post_url, data))
def test_absolute_url(self): original_url = "https://api.github.com/notifications" url = sansio.format_url(original_url, {}) assert url == original_url
async def getiter(self, url, url_vars={}): self.getiter_url = sansio.format_url(url, url_vars) to_iterate = self._getiter_return[self.getiter_url] for item in to_iterate: yield item
def test_relative_url(self, base_url): url = sansio.format_url("/notifications", {}, base_url=base_url) assert url == f"{base_url}/notifications"
def test_relative_url(self): url = sansio.format_url("/notifications", {}) assert url == "https://api.github.com/notifications"
def test_absolute_url(self, base_url): url = sansio.format_url(base_url, {}, base_url=base_url) assert url == base_url