-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Create direct API, first try, closes #2 * Review changes * Some changes to be compatible with new restfulpy * Coding style improvement * Review changes * Empty dict is changed to None
- Loading branch information
Showing
8 changed files
with
146 additions
and
38 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
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,44 @@ | ||
from sqlalchemy import or_, and_ | ||
from nanohttp import json, context, HTTPStatus, validate | ||
from restfulpy.controllers import ModelRestController | ||
from restfulpy.authorization import authorize | ||
from restfulpy.orm import DBSession, commit | ||
|
||
from ..models import Direct, User, blocked | ||
|
||
|
||
class DirectController(ModelRestController): | ||
__model__ = Direct | ||
|
||
@authorize | ||
@validate(userId=dict(type_=(int, '705 Invalid User Id'))) | ||
@json(prevent_empty_form='710 Empty Form') | ||
@Direct.expose | ||
@commit | ||
def create(self): | ||
user_id = context.form.get('userId') | ||
destination = DBSession.query(User) \ | ||
.filter(User.id == user_id).one_or_none() | ||
if destination is None: | ||
raise HTTPStatus('611 User Not Found') | ||
|
||
is_blocked = DBSession.query(blocked) \ | ||
.filter(or_( | ||
and_( | ||
blocked.c.source == user_id, | ||
blocked.c.destination == context.identity.id | ||
), | ||
and_( | ||
blocked.c.source == context.identity.id, | ||
blocked.c.destination == user_id | ||
) | ||
)) \ | ||
.count() | ||
if is_blocked: | ||
raise HTTPStatus('613 Not Allowed To Create Direct With This User') | ||
|
||
source = User.current() | ||
direct = Direct(title=destination.title, type='direct') | ||
direct.members = [source, destination] | ||
return direct | ||
|
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 |
---|---|---|
|
@@ -5,44 +5,18 @@ | |
from restfulpy.testing import ApplicableTestCase | ||
from restfulpy.orm import DBSession | ||
|
||
from jaguar import Jaguar | ||
from jaguar.authentication import Authenticator | ||
from jaguar.controllers.root import Root | ||
from jaguar.models.membership import User | ||
|
||
|
||
HERE = path.abspath(path.dirname(__file__)) | ||
|
||
|
||
class AutoDocumentationBDDTest(ApplicableTestCase): | ||
|
||
__application__ = Application( | ||
'Mockup', | ||
root=Root(), | ||
authenticator=Authenticator() | ||
) | ||
__configuration__ = ''' | ||
db: | ||
url: postgresql://postgres:postgres@localhost/jaguar_dev | ||
test_url: postgresql://postgres:postgres@localhost/jaguar_test | ||
administrative_url: postgresql://postgres:postgres@localhost/postgres | ||
activation: | ||
secret: activation-secret | ||
max_age: 86400 # seconds | ||
url: http://nc.carrene.com/activate | ||
''' | ||
|
||
@classmethod | ||
def mockup(cls): | ||
user = User( | ||
email='[email protected]', | ||
title='example', | ||
password='123456', | ||
) | ||
user.is_active = True | ||
DBSession.add(user) | ||
DBSession.commit() | ||
__application_factory__ = Jaguar | ||
|
||
@classmethod | ||
def get_spec_filename(cls, story): | ||
|
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,81 @@ | ||
from bddrest.authoring import status, given, when, Update, response | ||
|
||
from jaguar.tests.helpers import AutoDocumentationBDDTest | ||
from jaguar.models import User, blocked | ||
|
||
|
||
class TestDirect(AutoDocumentationBDDTest): | ||
|
||
@classmethod | ||
def mockup(cls): | ||
session = cls.create_session() | ||
user1 = User( | ||
email='[email protected]', | ||
password='123456', | ||
title='user1' | ||
) | ||
user2 = User( | ||
email='[email protected]', | ||
password='123456', | ||
title='user2', | ||
) | ||
blocker = User( | ||
email='[email protected]', | ||
password='123456', | ||
title='blocker', | ||
) | ||
blocker.blocked_users.append(user1) | ||
session.add_all([blocker, user2]) | ||
session.commit() | ||
|
||
def test_creat_token(self): | ||
self.login( | ||
email='[email protected]', | ||
password='123456', | ||
url='/apiv1/tokens', | ||
verb='CREATE', | ||
) | ||
|
||
with self.given( | ||
'Try to create a direct with a user', | ||
'/apiv1/directs', | ||
'CREATE', | ||
form=dict(userId=3) | ||
): | ||
assert status == 200 | ||
assert response.json['title'] == 'user2' | ||
|
||
when('The user not exists', form=Update(userId=5)) | ||
assert status == '611 User Not Found' | ||
|
||
when( | ||
'Try to pass invalid user id in the form', | ||
form=Update(userId='Invalid') | ||
) | ||
assert status == '705 Invalid User Id' | ||
|
||
when('Try to pass empty form', form=None) | ||
assert status == '710 Empty Form' | ||
|
||
when('Blocked user tries to create a direct', form=Update(userId=1)) | ||
assert status == '613 Not Allowed To Create Direct With This User' | ||
|
||
self.logout() | ||
self.login( | ||
email='[email protected]', | ||
password='123456', | ||
url='/apiv1/tokens', | ||
verb='CREATE', | ||
) | ||
|
||
with self.given( | ||
'Try to create a direct with a blocked user', | ||
'/apiv1/directs', | ||
'CREATE', | ||
form=dict(userId=2) | ||
): | ||
assert status == '613 Not Allowed To Create Direct With This User' | ||
|
||
when('Try to pass an unauthorized request', authorization=None) | ||
assert status == 401 | ||
|
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 |
---|---|---|
|
@@ -6,6 +6,17 @@ | |
|
||
class TestToken(AutoDocumentationBDDTest): | ||
|
||
@classmethod | ||
def mockup(cls): | ||
session = cls.create_session() | ||
user = User( | ||
email='[email protected]', | ||
title='user', | ||
password='123456', | ||
) | ||
session.add(user) | ||
session.commit() | ||
|
||
def test_login(self): | ||
with self.given( | ||
'Login user', | ||
|
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 |
---|---|---|
|
@@ -2,6 +2,4 @@ coverage | |
coveralls | ||
pytest-pudb | ||
pytest-cov | ||
git+git://github.com/Carrene/[email protected] | ||
git+git://github.com/Carrene/restfulpy.git@33d64275bd53b15745e7c6ff6d264989fdb9cb01 | ||
pymlconf==0.8.9 | ||
|
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 |
---|---|---|
|
@@ -13,7 +13,7 @@ | |
|
||
|
||
dependencies = [ | ||
'restfulpy >= 1.0.0a0', | ||
'restfulpy >= 1.2.0b1', | ||
] | ||
|
||
|
||
|