def api_delete_comment(): """ Delete a comment. Requires a valid JWT """ claims = get_jwt_claims() user_email = User.from_claims(claims).email post_data = request.get_json() try: comment_id = expect(post_data.get('comment_id'), str, 'comment_id') movie_id = expect(post_data.get('movie_id'), str, 'movie_id') delete_comment(comment_id, user_email) updated_comments = get_movie(movie_id).get('comments') return jsonify({'comments': updated_comments}), 200 except Exception as e: return jsonify({'error': str(e)}), 400
def api_post_comment(): """ Posts a comment about a specific movie. Validates the user is logged in by ensuring a valid JWT is provided """ claims = get_jwt_claims() user = User.from_claims(claims) post_data = request.get_json() try: movie_id = expect(post_data.get('movie_id'), str, 'movie_id') comment = expect(post_data.get('comment'), str, 'comment') add_comment(movie_id, user, comment, datetime.now()) updated_comments = get_movie(movie_id).get('comments') return jsonify({"comments": updated_comments}), 200 except Exception as e: return jsonify({'error': str(e)}), 400
def api_update_comment(): """ Updates a user comment. Validates the user is logged in by ensuring a valid JWT is provided """ claims = get_jwt_claims() user_email = User.from_claims(claims).email post_data = request.get_json() try: comment_id = expect(post_data.get('comment_id'), str, 'comment_id') updated_comment = expect(post_data.get('updated_comment'), str, 'updated_comment') movie_id = expect(post_data.get('movie_id'), str, 'movie_id') update_comment(comment_id, user_email, updated_comment, datetime.now()) updated_comments = get_movie(movie_id).get('comments') return jsonify({"status": "success", "comments": updated_comments}) except Exception as e: return jsonify({'status': 'fail', 'error': str(e)})
def api_update_comment(): """ Updates a user comment. Validates the user is logged in by ensuring a valid JWT is provided """ claims = get_jwt_claims() user_email = User.from_claims(claims).email post_data = request.get_json() try: comment_id = expect(post_data.get('comment_id'), str, 'comment_id') updated_comment = expect(post_data.get('updated_comment'), str, 'updated_comment') movie_id = expect(post_data.get('movie_id'), str, 'movie_id') edit_result = update_comment(comment_id, user_email, updated_comment, datetime.now()) if edit_result.modified_count == 0: raise ValueError("no document updated") updated_comments = get_movie(movie_id).get('comments') return jsonify({"comments": updated_comments}), 200 except Exception as e: return jsonify({'error': str(e)}), 400
"name": "foobar", "email": "*****@*****.**", } fake_user = { "name": "barfoo", "email": "*****@*****.**" } # The Martian movie_id = "573a13eff29313caabdd82f3" now = datetime.now() comment = { 'text': 'fe-fi-fo-fum', 'id': '' } user = User(test_user) n_user = User(fake_user) @pytest.mark.create_update_comments def test_add_comment(client): result = add_comment(movie_id, user, comment['text'], now) assert isinstance(result, InsertOneResult) assert result.acknowledged is True assert result.inserted_id is not None comments = get_movie(movie_id).get('comments') assert comments[0].get('_id') == result.inserted_id assert comments[0].get('text') == comment['text'] comment['id'] = result.inserted_id