From 645f58b4eca3ebe1a5c59fe19ba4fa4e8a971487 Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Thu, 21 Jul 2022 23:29:19 +0530 Subject: [PATCH 1/3] fix: `Actor` is required to get user repositories --- models/repo/repo_list.go | 4 ++++ services/user/user.go | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/models/repo/repo_list.go b/models/repo/repo_list.go index a70fc8efd409a..9de76fa5ffa14 100644 --- a/models/repo/repo_list.go +++ b/models/repo/repo_list.go @@ -6,6 +6,7 @@ package repo import ( "context" + "errors" "fmt" "strings" @@ -695,6 +696,9 @@ func GetUserRepositories(opts *SearchRepoOptions) (RepositoryList, int64, error) } cond := builder.NewCond() + if opts.Actor == nil { + return nil, 0, errors.New("GetUserRepositories: Actor is needed but not given") + } cond = cond.And(builder.Eq{"owner_id": opts.Actor.ID}) if !opts.Private { cond = cond.And(builder.Eq{"is_private": false}) diff --git a/services/user/user.go b/services/user/user.go index 448b7c2daf8dc..2d351d4bf21aa 100644 --- a/services/user/user.go +++ b/services/user/user.go @@ -74,7 +74,7 @@ func DeleteUser(ctx context.Context, u *user_model.User, purge bool) error { Page: 1, }, Private: true, - OwnerID: u.ID, + Actor: u, }) if err != nil { return fmt.Errorf("SearchRepositoryByName: %v", err) From a55fea91be9f1311cd39486a7671c19d1a116a51 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 21 Jul 2022 21:23:35 +0200 Subject: [PATCH 2/3] Update services/user/user.go --- services/user/user.go | 1 + 1 file changed, 1 insertion(+) diff --git a/services/user/user.go b/services/user/user.go index 2d351d4bf21aa..1edd9294cbfbd 100644 --- a/services/user/user.go +++ b/services/user/user.go @@ -74,6 +74,7 @@ func DeleteUser(ctx context.Context, u *user_model.User, purge bool) error { Page: 1, }, Private: true, + OwnerID: u.ID, Actor: u, }) if err != nil { From 084f9b9a7baf55ee04247a2fdb46a59169b70c37 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Thu, 21 Jul 2022 21:05:58 +0100 Subject: [PATCH 3/3] add purge testcase Signed-off-by: Andrew Thornton --- services/user/user_test.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/services/user/user_test.go b/services/user/user_test.go index aefbcd9ecb491..d8673593df22c 100644 --- a/services/user/user_test.go +++ b/services/user/user_test.go @@ -60,6 +60,26 @@ func TestDeleteUser(t *testing.T) { assert.Error(t, DeleteUser(db.DefaultContext, org, false)) } +func TestPurgeUser(t *testing.T) { + test := func(userID int64) { + assert.NoError(t, unittest.PrepareTestDatabase()) + user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: userID}).(*user_model.User) + + err := DeleteUser(db.DefaultContext, user, true) + assert.NoError(t, err) + + unittest.AssertNotExistsBean(t, &user_model.User{ID: userID}) + unittest.CheckConsistencyFor(t, &user_model.User{}, &repo_model.Repository{}) + } + test(2) + test(4) + test(8) + test(11) + + org := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3}).(*user_model.User) + assert.Error(t, DeleteUser(db.DefaultContext, org, false)) +} + func TestCreateUser(t *testing.T) { user := &user_model.User{ Name: "GiteaBot",