Skip to content

Commit

Permalink
added POST method in blog.py
Browse files Browse the repository at this point in the history
updated test functions in test_blog.py
  • Loading branch information
taka-rl committed Oct 16, 2024
1 parent 31bf87a commit f2f8ff1
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 19 deletions.
2 changes: 1 addition & 1 deletion flask_app/routes/blog.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def edit_post(post_id):


# Use a decorator so only an admin user can delete a post
@blog_bp.route("/delete-post/<int:post_id>")
@blog_bp.route("/delete-post/<int:post_id>", methods=['POST'])
@admin_only
def delete_post(post_id):
post_to_delete = db.get_or_404(BlogPost, post_id)
Expand Down
99 changes: 81 additions & 18 deletions tests/test_blog.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from flask_app.models import db, BlogPost
from datetime import date
from flask_app.models import db, BlogPost, Comment


def test_create_new_post(super_admin_client):
Expand All @@ -14,10 +15,10 @@ def test_create_new_post(super_admin_client):
assert b"TakaTaka Blog" in post_response.data

# Verify the post exists in the database
blog = BlogPost.query.filter_by(title='Test Post').first()
assert blog is not None
assert blog.subtitle == 'Test Subtitle'
assert blog.body == 'This is a test body content for the post.'
post = BlogPost.query.filter_by(title='Test Post').first()
assert post is not None
assert post.subtitle == 'Test Subtitle'
assert post.body == 'This is a test body content for the post.'


def test_edit_post(super_admin_client):
Expand All @@ -34,13 +35,13 @@ def test_edit_post(super_admin_client):
assert b"TakaTaka Blog" in post_response.data

# Verify the post exists in the database
blog = BlogPost.query.filter_by(title='Test Post').first()
assert blog is not None
assert blog.subtitle == 'Test Subtitle'
assert blog.body == 'This is a test body content for the post.'
post = BlogPost.query.filter_by(title='Test Post').first()
assert post is not None
assert post.subtitle == 'Test Subtitle'
assert post.body == 'This is a test body content for the post.'

# Get post id
edit_response = super_admin_client.post(f'/edit-post/{blog.id}', data={
# Edit the post
edit_response = super_admin_client.post(f'/edit-post/{post.id}', data={
'title': 'Test Post edited',
'subtitle': 'Edited: Test Subtitle',
'img_url': 'https://pixabay.com/illustrations/test-testing-sign-laboratory-670091/',
Expand All @@ -51,17 +52,79 @@ def test_edit_post(super_admin_client):
assert edit_response.status_code == 200

# Reload the post from the database to check the updated values
updated_blog = BlogPost.query.get(blog.id)
assert updated_blog is not None
assert updated_blog.title == 'Test Post edited'
assert updated_blog.subtitle == 'Edited: Test Subtitle'
assert updated_blog.body == 'Edited. This is a test body content for the post.'
updated_post = BlogPost.query.get(post.id)
assert updated_post is not None
assert updated_post.title == 'Test Post edited'
assert updated_post.subtitle == 'Edited: Test Subtitle'
assert updated_post.body == 'Edited. This is a test body content for the post.'


def test_delete_post(super_admin_client):
pass
# Create a post
new_post = BlogPost(title='Test Post',
subtitle='Test Subtitle',
img_url='https://www.abcdefg/test',
body='This is a test body content for the post.',
author_id=1,
date=date.today().strftime("%B %d, %Y")
)
with super_admin_client.application.app_context():
db.session.add(new_post)
db.session.commit()

# Verify whether the post is registered in the database
post = BlogPost.query.filter_by(title='Test Post').first()
assert post is not None
assert post.subtitle == 'Test Subtitle'
assert post.body == 'This is a test body content for the post.'

# Delete a post
delete_response = super_admin_client.post(f'/delete-post/{post.id}')
print(delete_response.data)
assert delete_response.status_code == 302

# Verify whether the blog is deleted
delete_post = BlogPost.query.filter_by(title='Test Post').first()
assert delete_post is None


def test_add_comment(super_admin_client):
pass
# Create a post
new_post = BlogPost(title='Test Post',
subtitle='Test Subtitle',
img_url='https://www.abcdefg/test',
body='This is a test body content for the post.',
author_id=1,
date=date.today().strftime("%B %d, %Y")
)
with super_admin_client.application.app_context():
db.session.add(new_post)
db.session.commit()

# Verify whether the post is registered in the database
new_post = BlogPost.query.filter_by(title='Test Post').first()
assert new_post is not None
assert new_post.subtitle == 'Test Subtitle'
assert new_post.body == 'This is a test body content for the post.'

# Move to the post page
response = super_admin_client.get(f'/post/{new_post.id}')
assert response.status_code == 200
assert b'Test Post' in response.data

# Add a comment
comment_text = 'This is a test comment'
comment_response = super_admin_client.post(f'post/{new_post.id}', data={
'comment_text': comment_text
}, follow_redirects=True)

assert comment_response.status_code == 200
assert b'Test Post' in comment_response.data

# Verify whether the comment exists
comment = Comment.query.filter_by(text=comment_text).first()
assert comment is not None
assert comment.text == comment_text
assert comment.post_id == new_post.id
assert comment.comment_author is not None

0 comments on commit f2f8ff1

Please sign in to comment.