Skip to content

Commit

Permalink
Fix: deleting user that authored some comments (#913)
Browse files Browse the repository at this point in the history
  • Loading branch information
psrok1 committed Feb 21, 2024
1 parent bcd4c6e commit 49b6ae5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
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")
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

0 comments on commit 49b6ae5

Please sign in to comment.