desc
stringlengths
3
26.7k
decl
stringlengths
11
7.89k
bodies
stringlengths
8
553k
'Test the request to replace the scopes on an authorization.'
def test_replace_scopes(self):
self.instance.replace_scopes(['scope-one', 'scope-two', 'scope-three']) self.post_called_with(url_for(''), data={'scopes': ['scope-one', 'scope-two', 'scope-three']})
'Disable authentication on the Session.'
def after_setup(self):
self.session.has_auth.return_value = False self.session.auth = None
'Test that adding scopes requires authentication.'
def test_add_scopes(self):
self.assert_requires_auth(self.instance.add_scopes)
'Test that deleteing an authorization requires authentication.'
def test_delete(self):
self.assert_requires_auth(self.instance.delete)
'Test that removing scopes requires authentication.'
def test_remove_scopes(self):
self.assert_requires_auth(self.instance.remove_scopes)
'Test that replacing scopes requires authentication.'
def test_replace_scopes(self):
self.assert_requires_auth(self.instance.replace_scopes)
'Show that two instances are equal.'
def test_equality(self):
user = github3.users.User(get_users_example_data()) (self.instance == user) user._uniq += 1 assert (self.instance != user)
'Show that instance string is formatted correctly.'
def test_str(self):
assert (str(self.instance) == 'octocat') assert (repr(self.instance) == '<User [octocat:monalisa octocat]>')
'Verify the request for checking if user can be assignee.'
def test_is_assignee_on(self):
self.instance.is_assignee_on('octocat', 'hello-world') self.session.get.assert_called_once_with(github_url_for('repos/octocat/hello-world/assignees/octocat'))
'Verify request for checking if a user is following a user.'
def test_is_following(self):
self.instance.is_following('sigmavirus24') self.session.get.assert_called_once_with(url_for('/following/sigmavirus24'))
'Test that updating a key requires authentication.'
def test_update(self):
self.assert_requires_auth(self.instance.update, title='New Title', key='Fake key')
'Test that deleting a key requires authentication.'
def test_delete(self):
self.assert_requires_auth(self.instance.delete)
'Show that two instances of Key are equal.'
def test_equality(self):
key = github3.users.Key(get_user_key_example_data()) assert (self.instance == key) key._uniq += 'cruft' assert (self.instance != key)
'Show instance string is formatted properly.'
def test_repr(self):
assert (str(self.instance) == self.instance.key) assert repr(self.instance).startswith('<User Key')
'Test the request for deleting key.'
def test_delete(self):
self.instance.delete() assert (self.session.delete.called is True)
'Test the request for updating a key.'
def test_update(self):
data = {'title': 'New Title', 'key': 'Fake key'} self.instance.update(**data) self.patch_called_with(key_url_for('1'), data=data)
'Test the request to retrieve a user\'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 follwers.'
def test_followers(self):
f = self.instance.followers() self.get_next(f) self.session.get.assert_called_once_with(url_for('followers'), params={'per_page': 100}, headers={})
'Test the request to retrieve users a user is following.'
def test_following(self):
i = self.instance.following() self.get_next(i) self.session.get.assert_called_once_with(url_for('following'), params={'per_page': 100}, headers={})
'Test the request to retrieve a user\'s public keys.'
def test_keys(self):
i = self.instance.keys() self.get_next(i) self.session.get.assert_called_once_with(url_for('keys'), params={'per_page': 100}, headers={})
'Test the request to retrieve a user\'s organization events.'
def test_organization_events(self):
i = self.instance.organization_events('org-name') self.get_next(i) self.session.get.assert_called_once_with(url_for('events/orgs/org-name'), params={'per_page': 100}, headers={})
'Test that organization_events will ignore empty org names.'
def test_organization_events_requires_an_org(self):
i = self.instance.organization_events(None) with pytest.raises(StopIteration): next(i)
'Test the request to retrieve the orgs a user belongs to.'
def test_organizations(self):
i = self.instance.organizations() self.get_next(i) self.session.get.assert_called_once_with(url_for('orgs'), params={'per_page': 100}, headers={})
'Test the request to retrieve the events a user receives.'
def test_received_events(self):
i = self.instance.received_events() self.get_next(i) self.session.get.assert_called_once_with(url_for('received_events'), params={'per_page': 100}, headers={})
'Test the public request to retrieve the events a user received.'
def test_received_events_public_only(self):
i = self.instance.received_events(True) self.get_next(i) self.session.get.assert_called_once_with(url_for('received_events/public'), params={'per_page': 100}, headers={})
'Test the request to retrieve a user\'s starred repos.'
def test_starred_repositories(self):
i = self.instance.starred_repositories() self.get_next(i) self.session.get.assert_called_once_with(url_for('starred'), params={'per_page': 100}, headers={'Accept': 'application/vnd.github.v3.star+json'})
'Test the request to retrieve a user\'s subscriptions.'
def test_subscriptions(self):
i = self.instance.subscriptions() self.get_next(i) self.session.get.assert_called_once_with(url_for('subscriptions'), params={'per_page': 100}, headers={})
'Test that #organization_events requires authentication.'
def test_organization_events(self):
with pytest.raises(github3.GitHubError): self.instance.organization_events('foo')
'Show that the instance string is formatted correctly.'
def test_str(self):
assert (str(self.instance) == self.instance.name) assert (repr(self.instance) == '<Plan [{0}]>'.format(self.instance.name))
'Show that user can check if the plan is free.'
def test_is_free(self):
assert (self.instance.is_free() is False)
'Show that a user can close a Pull Request.'
def test_close(self):
self.instance.close() self.patch_called_with(url_for(), data={'title': self.instance.title, 'body': self.instance.body, 'state': 'closed'})
'Show that a user can comment on a PR.'
def test_create_comment(self):
self.instance.create_comment('body') self.post_called_with(url_for('comments').replace('pulls', 'issues'), data={'body': 'body'})
'Verify the request to create a review comment on a PR diff.'
def test_create_review_comment(self):
self.instance.create_review_comment('body', 'sha', 'path', 6) self.post_called_with(url_for('comments'), data={'body': 'body', 'commit_id': 'sha', 'path': 'path', 'position': 6})
'Show that a user can request the diff of a Pull Request.'
def test_diff(self):
self.instance.diff() self.session.get.assert_called_once_with(url_for(), headers={'Accept': 'application/vnd.github.diff'})
'Show that a user can request the merge status of a PR.'
def test_is_merged_request(self):
self.instance.merged = False self.instance.is_merged() self.session.get.assert_called_once_with(url_for('merge'))
'Show that no request is needed if .merged is True.'
def test_is_merged_no_requset(self):
self.instance.merged = True assert self.instance.is_merged() assert (self.session.get.called is False)
'Show that a user can retrieve the associated issue of a PR.'
def test_issue(self):
self.instance.issue() self.session.get.assert_called_once_with(url_for().replace('pulls', 'issues'))
'Show that a user can merge a Pull Request.'
def test_merge(self):
self.instance.merge() self.put_called_with(url_for('merge'), data={'squash': False})
'Show that a user can merge a Pull Request.'
def test_merge_squash_message(self):
self.instance.merge('commit message', squash=True) self.put_called_with(url_for('merge'), data={'squash': True, 'commit_message': 'commit message'})
'Show that a user can fetch the patch from a Pull Request.'
def test_patch(self):
self.instance.patch() self.session.get.assert_called_once_with(url_for(), headers={'Accept': 'application/vnd.github.patch'})
'Show that a user can reopen a Pull Request that was closed.'
def test_reopen(self):
self.instance.reopen() self.patch_called_with(url_for(), data={'title': self.instance.title, 'body': self.instance.body, 'state': 'open'})
'Show that a user can update a Pull Request.'
def test_update(self):
self.instance.update('my new title', 'my new body', 'open') self.patch_called_with(url_for(), data={'title': 'my new title', 'body': 'my new body', 'state': 'open'})
'Show that you must be authenticated to close a Pull Request.'
def test_close(self):
with pytest.raises(GitHubError): self.instance.close()
'Show that you must be authenticated to close a Pull Request.'
def test_create_review_comment(self):
with pytest.raises(GitHubError): self.instance.create_review_comment('', '', '', 1)
'Show that you must be authenticated to merge a Pull Request.'
def test_merge(self):
with pytest.raises(GitHubError): self.instance.merge()
'Show that you must be authenticated to reopen a Pull Request.'
def test_reopen(self):
with pytest.raises(GitHubError): self.instance.reopen()
'Show that you must be authenticated to update a Pull Request.'
def test_update(self):
with pytest.raises(GitHubError): self.instance.update('foo', 'bar', 'bogus')
'Show that a user can retrieve the commits in a Pull Request.'
def test_commits(self):
i = self.instance.commits() self.get_next(i) self.session.get.assert_called_once_with(url_for('commits'), params={'per_page': 100}, headers={})
'Show that a user can retrieve the issue-like comments on a PR.'
def test_issue_comments(self):
i = self.instance.issue_comments() self.get_next(i) self.session.get.assert_called_once_with(url_for('comments').replace('pulls', 'issues'), params={'per_page': 100}, headers={})
'Show that a user can retrieve the files from a Pull Request.'
def test_files(self):
i = self.instance.files() self.get_next(i) self.session.get.assert_called_once_with(url_for('files'), params={'per_page': 100}, headers={})
'Show that a user can retrieve the review comments on a PR.'
def test_review_comments(self):
i = self.instance.review_comments() self.get_next(i) self.session.get.assert_called_once_with(url_for('comments'), params={'per_page': 100}, headers={})
'Show that a user can retrieve the reviews from a Pull Request.'
def test_reviews(self):
i = self.instance.reviews() self.get_next(i) self.session.get.assert_called_once_with(url_for('reviews'), params={'per_page': 100}, headers={'Accept': 'application/vnd.github.black-cat-preview+json'})
'Verify the request to reply to a review comment.'
def test_reply(self):
self.instance.reply('foo') self.post_called_with(url_for('comments'), data={'body': 'foo', 'in_reply_to': '1'})
'Verify that a user needs to be authenticated to reply.'
def test_reply_requires_authentication(self):
self.session.has_auth.return_value = False with pytest.raises(GitHubError): self.instance.reply('')
'Verify the request made to fetch a pull request file contents.'
def test_contents(self):
self.instance.contents() self.session.get.assert_called_once_with(self.example_data['contents_url'])
'Assert that two trees are equal.'
def test_eq(self):
tree = github3.git.Tree(get_example_data()) assert (self.instance == tree)
'Assert that two trees are not equal.'
def test_ne(self):
tree = github3.git.Tree(get_example_data()) tree._json_data['truncated'] = True assert (self.instance != tree)
'Assert Tree in in the repr.'
def test_repr(self):
assert isinstance(self.instance, github3.git.Tree) assert repr(self.instance).startswith('<Tree')
'Assert that URL is called'
def test_recurse(self):
self.instance.recurse() self.session.get.assert_called_once_with(url_for(), params={'recursive': '1'})
'Show that a user can update the reference.'
def test_update(self):
self.instance.update('fakesha', True) try: self.session.patch.assert_called_once_with(reference_url_for(), data='{"force": true, "sha": "fakesha"}') except AssertionError: self.session.patch.assert_called_once_with(reference_url_for(), data='{"sha": "fakesha", "force": true...
'Generate URLs with the base GitHubEnterprise URL.'
def url_for(self, path=''):
return ((base_url + 'api/v3/') + path.strip('/'))
'Show that an admin can ask for user creation.'
def test_create_user(self):
self.instance.create_user('login_test', 'email_test') self.post_called_with(self.url_for('admin/users'), data={'login': 'login_test', 'email': 'email_test'})
'Show that an admin can ask for user deletion.'
def test_delete_user(self):
self.instance.delete() self.session.delete.assert_called_once_with(self.url_for_admin())
'Show that an admin can ask for user renaming.'
def test_rename_user(self):
self.instance.rename('new_login') self.session.patch.assert_called_once_with(self.url_for_admin(), data={'login': 'new_login'})
'Show that an admin can ask for an impersonation token for a user.'
def test_impersonate(self):
self.instance.impersonate(scopes=['repo', 'user']) self.post_called_with(self.url_for_admin('/authorizations'), data={'scopes': ['repo', 'user']})
'Show that an admin can revoke impersonation tokens for a user.'
def test_revoke_impersonation(self):
self.instance.revoke_impersonation() self.session.delete.assert_called_once_with(self.url_for_admin('/authorizations'))
'Show that an admin can promote a specific user.'
def test_promote(self):
self.instance.promote() self.session.put.assert_called_once_with(self.url_for_user('/site_admin'))
'Show that an admin can demote another admin.'
def test_demote(self):
self.instance.demote() self.session.delete.assert_called_once_with(self.url_for_user('/site_admin'))
'Show that an admin can suspend a user.'
def test_suspend(self):
self.instance.suspend() self.session.put.assert_called_once_with(self.url_for_user('/suspended'))
'Show that an admin can unsuspend a user.'
def test_unsuspend(self):
self.instance.unsuspend() self.session.delete.assert_called_once_with(self.url_for_user('/suspended'))
'Use mock to auto-spec a GitHubSession and return an instance.'
def create_mocked_session(self):
MockedSession = mock.create_autospec(github3.session.GitHubSession) return MockedSession()
'Create a mocked session and add headers and auth attributes.'
def create_session_mock(self, *args):
session = self.create_mocked_session() base_attrs = ['headers', 'auth'] attrs = dict(((key, mock.Mock()) for key in set(args).union(base_attrs))) session.configure_mock(**attrs) session.delete.return_value = None session.get.return_value = None session.patch.return_value = None session.p...
'Use cls.example_data to create an instance of the described class. If cls.example_data is None, just create a simple instance of the class.'
def create_instance_of_described_class(self):
if (self.example_data and self.session): instance = self.described_class(self.example_data, self.session) elif (self.example_data and (not self.session)): instance = self.described_class(self.example_data) else: instance = self.described_class() instance.session = self.sessio...
'Use to assert delete was called with JSON.'
def delete_called_with(self, *args, **kwargs):
self.method_called_with('delete', args, kwargs)
'Assert that a method was called on a session with JSON.'
def method_called_with(self, method_name, args, kwargs):
mock_method = getattr(self.session, method_name) assert (mock_method.called is True) (call_args, call_kwargs) = mock_method.call_args data = kwargs.pop('data', None) call_data = call_kwargs.pop('data', None) if (call_data is None): (call_args, call_data) = (call_args[:1], call_args[1]) ...
'Use to assert patch was called with JSON.'
def patch_called_with(self, *args, **kwargs):
self.method_called_with('patch', args, kwargs)
'Use to assert post was called with JSON.'
def post_called_with(self, *args, **kwargs):
assert (self.session.post.called is True) (call_args, call_kwargs) = self.session.post.call_args data = kwargs.pop('data', None) (call_args, call_data) = (call_args[:1], call_args[1]) if ((not isinstance(data, str)) and call_data): call_data = json.loads(call_data) assert (args == call_a...
'Use to assert put was called with JSON.'
def put_called_with(self, *args, **kwargs):
self.method_called_with('put', args, kwargs)
'Use to set up attributes on self before each test.'
def setUp(self):
self.session = self.create_session_mock() self.instance = self.create_instance_of_described_class() self.described_class._build_url = build_url self.after_setup()
'No-op method to avoid people having to override setUp.'
def after_setup(self):
pass
'Override UnitHelper\'s create_session_mock method. We want all methods to return an instance of the NullObject. This class has a dummy ``__iter__`` implementation which we want for methods that iterate over the results of a response.'
def create_session_mock(self, *args):
session = super(UnitIteratorHelper, self).create_mocked_session(*args) null = NullObject() session.delete.return_value = null session.get.return_value = null session.patch.return_value = null session.post.return_value = null session.put.return_value = null return session
'Nicely wrap up a call to the iterator.'
def get_next(self, iterator):
try: next(iterator) except StopIteration: pass
'Patch a GitHubIterator\'s _get_json method.'
def patch_get_json(self):
self.get_json_mock = mock.patch.object(github3.structs.GitHubIterator, '_get_json') self.patched_get_json = self.get_json_mock.start() self.patched_get_json.return_value = []
'Use UnitHelper\'s setUp but also patch _get_json.'
def setUp(self):
super(UnitIteratorHelper, self).setUp() self.patch_get_json()
'Stop mocking _get_json.'
def tearDown(self):
super(UnitIteratorHelper, self).tearDown() self.get_json_mock.stop()
'Disable authentication on the session.'
def after_setup(self):
self.session.auth = None self.session.has_auth.return_value = False
'Assert error is raised if function is called without authentication.'
def assert_requires_auth(self, func, *args, **kwargs):
with pytest.raises(github3.exceptions.AuthenticationFailed): func(*args, **kwargs)
'A function to proxy to the actual GitHubSession#build_url method.'
def build_url(self, *args, **kwargs):
return github3.session.GitHubSession().build_url(base_url=self.enterprise_url, *args, **kwargs)
'Show that github3.all_events proxies to GitHub.'
def test_all_events(self):
github3.all_events() self.gh.all_events.assert_called_once_with((-1), None)
'Show that github3.public_gists proxies to GitHub.'
def test_public_gists(self):
github3.public_gists() self.gh.public_gists.assert_called_once_with((-1), None)
'Show that github3.all_repositories proxies to GitHub.'
def test_all_repositories(self):
github3.all_repositories() self.gh.all_repositories.assert_called_once_with((-1), None)
'Show that github3.all_users proxies to GitHub.'
def test_all_users(self):
github3.all_users() self.gh.all_users.assert_called_once_with((-1), None)
'Show that github3.authorize proxies to GitHub.'
def test_authorize(self):
args = ('login', 'password', ['scope'], 'note', 'url.com', '', '') with mock.patch('github3.api.GitHub') as gh: github3.authorize(*args) gh().authorize.assert_called_once_with(*args)
'Show that github3.authorize can use an existing GitHub object.'
def test_authorize_with_github_argument(self):
args = ('login', 'password', ['scope'], 'note', 'url.com', '', '') github = mock.Mock(spec_set=github3.GitHub) with mock.patch('github3.api.GitHub') as gh: github3.authorize(github=github, *args) gh().assert_not_called() github.authorize.assert_called_once_with(*args)
'Show that github3.create_gist proxies to GitHub.'
def test_create_gist(self):
args = ('description', {'files': ['file']}) github3.create_gist(*args) self.gh.create_gist.assert_called_once_with(*args)
'Show that github3.enterprise_login returns GitHubEnterprise.'
def test_enterprise_login(self):
args = ('login', 'password', None, 'https://url.com/', None) with mock.patch.object(github3.GitHubEnterprise, 'login') as login: g = github3.enterprise_login(*args) assert isinstance(g, github3.GitHubEnterprise) login.assert_called_once_with('login', 'password', None, None)
'Show that github3.followers_of proxies to GitHub.'
def test_followers_of(self):
github3.followers_of('login') self.gh.followers_of.assert_called_with('login', (-1), None)
'Show that github3.followed_by proxies to GitHub.'
def test_followed_by(self):
github3.followed_by('login') self.gh.followed_by.assert_called_with('login', (-1), None)
'Show that github3.gist proxies to GitHub.'
def test_gist(self):
gist_id = 123 github3.gist(gist_id) self.gh.gist.assert_called_once_with(gist_id)
'Show that github3.gists_by proxies to GitHub.'
def test_gists_by(self):
github3.gists_by('username') self.gh.gists_by.assert_called_once_with('username', (-1), None)