Skip to content

Commit e47bba0

Browse files
authored
Fix test code (go-gitea#33829)
Do not use onGiteaRun if there is no git operation
1 parent 7fa47de commit e47bba0

6 files changed

+107
-135
lines changed

tests/integration/api_activitypub_person_test.go

+15-38
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,28 @@ import (
77
"fmt"
88
"net/http"
99
"net/http/httptest"
10-
"net/url"
1110
"testing"
1211

1312
"code.gitea.io/gitea/models/db"
1413
user_model "code.gitea.io/gitea/models/user"
1514
"code.gitea.io/gitea/modules/activitypub"
1615
"code.gitea.io/gitea/modules/setting"
16+
"code.gitea.io/gitea/modules/test"
1717
"code.gitea.io/gitea/routers"
18+
"code.gitea.io/gitea/tests"
1819

1920
ap "github.com/go-ap/activitypub"
2021
"github.com/stretchr/testify/assert"
2122
)
2223

2324
func TestActivityPubPerson(t *testing.T) {
24-
setting.Federation.Enabled = true
25-
testWebRoutes = routers.NormalRoutes()
26-
defer func() {
27-
setting.Federation.Enabled = false
28-
testWebRoutes = routers.NormalRoutes()
29-
}()
25+
defer tests.PrepareTestEnv(t)()
26+
defer test.MockVariableValue(&setting.Federation.Enabled, true)()
27+
defer test.MockVariableValue(&testWebRoutes, routers.NormalRoutes())()
28+
29+
t.Run("ExistingPerson", func(t *testing.T) {
30+
defer tests.PrintCurrentTest(t)()
3031

31-
onGiteaRun(t, func(*testing.T, *url.URL) {
3232
userID := 2
3333
username := "user2"
3434
req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/activitypub/user-id/%v", userID))
@@ -56,41 +56,18 @@ func TestActivityPubPerson(t *testing.T) {
5656
assert.NotNil(t, pubKeyPem)
5757
assert.Regexp(t, "^-----BEGIN PUBLIC KEY-----", pubKeyPem)
5858
})
59-
}
60-
61-
func TestActivityPubMissingPerson(t *testing.T) {
62-
setting.Federation.Enabled = true
63-
testWebRoutes = routers.NormalRoutes()
64-
defer func() {
65-
setting.Federation.Enabled = false
66-
testWebRoutes = routers.NormalRoutes()
67-
}()
68-
69-
onGiteaRun(t, func(*testing.T, *url.URL) {
59+
t.Run("MissingPerson", func(t *testing.T) {
60+
defer tests.PrintCurrentTest(t)()
7061
req := NewRequest(t, "GET", "/api/v1/activitypub/user-id/999999999")
7162
resp := MakeRequest(t, req, http.StatusNotFound)
7263
assert.Contains(t, resp.Body.String(), "user does not exist")
7364
})
74-
}
75-
76-
func TestActivityPubPersonInbox(t *testing.T) {
77-
setting.Federation.Enabled = true
78-
testWebRoutes = routers.NormalRoutes()
79-
defer func() {
80-
setting.Federation.Enabled = false
81-
testWebRoutes = routers.NormalRoutes()
82-
}()
83-
84-
srv := httptest.NewServer(testWebRoutes)
85-
defer srv.Close()
65+
t.Run("MissingPersonInbox", func(t *testing.T) {
66+
defer tests.PrintCurrentTest(t)()
67+
srv := httptest.NewServer(testWebRoutes)
68+
defer srv.Close()
69+
defer test.MockVariableValue(&setting.AppURL, srv.URL+"/")()
8670

87-
onGiteaRun(t, func(*testing.T, *url.URL) {
88-
appURL := setting.AppURL
89-
setting.AppURL = srv.URL + "/"
90-
defer func() {
91-
setting.Database.LogSQL = false
92-
setting.AppURL = appURL
93-
}()
9471
username1 := "user1"
9572
ctx := t.Context()
9673
user1, err := user_model.GetUserByName(ctx, username1)

tests/integration/api_admin_org_test.go

+24-29
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package integration
55

66
import (
77
"net/http"
8-
"net/url"
98
"strings"
109
"testing"
1110

@@ -19,10 +18,12 @@ import (
1918
)
2019

2120
func TestAPIAdminOrgCreate(t *testing.T) {
22-
onGiteaRun(t, func(*testing.T, *url.URL) {
23-
session := loginUser(t, "user1")
24-
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteAdmin)
21+
defer tests.PrepareTestEnv(t)()
22+
session := loginUser(t, "user1")
23+
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteAdmin)
2524

25+
t.Run("CreateOrg", func(t *testing.T) {
26+
defer tests.PrintCurrentTest(t)()
2627
org := api.CreateOrgOption{
2728
UserName: "user2_org",
2829
FullName: "User2's organization",
@@ -51,13 +52,8 @@ func TestAPIAdminOrgCreate(t *testing.T) {
5152
FullName: org.FullName,
5253
})
5354
})
54-
}
55-
56-
func TestAPIAdminOrgCreateBadVisibility(t *testing.T) {
57-
onGiteaRun(t, func(*testing.T, *url.URL) {
58-
session := loginUser(t, "user1")
59-
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteAdmin)
60-
55+
t.Run("CreateBadVisibility", func(t *testing.T) {
56+
defer tests.PrintCurrentTest(t)()
6157
org := api.CreateOrgOption{
6258
UserName: "user2_org",
6359
FullName: "User2's organization",
@@ -70,22 +66,21 @@ func TestAPIAdminOrgCreateBadVisibility(t *testing.T) {
7066
AddTokenAuth(token)
7167
MakeRequest(t, req, http.StatusUnprocessableEntity)
7268
})
73-
}
74-
75-
func TestAPIAdminOrgCreateNotAdmin(t *testing.T) {
76-
defer tests.PrepareTestEnv(t)()
77-
nonAdminUsername := "user2"
78-
session := loginUser(t, nonAdminUsername)
79-
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeAll)
80-
org := api.CreateOrgOption{
81-
UserName: "user2_org",
82-
FullName: "User2's organization",
83-
Description: "This organization created by admin for user2",
84-
Website: "https://try.gitea.io",
85-
Location: "Shanghai",
86-
Visibility: "public",
87-
}
88-
req := NewRequestWithJSON(t, "POST", "/api/v1/admin/users/user2/orgs", &org).
89-
AddTokenAuth(token)
90-
MakeRequest(t, req, http.StatusForbidden)
69+
t.Run("CreateNotAdmin", func(t *testing.T) {
70+
defer tests.PrintCurrentTest(t)()
71+
nonAdminUsername := "user2"
72+
session := loginUser(t, nonAdminUsername)
73+
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeAll)
74+
org := api.CreateOrgOption{
75+
UserName: "user2_org",
76+
FullName: "User2's organization",
77+
Description: "This organization created by admin for user2",
78+
Website: "https://try.gitea.io",
79+
Location: "Shanghai",
80+
Visibility: "public",
81+
}
82+
req := NewRequestWithJSON(t, "POST", "/api/v1/admin/users/user2/orgs", &org).
83+
AddTokenAuth(token)
84+
MakeRequest(t, req, http.StatusForbidden)
85+
})
9186
}

tests/integration/api_nodeinfo_test.go

+15-19
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,31 @@ package integration
55

66
import (
77
"net/http"
8-
"net/url"
98
"testing"
109

1110
"code.gitea.io/gitea/modules/setting"
1211
api "code.gitea.io/gitea/modules/structs"
12+
"code.gitea.io/gitea/modules/test"
1313
"code.gitea.io/gitea/routers"
14+
"code.gitea.io/gitea/tests"
1415

1516
"github.com/stretchr/testify/assert"
1617
)
1718

1819
func TestNodeinfo(t *testing.T) {
19-
setting.Federation.Enabled = true
20-
testWebRoutes = routers.NormalRoutes()
21-
defer func() {
22-
setting.Federation.Enabled = false
23-
testWebRoutes = routers.NormalRoutes()
24-
}()
20+
defer tests.PrepareTestEnv(t)()
21+
defer test.MockVariableValue(&setting.Federation.Enabled, true)()
22+
defer test.MockVariableValue(&testWebRoutes, routers.NormalRoutes())()
2523

26-
onGiteaRun(t, func(*testing.T, *url.URL) {
27-
req := NewRequest(t, "GET", "/api/v1/nodeinfo")
28-
resp := MakeRequest(t, req, http.StatusOK)
29-
VerifyJSONSchema(t, resp, "nodeinfo_2.1.json")
24+
req := NewRequest(t, "GET", "/api/v1/nodeinfo")
25+
resp := MakeRequest(t, req, http.StatusOK)
26+
VerifyJSONSchema(t, resp, "nodeinfo_2.1.json")
3027

31-
var nodeinfo api.NodeInfo
32-
DecodeJSON(t, resp, &nodeinfo)
33-
assert.True(t, nodeinfo.OpenRegistrations)
34-
assert.Equal(t, "gitea", nodeinfo.Software.Name)
35-
assert.Equal(t, 29, nodeinfo.Usage.Users.Total)
36-
assert.Equal(t, 22, nodeinfo.Usage.LocalPosts)
37-
assert.Equal(t, 3, nodeinfo.Usage.LocalComments)
38-
})
28+
var nodeinfo api.NodeInfo
29+
DecodeJSON(t, resp, &nodeinfo)
30+
assert.True(t, nodeinfo.OpenRegistrations)
31+
assert.Equal(t, "gitea", nodeinfo.Software.Name)
32+
assert.Equal(t, 29, nodeinfo.Usage.Users.Total)
33+
assert.Equal(t, 22, nodeinfo.Usage.LocalPosts)
34+
assert.Equal(t, 3, nodeinfo.Usage.LocalComments)
3935
}

tests/integration/org_count_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package integration
55

66
import (
7-
"net/url"
87
"strings"
98
"testing"
109

@@ -14,15 +13,17 @@ import (
1413
"code.gitea.io/gitea/models/unittest"
1514
user_model "code.gitea.io/gitea/models/user"
1615
api "code.gitea.io/gitea/modules/structs"
16+
"code.gitea.io/gitea/tests"
1717

1818
"github.com/stretchr/testify/assert"
1919
)
2020

2121
func TestOrgCounts(t *testing.T) {
22-
onGiteaRun(t, testOrgCounts)
22+
defer tests.PrepareTestEnv(t)()
23+
testOrgCounts(t)
2324
}
2425

25-
func testOrgCounts(t *testing.T, u *url.URL) {
26+
func testOrgCounts(t *testing.T) {
2627
orgOwner := "user2"
2728
orgName := "testOrg"
2829
orgCollaborator := "user4"

tests/integration/pull_commit_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ import (
88
"testing"
99

1010
pull_service "code.gitea.io/gitea/services/pull"
11+
"code.gitea.io/gitea/tests"
1112

1213
"github.com/stretchr/testify/assert"
1314
"github.com/stretchr/testify/require"
1415
)
1516

1617
func TestListPullCommits(t *testing.T) {
18+
defer tests.PrepareTestEnv(t)()
19+
1720
session := loginUser(t, "user5")
1821
req := NewRequest(t, "GET", "/user2/repo1/pulls/3/commits/list")
1922
resp := session.MakeRequest(t, req, http.StatusOK)
@@ -30,6 +33,7 @@ func TestListPullCommits(t *testing.T) {
3033
assert.Equal(t, "4a357436d925b5c974181ff12a994538ddc5a269", pullCommitList.LastReviewCommitSha)
3134

3235
t.Run("CommitBlobExcerpt", func(t *testing.T) {
36+
defer tests.PrintCurrentTest(t)()
3337
req = NewRequest(t, "GET", "/user2/repo1/blob_excerpt/985f0301dba5e7b34be866819cd15ad3d8f508ee?last_left=0&last_right=0&left=2&right=2&left_hunk_size=2&right_hunk_size=2&path=README.md&style=split&direction=up")
3438
resp = session.MakeRequest(t, req, http.StatusOK)
3539
assert.Contains(t, resp.Body.String(), `<td class="lines-code lines-code-new"><code class="code-inner"># repo1</code>`)

tests/integration/user_avatar_test.go

+45-46
Original file line numberDiff line numberDiff line change
@@ -10,78 +10,77 @@ import (
1010
"io"
1111
"mime/multipart"
1212
"net/http"
13-
"net/url"
1413
"testing"
1514

1615
"code.gitea.io/gitea/models/db"
1716
"code.gitea.io/gitea/models/unittest"
1817
user_model "code.gitea.io/gitea/models/user"
1918
"code.gitea.io/gitea/modules/avatar"
19+
"code.gitea.io/gitea/tests"
2020

2121
"github.com/stretchr/testify/assert"
2222
)
2323

2424
func TestUserAvatar(t *testing.T) {
25-
onGiteaRun(t, func(t *testing.T, u *url.URL) {
26-
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) // owner of the repo3, is an org
25+
defer tests.PrepareTestEnv(t)()
26+
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) // owner of the repo3, is an org
2727

28-
seed := user2.Email
29-
if len(seed) == 0 {
30-
seed = user2.Name
31-
}
28+
seed := user2.Email
29+
if len(seed) == 0 {
30+
seed = user2.Name
31+
}
3232

33-
img, err := avatar.RandomImage([]byte(seed))
34-
if err != nil {
35-
assert.NoError(t, err)
36-
return
37-
}
33+
img, err := avatar.RandomImage([]byte(seed))
34+
if err != nil {
35+
assert.NoError(t, err)
36+
return
37+
}
3838

39-
session := loginUser(t, "user2")
40-
csrf := GetUserCSRFToken(t, session)
39+
session := loginUser(t, "user2")
40+
csrf := GetUserCSRFToken(t, session)
4141

42-
imgData := &bytes.Buffer{}
42+
imgData := &bytes.Buffer{}
4343

44-
body := &bytes.Buffer{}
44+
body := &bytes.Buffer{}
4545

46-
// Setup multi-part
47-
writer := multipart.NewWriter(body)
48-
writer.WriteField("source", "local")
49-
part, err := writer.CreateFormFile("avatar", "avatar-for-testuseravatar.png")
50-
if err != nil {
51-
assert.NoError(t, err)
52-
return
53-
}
46+
// Setup multi-part
47+
writer := multipart.NewWriter(body)
48+
writer.WriteField("source", "local")
49+
part, err := writer.CreateFormFile("avatar", "avatar-for-testuseravatar.png")
50+
if err != nil {
51+
assert.NoError(t, err)
52+
return
53+
}
5454

55-
if err := png.Encode(imgData, img); err != nil {
56-
assert.NoError(t, err)
57-
return
58-
}
55+
if err := png.Encode(imgData, img); err != nil {
56+
assert.NoError(t, err)
57+
return
58+
}
5959

60-
if _, err := io.Copy(part, imgData); err != nil {
61-
assert.NoError(t, err)
62-
return
63-
}
60+
if _, err := io.Copy(part, imgData); err != nil {
61+
assert.NoError(t, err)
62+
return
63+
}
6464

65-
if err := writer.Close(); err != nil {
66-
assert.NoError(t, err)
67-
return
68-
}
65+
if err := writer.Close(); err != nil {
66+
assert.NoError(t, err)
67+
return
68+
}
6969

70-
req := NewRequestWithBody(t, "POST", "/user/settings/avatar", body)
71-
req.Header.Add("X-Csrf-Token", csrf)
72-
req.Header.Add("Content-Type", writer.FormDataContentType())
70+
req := NewRequestWithBody(t, "POST", "/user/settings/avatar", body)
71+
req.Header.Add("X-Csrf-Token", csrf)
72+
req.Header.Add("Content-Type", writer.FormDataContentType())
7373

74-
session.MakeRequest(t, req, http.StatusSeeOther)
74+
session.MakeRequest(t, req, http.StatusSeeOther)
7575

76-
user2 = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) // owner of the repo3, is an org
76+
user2 = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) // owner of the repo3, is an org
7777

78-
req = NewRequest(t, "GET", user2.AvatarLinkWithSize(db.DefaultContext, 0))
79-
_ = session.MakeRequest(t, req, http.StatusOK)
78+
req = NewRequest(t, "GET", user2.AvatarLinkWithSize(db.DefaultContext, 0))
79+
_ = session.MakeRequest(t, req, http.StatusOK)
8080

81-
testGetAvatarRedirect(t, user2)
81+
testGetAvatarRedirect(t, user2)
8282

83-
// Can't test if the response matches because the image is re-generated on upload but checking that this at least doesn't give a 404 should be enough.
84-
})
83+
// Can't test if the response matches because the image is re-generated on upload but checking that this at least doesn't give a 404 should be enough.
8584
}
8685

8786
func testGetAvatarRedirect(t *testing.T, user *user_model.User) {

0 commit comments

Comments
 (0)