From cf3f046d7acfad68c165d37cac69d90669561427 Mon Sep 17 00:00:00 2001 From: Gusted Date: Sat, 29 Jul 2023 23:30:25 +0200 Subject: [PATCH 1/2] [TESTS] Add some Wiki unit tests - Just to get 100% coverage on services/wiki/wiki_path.go, nothing special. This is just an formality. --- services/wiki/wiki_test.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/services/wiki/wiki_test.go b/services/wiki/wiki_test.go index ccb230e06f..f126224244 100644 --- a/services/wiki/wiki_test.go +++ b/services/wiki/wiki_test.go @@ -307,3 +307,15 @@ func TestPrepareWikiFileName_FirstPage(t *testing.T) { assert.NoError(t, err) assert.EqualValues(t, "Home.md", newWikiPath) } + +func TestWebPathConversion(t *testing.T) { + assert.Equal(t, "path/wiki", WebPathToURLPath(WebPath("path/wiki"))) + assert.Equal(t, "wiki", WebPathToURLPath(WebPath("wiki"))) + assert.Equal(t, "", WebPathToURLPath(WebPath(""))) +} + +func TestWebPathFromRequest(t *testing.T) { + assert.Equal(t, WebPath("a%2Fb"), WebPathFromRequest("a/b")) + assert.Equal(t, WebPath("a"), WebPathFromRequest("a")) + assert.Equal(t, WebPath("b"), WebPathFromRequest("a/../b")) +} From ec5901fcd04f3030c947a3a64a9b128418db8a23 Mon Sep 17 00:00:00 2001 From: Gusted Date: Sat, 29 Jul 2023 23:31:00 +0200 Subject: [PATCH 2/2] [TESTS] Add unit test for user renaming - The user renaming function has zero test coverage. - This patch brings that up to speed to test for various scenarios and ensure that in a normal workflow the correct things has changed to their respective new value. Most scenarios are to ensure certain things DO NOT happen. --- services/user/user_test.go | 64 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/services/user/user_test.go b/services/user/user_test.go index a25804fceb..3f1bf9a0f8 100644 --- a/services/user/user_test.go +++ b/services/user/user_test.go @@ -4,10 +4,13 @@ package user import ( + "fmt" "path/filepath" + "strings" "testing" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/auth" "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/organization" repo_model "code.gitea.io/gitea/models/repo" @@ -94,6 +97,67 @@ func TestCreateUser(t *testing.T) { assert.NoError(t, DeleteUser(db.DefaultContext, user, false)) } +func TestRenameUser(t *testing.T) { + assert.NoError(t, unittest.PrepareTestDatabase()) + user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 21}) + + t.Run("Non-Local", func(t *testing.T) { + u := &user_model.User{ + Type: user_model.UserTypeIndividual, + LoginType: auth.OAuth2, + } + assert.ErrorIs(t, RenameUser(db.DefaultContext, u, "user_rename"), user_model.ErrUserIsNotLocal{}) + }) + + t.Run("Same username", func(t *testing.T) { + assert.ErrorIs(t, RenameUser(db.DefaultContext, user, user.Name), user_model.ErrUsernameNotChanged{UID: user.ID, Name: user.Name}) + }) + + t.Run("Non usable username", func(t *testing.T) { + usernames := []string{"--diff", "aa.png", ".well-known", "search", "aaa.atom"} + for _, username := range usernames { + t.Run(username, func(t *testing.T) { + assert.Error(t, user_model.IsUsableUsername(username)) + assert.Error(t, RenameUser(db.DefaultContext, user, username)) + }) + } + }) + + t.Run("Only capitalization", func(t *testing.T) { + caps := strings.ToUpper(user.Name) + unittest.AssertNotExistsBean(t, &user_model.User{ID: user.ID, Name: caps}) + unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerID: user.ID, OwnerName: user.Name}) + + assert.NoError(t, RenameUser(db.DefaultContext, user, caps)) + + unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: user.ID, Name: caps}) + unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerID: user.ID, OwnerName: caps}) + }) + + t.Run("Already exists", func(t *testing.T) { + existUser := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}) + + assert.ErrorIs(t, RenameUser(db.DefaultContext, user, existUser.Name), user_model.ErrUserAlreadyExist{Name: existUser.Name}) + assert.ErrorIs(t, RenameUser(db.DefaultContext, user, existUser.LowerName), user_model.ErrUserAlreadyExist{Name: existUser.LowerName}) + newUsername := fmt.Sprintf("uSEr%d", existUser.ID) + assert.ErrorIs(t, RenameUser(db.DefaultContext, user, newUsername), user_model.ErrUserAlreadyExist{Name: newUsername}) + }) + + t.Run("Normal", func(t *testing.T) { + oldUsername := user.Name + newUsername := "User_Rename" + + assert.NoError(t, RenameUser(db.DefaultContext, user, newUsername)) + unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: user.ID, Name: newUsername, LowerName: strings.ToLower(newUsername)}) + + redirectUID, err := user_model.LookupUserRedirect(oldUsername) + assert.NoError(t, err) + assert.EqualValues(t, user.ID, redirectUID) + + unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerID: user.ID, OwnerName: user.Name}) + }) +} + func TestCreateUser_Issue5882(t *testing.T) { // Init settings _ = setting.Admin