Skip to content
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

Fix: deleting user that authored some comments #913

Merged
merged 5 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion mwdb/model/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ class User(db.Model):
)

commented_objects = db.relationship(
"Object", secondary="comment", back_populates="comment_authors"
"Object",
secondary="comment",
back_populates="comment_authors",
passive_deletes=True,
)

comments = db.relationship(
Expand Down
2 changes: 1 addition & 1 deletion mwdb/schema/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ class CommentRequestSchema(CommentSchemaBase):

class CommentItemResponseSchema(CommentSchemaBase):
id = fields.Int(required=True, allow_none=False)
author = fields.Str(required=True, allow_none=False, attribute="author_login")
author = fields.Str(required=True, default=None, attribute="author_login")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we won't remove comments when the user is deleted? How does it look in the mwdb interface?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we don't want to lose information that is potentially valuable.
image

timestamp = UTCDateTime(required=True, allow_none=False)
19 changes: 19 additions & 0 deletions tests/backend/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import requests
from dateutil.parser import parse

from .relations import RelationTestCase
from .utils import rand_string, ShouldRaise


Expand Down Expand Up @@ -229,3 +230,21 @@ def test_zero_starting_crc32(admin_session):
content = b'\xf7\xb8\xb4\xab\x6b\x35\x31\x8a'
sample = admin_session.add_sample(name, content)
assert sample["crc32"] == "000d06ec"


def test_user_delete(admin_session):
# Make user and use it to comment new sample
case = RelationTestCase(admin_session)
alice = case.new_user("Alice", capabilities=["adding_comments"])
sample = case.new_sample("Sample")
sample.create(alice)
alice.session.add_comment(sample.dhash, "random comment")
# Then try to remove that user
admin_session.remove_user(alice.identity)
# Object should be still reachable
admin_session.get_sample(sample.dhash)
# And should still have one comment (with nulled author)
comments = admin_session.get_comments(sample.dhash)
assert len(comments) == 1
assert comments[0]["comment"] == "random comment"
assert comments[0]["author"] is None
Loading