Skip to content

Commit b15d136

Browse files
committed
improve the test
1 parent 35ebe99 commit b15d136

File tree

5 files changed

+83
-16
lines changed

5 files changed

+83
-16
lines changed

models/fixtures/attachment.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,16 @@
153153
download_count: 0
154154
size: 0
155155
created_unix: 946684800
156+
157+
-
158+
id: 13
159+
uuid: a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a23
160+
repo_id: 1
161+
issue_id: 3
162+
release_id: 0
163+
uploader_id: 0
164+
comment_id: 7
165+
name: code_comment_uploaded_attachment.png
166+
download_count: 0
167+
size: 0
168+
created_unix: 946684812

models/fixtures/comment.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,12 @@
102102
review_id: 22
103103
assignee_id: 5
104104
created_unix: 946684817
105+
106+
-
107+
id: 12
108+
type: 22 # review
109+
poster_id: 100
110+
issue_id: 3
111+
content: ""
112+
review_id: 10
113+
created_unix: 946684812

models/issues/comment_test.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -124,18 +124,3 @@ func Test_UpdateIssueNumComments(t *testing.T) {
124124
issue2 = unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 2})
125125
assert.Equal(t, 1, issue2.NumComments)
126126
}
127-
128-
func Test_DeleteCommentWithReview(t *testing.T) {
129-
assert.NoError(t, unittest.PrepareTestDatabase())
130-
131-
comment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: 7})
132-
assert.Equal(t, int64(10), comment.ReviewID)
133-
review := unittest.AssertExistsAndLoadBean(t, &issues_model.Review{ID: comment.ReviewID})
134-
135-
// FIXME: the test fixtures needs a review type comment to be created
136-
137-
// since this is the last comment of the review, it should be deleted when the comment is deleted
138-
assert.NoError(t, issues_model.DeleteComment(db.DefaultContext, comment))
139-
140-
unittest.AssertNotExistsBean(t, &issues_model.Review{ID: review.ID})
141-
}

services/issue/comments_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package issue
5+
6+
import (
7+
"testing"
8+
9+
"code.gitea.io/gitea/models/db"
10+
issues_model "code.gitea.io/gitea/models/issues"
11+
repo_model "code.gitea.io/gitea/models/repo"
12+
"code.gitea.io/gitea/models/unittest"
13+
user_model "code.gitea.io/gitea/models/user"
14+
15+
"github.com/stretchr/testify/assert"
16+
)
17+
18+
func Test_DeleteCommentWithReview(t *testing.T) {
19+
assert.NoError(t, unittest.PrepareTestDatabase())
20+
21+
comment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: 7})
22+
assert.NoError(t, comment.LoadAttachments(t.Context()))
23+
assert.Len(t, comment.Attachments, 1)
24+
assert.Equal(t, int64(13), comment.Attachments[0].ID)
25+
assert.Equal(t, int64(10), comment.ReviewID)
26+
review := unittest.AssertExistsAndLoadBean(t, &issues_model.Review{ID: comment.ReviewID})
27+
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
28+
29+
// since this is the last comment of the review, it should be deleted when the comment is deleted
30+
deletedReviewComment, err := DeleteComment(db.DefaultContext, user1, comment)
31+
assert.NoError(t, err)
32+
assert.NotNil(t, deletedReviewComment)
33+
34+
// the review should be deleted as well
35+
unittest.AssertNotExistsBean(t, &issues_model.Review{ID: review.ID})
36+
// the attachment should be deleted as well
37+
unittest.AssertNotExistsBean(t, &repo_model.Attachment{ID: comment.Attachments[0].ID})
38+
}

services/user/delete.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ import (
2222
pull_model "code.gitea.io/gitea/models/pull"
2323
repo_model "code.gitea.io/gitea/models/repo"
2424
user_model "code.gitea.io/gitea/models/user"
25+
"code.gitea.io/gitea/modules/log"
2526
"code.gitea.io/gitea/modules/setting"
27+
"code.gitea.io/gitea/modules/storage"
2628

2729
"xorm.io/builder"
2830
)
@@ -105,7 +107,7 @@ func deleteUser(ctx context.Context, u *user_model.User, purge bool) (err error)
105107

106108
if purge || (setting.Service.UserDeleteWithCommentsMaxTime != 0 &&
107109
u.CreatedUnix.AsTime().Add(setting.Service.UserDeleteWithCommentsMaxTime).After(time.Now())) {
108-
// Delete Comments
110+
// Delete Comments with attachments
109111
const batchSize = 50
110112
for {
111113
comments := make([]*issues_model.Comment, 0, batchSize)
@@ -117,9 +119,29 @@ func deleteUser(ctx context.Context, u *user_model.User, purge bool) (err error)
117119
}
118120

119121
for _, comment := range comments {
122+
// Delete attachments of the comments
123+
if err := comment.LoadAttachments(ctx); err != nil {
124+
return err
125+
}
126+
120127
if _, err = issues_model.DeleteComment(ctx, comment); err != nil {
121128
return err
122129
}
130+
131+
// delete comment attachments
132+
if _, err := repo_model.DeleteAttachments(ctx, comment.Attachments, true); err != nil {
133+
return fmt.Errorf("delete attachments: %w", err)
134+
}
135+
136+
for _, attachment := range comment.Attachments {
137+
if err := storage.Attachments.Delete(repo_model.AttachmentRelativePath(attachment.UUID)); err != nil {
138+
// Even delete files failed, but the attachments has been removed from database, so we
139+
// should not return error but only record the error on logs.
140+
// users have to delete this attachments manually or we should have a
141+
// synchronize between database attachment table and attachment storage
142+
log.Error("delete attachment[uuid: %s] failed: %v", attachment.UUID, err)
143+
}
144+
}
123145
}
124146
}
125147

0 commit comments

Comments
 (0)