def authorize(self, login, password, scopes, note='', note_url='', client_id='', client_secret=''): """Obtain an authorization token from the GitHub API for the GitHub API. :param str login: (required) :param str password: (required) :param list scopes: (required), areas you want this token to apply to, i.e., 'gist', 'user' :param str note: (optional), note about the authorization :param str note_url: (optional), url for the application :param str client_id: (optional), 20 character OAuth client key for which to create a token :param str client_secret: (optional), 40 character OAuth client secret for which to create the token :returns: :class:`Authorization <Authorization>` """ json = None auth = self._session.auth or (login and password) if isinstance(scopes, list) and auth: url = self._build_url('authorizations') data = {'scopes': scopes, 'note': note, 'note_url': note_url, 'client_id': client_id, 'client_secret': client_secret} if self._session.auth: json = self._json(self._post(url, data=data), 201) else: ses = session() ses.auth = (login, password) json = self._json(ses.post(url, data=dumps(data)), 201) return Authorization(json, self) if json else None
def authorization(self, id_num): """Get information about authorization ``id``. :param int id_num: (required), unique id of the authorization :returns: :class:`Authorization <Authorization>` """ json = None if int(id_num) > 0: url = self._build_url('authorizations', str(id_num)) json = self._json(self._get(url), 200) return Authorization(json, self) if json else None
def setUp(self): if not self.auth: json = {'scopes': ['public_repo'], 'url': 'https://api.github.com', 'app': {'url': 'travis-ci.org', 'name': 'Travis'}, 'updated_at': '2012-09-28T03:43:11Z', 'id': 1, 'note': None, 'note_url': None, 'token': 'upupdowndownleftrightba', 'created_at': '2012-02-28T01:45:49Z', } self.authorization = Authorization(json, None) else: self.authorization = self._g.authorize(None, None, []) self.deleted = False
def load_gh_auth(): with open(CREDENTIALS_FILE, 'r') as fd: data = fd.read() return Authorization.from_json(data) return None
class TestAuthorization(BaseTest): def setUp(self): if not self.auth: json = {'scopes': ['public_repo'], 'url': 'https://api.github.com', 'app': {'url': 'travis-ci.org', 'name': 'Travis'}, 'updated_at': '2012-09-28T03:43:11Z', 'id': 1, 'note': None, 'note_url': None, 'token': 'upupdowndownleftrightba', 'created_at': '2012-02-28T01:45:49Z', } self.authorization = Authorization(json, None) else: self.authorization = self._g.authorize(None, None, []) self.deleted = False def tearDown(self): if self.auth and not self.deleted: self.authorization.delete() def test_authorization(self): expect(self.authorization).isinstance(Authorization) expect(repr(self.authorization)) != '' def test_app(self): expect(self.authorization.app).isinstance(dict) def test_id(self): expect(self.authorization.id) > 0 def test_name(self): expect(self.authorization.name) == self.authorization.app.get('name', '') def test_token(self): expect(self.authorization.token) != '' def test_updated_at(self): expect(self.authorization.updated_at).isinstance(datetime) def test_created_at(self): expect(self.authorization.created_at).isinstance(datetime) def test_note(self): expect(self.authorization.note) >= '' def test_note_url(self): expect(self.authorization.note_url) >= '' def test_scopes(self): expect(self.authorization.scopes).isinstance(list) def test_update(self): if not self.auth: return self.authorization.update(['repo', 'repo:status'], ['user'], ['repo:status'], 'https://github.com/sigmavirus24/github3.py') def test_delete(self): if not self.auth: return expect(self.authorization.delete()).is_True() self.deleted = True