Skip to content

Commit 53146a8

Browse files
committed
fix test
1 parent 838b984 commit 53146a8

File tree

5 files changed

+60
-96
lines changed

5 files changed

+60
-96
lines changed

models/user/user.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,12 +1189,14 @@ func GetUsersByEmails(ctx context.Context, emails []string) (*EmailUserMap, erro
11891189

11901190
needCheckEmails := make(container.Set[string])
11911191
needCheckUserNames := make(container.Set[string])
1192+
noReplyAddressSuffix := "@" + strings.ToLower(setting.Service.NoReplyAddress)
11921193
for _, email := range emails {
1193-
if strings.HasSuffix(email, "@"+setting.Service.NoReplyAddress) {
1194-
username := strings.TrimSuffix(email, "@"+setting.Service.NoReplyAddress)
1195-
needCheckUserNames.Add(strings.ToLower(username))
1194+
emailLower := strings.ToLower(email)
1195+
if noReplyUserNameLower, ok := strings.CutSuffix(emailLower, noReplyAddressSuffix); ok {
1196+
needCheckUserNames.Add(noReplyUserNameLower)
1197+
needCheckEmails.Add(emailLower)
11961198
} else {
1197-
needCheckEmails.Add(strings.ToLower(email))
1199+
needCheckEmails.Add(emailLower)
11981200
}
11991201
}
12001202

models/user/user_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ func TestUserEmails(t *testing.T) {
8585
testGetUserByEmail(t, c.Email, c.UID)
8686
})
8787
}
88+
89+
t.Run("NoReplyConflict", func(t *testing.T) {
90+
setting.Service.NoReplyAddress = "example.com"
91+
testGetUserByEmail(t, "user1-2@example.COM", 1)
92+
})
8893
})
8994
}
9095

modules/git/commit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
type Commit struct {
2323
Tree // FIXME: bad design, this field can be nil if the commit is from "last commit cache"
2424

25-
ID ObjectID // The ID of this commit object
25+
ID ObjectID
2626
Author *Signature // never nil
2727
Committer *Signature // never nil
2828
CommitMessage string

services/asymkey/commit.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ func ParseCommitWithSignature(ctx context.Context, c *git.Commit) *asymkey_model
3636
}
3737

