Skip to content
This repository has been archived by the owner on May 3, 2023. It is now read-only.

Commit

Permalink
Error checking on Post Versions
Browse files Browse the repository at this point in the history
  • Loading branch information
borntyping committed Feb 1, 2013
1 parent 62c114e commit 8a1e166
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
3 changes: 3 additions & 0 deletions tentd/blueprints/posts.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ def post(self):
TODO: Separate between apps creating a new post and a notification
from a non-followed entity.
"""
if 'received_at' in request.json:
raise APIBadRequest("You may not set received_at on a post")

post = Post(entity=g.entity, schema=request.json.pop('schema'))
post.new_version(**request.json)
post.save()
Expand Down
23 changes: 19 additions & 4 deletions tentd/tests/blueprints/test_posts.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

from json import dumps

from flask import url_for

from tentd.documents.entity import Post
from tentd.flask import jsonify
from tentd.tests import EntityTentdTestCase
from tentd.utils import time_to_string

class PostTests(EntityTentdTestCase):
"""Tests relating to the post routes."""
Expand Down Expand Up @@ -134,8 +137,20 @@ def test_delete_post_version(self):

class MorePostsTest(EntityTentdTestCase):
"""Tests for posts without having existing posts."""
def test_entity_get_empty_posts(self):
def test_create_invalid_post(self):
response = self.client.post(
url_for('posts.posts', entity=self.entity),
data=dumps({
'content': "Hello world",
'received_at': time_to_string('now')}))

self.assertStatus(response, 400)
self.assertJSONError(response)

def test_get_empty_posts(self):
"""Test that /posts works when there are no posts to return"""
resp = self.client.get('/{}/posts'.format(self.name))
self.assertStatus(resp, 200)
self.assertEquals(resp.json(), [])
response = self.client.get(
url_for('posts.posts', entity=self.entity))

self.assertStatus(response, 200)
self.assertEquals(response.json(), [])
7 changes: 5 additions & 2 deletions tentd/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Miscellaneous utilities for pytentd"""

from datetime import datetime
from functools import wraps
from time import mktime

Expand Down Expand Up @@ -45,6 +46,8 @@ def json_attributes(obj, *names, **kwargs):
json.update(kwargs)
return json

def time_to_string(time_field):
def time_to_string(time):
"""Converts a datetime instance to a string"""
return mktime(time_field.timetuple())
if time is None: return time
if time == "now": time = datetime.now()
return mktime(time.timetuple())

0 comments on commit 8a1e166

Please sign in to comment.