-
Notifications
You must be signed in to change notification settings - Fork 213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FEAT]: Endpoint to Implement Edit or Update of Comments #1199
Open
BlessOnyi
wants to merge
6
commits into
hngprojects:dev
Choose a base branch
from
BlessOnyi:FEAT-endpoint-edit-comments
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
83effc8
Updated requirements.txt
BlessOnyi e9319c3
Merge branch 'dev' of https://github.com/BlessOnyi/hng_boilerplate_py…
BlessOnyi 606775d
Merge branch 'dev' of https://github.com/BlessOnyi/hng_boilerplate_py…
BlessOnyi d41301f
committing feat-endpoint-edit-comment
BlessOnyi 1640721
committing feat-endpoint-edit-comment
BlessOnyi 3b59c1c
commiting changes to feat-endpoint-edit-comment
BlessOnyi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,25 @@ def update(self, db: Session, id: str, schema): | |
db.commit() | ||
db.refresh(comment) | ||
return comment | ||
|
||
def update_comments(self, db: Session, id: str, content: str): | ||
"""Updates a comment""" | ||
|
||
comment = self.fetch(db=db, id=id) | ||
# Update the fields with the provided schema data | ||
if comment is None: | ||
return None | ||
|
||
# Update the comment's content field | ||
comment.content = content | ||
|
||
# Commit the changes to the database | ||
db.commit() | ||
db.refresh(comment) | ||
|
||
return comment | ||
Comment on lines
+61
to
+77
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The update_comments method in the service layer is very similar to the existing update method. Consider consolidating them or reusing the update method. |
||
|
||
|
||
|
||
def delete(self, db: Session, id: str): | ||
"""Deletes a comment""" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import pytest | ||
from fastapi.testclient import TestClient | ||
from main import app # Ensure you import your FastAPI app | ||
from api.db.database import get_db | ||
from unittest.mock import MagicMock | ||
from api.v1.models.user import User | ||
from api.v1.services.comment import comment_service | ||
from api.v1.services.auth import get_current_user | ||
|
||
client = TestClient(app) | ||
|
||
# Override authentication dependency | ||
app.dependency_overrides[get_current_user] = lambda: User(id="user-123") | ||
|
||
@pytest.fixture | ||
def mock_db(): | ||
return MagicMock() | ||
|
||
@pytest.fixture | ||
def mock_current_user(): | ||
return User(id="user-123") | ||
|
||
@pytest.fixture | ||
def mock_comment(): | ||
return MagicMock(id="comment-123", user_id="user-123", content="Old content") | ||
|
||
def test_update_comment_success(mock_db, mock_current_user, mock_comment, monkeypatch): | ||
monkeypatch.setattr(comment_service, "fetch", lambda db, id: mock_comment) | ||
monkeypatch.setattr(comment_service, "update_comments", lambda db, id, content: mock_comment) | ||
|
||
headers = {"Authorization": "Bearer valid_token"} | ||
response = client.patch( | ||
"/api/v1/comments/comment-123", | ||
json={"content": "Updated comment text"}, | ||
headers=headers | ||
) | ||
|
||
assert response.status_code == 200 | ||
assert response.json()["message"] == "Comment updated successfully" | ||
assert response.json()["data"]["content"] == "Updated comment text" | ||
|
||
def test_update_comment_not_found(mock_db, mock_current_user, monkeypatch): | ||
monkeypatch.setattr(comment_service, "fetch", lambda db, id: None) | ||
|
||
headers = {"Authorization": "Bearer valid_token"} | ||
response = client.patch( | ||
"/api/v1/comments/invalid-id", | ||
json={"content": "Updated comment text"}, | ||
headers=headers | ||
) | ||
|
||
assert response.status_code == 404 | ||
assert response.json()["message"] == "Comment not found." | ||
|
||
def test_update_comment_unauthorized(mock_db, mock_comment, monkeypatch): | ||
mock_comment.user_id = "another-user" | ||
monkeypatch.setattr(comment_service, "fetch", lambda db, id: mock_comment) | ||
|
||
headers = {"Authorization": "Bearer valid_token"} | ||
response = client.patch( | ||
"/api/v1/comments/comment-123", | ||
json={"content": "Updated comment text"}, | ||
headers=headers | ||
) | ||
|
||
assert response.status_code == 403 | ||
assert response.json()["message"] == "You do not have permission to edit this comment." |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reduce this space here.