-
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.
* Implementing send message API, first try * adding some test for checking invalid target id * All exceptions are handeled * Travis badge is added, closes #5 * The coveralls is enabled * Naming change and closes #5
- Loading branch information
Showing
9 changed files
with
132 additions
and
5 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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
service_name: travis-pro | ||
repo_token: 4il98EU1yJPayaqnEIoM9iK8K9wWsr8v7 | ||
repo_token: plUwptjujYNJ6t2DUZBS49T4YvkEAIT5c |
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 |
---|---|---|
@@ -1,3 +1,2 @@ | ||
|
||
from .root import Root | ||
|
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,39 @@ | ||
from nanohttp import json, context, HTTPStatus, validate | ||
from restfulpy.authorization import authorize | ||
from restfulpy.orm import commit, DBSession | ||
from restfulpy.controllers import ModelRestController | ||
|
||
from ..models import Envelop, Message | ||
|
||
|
||
SUPPORTED_MIME_TYPES=['text/plain'] | ||
|
||
|
||
class MessageController(ModelRestController): | ||
__model__ = Envelop | ||
|
||
def __init__(self, target): | ||
self.target = target | ||
|
||
@authorize | ||
@validate( | ||
body=dict( | ||
max_length=(1024, '702 Must be less than 1024 charecters'), | ||
required='400 Bad Request', | ||
) | ||
) | ||
@json | ||
@Message.expose | ||
@commit | ||
def send(self): | ||
body = context.form.get('body') | ||
mime_type = context.form.get('mimeType') | ||
if not mime_type in SUPPORTED_MIME_TYPES: | ||
raise HTTPStatus('415 Unsupported Media Type') | ||
|
||
message = Message(body=body, mime_type=mime_type) | ||
message.target_id = self.target.id | ||
message.sender_id = context.identity.id | ||
DBSession.add(message) | ||
return message | ||
|
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
|
||
from .target import Target, Room, Direct, target_member | ||
from .envelop import Envelop | ||
from .envelop import Envelop, Message | ||
from .membership import Member, User, blocked, Contact | ||
from .messaging import ActivationEmail |
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,63 @@ | ||
from bddrest.authoring import given, when, Update, status, response, Remove | ||
|
||
from jaguar.tests.helpers import AutoDocumentationBDDTest | ||
from jaguar.models import User, Room, Direct | ||
|
||
|
||
class TestSendMessage(AutoDocumentationBDDTest): | ||
|
||
@classmethod | ||
def mockup(cls): | ||
session = cls.create_session() | ||
user1 = User( | ||
email='[email protected]', | ||
password='123456', | ||
title='user1', | ||
) | ||
room = Room(title='example', type='room') | ||
direct = Direct(title='direct', type='direct') | ||
session.add(user1) | ||
session.add(room) | ||
session.commit() | ||
|
||
def test_send_message_to_target(self): | ||
self.login( | ||
email='[email protected]', | ||
password='123456', | ||
url='/apiv1/tokens', | ||
verb='CREATE' | ||
) | ||
|
||
with self.given( | ||
'Send a message to a target', | ||
'/apiv1/targets/id:1/messages', | ||
'SEND', | ||
form=dict(body='hello world!', mimeType='text/plain') | ||
): | ||
assert status == 200 | ||
assert response.json['body'] == 'hello world!' | ||
|
||
when('Invalid target id', url_parameters=Update(id='Invalid')) | ||
assert status == '706 Invalid Target Id' | ||
|
||
when('Target does not exist', url_parameters=Update(id=3)) | ||
assert status == '614 Target Not Exist' | ||
|
||
when( | ||
'Try to send unsopported media type', | ||
form=Update(mimeType='video/3gpp') | ||
) | ||
assert status == 415 | ||
|
||
when( | ||
'Try to send long text', | ||
form=Update(body=(1024 + 1) * 'a') | ||
) | ||
assert status == '702 Must be less than 1024 charecters' | ||
|
||
when('Remove body from the form', form=Remove('body')) | ||
assert status == 400 | ||
|
||
when('Try to pass an unauthorized request', authorization=None) | ||
assert status == 401 | ||
|