def post(self): user_id = g.current_user_id try: article_id = request.get_json()['articleId'] except Exception: article_id = None if article_id is None: return {'message': 'Something went wrong.'}, 500 try: comment = request.get_json()['comment'] except Exception: comment = None try: CommentSchema().load({'comment': comment}) except ValidationError as e: return e.messages, 422 comment = Comment(user_id=user_id, article_id=article_id, comment=comment).save() id = str(comment.id) return {'id': id}, 201
def test_update_comment_validation(self): with patch('src.middleware.get_jwk.get_jwk') as get_jwk: get_jwk.return_value = json.dumps(self.jwk) existing_comment = Comment(user_id=self.token_subject, article_id=1, comment='This is a comment.').save() payload = json.dumps({'comment': None}) request = self.app.put('/comments/{}'.format(existing_comment.id), headers=self.headers, data=payload) self.assertEqual('Field may not be null.', request.json['comment'][0]) self.assertEqual(422, request.status_code) payload = json.dumps({'comment': ''}) request = self.app.put('/comments/{}'.format(existing_comment.id), headers=self.headers, data=payload) self.assertEqual('Shorter than minimum length 1.', request.json['comment'][0]) self.assertEqual(422, request.status_code)
def test_get_article_comments(self): with patch('src.middleware.get_jwk.get_jwk') as get_jwk: get_jwk.return_value = json.dumps(self.jwk) expected_article_comments = 1 Comment(user_id=self.token_subject, article_id=1, comment='This is a comment.').save() Comment(user_id=self.token_subject, article_id=1, comment='This is a comment.').save() Comment(user_id=self.token_subject, article_id=2, comment='This is a comment.').save() request = self.app.get( '/comments/article/{}'.format(expected_article_comments), headers=self.headers) self.assertEqual(2, len(request.json)) self.assertEqual(200, request.status_code)
def test_create_comment(self): expected_user_id = '854c9a9b-4a4a-410f-867c-9985c17878d8' expected_article_id = 1 expected_comment = 'This is a comment.' comment = Comment(user_id=expected_user_id, article_id=expected_article_id, comment=expected_comment) self.assertEqual(str(comment.user_id), expected_user_id) self.assertEqual(comment.article_id, expected_article_id) self.assertEqual(comment.comment, expected_comment)
def test_create_comment(self): user_id = self.token_subject article_id = 1 comment = 'This is a comment.' Comment(user_id=user_id, article_id=article_id, comment=comment).save() new_comment = Comment.objects.get(user_id=user_id, article_id=article_id, comment=comment) self.assertEqual(article_id, new_comment.article_id) self.assertEqual(comment, new_comment.comment) self.assertIsNotNone(new_comment)
def test_delete_comment(self): with patch('src.middleware.get_jwk.get_jwk') as get_jwk: get_jwk.return_value = json.dumps(self.jwk) existing_comment = Comment(user_id=self.token_subject, article_id=1, comment='This is a comment.').save() request = self.app.delete('/comments/{}'.format( existing_comment.id), headers=self.headers) with self.assertRaises(DoesNotExist): Comment.objects.get(user_id=self.token_subject) self.assertEqual(204, request.status_code)
def test_update_comment(self): with patch('src.middleware.get_jwk.get_jwk') as get_jwk: get_jwk.return_value = json.dumps(self.jwk) expected_comment = 'This is an updated comment.' existing_comment = Comment(user_id=self.token_subject, article_id=1, comment='This is a comment.').save() payload = json.dumps({'comment': expected_comment}) request = self.app.put('/comments/{}'.format(existing_comment.id), headers=self.headers, data=payload) updated_comment = Comment.objects.get(user_id=self.token_subject) self.assertEqual(expected_comment, updated_comment.comment) self.assertEqual(204, request.status_code)
def get(self, id): pipeline = [ { '$project': { '_id': 0, 'id': { '$toString': '$_id' }, 'userId': '$user_id', 'comment': 1, 'createdAt': { '$dateToString': { 'date': '$created_at', } } } } ] comments = Comment.objects(article_id=id).aggregate(pipeline) return list(comments), 200