Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split Authorization class into it's own module.
Because of this, create tests for it and documentation.
- Loading branch information
1 parent
c2bf97f
commit 8dc24b2
Showing
7 changed files
with
190 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
.. module:: github3 | ||
.. module:: github3.auths | ||
|
||
Authorization | ||
============= | ||
|
||
This part of the documentation covers the | ||
:class:`Authorization <Authorization>` object. | ||
|
||
.. autoclass:: Authorization | ||
:inherited-members: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
""" | ||
github3.auths | ||
============= | ||
This module contains the Authorization object. | ||
""" | ||
|
||
from github3.models import GitHubCore | ||
from json import dumps | ||
|
||
|
||
class Authorization(GitHubCore): | ||
"""The :class:`Authorization <Authorization>` object.""" | ||
def __init__(self, auth, session): | ||
super(Authorization, self).__init__(auth, session) | ||
#: Details about the application (name, url) | ||
self.app = auth.get('app', {}) | ||
#: Returns the Authorization token | ||
self.token = auth.get('token', '') | ||
#: App name | ||
self.name = auth.app.get('name', '') | ||
#: URL about the note | ||
self.note_url = auth.get('note_url', '') | ||
#: Note about the authorization | ||
self.note = auth.get('note', '') | ||
#: List of scopes this applies to | ||
self.scopes = auth.get('scopes', []) | ||
#: Unique id of the authorization | ||
self.id = auth.get('id', 0) | ||
self._api = self._build_url('authorizations', str(self.id)) | ||
#: datetime object representing when the authorization was created. | ||
self.created_at = None | ||
if auth.get('created_at'): | ||
self.created_at = self._strptime(auth.get('created_at')) | ||
#: datetime object representing when the authorization was created. | ||
self.updated_at = None | ||
if auth.get('updated_at'): | ||
self.updated_at = self._strptime(auth.get('updated_at')) | ||
|
||
def __repr__(self): | ||
return '<Authorization [{0}]>'.format(self.name) | ||
|
||
def _update_(self, auth): | ||
self.__init__(auth, self._session) | ||
|
||
def delete(self): | ||
"""delete this authorization""" | ||
return self._boolean(self._delete(self._api), 204, 404) | ||
|
||
def update(self, scopes=[], add_scopes=[], rm_scopes=[], note='', | ||
note_url=''): | ||
"""Update this authorization. | ||
:param scopes: (optional), replaces the authorization scopes with these | ||
:type scopes: list | ||
:param add_scopes: (optional), scopes to be added | ||
:type add_scopes: list | ||
:param rm_scopes: (optional), scopes to be removed | ||
:type rm_scopes: list | ||
:param note: (optional), new note about authorization | ||
:type note: str | ||
:param note_url: (optional), new note URL about this authorization | ||
:type note_url: str | ||
:returns: bool | ||
""" | ||
success = False | ||
if scopes: | ||
d = dumps({'scopes': scopes}) | ||
json = self._json(self._get(self._api, data=d), 200) | ||
self._update_(json) | ||
success = True | ||
if add_scopes: | ||
d = dumps({'add_scopes': add_scopes}) | ||
json = self._json(self._get(self._api, data=d), 200) | ||
self._update_(json) | ||
success = True | ||
if rm_scopes: | ||
d = dumps({'remove_scopes': rm_scopes}) | ||
json = self._json(self._get(self._api, data=d), 200) | ||
self._update_(json) | ||
success = True | ||
if note or note_url: | ||
d = dumps({'note': note, 'note_url': note_url}) | ||
json = self._json(self._get(self._api, data=d), 200) | ||
self._update_(json) | ||
success = True | ||
return success |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from .base import expect, BaseTest | ||
from datetime import datetime | ||
from github3.auths import Authorization | ||
|
||
|
||
class TestAuthorization(BaseTest): | ||
def __init__(self, methodName='runTest'): | ||
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': 0, | ||
'note': None, | ||
'note_url': None, | ||
'token': 'upupdowndownleftrightba', | ||
'created_at': '2012-02-28T01:45:49Z', | ||
} | ||
self.authorization = Authorization(json) | ||
else: | ||
self.authorization = self.g.authorize(self.user, self.pw, []) | ||
self.deleted = False | ||
|
||
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_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 | ||
if self.deleted: | ||
self.authorization = self._g.authorize(None, None, []) | ||
self.authorization.update(['repo', 'repo:status'], ['user'], | ||
['repo:status'], 'https://github.com/sigmavirus24/github3.py') | ||
if self.deleted: | ||
self.authorization.delete() | ||
|
||
def test_delelte(self): | ||
if not self.auth: | ||
return | ||
expect(self.authorization.delete()).is_True() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters