8000 Update implementation of create_ref · pythonthings/github3.py@0a92f71 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0a92f71

Browse files
committed
Update implementation of create_ref
Add unit tests for Repository#create_ref and Repository#create_tag
1 parent c8e3588 commit 0a92f71

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

github3/repos/repo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,7 @@ def create_ref(self, ref, sha):
902902
else None
903903
"""
904904
json = None
905-
if ref and ref.count('/') >= 2 and sha:
905+
if ref and ref.startswith('refs') and ref.count('/') >= 2 and sha:
906906
data = {'ref': ref, 'sha': sha}
907907
url = self._build_url('git', 'refs', base_url=self._api)
908908
json = self._json(self._post(url, data=data), 201)

tests/unit/test_repos_repo.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,69 @@ def test_asset_requires_a_positive_id(self):
171171

172172
assert self.session.get.called is False
173173

174+
def test_create_ref(self):
175+
"""Verify the request to create a reference."""
176+
self.instance.create_ref('refs/heads/foo', 'my-fake-sha')
177+
178+
self.post_called_with(
179+
url_for('git/refs'),
180+
data={
181+
'ref': 'refs/heads/foo',
182+
'sha': 'my-fake-sha',
183+
},
184+
)
185+
186+
def test_create_ref_requires_a_reference_with_two_slashes(self):
187+
"""Test that we check the validity of a reference."""
188+
self.instance.create_ref('refs/heads', 'my-fake-sha')
189+
190+
assert self.session.post.called is False
191+
192+
def test_create_ref_requires_a_reference_start_with_refs(self):
193+
"""Test that we check the validity of a reference."""
194+
self.instance.create_ref('my-silly-ref/foo/bar', 'my-fake-sha')
195+
196+
assert self.session.post.called is False
197+
198+
def test_create_ref_requires_a_non_None_sha(self):
199+
"""Test that we don't send an empty SHA."""
200+
self.instance.create_ref('refs/heads/valid', None)
201+
202+
assert self.session.post.called is False
203+
204+
def test_create_ref_requires_a_truthy_sha(self):
205+
"""Test that we don't send an empty SHA."""
206+
self.instance.create_ref('refs/heads/valid', '')
207+
208+
assert self.session.post.called is False
209+
210+
def test_create_tag_that_is_not_lightweight(self):
211+
"""Verify we can create an annotated tag."""
212+
self.instance.create_tag(
213+
tag='tag-name',
214+
message='message',
215+
sha='my-sha',
216+
obj_type='commit',
217+
tagger={'name': 'Ian Cordasco',
218+
'email': 'example@example.com',
219+
'date': '2015-11-01T12:16:00Z'},
220+
)
221+
222+
self.post_called_with(
223+
url_for('git/tags'),
224+
data={
225+
'tag': 'tag-name',
226+
'message': 'message',
227+
'object': 'my-sha',
228+
'type': 'commit',
229+
'tagger': {
230+
'name': 'Ian Cordasco',
231+
'email': 'example@example.com',
232+
'date': '2015-11-01T12:16:00Z',
233+
},
234+
},
235+
)
236+
174237
def test_create_tree(self):
175238
"""Verify the request to create a tree."""
176239
self.instance.create_tree([{'foo': 'bar'}])
@@ -697,6 +760,11 @@ def test_add_collaborator(self):
697760
with pytest.raises(GitHubError):
698761
self.instance.add_collaborator('foo')
699762

763+
def test_create_ref(self):
764+
"""Verify that creating a tag requires authentication."""
765+
with pytest.raises(GitHubError):
766+
self.instance.create_ref('some ref', 'some sha')
767+
700768
def test_hooks(self):
701769
"""Show that a user must be authenticated to list hooks."""
702770
with pytest.raises(GitHubError):

0 commit comments

Comments
 (0)
0