def test_get_a_unreviewed_comment_without_permission(self): with self.app.test_request_context(), self.db.atomic(): comment = Comment.create(content='comment_test', article=self.art, nickname='tester', reviewed=False) self.login_as_su() resp = self.get_a_comment(comment.id) self.assertResponseErrorInField(resp, 'comment_id')
def test_get_a_comment_mismatch_article(self): with self.app.test_request_context(), self.db.atomic(): article = Article.create(id='testart_', title='', text_type='', source_text='') comment = Comment.create(content='comment_test', article=self.art, nickname='tester', reviewed=True) resp = self.client.get(self.api_url_base + '/article/' + article.id + '/comment/' + str(comment.id)) self.assertResponseErrorInField(resp, 'comment_id')
def test_get_a_unreviewed_comment(self): with self.app.test_request_context(), self.db.atomic(): comment = Comment.create(content='comment_test', article=self.art, nickname='tester', reviewed=False) resp = self.get_a_comment(comment.id) self.assertResponseRestfulAndSuccess(resp) self.assertJSONHasKey(resp, 'comment') comment_ = self.get_json(resp)['comment'] self.assertEqual(comment_['content'], comment.content)
def insert_a_comment(self, reviewed=False): with self.app.test_request_context(): return Comment.create(article=self.art, reviewed=reviewed, content='', nickname='')
def write_comment(aid, reply_to_cid=None): '''``POST`` |API_URL_BASE|/article/:aid/comment/ ``POST`` |API_URL_BASE|/article/:aid/comment/:cid Write a comment to an article. :param nickname: **JSON Param** this argument will be ignored when current user is authenticated :param content: **JSON Param** required Response JSON: .. code-block:: javascript // success { $errors: null, comment: { id: integer, nickname: string, content: string, time: datetime, reply_to: integer // maybe null if no references. } } // failed { $errors: { comment_id: 'the comment you reply to doesn't not exist.' nickname: 'this nickname is illegal', content: 'this content is illegal.' } } Permission required: ``WRITE_COMMENT`` ''' json = request.get_json() try: author_id = (Article.select(Article.author) .where(Article.id == aid).get()).author_id reply_to = reply_to_cid if reply_to is not None: if not Comment.exist((Comment.article == aid) & (Comment.id == reply_to)): raise Comment.DoesNotExist() except (Article.DoesNotExist, Comment.DoesNotExist): return {'comment_id': '欲回复的评论不存在'} is_author = author_id == current_user.get_id() if is_author: nickname = current_user.name else: nickname = json.get('nickname') or '' nickname = nickname.strip() if not nickname: return {'nickname': '请输入有效的昵称'} content = json.get('content') or '' content = content.strip() if not content: return {'content': '请输入有效的内容'} reviewed = is_author or not app_config['COMMENT_NEED_REVIEW'] comment = Comment.create(article=aid, content=content, nickname=nickname, reviewed=reviewed, is_author=is_author, reply_to=reply_to) event_emitted.send( current_app._get_current_object(), type='Comment: Create', description='new comment(%d) in article(%s) has been added.' % (comment.id, aid) ) return None, {'comment': comment.to_dict()}