forked from go-gitea/gitea
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor DeleteInactiveUsers, fix bug and add tests (go-gitea#30206)
1. check `IsActive` before calling `IsLastAdminUser`. 2. Fix some comments and error messages. 3. Don't `return err` if "removing file" fails in `DeleteUser`. 4. Remove incorrect `DeleteInactiveEmailAddresses`. Active users could also have inactive emails, and inactive emails do not support "olderThan" 5. Add tests
- Loading branch information
1 parent
e579ddc
commit 5360174
Showing
3 changed files
with
45 additions
and
33 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ import ( | |
"fmt" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"code.gitea.io/gitea/models" | ||
"code.gitea.io/gitea/models/auth" | ||
|
@@ -16,6 +17,7 @@ import ( | |
"code.gitea.io/gitea/models/unittest" | ||
user_model "code.gitea.io/gitea/models/user" | ||
"code.gitea.io/gitea/modules/setting" | ||
"code.gitea.io/gitea/modules/timeutil" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
@@ -185,3 +187,26 @@ func TestCreateUser_Issue5882(t *testing.T) { | |
assert.NoError(t, DeleteUser(db.DefaultContext, v.user, false)) | ||
} | ||
} | ||
|
||
func TestDeleteInactiveUsers(t *testing.T) { | ||
addUser := func(name, email string, createdUnix timeutil.TimeStamp, active bool) { | ||
inactiveUser := &user_model.User{Name: name, LowerName: strings.ToLower(name), Email: email, CreatedUnix: createdUnix, IsActive: active} | ||
_, err := db.GetEngine(db.DefaultContext).NoAutoTime().Insert(inactiveUser) | ||
assert.NoError(t, err) | ||
inactiveUserEmail := &user_model.EmailAddress{UID: inactiveUser.ID, IsPrimary: true, Email: email, LowerEmail: strings.ToLower(email), IsActivated: active} | ||
err = db.Insert(db.DefaultContext, inactiveUserEmail) | ||
assert.NoError(t, err) | ||
} | ||
addUser("user-inactive-10", "[email protected]", timeutil.TimeStampNow().Add(-600), false) | ||
addUser("user-inactive-5", "[email protected]", timeutil.TimeStampNow().Add(-300), false) | ||
addUser("user-active-10", "[email protected]", timeutil.TimeStampNow().Add(-600), true) | ||
addUser("user-active-5", "[email protected]", timeutil.TimeStampNow().Add(-300), true) | ||
unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user-inactive-10"}) | ||
unittest.AssertExistsAndLoadBean(t, &user_model.EmailAddress{Email: "[email protected]"}) | ||
assert.NoError(t, DeleteInactiveUsers(db.DefaultContext, 8*time.Minute)) | ||
unittest.AssertNotExistsBean(t, &user_model.User{Name: "user-inactive-10"}) | ||
unittest.AssertNotExistsBean(t, &user_model.EmailAddress{Email: "[email protected]"}) | ||
unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user-inactive-5"}) | ||
unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user-active-10"}) | ||
unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user-active-5"}) | ||
} |