Skip to content

Commit

Permalink
The send message API (#88)
Browse files Browse the repository at this point in the history
* 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
mkhfring authored and pylover committed Aug 15, 2018
1 parent 44ab47b commit 7d2b46c
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .coveralls.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
service_name: travis-pro
repo_token: 4il98EU1yJPayaqnEIoM9iK8K9wWsr8v7
repo_token: plUwptjujYNJ6t2DUZBS49T4YvkEAIT5c
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ A chat application

![Jaguar](https://img00.deviantart.net/0a9d/i/2010/343/9/6/jaguar_by_alannahily-d34ju3t.jpg)

## Branches

### master

[![Build Status](https://travis-ci.com/Carrene/jaguar.svg?token=JgyQwxgapUeYpgeJwWxz&branch=master)](https://travis-ci.com/Carrene/jaguar)

Setting up development Environment on Linux
----------------------------------

Expand Down
1 change: 0 additions & 1 deletion jaguar/controllers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@

from .root import Root

39 changes: 39 additions & 0 deletions jaguar/controllers/message.py
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

21 changes: 20 additions & 1 deletion jaguar/controllers/target.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@

from nanohttp import json, context
from nanohttp import json, context, HTTPUnauthorized, HTTPStatus
from restfulpy.authorization import authorize
from restfulpy.controllers import ModelRestController
from restfulpy.orm import DBSession

from ..models import Target, target_member
from .message import MessageController


class TargetController(ModelRestController):
__model__ = Target

def __call__(self, *remaining_paths):
if len(remaining_paths) > 1 and remaining_paths[1] == 'messages':
target = self.get_target(remaining_paths[0])
return MessageController(target)(*remaining_paths[2:])

return super().__call__(*remaining_paths)

def get_target(self, id):
try:
int(id)
except:
raise HTTPStatus('706 Invalid Target Id')
target = DBSession.query(Target).filter(Target.id == id).one_or_none()
if target is None:
raise HTTPStatus('614 Target Not Exist')

return target

@authorize
@json
@Target.expose
Expand Down
2 changes: 1 addition & 1 deletion jaguar/models/__init__.py
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
1 change: 1 addition & 0 deletions jaguar/models/envelop.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Message(Envelop):
ForeignKey('envelop.id'),
primary_key=True,
)
mime_type=Field(Unicode(25))

# Since collections would be fairly small,
# selecin loding is chosen for this relationship.
Expand Down
2 changes: 1 addition & 1 deletion jaguar/models/tests/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def test_message_model(db):
# Test message model. As every message should have a sender
# to be send, sender_id and target_id can not be nullable
message = Message(
type='message',
mime_type='message',
body='Hello world!',
sender_id=member.id,
target_id=room.id,
Expand Down
63 changes: 63 additions & 0 deletions jaguar/tests/test_send_message.py
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

0 comments on commit 7d2b46c

Please sign in to comment.