3838
// ParseCommitWithSignatureCommitter parses a commit's GPG or SSH signature.
39-
// If the commit is singed by an instance key, then committer is nil.
39+
// If the commit is singed by an instance key, then committer can be nil.
40+
// If the signature exists, even if committer is nil, the returned CommittingUser will be a non-nil fake user.
4041
func ParseCommitWithSignatureCommitter(ctx context.Context, c *git.Commit, committer *user_model.User) *asymkey_model.CommitVerification {
4142
// If no signature, just report the committer
4243
if c.Signature == nil {
@@ -91,7 +92,7 @@ func parseCommitWithGPGSignature(ctx context.Context, c *git.Commit, committer *
9192
}
9293

9394
// Now try to associate the signature with the committer, if present
94-
if committer != nil && committer.ID != 0 {
95+
if committer.ID != 0 {
9596
keys, err := db.Find[asymkey_model.GPGKey](ctx, asymkey_model.FindGPGKeyOptions{
9697
OwnerID: committer.ID,
9798
})
@@ -373,7 +374,7 @@ func verifySSHCommitVerificationByInstanceKey(c *git.Commit, committerUser, sign
373374
// parseCommitWithSSHSignature check if signature is good against keystore.
374375
func parseCommitWithSSHSignature(ctx context.Context, c *git.Commit, committerUser *user_model.User) *asymkey_model.CommitVerification {
375376
// Now try to associate the signature with the committer, if present
376-
if committerUser != nil && committerUser.ID != 0 {
377+
if committerUser.ID != 0 {
377378
keys, err := db.Find[asymkey_model.PublicKey](ctx, asymkey_model.FindPublicKeyOptions{
378379
OwnerID: committerUser.ID,
379380
NotKeytype: asymkey_model.KeyTypePrincipal,

tests/integration/repo_commits_test.go

Lines changed: 44 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -24,103 +24,59 @@ import (
2424

2525
func TestRepoCommits(t *testing.T) {
2626
defer tests.PrepareTestEnv(t)()
27-
28-
session := loginUser(t, "user2")
29-
30-
// Request repository commits page
31-
req := NewRequest(t, "GET", "/user2/repo1/commits/branch/master")
32-
resp := session.MakeRequest(t, req, http.StatusOK)
33-
34-
doc := NewHTMLParser(t, resp.Body)
35-
commitURL, exists := doc.doc.Find("#commits-table .commit-id-short").Attr("href")
36-
assert.True(t, exists)
37-
assert.NotEmpty(t, commitURL)
38-
}
39-
40-
func Test_ReposGitCommitListNotMaster(t *testing.T) {
41-
defer tests.PrepareTestEnv(t)()
42-
session := loginUser(t, "user2")
43-
req := NewRequest(t, "GET", "/user2/repo16/commits/branch/master")
44-
resp := session.MakeRequest(t, req, http.StatusOK)
45-
46-
doc := NewHTMLParser(t, resp.Body)
47-
var commits []string
48-
doc.doc.Find("#commits-table .commit-id-short").Each(func(i int, s *goquery.Selection) {
49-
commitURL, _ := s.Attr("href")
50-
commits = append(commits, path.Base(commitURL))
51-
})
52-
assert.Equal(t, []string{"69554a64c1e6030f051e5c3f94bfbd773cd6a324", "27566bd5738fc8b4e3fef3c5e72cce608537bd95", "5099b81332712fe655e34e8dd63574f503f61811"}, commits)
53-
54-
var userHrefs []string
55-
doc.doc.Find("#commits-table .author-wrapper").Each(func(i int, s *goquery.Selection) {
56-
userHref, _ := s.Attr("href")
57-
userHrefs = append(userHrefs, userHref)
58-
})
59-
assert.Equal(t, []string{"/user2", "/user21", "/user2"}, userHrefs)
60-
61-
// check last commit author wrapper
62-
req = NewRequest(t, "GET", "/user2/repo16")
63-
resp = session.MakeRequest(t, req, http.StatusOK)
64-
65-
doc = NewHTMLParser(t, resp.Body)
66-
commits = []string{}
67-
doc.doc.Find(".latest-commit .commit-id-short").Each(func(i int, s *goquery.Selection) {
68-
commitURL, _ := s.Attr("href")
69-
commits = append(commits, path.Base(commitURL))
70-
})
71-
assert.Equal(t, []string{"69554a64c1e6030f051e5c3f94bfbd773cd6a324"}, commits)
72-
73-
userHrefs = []string{}
74-
doc.doc.Find(".latest-commit .author-wrapper").Each(func(i int, s *goquery.Selection) {
75-
userHref, _ := s.Attr("href")
76-
userHrefs = append(userHrefs, userHref)
77-
})
78-
assert.Equal(t, []string{"/user2"}, userHrefs)
79-
}
80-
81-
func Test_ReposGitCommitListNoGiteaUser(t *testing.T) {
82-
// Commits list with Gitea User has been tested in Test_ReposGitCommitListNotMaster
83-
defer tests.PrepareTestEnv(t)()
8427
session := loginUser(t, "user2")
8528

86-
// check commits list for a repository with no gitea user
87-
req := NewRequest(t, "GET", "/user2/repo1/commits/branch/master")
88-
resp := session.MakeRequest(t, req, http.StatusOK)
89-
90-
doc := NewHTMLParser(t, resp.Body)
91-
var commits []string
92-
doc.doc.Find("#commits-table .commit-id-short").Each(func(i int, s *goquery.Selection) {
93-
commitURL, _ := s.Attr("href")
94-
commits = append(commits, path.Base(commitURL))
29+
t.Run("CommitList", func(t *testing.T) {
30+
req := NewRequest(t, "GET", "/user2/repo16/commits/branch/master")
31+
resp := session.MakeRequest(t, req, http.StatusOK)
32+
33+
var commits, userHrefs []string
34+
doc := NewHTMLParser(t, resp.Body)
35+
doc.doc.Find("#commits-table .commit-id-short").Each(func(i int, s *goquery.Selection) {
36+
commits = append(commits, path.Base(s.AttrOr("href", "")))
37+
})
38+
doc.doc.Find("#commits-table .author-wrapper").Each(func(i int, s *goquery.Selection) {
39+
userHrefs = append(userHrefs, s.AttrOr("href", ""))
40+
})
41+
assert.Equal(t, []string{"69554a64c1e6030f051e5c3f94bfbd773cd6a324", "27566bd5738fc8b4e3fef3c5e72cce608537bd95", "5099b81332712fe655e34e8dd63574f503f61811"}, commits)
42+
assert.Equal(t, []string{"/user2", "/user21", "/user2"}, userHrefs)
9543
})
96-
assert.Equal(t, []string{"65f1bf27bc3bf70f64657658635e66094edbcb4d"}, commits)
9744

98-
var gitUsers []string
99-
doc.doc.Find("#commits-table .author-wrapper").Each(func(i int, s *goquery.Selection) {
100-
assert.Equal(t, "span", goquery.NodeName(s))
101-
gitUser := s.Text()
102-
gitUsers = append(gitUsers, gitUser)
45+
t.Run("LastCommit", func(t *testing.T) {
46+
req := NewRequest(t, "GET", "/user2/repo16")
47+
resp := session.MakeRequest(t, req, http.StatusOK)
48+
doc := NewHTMLParser(t, resp.Body)
49+
commitHref := doc.doc.Find(".latest-commit .commit-id-short").AttrOr("href", "")
50+
authorHref := doc.doc.Find(".latest-commit .author-wrapper").AttrOr("href", "")
51+
assert.Equal(t, "/user2/repo16/commit/69554a64c1e6030f051e5c3f94bfbd773cd6a324", commitHref)
52+
assert.Equal(t, "/user2", authorHref)
10353
})
104-
assert.Equal(t, []string{"user1"}, gitUsers)
105-
106-
// check last commit author wrapper
107-
req = NewRequest(t, "GET", "/user2/repo1")
108-
resp = session.MakeRequest(t, req, http.StatusOK)
10954

110-
doc = NewHTMLParser(t, resp.Body)
111-
commits = []string{}
112-
doc.doc.Find(".latest-commit .commit-id-short").Each(func(i int, s *goquery.Selection) {
113-
commitURL, _ := s.Attr("href")
114-
commits = append(commits, path.Base(commitURL))
55+
t.Run("CommitListNonExistingCommiter", func(t *testing.T) {
56+
// check the commit list for a repository with no gitea user
57+
// * commit 985f0301dba5e7b34be866819cd15ad3d8f508ee (branch2)
58+
// * Author: 6543 <6543@obermui.de>
59+
req := NewRequest(t, "GET", "/user2/repo1/commits/branch/branch2")
60+
resp := session.MakeRequest(t, req, http.StatusOK)
61+
62+
doc := NewHTMLParser(t, resp.Body)
63+
commitHref := doc.doc.Find("#commits-table tr:first-child .commit-id-short").AttrOr("href", "")
64+
assert.Equal(t, "/user2/repo1/commit/985f0301dba5e7b34be866819cd15ad3d8f508ee", commitHref)
65+
authorElem := doc.doc.Find("#commits-table tr:first-child .author-wrapper")
66+
assert.Equal(t, "6543", authorElem.Text())
67+
assert.Equal(t, "span", authorElem.Nodes[0].Data)
11568
})
116-
assert.Equal(t, []string{"65f1bf27bc3bf70f64657658635e66094edbcb4d"}, commits)
11769

118-
gitUsers = []string{}
119-
doc.doc.Find(".latest-commit .author-wrapper").Each(func(i int, s *goquery.Selection) {
120-
assert.Equal(t, "span", goquery.NodeName(s))
121-
gitUsers = append(gitUsers, s.Text())
70+
t.Run("LastCommitNonExistingCommiter", func(t *testing.T) {
71+
req := NewRequest(t, "GET", "/user2/repo1/src/branch/branch2")
72+
resp := session.MakeRequest(t, req, http.StatusOK)
73+
doc := NewHTMLParser(t, resp.Body)
74+
commitHref := doc.doc.Find(".latest-commit .commit-id-short").AttrOr("href", "")
75+
assert.Equal(t, "/user2/repo1/commit/985f0301dba5e7b34be866819cd15ad3d8f508ee", commitHref)
76+
authorElem := doc.doc.Find(".latest-commit .author-wrapper")
77+
assert.Equal(t, "6543", authorElem.Text())
78+
assert.Equal(t, "span", authorElem.Nodes[0].Data)
12279
})
123-
assert.Equal(t, []string{"user1"}, gitUsers)
12480
}
12581

12682
func doTestRepoCommitWithStatus(t *testing.T, state string, classes ...string) {

0 commit comments

Comments
 (0)