desc
stringlengths
3
26.7k
decl
stringlengths
11
7.89k
bodies
stringlengths
8
553k
'Verify JSON attributes are exposed even if not explicitly set.'
def test_exposes_attributes(self):
assert (self.instance.fake_attr == 'foo')
'Verify that method returns GitHubObject from json.'
def test_from_json(self):
github_core = GitHubCore.from_json('{}') assert isinstance(github_core, GitHubCore)
'Verify method raises exception when json is not a dict.'
def test_instance_or_null(self):
with pytest.raises(exceptions.UnprocessableResponseBody): self.instance._instance_or_null(GitHubCore, [])
'Verify JSON information is retrieved correctly.'
def test_json(self):
response = requests.Response() response.headers['Last-Modified'] = 'foo' response.headers['ETag'] = 'bar' response.raw = io.BytesIO('{}') response.status_code = 200 json = self.instance._json(response, 200) assert (json['Last-Modified'] == 'foo') assert (json['ETag'] == 'bar')
'Verify JSON information is retrieved correctly.'
def test_json_status_code_does_not_match(self):
response = requests.Response() response.status_code = 204 json = self.instance._json(response, 200) assert (json is None)
'Test AttributeError is raised when attribute is not in JSON.'
def test_missingattribute(self):
with pytest.raises(AttributeError): self.instance.missingattribute
'Verify the request of refreshing an object.'
def test_refresh(self):
instance = self.instance.refresh() assert isinstance(instance, MyTestRefreshClass) expected_headers = None self.session.get.assert_called_once_with(self.url, headers=expected_headers)
'Verify the request of refreshing an object.'
def test_refresh_custom_headers(self):
self.instance.CUSTOM_HEADERS = {'Accept': 'application/vnd.github.drax-preview+json'} expected_headers = {'Accept': 'application/vnd.github.drax-preview+json'} self.instance.refresh() self.session.get.assert_called_once_with(self.url, headers=expected_headers)
'Verify the request of refreshing an object.'
def test_refresh_last_modified(self):
expected_headers = {'If-Modified-Since': self.last_modified} self.instance.refresh(conditional=True) self.session.get.assert_called_once_with(self.url, headers=expected_headers)
'Verify the request of refreshing an object.'
def test_refresh_etag(self):
self.instance.last_modified = None expected_headers = {'If-None-Match': self.etag} self.instance.refresh(conditional=True) self.session.get.assert_called_once_with(self.url, headers=expected_headers)
'Verify refreshing an object updates stored json data.'
def test_refresh_json(self):
expected_data = {'changed_files': 4} response = requests.Response() response.status_code = 200 response.raw = io.BytesIO(json.dumps(expected_data).encode('utf8')) self.session.get.return_value = response self.instance.refresh() assert ('changed_files' in self.instance.as_dict()) assert (...
'Verify that method converts ISO 8601 formatted string.'
def test_strptime(self):
dt = self.instance._strptime('2015-06-18T19:53:04Z') assert (dt.tzname() == 'UTC') assert (dt.dst() == timedelta(0)) assert (dt.utcoffset() == timedelta(0))
'Verify that method converts ISO 8601 formatted string.'
def test_strptime_time_str_required(self):
assert (self.instance._strptime('') is None)
'Verify that _api property contains URL query'
def test_issue_672(self):
assert ('?' in self.instance._api) assert (self.instance._api == self.url)
'Verify that we generate the correct URL for a tarball archive.'
def test_tarball_archive(self):
self.instance.archive(format='tarball') self.session.get.assert_called_once_with('https://api.github.com/repos/octocat/Hello-World/tarball/v1.0.0', allow_redirects=True, stream=True)
'Verify that we generate the correct URL for a zipball archive.'
def test_zipball_archive(self):
self.instance.archive(format='zipball') self.session.get.assert_called_once_with('https://api.github.com/repos/octocat/Hello-World/zipball/v1.0.0', allow_redirects=True, stream=True)
'Do not make a request if the archive format is unsupported.'
def test_unsupported_archive(self):
self.instance.archive(format='clearly fake') assert (self.session.get.called is False)
'Test the request to retrieve a release\'s assets.'
def test_assets(self):
i = self.instance.assets() self.get_next(i) self.session.get.assert_called_once_with(url_for('1/assets'), params={'per_page': 100}, headers={})
'Verify the request to delete an Asset.'
def test_delete(self):
self.instance.delete() self.session.delete.assert_called_once_with(url_for('/assets/1'), headers=Release.CUSTOM_HEADERS)
'Verify the request to download an Asset file.'
@pytest.mark.xfail def test_download(self):
with mock.patch('github3.utils.stream_response_to_file') as stream: self.instance.download() self.session.get.assert_called_once_with(url_for('/assets/1'), stream=True, allow_redirects=False, headers={'Accept': 'application/octect-stream'}) assert (stream.called is False)
'Verify the request to download an Asset file.'
def test_download_with_302(self):
with mock.patch.object(github3.models.GitHubCore, '_get') as get: get.return_value.status_code = 302 get.return_value.headers = {'location': 'https://fakeurl'} self.instance.download() data = {'headers': {'Content-Type': None, 'Accept': 'application/octet-stream'}, 'stream': True} ...
'Test equality/inequality between two instances.'
def test_equality(self):
thread = github3.notifications.Thread(get_example_data()) assert (self.instance == thread) thread._uniq = 1 assert (self.instance != thread)
'Show that is_unread() equals unread property.'
def test_is_unread(self):
assert (self.instance.is_unread() == self.instance.unread)
'Show instance string is formatted correctly.'
def test_repr(self):
assert (repr(self.instance) == '<Thread [{0}]>'.format(self.instance.subject.get('title')))
'Show that a user can delete a subscription.'
def test_delete_description(self):
self.instance.delete_subscription() self.session.delete.assert_called_once_with(url_for('subscription'))
'Show that a user can mark the subscription.'
def test_mark(self):
self.instance.mark() self.session.patch.assert_called_once_with(url_for())
'Show that a user can subscribe to notification.'
def test_set_subscription(self):
self.instance.set_subscription(True, False) self.put_called_with(url_for('subscription'), data={'ignored': False, 'subscribed': True})
'Show that a user can retrieve a subscription.'
def test_subscription(self):
self.instance.subscription() self.session.get.assert_called_once_with(url_for('subscription'))
'Verify the request for retreiving the latest_sha.'
def test_latest_sha(self):
headers = {'Accept': 'application/vnd.github.chitauri-preview+sha', 'If-None-Match': '"123"'} self.instance.latest_sha(differs_from='123') self.session.get.assert_called_once_with(url_for(), headers=headers)
'Assert the default headers are there upon initialization'
def test_has_default_headers(self):
s = self.build_session() assert ('Accept' in s.headers) assert (s.headers['Accept'] == 'application/vnd.github.v3.full+json') assert ('Accept-Charset' in s.headers) assert (s.headers['Accept-Charset'] == 'utf-8') assert ('Content-Type' in s.headers) assert (s.headers['Content-Type'] == 'appl...
'Test that GitHubSessions build basic URLs'
def test_build_url(self):
s = self.build_session() url = s.build_url('gists', '123456', 'history') assert (url == 'https://api.github.com/gists/123456/history')
'Test that building a URL caches it'
def test_build_url_caches_built_urls(self):
s = self.build_session() url = s.build_url('gists', '123456', 'history') url_parts = ('https://api.github.com', 'gists', '123456', 'history') assert (url_parts in session.__url_cache__) assert (url in session.__url_cache__.values())
'Test that you can pass in a different base URL to build_url'
def test_build_url_uses_a_different_base(self):
s = self.build_session() url = s.build_url('gists', '123456', 'history', base_url='https://status.github.com') assert (url == 'https://status.github.com/gists/123456/history')
'Test that build_url uses the session\'s base_url'
def test_build_url_respects_the_session_base_url(self):
s = self.build_session('https://enterprise.customer.com') url = s.build_url('gists') assert (url == 'https://enterprise.customer.com/gists')
'Test that basic auth will not authenticate with falsey values'
def test_basic_login_does_not_use_falsey_values(self):
bad_auths = [(None, 'password'), ('username', None), ('', 'password'), ('username', '')] for auth in bad_auths: s = self.build_session() s.basic_auth(*auth) assert (s.auth != auth)
'Test that basic auth will work with a valid combination'
def test_basic_login(self):
s = self.build_session() s.basic_auth('username', 'password') assert (s.auth == ('username', 'password'))
'Test that basic auth will remove the Authorization header. Token and basic authentication will conflict so remove the token authentication.'
def test_basic_login_disables_token_auth(self):
s = self.build_session() s.token_auth('token goes here') assert ('Authorization' in s.headers) s.basic_auth('username', 'password') assert ('Authorization' not in s.headers)
'Test the method that handles getting the 2fa code'
@mock.patch.object(requests.Session, 'request') def test_handle_two_factor_auth(self, request_mock):
s = self.build_session() s.two_factor_auth_callback((lambda : 'fake')) args = ('GET', 'http://example.com') s.handle_two_factor_auth(args, {}) request_mock.assert_called_once_with(headers={'X-GitHub-OTP': 'fake'}, *args)
'Test that request does not try to handle 2fa when it should not'
@mock.patch.object(requests.Session, 'request') def test_request_ignores_responses_that_do_not_require_2fa(self, request_mock):
response = mock.Mock() response.configure_mock(status_code=200, headers={}) request_mock.return_value = response s = self.build_session() s.two_factor_auth_callback((lambda : 'fake')) r = s.get('http://example.com') assert (r is response) request_mock.assert_called_once_with('GET', 'http...
'Test that the overridden request method will create history'
@mock.patch.object(requests.Session, 'request') def test_creates_history_while_handling_2fa(self, request_mock):
response = mock.Mock() response.configure_mock(status_code=401, headers={'X-GitHub-OTP': 'required;2fa'}, history=[]) request_mock.return_value = response s = self.build_session() s.two_factor_auth_callback((lambda : 'fake')) r = s.get('http://example.com') assert (len(r.history) != 0) a...
'Test that token auth will work with a valid token'
def test_token_auth(self):
s = self.build_session() s.token_auth('token goes here') assert (s.headers['Authorization'] == 'token token goes here')
'Test that using token auth removes the value of the auth attribute. If `GitHubSession.auth` is set then it conflicts with the token value.'
def test_token_auth_disables_basic_auth(self):
s = self.build_session() s.auth = ('foo', 'bar') s.token_auth('token goes here') assert (s.auth is None)
'Test that token auth will not authenticate with falsey values'
def test_token_auth_does_not_use_falsey_values(self):
bad_tokens = [None, ''] for token in bad_tokens: s = self.build_session() s.token_auth(token) assert ('Authorization' not in s.headers)
'Test that oauth2 authentication works For now though, it doesn\'t because it isn\'t implemented.'
def test_oauth2_auth(self):
s = self.build_session() with pytest.raises(NotImplementedError): s.oauth2_auth('Foo', 'bar')
'Test that GitHubSession is a subclass of requests.Session'
def test_issubclass_of_requests_Session(self):
assert issubclass(session.GitHubSession, requests.Session)
'Test that temporary_basic_auth resets old auth.'
def test_can_use_temporary_basic_auth(self):
s = self.build_session() s.basic_auth('foo', 'bar') with s.temporary_basic_auth('temp', 'pass'): assert (s.auth != ('foo', 'bar')) assert (s.auth == ('foo', 'bar'))
'Test that temporary_basic_auth sets the proper credentials.'
def test_temporary_basic_auth_replaces_auth(self):
s = self.build_session() s.basic_auth('foo', 'bar') with s.temporary_basic_auth('temp', 'pass'): assert (s.auth == ('temp', 'pass'))
'Verify that no_auth removes existing authentication.'
def test_no_auth(self):
s = self.build_session() s.basic_auth('user', 'password') s.headers['Authorization'] = 'token foobarbogus' with s.no_auth(): assert ('Authentication' not in s.headers) assert (s.auth is None) assert (s.headers['Authorization'] == 'token foobarbogus') assert (s.auth == ('use...
'Test that retrieve_client_credentials will return the credentials. We must assert that when set, this function will return them.'
def test_retrieve_client_credentials_when_set(self):
s = self.build_session() s.params = {'client_id': 'id', 'client_secret': 'secret'} assert (s.retrieve_client_credentials() == ('id', 'secret'))
'Test that retrieve_client_credentials will return (None, None). Namely, then the necessary parameters are set, it will not raise an error.'
def test_retrieve_client_credentials_returns_none(self):
s = self.build_session() assert (s.retrieve_client_credentials() == (None, None))
'Verify that adding a label requires authentication.'
def test_add_labels(self):
self.assert_requires_auth(self.instance.add_labels, 'enhancement')
'Verify that assigning an issue requires authentication.'
def test_assign(self):
self.assert_requires_auth(self.instance.assign, 'sigmavirus24')
'Verify that closing an issue requires authentication.'
def test_close(self):
self.assert_requires_auth(self.instance.close)
'Verify that creating a comment requires authentication.'
def test_create_comment(self):
self.assert_requires_auth(self.instance.create_comment, body='comment body')
'Verify that editing a comment requires authentication.'
def test_edit_comment(self):
self.assert_requires_auth(self.instance.edit)
'Verify that locking an issue requires authentication.'
def test_lock(self):
self.assert_requires_auth(self.instance.lock)
'Verify that removing all labels requires authentication.'
def test_remove_all_labels(self):
self.assert_requires_auth(self.instance.remove_all_labels)
'Verify that removing a label requires authentication.'
def test_remove_label(self):
self.assert_requires_auth(self.instance.remove_label, 'enhancement')
'Verify that reopening an issue equires authentication.'
def test_reopen(self):
self.assert_requires_auth(self.instance.reopen)
'Verify that unlocking an issue requires authentication.'
def test_unlock(self):
self.assert_requires_auth(self.instance.unlock)
'Verify the request for adding a label.'
def test_add_labels(self):
self.instance.add_labels('enhancement') self.post_called_with(url_for('labels'), data=['enhancement'])
'Verify the request for assigning an issue.'
def test_assign(self):
with mock.patch.object(Issue, 'edit') as edit: edit.return_value = True labels = [str(label) for label in self.instance.original_labels] self.instance.assign(username='sigmavirus24') edit.assert_called_once_with(self.instance.title, self.instance.body, 'sigmavirus24', self.instance.s...
'Verify the request when assigning a username.'
def test_assign_empty_username(self):
self.instance.assign('') assert (self.session.patch.called is False)
'Verify the request for closing an issue.'
def test_close(self):
self.instance.close() labels = [str(label) for label in self.instance.original_labels] self.patch_called_with(url_for(), data={'assignee': (self.instance.assignee.login or ''), 'body': self.instance.body, 'labels': labels, 'milestone': (self.instance.milestone.number or ''), 'state': 'closed', 'title': self...
'Verify the request for retrieving an issue comment.'
def test_comment(self):
self.instance.comment(1) self.session.get.assert_called_once_with(comment_url_for('1'))
'Verify the request for creating a comment.'
def test_create_comment(self):
data = {'body': 'comment body'} self.instance.create_comment(**data) self.post_called_with(url_for('comments'), data=data)
'Verify request is not made when comment body is empty.'
def test_create_comment_required_body(self):
self.instance.create_comment(body='') assert (self.session.post.called is False)
'Verify the request for removing a lock from an issue.'
def test_create_lock(self):
self.instance.lock() self.session.put.assert_called_once_with(url_for('lock'))
'Verify the request for retrieving an issue comment.'
def test_comment_positive_id(self):
self.instance.comment((-1)) assert (self.session.get.called is False)
'Verify the request for editing an issue.'
def test_edit(self):
data = {'title': 'issue title', 'body': 'issue body', 'assignee': 'sigmavirus24', 'state': 'closed', 'labels': []} self.instance.edit(**data) self.patch_called_with(url_for(), data=data)
'Verify the request for editing an issue with assignees.'
def test_edit_multiple_assignees(self):
data = {'title': 'issue title', 'body': 'issue body', 'assignees': ['itsmemattchung', 'sigmavirus24'], 'state': 'closed', 'labels': []} self.instance.edit(**data) self.patch_called_with(url_for(), data=data)
'Verify the request for editing an issue.'
def test_edit_milestone(self):
data = {'title': 'issue title', 'body': 'issue body', 'assignee': 'sigmavirus24', 'state': 'closed', 'labels': [], 'milestone': 0} self.instance.edit(**data) data['milestone'] = None self.patch_called_with(url_for(), data=data)
'Verify request is not made editing an issue with no parameters.'
def test_edit_no_parameters(self):
self.instance.edit() assert (self.session.patch.called is False)
'Show that enterprise data can be instantiated as Issue.'
def test_enterprise(self):
json = helper.create_example_data_helper('issue_enterprise')() assert github3.issues.Issue(json)
'Show that two instances of Issue are equal.'
def test_equality(self):
issue = github3.issues.Issue(get_issue_example_data()) assert (self.instance == issue) issue._uniq = 1 assert (self.instance != issue)
'Test an issue is closed.'
def test_is_closed(self):
assert (self.instance.is_closed() is False) self.instance.state = 'closed' assert (self.instance.is_closed() is True)
'GitHub sometimes returns `pull` as part of of the `html_url` for Issue requests.'
def test_issue_137(self):
issue = Issue(helper.create_example_data_helper('issue_137')()) self.assertEqual(issue.html_url, 'https://github.com/sigmavirus24/github3.py/pull/1') self.assertEqual(issue.repository, ('sigmavirus24', 'github3.py'))
'Verify the request to retrieve an associated Pull Request.'
def test_pull_request(self):
self.instance.pull_request() self.session.get.assert_called_once_with(self.instance.pull_request_urls['url'])
'Verify no request is made if no pull request url is present.'
def test_pull_request_without_urls(self):
self.instance.pull_request_urls = {} self.instance.pull_request() assert (self.session.get.called is False)
'Show that instance string is formattted properly.'
def test_repr(self):
assert (repr(self.instance) == '<Issue [{r[0]}/{r[1]} #{n}]>'.format(r=self.instance.repository, n=self.instance.number))
'Verify that all labels are removed.'
def test_remove_all_labels(self):
with mock.patch.object(Issue, 'replace_labels') as replace_labels: replace_labels.return_value = [] assert (self.instance.remove_all_labels() == []) replace_labels.assert_called_once_with([])
'Verify the request for removing a label from an issue.'
def test_remove_label(self):
self.instance.remove_label('enhancement') self.session.delete.assert_called_once_with(url_for('labels/enhancement'))
'Verify the request for removing a lock from an issue.'
def test_remove_lock(self):
self.instance.unlock() self.session.delete.assert_called_once_with(url_for('lock'))
'Test the request for reopening an issue.'
def test_reopen(self):
labels = [str(label) for label in self.instance.original_labels] with mock.patch.object(Issue, 'edit') as edit: self.instance.reopen() edit.assert_called_once_with(self.instance.title, self.instance.body, self.instance.assignee.login, 'open', self.instance.milestone.number, labels)
'Verify the request for replacing labels.'
def test_replace_labels(self):
labels = ['foo', 'bar'] self.instance.replace_labels(labels) self.put_called_with(url_for('labels'), data=labels)
'Test the request to retrieve an issue\'s comments.'
def test_comments(self):
i = self.instance.comments() self.get_next(i) self.session.get.assert_called_once_with(url_for('comments'), params={'per_page': 100}, headers={})
'Test the request to retrieve an issue\'s events.'
def test_events(self):
i = self.instance.events() self.get_next(i) self.session.get.assert_called_once_with(url_for('events'), params={'per_page': 100}, headers={})
'Test the request to retrieve an issue\'s labels.'
def test_labels(self):
i = self.instance.labels() self.get_next(i) self.session.get.assert_called_once_with(url_for('labels'), params={'per_page': 100}, headers={})
'Test that deleting a label requires authentication.'
def test_delete(self):
self.assert_requires_auth(self.instance.delete)
'Test that updating label requires authentication.'
def test_update(self):
data = {'name': 'newname', 'color': 'afafaf'} self.assert_requires_auth(self.instance.update, **data)
'Show that two instances of Label are equal.'
def test_equality(self):
label = Label(get_issue_label_example_data()) assert (self.instance == label) label._uniq = 'https://https//api.github.com/repos/sigmavirus24/github3.py/labels/wontfix' assert (self.instance != label)
'Show that instance string is formatted correctly.'
def test_repr(self):
assert (repr(self.instance) == '<Label [{0}]>'.format(self.instance.name))
'Show that instance is formated as a string correctly.'
def test_str(self):
assert (str(self.instance) == self.instance.name)
'Test the request for deleting a label.'
def test_delete(self):
self.instance.delete() assert self.session.delete.called
'Test the request for updating a label.'
def test_update(self):
data = {'name': 'newname', 'color': 'afafaf'} self.instance.update(**data) self.patch_called_with(label_url_for(), data=data)
'Show that instance string is formatted correctly.'
def test_repr(self):
assert (repr(self.instance) == '<Issue Event [{0} by {1}]>'.format('closed', 'octocat'))
'Show that two instances of IssueEvent are equal.'
def test_equality(self):
issue_event = github3.issues.event.IssueEvent(get_issue_event_example_data()) assert (self.instance == issue_event) issue_event._uniq = 'foo' assert (self.instance != issue_event)
'Test the request to add scopes to an authorization.'
def test_add_scopes(self):
self.instance.add_scopes(['scope-one', 'scope-two']) self.post_called_with(url_for(''), data={'add_scopes': ['scope-one', 'scope-two']})
'Test the request to delete an authorization.'
def test_delete(self):
self.instance.delete() self.session.delete.assert_called_once_with(url_for(''))
'Test the request to remove scopes from an authorization.'
def test_remove_scopes(self):
self.instance.remove_scopes(['scope-one', 'scope-two', 'scope-three']) self.post_called_with(url_for(''), data={'rm_scopes': ['scope-one', 'scope-two', 'scope-three']})