Skip to content

Commit

Permalink
add comment api test
Browse files Browse the repository at this point in the history
  • Loading branch information
JaymzZh committed Oct 27, 2015
1 parent 634833d commit 9034003
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/api_1_0/comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def get_comments():
})


@api.route('/comment/<int:id>')
@api.route('/comments/<int:id>')
def get_comment(id):
comment = Comment.query.get_or_404(id)
return jsonify(comment.to_json())
Expand Down
44 changes: 44 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,47 @@ def test_user(self):
json_response = json.loads(response.data.decode('utf-8'))
self.assertTrue(json_response['username'] == 'zhangmin')

def test_comment(self):
# add two users
r = Role.query.filter_by(name='User').first()
self.assertIsNotNone(r)
u1 = User(email='zhangmin6105@qq.com', username='zhangmm', password='cat', confirmed=True, role=r)
u2 = User(email='zhangmin@qq.com', username='zhangmin', password='cat', confirmed=True, role=r)
db.session.add_all([u1, u2])
db.session.commit()

# add a post
post = Post(body='body of post', author=u1)
db.session.add(post)
db.session.commit()

# write a comment
response = self.client.post(url_for('api.new_post_comment', id=post.id),
headers=self.get_api_headers('zhangmin@qq.com', 'cat'),
data=json.dumps({'body': 'Good [post](http://localhost:5000)!'}))
self.assertTrue(response.status_code == 201)
json_response = json.loads(response.data.decode('utf-8'))
url = response.headers.get('Location')
self.assertIsNotNone(url)
self.assertTrue(json_response['body'] == 'Good [post](http://localhost:5000)!')
self.assertTrue(re.sub('<.*?>', '', json_response['body_html']) == 'Good post!')

# get the new comment
response = self.client.get(url, headers=self.get_api_headers('zhangmin6105@qq.com', 'cat'))
self.assertTrue(response.status_code == 200)
json_response = json.loads(response.data.decode('utf-8'))
self.assertTrue(json_response['url'] == url)
self.assertTrue(json_response['body'] == 'Good [post](http://localhost:5000)!')

# add another comment
comment = Comment(body='Thank you', author=u1, post=post)
db.session.add(comment)
db.session.commit()

# get the two comments from the post
response = self.client.get(url_for('api.get_post_comments', id=post.id),
headers=self.get_api_headers('zhangmin@qq.com', 'cat'))
self.assertTrue(response.status_code == 200)
json_response = json.loads(response.data.decode('utf-8'))
self.assertIsNotNone(json_response.get('comments'))
self.assertTrue(json_response.get('count', 0) == 2)

0 comments on commit 9034003

Please sign in to comment.