From 53146a8ca7092c98dda2cfad4b18c3d6479c837a Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Wed, 9 Jul 2025 13:14:54 +0800 Subject: [PATCH] fix test --- models/user/user.go | 10 +- models/user/user_test.go | 5 + modules/git/commit.go | 2 +- services/asymkey/commit.go | 7 +- tests/integration/repo_commits_test.go | 128 ++++++++----------------- 5 files changed, 58 insertions(+), 94 deletions(-) diff --git a/models/user/user.go b/models/user/user.go index 4c27d08d89..c362cbc6d2 100644 --- a/models/user/user.go +++ b/models/user/user.go @@ -1189,12 +1189,14 @@ func GetUsersByEmails(ctx context.Context, emails []string) (*EmailUserMap, erro needCheckEmails := make(container.Set[string]) needCheckUserNames := make(container.Set[string]) + noReplyAddressSuffix := "@" + strings.ToLower(setting.Service.NoReplyAddress) for _, email := range emails { - if strings.HasSuffix(email, "@"+setting.Service.NoReplyAddress) { - username := strings.TrimSuffix(email, "@"+setting.Service.NoReplyAddress) - needCheckUserNames.Add(strings.ToLower(username)) + emailLower := strings.ToLower(email) + if noReplyUserNameLower, ok := strings.CutSuffix(emailLower, noReplyAddressSuffix); ok { + needCheckUserNames.Add(noReplyUserNameLower) + needCheckEmails.Add(emailLower) } else { - needCheckEmails.Add(strings.ToLower(email)) + needCheckEmails.Add(emailLower) } } diff --git a/models/user/user_test.go b/models/user/user_test.go index a2597ba3f5..7944fc4b73 100644 --- a/models/user/user_test.go +++ b/models/user/user_test.go @@ -85,6 +85,11 @@ func TestUserEmails(t *testing.T) { testGetUserByEmail(t, c.Email, c.UID) }) } + + t.Run("NoReplyConflict", func(t *testing.T) { + setting.Service.NoReplyAddress = "example.com" + testGetUserByEmail(t, "user1-2@example.COM", 1) + }) }) } diff --git a/modules/git/commit.go b/modules/git/commit.go index bcb71c8f8f..aae40c575b 100644 --- a/modules/git/commit.go +++ b/modules/git/commit.go @@ -22,7 +22,7 @@ import ( type Commit struct { Tree // FIXME: bad design, this field can be nil if the commit is from "last commit cache" - ID ObjectID // The ID of this commit object + ID ObjectID Author *Signature // never nil Committer *Signature // never nil CommitMessage string diff --git a/services/asymkey/commit.go b/services/asymkey/commit.go index 54c980d32f..773e7ca83c 100644 --- a/services/asymkey/commit.go +++ b/services/asymkey/commit.go @@ -36,7 +36,8 @@ func ParseCommitWithSignature(ctx context.Context, c *git.Commit) *asymkey_model } // ParseCommitWithSignatureCommitter parses a commit's GPG or SSH signature. -// If the commit is singed by an instance key, then committer is nil. +// If the commit is singed by an instance key, then committer can be nil. +// If the signature exists, even if committer is nil, the returned CommittingUser will be a non-nil fake user. func ParseCommitWithSignatureCommitter(ctx context.Context, c *git.Commit, committer *user_model.User) *asymkey_model.CommitVerification { // If no signature, just report the committer if c.Signature == nil { @@ -91,7 +92,7 @@ func parseCommitWithGPGSignature(ctx context.Context, c *git.Commit, committer * } // Now try to associate the signature with the committer, if present - if committer != nil && committer.ID != 0 { + if committer.ID != 0 { keys, err := db.Find[asymkey_model.GPGKey](ctx, asymkey_model.FindGPGKeyOptions{ OwnerID: committer.ID, }) @@ -373,7 +374,7 @@ func verifySSHCommitVerificationByInstanceKey(c *git.Commit, committerUser, sign // parseCommitWithSSHSignature check if signature is good against keystore. func parseCommitWithSSHSignature(ctx context.Context, c *git.Commit, committerUser *user_model.User) *asymkey_model.CommitVerification { // Now try to associate the signature with the committer, if present - if committerUser != nil && committerUser.ID != 0 { + if committerUser.ID != 0 { keys, err := db.Find[asymkey_model.PublicKey](ctx, asymkey_model.FindPublicKeyOptions{ OwnerID: committerUser.ID, NotKeytype: asymkey_model.KeyTypePrincipal, diff --git a/tests/integration/repo_commits_test.go b/tests/integration/repo_commits_test.go index f1a268c71d..b8f086e2b1 100644 --- a/tests/integration/repo_commits_test.go +++ b/tests/integration/repo_commits_test.go @@ -24,103 +24,59 @@ import ( func TestRepoCommits(t *testing.T) { defer tests.PrepareTestEnv(t)() - session := loginUser(t, "user2") - // Request repository commits page - req := NewRequest(t, "GET", "/user2/repo1/commits/branch/master") - resp := session.MakeRequest(t, req, http.StatusOK) + t.Run("CommitList", func(t *testing.T) { + req := NewRequest(t, "GET", "/user2/repo16/commits/branch/master") + resp := session.MakeRequest(t, req, http.StatusOK) - doc := NewHTMLParser(t, resp.Body) - commitURL, exists := doc.doc.Find("#commits-table .commit-id-short").Attr("href") - assert.True(t, exists) - assert.NotEmpty(t, commitURL) -} - -func Test_ReposGitCommitListNotMaster(t *testing.T) { - defer tests.PrepareTestEnv(t)() - session := loginUser(t, "user2") - req := NewRequest(t, "GET", "/user2/repo16/commits/branch/master") - resp := session.MakeRequest(t, req, http.StatusOK) - - doc := NewHTMLParser(t, resp.Body) - var commits []string - doc.doc.Find("#commits-table .commit-id-short").Each(func(i int, s *goquery.Selection) { - commitURL, _ := s.Attr("href") - commits = append(commits, path.Base(commitURL)) + var commits, userHrefs []string + doc := NewHTMLParser(t, resp.Body) + doc.doc.Find("#commits-table .commit-id-short").Each(func(i int, s *goquery.Selection) { + commits = append(commits, path.Base(s.AttrOr("href", ""))) + }) + doc.doc.Find("#commits-table .author-wrapper").Each(func(i int, s *goquery.Selection) { + userHrefs = append(userHrefs, s.AttrOr("href", "")) + }) + assert.Equal(t, []string{"69554a64c1e6030f051e5c3f94bfbd773cd6a324", "27566bd5738fc8b4e3fef3c5e72cce608537bd95", "5099b81332712fe655e34e8dd63574f503f61811"}, commits) + assert.Equal(t, []string{"/user2", "/user21", "/user2"}, userHrefs) }) - assert.Equal(t, []string{"69554a64c1e6030f051e5c3f94bfbd773cd6a324", "27566bd5738fc8b4e3fef3c5e72cce608537bd95", "5099b81332712fe655e34e8dd63574f503f61811"}, commits) - var userHrefs []string - doc.doc.Find("#commits-table .author-wrapper").Each(func(i int, s *goquery.Selection) { - userHref, _ := s.Attr("href") - userHrefs = append(userHrefs, userHref) + t.Run("LastCommit", func(t *testing.T) { + req := NewRequest(t, "GET", "/user2/repo16") + resp := session.MakeRequest(t, req, http.StatusOK) + doc := NewHTMLParser(t, resp.Body) + commitHref := doc.doc.Find(".latest-commit .commit-id-short").AttrOr("href", "") + authorHref := doc.doc.Find(".latest-commit .author-wrapper").AttrOr("href", "") + assert.Equal(t, "/user2/repo16/commit/69554a64c1e6030f051e5c3f94bfbd773cd6a324", commitHref) + assert.Equal(t, "/user2", authorHref) }) - assert.Equal(t, []string{"/user2", "/user21", "/user2"}, userHrefs) - // check last commit author wrapper - req = NewRequest(t, "GET", "/user2/repo16") - resp = session.MakeRequest(t, req, http.StatusOK) + t.Run("CommitListNonExistingCommiter", func(t *testing.T) { + // check the commit list for a repository with no gitea user + // * commit 985f0301dba5e7b34be866819cd15ad3d8f508ee (branch2) + // * Author: 6543 <6543@obermui.de> + req := NewRequest(t, "GET", "/user2/repo1/commits/branch/branch2") + resp := session.MakeRequest(t, req, http.StatusOK) - doc = NewHTMLParser(t, resp.Body) - commits = []string{} - doc.doc.Find(".latest-commit .commit-id-short").Each(func(i int, s *goquery.Selection) { - commitURL, _ := s.Attr("href") - commits = append(commits, path.Base(commitURL)) + doc := NewHTMLParser(t, resp.Body) + commitHref := doc.doc.Find("#commits-table tr:first-child .commit-id-short").AttrOr("href", "") + assert.Equal(t, "/user2/repo1/commit/985f0301dba5e7b34be866819cd15ad3d8f508ee", commitHref) + authorElem := doc.doc.Find("#commits-table tr:first-child .author-wrapper") + assert.Equal(t, "6543", authorElem.Text()) + assert.Equal(t, "span", authorElem.Nodes[0].Data) }) - assert.Equal(t, []string{"69554a64c1e6030f051e5c3f94bfbd773cd6a324"}, commits) - userHrefs = []string{} - doc.doc.Find(".latest-commit .author-wrapper").Each(func(i int, s *goquery.Selection) { - userHref, _ := s.Attr("href") - userHrefs = append(userHrefs, userHref) + t.Run("LastCommitNonExistingCommiter", func(t *testing.T) { + req := NewRequest(t, "GET", "/user2/repo1/src/branch/branch2") + resp := session.MakeRequest(t, req, http.StatusOK) + doc := NewHTMLParser(t, resp.Body) + commitHref := doc.doc.Find(".latest-commit .commit-id-short").AttrOr("href", "") + assert.Equal(t, "/user2/repo1/commit/985f0301dba5e7b34be866819cd15ad3d8f508ee", commitHref) + authorElem := doc.doc.Find(".latest-commit .author-wrapper") + assert.Equal(t, "6543", authorElem.Text()) + assert.Equal(t, "span", authorElem.Nodes[0].Data) }) - assert.Equal(t, []string{"/user2"}, userHrefs) -} - -func Test_ReposGitCommitListNoGiteaUser(t *testing.T) { - // Commits list with Gitea User has been tested in Test_ReposGitCommitListNotMaster - defer tests.PrepareTestEnv(t)() - session := loginUser(t, "user2") - - // check commits list for a repository with no gitea user - req := NewRequest(t, "GET", "/user2/repo1/commits/branch/master") - resp := session.MakeRequest(t, req, http.StatusOK) - - doc := NewHTMLParser(t, resp.Body) - var commits []string - doc.doc.Find("#commits-table .commit-id-short").Each(func(i int, s *goquery.Selection) { - commitURL, _ := s.Attr("href") - commits = append(commits, path.Base(commitURL)) - }) - assert.Equal(t, []string{"65f1bf27bc3bf70f64657658635e66094edbcb4d"}, commits) - - var gitUsers []string - doc.doc.Find("#commits-table .author-wrapper").Each(func(i int, s *goquery.Selection) { - assert.Equal(t, "span", goquery.NodeName(s)) - gitUser := s.Text() - gitUsers = append(gitUsers, gitUser) - }) - assert.Equal(t, []string{"user1"}, gitUsers) - - // check last commit author wrapper - req = NewRequest(t, "GET", "/user2/repo1") - resp = session.MakeRequest(t, req, http.StatusOK) - - doc = NewHTMLParser(t, resp.Body) - commits = []string{} - doc.doc.Find(".latest-commit .commit-id-short").Each(func(i int, s *goquery.Selection) { - commitURL, _ := s.Attr("href") - commits = append(commits, path.Base(commitURL)) - }) - assert.Equal(t, []string{"65f1bf27bc3bf70f64657658635e66094edbcb4d"}, commits) - - gitUsers = []string{} - doc.doc.Find(".latest-commit .author-wrapper").Each(func(i int, s *goquery.Selection) { - assert.Equal(t, "span", goquery.NodeName(s)) - gitUsers = append(gitUsers, s.Text()) - }) - assert.Equal(t, []string{"user1"}, gitUsers) } func doTestRepoCommitWithStatus(t *testing.T, state string, classes ...string) {