loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request 'chore: improve test quality' (#5904) from gusted/forgejo-improve-test into forgejo

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/5904
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Reviewed-by: Otto <otto@codeberg.org>

+612 -776
+2 -1
tests/integration/actions_commit_status_test.go
··· 14 14 "code.gitea.io/gitea/models/unittest" 15 15 user_model "code.gitea.io/gitea/models/user" 16 16 "code.gitea.io/gitea/modules/setting" 17 + "code.gitea.io/gitea/modules/test" 17 18 "code.gitea.io/gitea/services/actions" 18 19 "code.gitea.io/gitea/services/automerge" 19 20 ··· 23 24 24 25 func TestActionsAutomerge(t *testing.T) { 25 26 onGiteaRun(t, func(t *testing.T, u *url.URL) { 26 - assert.True(t, setting.Actions.Enabled, "Actions should be enabled") 27 + defer test.MockVariableValue(&setting.Actions.Enabled, true)() 27 28 28 29 ctx := db.DefaultContext 29 30
+40 -43
tests/integration/api_admin_org_test.go
··· 5 5 6 6 import ( 7 7 "net/http" 8 - "net/url" 9 8 "strings" 10 9 "testing" 11 10 ··· 19 18 ) 20 19 21 20 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) 25 24 26 - org := api.CreateOrgOption{ 27 - UserName: "user2_org", 28 - FullName: "User2's organization", 29 - Description: "This organization created by admin for user2", 30 - Website: "https://try.gitea.io", 31 - Location: "Shanghai", 32 - Visibility: "private", 33 - } 34 - req := NewRequestWithJSON(t, "POST", "/api/v1/admin/users/user2/orgs", &org). 35 - AddTokenAuth(token) 36 - resp := MakeRequest(t, req, http.StatusCreated) 25 + org := api.CreateOrgOption{ 26 + UserName: "user2_org", 27 + FullName: "User2's organization", 28 + Description: "This organization created by admin for user2", 29 + Website: "https://try.gitea.io", 30 + Location: "Shanghai", 31 + Visibility: "private", 32 + } 33 + req := NewRequestWithJSON(t, "POST", "/api/v1/admin/users/user2/orgs", &org). 34 + AddTokenAuth(token) 35 + resp := MakeRequest(t, req, http.StatusCreated) 37 36 38 - var apiOrg api.Organization 39 - DecodeJSON(t, resp, &apiOrg) 37 + var apiOrg api.Organization 38 + DecodeJSON(t, resp, &apiOrg) 40 39 41 - assert.Equal(t, org.UserName, apiOrg.Name) 42 - assert.Equal(t, org.FullName, apiOrg.FullName) 43 - assert.Equal(t, org.Description, apiOrg.Description) 44 - assert.Equal(t, org.Website, apiOrg.Website) 45 - assert.Equal(t, org.Location, apiOrg.Location) 46 - assert.Equal(t, org.Visibility, apiOrg.Visibility) 40 + assert.Equal(t, org.UserName, apiOrg.Name) 41 + assert.Equal(t, org.FullName, apiOrg.FullName) 42 + assert.Equal(t, org.Description, apiOrg.Description) 43 + assert.Equal(t, org.Website, apiOrg.Website) 44 + assert.Equal(t, org.Location, apiOrg.Location) 45 + assert.Equal(t, org.Visibility, apiOrg.Visibility) 47 46 48 - unittest.AssertExistsAndLoadBean(t, &user_model.User{ 49 - Name: org.UserName, 50 - LowerName: strings.ToLower(org.UserName), 51 - FullName: org.FullName, 52 - }) 47 + unittest.AssertExistsAndLoadBean(t, &user_model.User{ 48 + Name: org.UserName, 49 + LowerName: strings.ToLower(org.UserName), 50 + FullName: org.FullName, 53 51 }) 54 52 } 55 53 56 54 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) 55 + defer tests.PrepareTestEnv(t)() 56 + session := loginUser(t, "user1") 57 + token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteAdmin) 60 58 61 - org := api.CreateOrgOption{ 62 - UserName: "user2_org", 63 - FullName: "User2's organization", 64 - Description: "This organization created by admin for user2", 65 - Website: "https://try.gitea.io", 66 - Location: "Shanghai", 67 - Visibility: "notvalid", 68 - } 69 - req := NewRequestWithJSON(t, "POST", "/api/v1/admin/users/user2/orgs", &org). 70 - AddTokenAuth(token) 71 - MakeRequest(t, req, http.StatusUnprocessableEntity) 72 - }) 59 + org := api.CreateOrgOption{ 60 + UserName: "user2_org", 61 + FullName: "User2's organization", 62 + Description: "This organization created by admin for user2", 63 + Website: "https://try.gitea.io", 64 + Location: "Shanghai", 65 + Visibility: "notvalid", 66 + } 67 + req := NewRequestWithJSON(t, "POST", "/api/v1/admin/users/user2/orgs", &org). 68 + AddTokenAuth(token) 69 + MakeRequest(t, req, http.StatusUnprocessableEntity) 73 70 } 74 71 75 72 func TestAPIAdminOrgCreateNotAdmin(t *testing.T) {
+6 -25
tests/integration/api_branch_test.go
··· 9 9 "testing" 10 10 11 11 auth_model "code.gitea.io/gitea/models/auth" 12 - "code.gitea.io/gitea/models/db" 13 12 git_model "code.gitea.io/gitea/models/git" 13 + "code.gitea.io/gitea/models/unittest" 14 14 "code.gitea.io/gitea/modules/git" 15 15 api "code.gitea.io/gitea/modules/structs" 16 16 "code.gitea.io/gitea/tests" 17 17 18 18 "github.com/stretchr/testify/assert" 19 - "github.com/stretchr/testify/require" 20 19 ) 21 20 22 21 func testAPIGetBranch(t *testing.T, branchName string, exists bool) { ··· 234 233 } 235 234 236 235 func TestAPICreateBranchWithSyncBranches(t *testing.T) { 237 - defer tests.PrepareTestEnv(t)() 238 - 239 - branches, err := db.Find[git_model.Branch](db.DefaultContext, git_model.FindBranchOptions{ 240 - RepoID: 1, 241 - }) 242 - require.NoError(t, err) 243 - assert.Len(t, branches, 4) 236 + onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) { 237 + unittest.AssertCount(t, &git_model.Branch{RepoID: 1}, 4) 244 238 245 - // make a broke repository with no branch on database 246 - _, err = db.DeleteByBean(db.DefaultContext, git_model.Branch{RepoID: 1}) 247 - require.NoError(t, err) 239 + // make a broke repository with no branch on database 240 + unittest.AssertSuccessfulDelete(t, &git_model.Branch{RepoID: 1}) 248 241 249 - onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) { 250 242 ctx := NewAPITestContext(t, "user2", "repo1", auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteUser) 251 243 giteaURL.Path = ctx.GitPath() 252 244 253 245 testAPICreateBranch(t, ctx.Session, "user2", "repo1", "", "new_branch", http.StatusCreated) 254 - }) 255 246 256 - branches, err = db.Find[git_model.Branch](db.DefaultContext, git_model.FindBranchOptions{ 257 - RepoID: 1, 258 - }) 259 - require.NoError(t, err) 260 - assert.Len(t, branches, 5) 261 - 262 - branches, err = db.Find[git_model.Branch](db.DefaultContext, git_model.FindBranchOptions{ 263 - RepoID: 1, 264 - Keyword: "new_branch", 247 + unittest.AssertExistsIf(t, true, &git_model.Branch{RepoID: 1, Name: "new_branch"}) 265 248 }) 266 - require.NoError(t, err) 267 - assert.Len(t, branches, 1) 268 249 }
+14 -16
tests/integration/api_fork_test.go
··· 7 7 import ( 8 8 "fmt" 9 9 "net/http" 10 - "net/url" 11 10 "testing" 12 11 13 12 auth_model "code.gitea.io/gitea/models/auth" ··· 86 85 } 87 86 88 87 func TestAPIDisabledForkRepo(t *testing.T) { 89 - onGiteaRun(t, func(t *testing.T, u *url.URL) { 90 - defer test.MockVariableValue(&setting.Repository.DisableForks, true)() 91 - defer test.MockVariableValue(&testWebRoutes, routers.NormalRoutes())() 88 + defer test.MockVariableValue(&setting.Repository.DisableForks, true)() 89 + defer test.MockVariableValue(&testWebRoutes, routers.NormalRoutes())() 90 + defer tests.PrepareTestEnv(t)() 92 91 93 - t.Run("fork listing", func(t *testing.T) { 94 - defer tests.PrintCurrentTest(t)() 92 + t.Run("fork listing", func(t *testing.T) { 93 + defer tests.PrintCurrentTest(t)() 95 94 96 - req := NewRequest(t, "GET", "/api/v1/repos/user2/repo1/forks") 97 - MakeRequest(t, req, http.StatusNotFound) 98 - }) 95 + req := NewRequest(t, "GET", "/api/v1/repos/user2/repo1/forks") 96 + MakeRequest(t, req, http.StatusNotFound) 97 + }) 99 98 100 - t.Run("forking", func(t *testing.T) { 101 - defer tests.PrintCurrentTest(t)() 99 + t.Run("forking", func(t *testing.T) { 100 + defer tests.PrintCurrentTest(t)() 102 101 103 - session := loginUser(t, "user5") 104 - token := getTokenForLoggedInUser(t, session) 102 + session := loginUser(t, "user5") 103 + token := getTokenForLoggedInUser(t, session) 105 104 106 - req := NewRequestWithJSON(t, "POST", "/api/v1/repos/user2/repo1/forks", &api.CreateForkOption{}).AddTokenAuth(token) 107 - session.MakeRequest(t, req, http.StatusNotFound) 108 - }) 105 + req := NewRequestWithJSON(t, "POST", "/api/v1/repos/user2/repo1/forks", &api.CreateForkOption{}).AddTokenAuth(token) 106 + session.MakeRequest(t, req, http.StatusNotFound) 109 107 }) 110 108 }
+15 -19
tests/integration/api_nodeinfo_test.go
··· 5 5 6 6 import ( 7 7 "net/http" 8 - "net/url" 9 8 "testing" 10 9 11 10 "code.gitea.io/gitea/modules/setting" 12 11 api "code.gitea.io/gitea/modules/structs" 12 + "code.gitea.io/gitea/modules/test" 13 13 "code.gitea.io/gitea/routers" 14 + "code.gitea.io/gitea/tests" 14 15 15 16 "github.com/stretchr/testify/assert" 16 17 ) 17 18 18 19 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 test.MockVariableValue(&setting.Federation.Enabled, true)() 21 + defer test.MockVariableValue(&testWebRoutes, routers.NormalRoutes())() 22 + defer tests.PrepareTestEnv(t)() 25 23 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") 30 27 31 - var nodeinfo api.NodeInfo 32 - DecodeJSON(t, resp, &nodeinfo) 33 - assert.True(t, nodeinfo.OpenRegistrations) 34 - assert.Equal(t, "forgejo", nodeinfo.Software.Name) 35 - assert.Equal(t, 29, nodeinfo.Usage.Users.Total) 36 - assert.Equal(t, 22, nodeinfo.Usage.LocalPosts) 37 - assert.Equal(t, 4, nodeinfo.Usage.LocalComments) 38 - }) 28 + var nodeinfo api.NodeInfo 29 + DecodeJSON(t, resp, &nodeinfo) 30 + assert.True(t, nodeinfo.OpenRegistrations) 31 + assert.Equal(t, "forgejo", nodeinfo.Software.Name) 32 + assert.Equal(t, 29, nodeinfo.Usage.Users.Total) 33 + assert.Equal(t, 22, nodeinfo.Usage.LocalPosts) 34 + assert.Equal(t, 4, nodeinfo.Usage.LocalComments) 39 35 }
+133 -141
tests/integration/api_org_test.go
··· 6 6 import ( 7 7 "fmt" 8 8 "net/http" 9 - "net/url" 10 9 "strings" 11 10 "testing" 12 11 ··· 19 18 user_model "code.gitea.io/gitea/models/user" 20 19 "code.gitea.io/gitea/modules/setting" 21 20 api "code.gitea.io/gitea/modules/structs" 21 + "code.gitea.io/gitea/modules/test" 22 22 "code.gitea.io/gitea/tests" 23 23 24 24 "github.com/stretchr/testify/assert" 25 25 ) 26 26 27 27 func TestAPIOrgCreate(t *testing.T) { 28 - onGiteaRun(t, func(*testing.T, *url.URL) { 29 - token := getUserToken(t, "user1", auth_model.AccessTokenScopeWriteOrganization) 28 + defer tests.PrepareTestEnv(t)() 29 + token := getUserToken(t, "user1", auth_model.AccessTokenScopeWriteOrganization) 30 30 31 - org := api.CreateOrgOption{ 32 - UserName: "user1_org", 33 - FullName: "User1's organization", 34 - Description: "This organization created by user1", 35 - Website: "https://try.gitea.io", 36 - Location: "Shanghai", 37 - Visibility: "limited", 38 - } 39 - req := NewRequestWithJSON(t, "POST", "/api/v1/orgs", &org). 40 - AddTokenAuth(token) 41 - resp := MakeRequest(t, req, http.StatusCreated) 31 + org := api.CreateOrgOption{ 32 + UserName: "user1_org", 33 + FullName: "User1's organization", 34 + Description: "This organization created by user1", 35 + Website: "https://try.gitea.io", 36 + Location: "Shanghai", 37 + Visibility: "limited", 38 + } 39 + req := NewRequestWithJSON(t, "POST", "/api/v1/orgs", &org). 40 + AddTokenAuth(token) 41 + resp := MakeRequest(t, req, http.StatusCreated) 42 42 43 - var apiOrg api.Organization 44 - DecodeJSON(t, resp, &apiOrg) 43 + var apiOrg api.Organization 44 + DecodeJSON(t, resp, &apiOrg) 45 45 46 - assert.Equal(t, org.UserName, apiOrg.Name) 47 - assert.Equal(t, org.FullName, apiOrg.FullName) 48 - assert.Equal(t, org.Description, apiOrg.Description) 49 - assert.Equal(t, org.Website, apiOrg.Website) 50 - assert.Equal(t, org.Location, apiOrg.Location) 51 - assert.Equal(t, org.Visibility, apiOrg.Visibility) 46 + assert.Equal(t, org.UserName, apiOrg.Name) 47 + assert.Equal(t, org.FullName, apiOrg.FullName) 48 + assert.Equal(t, org.Description, apiOrg.Description) 49 + assert.Equal(t, org.Website, apiOrg.Website) 50 + assert.Equal(t, org.Location, apiOrg.Location) 51 + assert.Equal(t, org.Visibility, apiOrg.Visibility) 52 52 53 - unittest.AssertExistsAndLoadBean(t, &user_model.User{ 54 - Name: org.UserName, 55 - LowerName: strings.ToLower(org.UserName), 56 - FullName: org.FullName, 57 - }) 53 + unittest.AssertExistsAndLoadBean(t, &user_model.User{ 54 + Name: org.UserName, 55 + LowerName: strings.ToLower(org.UserName), 56 + FullName: org.FullName, 57 + }) 58 58 59 - // Check owner team permission 60 - ownerTeam, _ := org_model.GetOwnerTeam(db.DefaultContext, apiOrg.ID) 59 + // Check owner team permission 60 + ownerTeam, _ := org_model.GetOwnerTeam(db.DefaultContext, apiOrg.ID) 61 61 62 - for _, ut := range unit_model.AllRepoUnitTypes { 63 - up := perm.AccessModeOwner 64 - if ut == unit_model.TypeExternalTracker || ut == unit_model.TypeExternalWiki { 65 - up = perm.AccessModeRead 66 - } 67 - unittest.AssertExistsAndLoadBean(t, &org_model.TeamUnit{ 68 - OrgID: apiOrg.ID, 69 - TeamID: ownerTeam.ID, 70 - Type: ut, 71 - AccessMode: up, 72 - }) 62 + for _, ut := range unit_model.AllRepoUnitTypes { 63 + up := perm.AccessModeOwner 64 + if ut == unit_model.TypeExternalTracker || ut == unit_model.TypeExternalWiki { 65 + up = perm.AccessModeRead 73 66 } 67 + unittest.AssertExistsAndLoadBean(t, &org_model.TeamUnit{ 68 + OrgID: apiOrg.ID, 69 + TeamID: ownerTeam.ID, 70 + Type: ut, 71 + AccessMode: up, 72 + }) 73 + } 74 74 75 - req = NewRequestf(t, "GET", "/api/v1/orgs/%s", org.UserName). 76 - AddTokenAuth(token) 77 - resp = MakeRequest(t, req, http.StatusOK) 78 - DecodeJSON(t, resp, &apiOrg) 79 - assert.EqualValues(t, org.UserName, apiOrg.Name) 75 + req = NewRequestf(t, "GET", "/api/v1/orgs/%s", org.UserName). 76 + AddTokenAuth(token) 77 + resp = MakeRequest(t, req, http.StatusOK) 78 + DecodeJSON(t, resp, &apiOrg) 79 + assert.EqualValues(t, org.UserName, apiOrg.Name) 80 80 81 - req = NewRequestf(t, "GET", "/api/v1/orgs/%s/repos", org.UserName). 82 - AddTokenAuth(token) 83 - resp = MakeRequest(t, req, http.StatusOK) 81 + req = NewRequestf(t, "GET", "/api/v1/orgs/%s/repos", org.UserName). 82 + AddTokenAuth(token) 83 + resp = MakeRequest(t, req, http.StatusOK) 84 84 85 - var repos []*api.Repository 86 - DecodeJSON(t, resp, &repos) 87 - for _, repo := range repos { 88 - assert.False(t, repo.Private) 89 - } 85 + var repos []*api.Repository 86 + DecodeJSON(t, resp, &repos) 87 + for _, repo := range repos { 88 + assert.False(t, repo.Private) 89 + } 90 90 91 - req = NewRequestf(t, "GET", "/api/v1/orgs/%s/members", org.UserName). 92 - AddTokenAuth(token) 93 - resp = MakeRequest(t, req, http.StatusOK) 91 + req = NewRequestf(t, "GET", "/api/v1/orgs/%s/members", org.UserName). 92 + AddTokenAuth(token) 93 + resp = MakeRequest(t, req, http.StatusOK) 94 94 95 - // user1 on this org is public 96 - var users []*api.User 97 - DecodeJSON(t, resp, &users) 98 - assert.Len(t, users, 1) 99 - assert.EqualValues(t, "user1", users[0].UserName) 100 - }) 95 + // user1 on this org is public 96 + var users []*api.User 97 + DecodeJSON(t, resp, &users) 98 + assert.Len(t, users, 1) 99 + assert.EqualValues(t, "user1", users[0].UserName) 101 100 } 102 101 103 102 func TestAPIOrgEdit(t *testing.T) { 104 - onGiteaRun(t, func(*testing.T, *url.URL) { 105 - session := loginUser(t, "user1") 103 + defer tests.PrepareTestEnv(t)() 104 + session := loginUser(t, "user1") 106 105 107 - token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteOrganization) 108 - org := api.EditOrgOption{ 109 - FullName: "Org3 organization new full name", 110 - Description: "A new description", 111 - Website: "https://try.gitea.io/new", 112 - Location: "Beijing", 113 - Visibility: "private", 114 - } 115 - req := NewRequestWithJSON(t, "PATCH", "/api/v1/orgs/org3", &org). 116 - AddTokenAuth(token) 117 - resp := MakeRequest(t, req, http.StatusOK) 106 + token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteOrganization) 107 + org := api.EditOrgOption{ 108 + FullName: "Org3 organization new full name", 109 + Description: "A new description", 110 + Website: "https://try.gitea.io/new", 111 + Location: "Beijing", 112 + Visibility: "private", 113 + } 114 + req := NewRequestWithJSON(t, "PATCH", "/api/v1/orgs/org3", &org). 115 + AddTokenAuth(token) 116 + resp := MakeRequest(t, req, http.StatusOK) 118 117 119 - var apiOrg api.Organization 120 - DecodeJSON(t, resp, &apiOrg) 118 + var apiOrg api.Organization 119 + DecodeJSON(t, resp, &apiOrg) 121 120 122 - assert.Equal(t, "org3", apiOrg.Name) 123 - assert.Equal(t, org.FullName, apiOrg.FullName) 124 - assert.Equal(t, org.Description, apiOrg.Description) 125 - assert.Equal(t, org.Website, apiOrg.Website) 126 - assert.Equal(t, org.Location, apiOrg.Location) 127 - assert.Equal(t, org.Visibility, apiOrg.Visibility) 128 - }) 121 + assert.Equal(t, "org3", apiOrg.Name) 122 + assert.Equal(t, org.FullName, apiOrg.FullName) 123 + assert.Equal(t, org.Description, apiOrg.Description) 124 + assert.Equal(t, org.Website, apiOrg.Website) 125 + assert.Equal(t, org.Location, apiOrg.Location) 126 + assert.Equal(t, org.Visibility, apiOrg.Visibility) 129 127 } 130 128 131 129 func TestAPIOrgEditBadVisibility(t *testing.T) { 132 - onGiteaRun(t, func(*testing.T, *url.URL) { 133 - session := loginUser(t, "user1") 130 + defer tests.PrepareTestEnv(t)() 131 + session := loginUser(t, "user1") 134 132 135 - token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteOrganization) 136 - org := api.EditOrgOption{ 137 - FullName: "Org3 organization new full name", 138 - Description: "A new description", 139 - Website: "https://try.gitea.io/new", 140 - Location: "Beijing", 141 - Visibility: "badvisibility", 142 - } 143 - req := NewRequestWithJSON(t, "PATCH", "/api/v1/orgs/org3", &org). 144 - AddTokenAuth(token) 145 - MakeRequest(t, req, http.StatusUnprocessableEntity) 146 - }) 133 + token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteOrganization) 134 + org := api.EditOrgOption{ 135 + FullName: "Org3 organization new full name", 136 + Description: "A new description", 137 + Website: "https://try.gitea.io/new", 138 + Location: "Beijing", 139 + Visibility: "badvisibility", 140 + } 141 + req := NewRequestWithJSON(t, "PATCH", "/api/v1/orgs/org3", &org). 142 + AddTokenAuth(token) 143 + MakeRequest(t, req, http.StatusUnprocessableEntity) 147 144 } 148 145 149 146 func TestAPIOrgDeny(t *testing.T) { 150 - onGiteaRun(t, func(*testing.T, *url.URL) { 151 - setting.Service.RequireSignInView = true 152 - defer func() { 153 - setting.Service.RequireSignInView = false 154 - }() 147 + defer tests.PrepareTestEnv(t)() 148 + defer test.MockVariableValue(&setting.Service.RequireSignInView, true)() 155 149 156 - orgName := "user1_org" 157 - req := NewRequestf(t, "GET", "/api/v1/orgs/%s", orgName) 158 - MakeRequest(t, req, http.StatusNotFound) 150 + orgName := "user1_org" 151 + req := NewRequestf(t, "GET", "/api/v1/orgs/%s", orgName) 152 + MakeRequest(t, req, http.StatusNotFound) 159 153 160 - req = NewRequestf(t, "GET", "/api/v1/orgs/%s/repos", orgName) 161 - MakeRequest(t, req, http.StatusNotFound) 154 + req = NewRequestf(t, "GET", "/api/v1/orgs/%s/repos", orgName) 155 + MakeRequest(t, req, http.StatusNotFound) 162 156 163 - req = NewRequestf(t, "GET", "/api/v1/orgs/%s/members", orgName) 164 - MakeRequest(t, req, http.StatusNotFound) 165 - }) 157 + req = NewRequestf(t, "GET", "/api/v1/orgs/%s/members", orgName) 158 + MakeRequest(t, req, http.StatusNotFound) 166 159 } 167 160 168 161 func TestAPIGetAll(t *testing.T) { ··· 192 185 } 193 186 194 187 func TestAPIOrgSearchEmptyTeam(t *testing.T) { 195 - onGiteaRun(t, func(*testing.T, *url.URL) { 196 - token := getUserToken(t, "user1", auth_model.AccessTokenScopeWriteOrganization) 197 - orgName := "org_with_empty_team" 188 + defer tests.PrepareTestEnv(t)() 189 + token := getUserToken(t, "user1", auth_model.AccessTokenScopeWriteOrganization) 190 + orgName := "org_with_empty_team" 198 191 199 - // create org 200 - req := NewRequestWithJSON(t, "POST", "/api/v1/orgs", &api.CreateOrgOption{ 201 - UserName: orgName, 202 - }).AddTokenAuth(token) 203 - MakeRequest(t, req, http.StatusCreated) 192 + // create org 193 + req := NewRequestWithJSON(t, "POST", "/api/v1/orgs", &api.CreateOrgOption{ 194 + UserName: orgName, 195 + }).AddTokenAuth(token) 196 + MakeRequest(t, req, http.StatusCreated) 204 197 205 - // create team with no member 206 - req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/orgs/%s/teams", orgName), &api.CreateTeamOption{ 207 - Name: "Empty", 208 - IncludesAllRepositories: true, 209 - Permission: "read", 210 - Units: []string{"repo.code", "repo.issues", "repo.ext_issues", "repo.wiki", "repo.pulls"}, 211 - }).AddTokenAuth(token) 212 - MakeRequest(t, req, http.StatusCreated) 198 + // create team with no member 199 + req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/orgs/%s/teams", orgName), &api.CreateTeamOption{ 200 + Name: "Empty", 201 + IncludesAllRepositories: true, 202 + Permission: "read", 203 + Units: []string{"repo.code", "repo.issues", "repo.ext_issues", "repo.wiki", "repo.pulls"}, 204 + }).AddTokenAuth(token) 205 + MakeRequest(t, req, http.StatusCreated) 213 206 214 - // case-insensitive search for teams that have no members 215 - req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/orgs/%s/teams/search?q=%s", orgName, "empty")). 216 - AddTokenAuth(token) 217 - resp := MakeRequest(t, req, http.StatusOK) 218 - data := struct { 219 - Ok bool 220 - Data []*api.Team 221 - }{} 222 - DecodeJSON(t, resp, &data) 223 - assert.True(t, data.Ok) 224 - if assert.Len(t, data.Data, 1) { 225 - assert.EqualValues(t, "Empty", data.Data[0].Name) 226 - } 227 - }) 207 + // case-insensitive search for teams that have no members 208 + req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/orgs/%s/teams/search?q=%s", orgName, "empty")). 209 + AddTokenAuth(token) 210 + resp := MakeRequest(t, req, http.StatusOK) 211 + data := struct { 212 + Ok bool 213 + Data []*api.Team 214 + }{} 215 + DecodeJSON(t, resp, &data) 216 + assert.True(t, data.Ok) 217 + if assert.Len(t, data.Data, 1) { 218 + assert.EqualValues(t, "Empty", data.Data[0].Name) 219 + } 228 220 }
+36 -37
tests/integration/last_updated_time_test.go
··· 2 2 3 3 import ( 4 4 "net/http" 5 - "net/url" 6 5 "path" 7 6 "strings" 8 7 "testing" 8 + 9 + "code.gitea.io/gitea/tests" 9 10 10 11 "github.com/PuerkitoBio/goquery" 11 12 "github.com/stretchr/testify/assert" 12 13 ) 13 14 14 15 func TestRepoLastUpdatedTime(t *testing.T) { 15 - onGiteaRun(t, func(t *testing.T, u *url.URL) { 16 - user := "user2" 17 - session := loginUser(t, user) 16 + defer tests.PrepareTestEnv(t)() 17 + user := "user2" 18 + session := loginUser(t, user) 18 19 19 - req := NewRequest(t, "GET", "/explore/repos?q=repo1") 20 - resp := session.MakeRequest(t, req, http.StatusOK) 21 - doc := NewHTMLParser(t, resp.Body) 22 - node := doc.doc.Find(".flex-item-body").First() 23 - { 24 - buf := "" 25 - findTextNonNested(t, node, &buf) 26 - assert.Equal(t, "Updated", strings.TrimSpace(buf)) 27 - } 20 + req := NewRequest(t, "GET", "/explore/repos?q=repo1") 21 + resp := session.MakeRequest(t, req, http.StatusOK) 22 + doc := NewHTMLParser(t, resp.Body) 23 + node := doc.doc.Find(".flex-item-body").First() 24 + { 25 + buf := "" 26 + findTextNonNested(t, node, &buf) 27 + assert.Equal(t, "Updated", strings.TrimSpace(buf)) 28 + } 28 29 29 - // Relative time should be present as a descendent 30 - { 31 - relativeTime := node.Find("relative-time").Text() 32 - assert.True(t, strings.HasPrefix(relativeTime, "19")) // ~1970, might underflow with timezone 33 - } 34 - }) 30 + // Relative time should be present as a descendent 31 + { 32 + relativeTime := node.Find("relative-time").Text() 33 + assert.True(t, strings.HasPrefix(relativeTime, "19")) // ~1970, might underflow with timezone 34 + } 35 35 } 36 36 37 37 func TestBranchLastUpdatedTime(t *testing.T) { 38 - onGiteaRun(t, func(t *testing.T, u *url.URL) { 39 - user := "user2" 40 - repo := "repo1" 41 - session := loginUser(t, user) 38 + defer tests.PrepareTestEnv(t)() 39 + user := "user2" 40 + repo := "repo1" 41 + session := loginUser(t, user) 42 42 43 - req := NewRequest(t, "GET", path.Join(user, repo, "branches")) 44 - resp := session.MakeRequest(t, req, http.StatusOK) 45 - doc := NewHTMLParser(t, resp.Body) 46 - node := doc.doc.Find("p:has(span.commit-message)") 43 + req := NewRequest(t, "GET", path.Join(user, repo, "branches")) 44 + resp := session.MakeRequest(t, req, http.StatusOK) 45 + doc := NewHTMLParser(t, resp.Body) 46 + node := doc.doc.Find("p:has(span.commit-message)") 47 47 48 - { 49 - buf := "" 50 - findTextNonNested(t, node, &buf) 51 - assert.True(t, strings.Contains(buf, "Updated")) 52 - } 48 + { 49 + buf := "" 50 + findTextNonNested(t, node, &buf) 51 + assert.True(t, strings.Contains(buf, "Updated")) 52 + } 53 53 54 - { 55 - relativeTime := node.Find("relative-time").Text() 56 - assert.True(t, strings.HasPrefix(relativeTime, "2017")) 57 - } 58 - }) 54 + { 55 + relativeTime := node.Find("relative-time").Text() 56 + assert.True(t, strings.HasPrefix(relativeTime, "2017")) 57 + } 59 58 } 60 59 61 60 // Find all text that are direct descendents
+15 -16
tests/integration/pull_commit_test.go
··· 5 5 6 6 import ( 7 7 "net/http" 8 - "net/url" 9 8 "testing" 10 9 11 10 pull_service "code.gitea.io/gitea/services/pull" 11 + "code.gitea.io/gitea/tests" 12 12 13 13 "github.com/stretchr/testify/assert" 14 14 ) 15 15 16 16 func TestListPullCommits(t *testing.T) { 17 - onGiteaRun(t, func(t *testing.T, u *url.URL) { 18 - session := loginUser(t, "user5") 19 - req := NewRequest(t, "GET", "/user2/repo1/pulls/3/commits/list") 20 - resp := session.MakeRequest(t, req, http.StatusOK) 17 + defer tests.PrepareTestEnv(t)() 18 + session := loginUser(t, "user5") 19 + req := NewRequest(t, "GET", "/user2/repo1/pulls/3/commits/list") 20 + resp := session.MakeRequest(t, req, http.StatusOK) 21 21 22 - var pullCommitList struct { 23 - Commits []pull_service.CommitInfo `json:"commits"` 24 - LastReviewCommitSha string `json:"last_review_commit_sha"` 25 - } 26 - DecodeJSON(t, resp, &pullCommitList) 22 + var pullCommitList struct { 23 + Commits []pull_service.CommitInfo `json:"commits"` 24 + LastReviewCommitSha string `json:"last_review_commit_sha"` 25 + } 26 + DecodeJSON(t, resp, &pullCommitList) 27 27 28 - if assert.Len(t, pullCommitList.Commits, 2) { 29 - assert.Equal(t, "5f22f7d0d95d614d25a5b68592adb345a4b5c7fd", pullCommitList.Commits[0].ID) 30 - assert.Equal(t, "4a357436d925b5c974181ff12a994538ddc5a269", pullCommitList.Commits[1].ID) 31 - } 32 - assert.Equal(t, "4a357436d925b5c974181ff12a994538ddc5a269", pullCommitList.LastReviewCommitSha) 33 - }) 28 + if assert.Len(t, pullCommitList.Commits, 2) { 29 + assert.Equal(t, "5f22f7d0d95d614d25a5b68592adb345a4b5c7fd", pullCommitList.Commits[0].ID) 30 + assert.Equal(t, "4a357436d925b5c974181ff12a994538ddc5a269", pullCommitList.Commits[1].ID) 31 + } 32 + assert.Equal(t, "4a357436d925b5c974181ff12a994538ddc5a269", pullCommitList.LastReviewCommitSha) 34 33 }
+9 -24
tests/integration/pull_review_test.go
··· 18 18 repo_model "code.gitea.io/gitea/models/repo" 19 19 "code.gitea.io/gitea/models/unittest" 20 20 user_model "code.gitea.io/gitea/models/user" 21 - "code.gitea.io/gitea/modules/git" 22 21 "code.gitea.io/gitea/modules/gitrepo" 23 22 "code.gitea.io/gitea/modules/test" 24 23 issue_service "code.gitea.io/gitea/services/issue" ··· 283 282 onGiteaRun(t, func(t *testing.T, u *url.URL) { 284 283 user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) 285 284 286 - // Create the repo. 287 - repo, err := repo_service.CreateRepositoryDirectly(db.DefaultContext, user2, user2, repo_service.CreateRepoOptions{ 288 - Name: "test_codeowner", 289 - Readme: "Default", 290 - AutoInit: true, 291 - ObjectFormatName: git.Sha1ObjectFormat.Name(), 292 - DefaultBranch: "master", 293 - }) 294 - require.NoError(t, err) 295 - 296 - // add CODEOWNERS to default branch 297 - _, err = files_service.ChangeRepoFiles(db.DefaultContext, repo, user2, &files_service.ChangeRepoFilesOptions{ 298 - OldBranch: repo.DefaultBranch, 299 - Files: []*files_service.ChangeRepoFile{ 300 - { 301 - Operation: "create", 302 - TreePath: "CODEOWNERS", 303 - ContentReader: strings.NewReader("README.md @user5\n"), 304 - }, 285 + repo, _, f := tests.CreateDeclarativeRepo(t, user2, "test_codeowner", nil, nil, []*files_service.ChangeRepoFile{ 286 + { 287 + Operation: "create", 288 + TreePath: "CODEOWNERS", 289 + ContentReader: strings.NewReader("README.md @user5\n"), 305 290 }, 306 291 }) 307 - require.NoError(t, err) 292 + defer f() 308 293 309 294 t.Run("First Pull Request", func(t *testing.T) { 310 295 // create a new branch to prepare for pull request 311 - _, err = files_service.ChangeRepoFiles(db.DefaultContext, repo, user2, &files_service.ChangeRepoFilesOptions{ 296 + _, err := files_service.ChangeRepoFiles(db.DefaultContext, repo, user2, &files_service.ChangeRepoFilesOptions{ 312 297 NewBranch: "codeowner-basebranch", 313 298 Files: []*files_service.ChangeRepoFile{ 314 299 { ··· 328 313 unittest.AssertExistsIf(t, true, &issues_model.Review{IssueID: pr.IssueID, Type: issues_model.ReviewTypeRequest, ReviewerID: 5}) 329 314 require.NoError(t, pr.LoadIssue(db.DefaultContext)) 330 315 331 - err := issue_service.ChangeTitle(db.DefaultContext, pr.Issue, user2, "[WIP] Test Pull Request") 316 + err = issue_service.ChangeTitle(db.DefaultContext, pr.Issue, user2, "[WIP] Test Pull Request") 332 317 require.NoError(t, err) 333 318 prUpdated1 := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: pr.ID}) 334 319 require.NoError(t, prUpdated1.LoadIssue(db.DefaultContext)) ··· 342 327 }) 343 328 344 329 // change the default branch CODEOWNERS file to change README.md's codeowner 345 - _, err = files_service.ChangeRepoFiles(db.DefaultContext, repo, user2, &files_service.ChangeRepoFilesOptions{ 330 + _, err := files_service.ChangeRepoFiles(db.DefaultContext, repo, user2, &files_service.ChangeRepoFilesOptions{ 346 331 Files: []*files_service.ChangeRepoFile{ 347 332 { 348 333 Operation: "update",
-2
tests/integration/rename_branch_test.go
··· 22 22 } 23 23 24 24 func testRenameBranch(t *testing.T, u *url.URL) { 25 - defer tests.PrepareTestEnv(t)() 26 - 27 25 repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) 28 26 unittest.AssertExistsAndLoadBean(t, &git_model.Branch{RepoID: repo.ID, Name: "master"}) 29 27
+16 -16
tests/integration/repo_collaborator_test.go
··· 5 5 6 6 import ( 7 7 "net/http" 8 - "net/url" 9 8 "strings" 10 9 "testing" 10 + 11 + "code.gitea.io/gitea/tests" 11 12 12 13 "github.com/stretchr/testify/assert" 13 14 ) ··· 15 16 // TestRepoCollaborators is a test for contents of Collaborators tab in the repo settings 16 17 // It only covers a few elements and can be extended as needed 17 18 func TestRepoCollaborators(t *testing.T) { 18 - onGiteaRun(t, func(t *testing.T, u *url.URL) { 19 - session := loginUser(t, "user2") 19 + defer tests.PrepareTestEnv(t)() 20 + session := loginUser(t, "user2") 20 21 21 - // Visit Collaborators tab of repo settings 22 - response := session.MakeRequest(t, NewRequest(t, "GET", "/user2/repo1/settings/collaboration"), http.StatusOK) 23 - page := NewHTMLParser(t, response.Body).Find(".repo-setting-content") 22 + // Visit Collaborators tab of repo settings 23 + response := session.MakeRequest(t, NewRequest(t, "GET", "/user2/repo1/settings/collaboration"), http.StatusOK) 24 + page := NewHTMLParser(t, response.Body).Find(".repo-setting-content") 24 25 25 - // Veirfy header 26 - assert.EqualValues(t, "Collaborators", strings.TrimSpace(page.Find("h4").Text())) 26 + // Veirfy header 27 + assert.EqualValues(t, "Collaborators", strings.TrimSpace(page.Find("h4").Text())) 27 28 28 - // Veirfy button text 29 - page = page.Find("#repo-collab-form") 30 - assert.EqualValues(t, "Add collaborator", strings.TrimSpace(page.Find("button.primary").Text())) 29 + // Veirfy button text 30 + page = page.Find("#repo-collab-form") 31 + assert.EqualValues(t, "Add collaborator", strings.TrimSpace(page.Find("button.primary").Text())) 31 32 32 - // Veirfy placeholder 33 - placeholder, exists := page.Find("#search-user-box input").Attr("placeholder") 34 - assert.True(t, exists) 35 - assert.EqualValues(t, "Search users...", placeholder) 36 - }) 33 + // Veirfy placeholder 34 + placeholder, exists := page.Find("#search-user-box input").Attr("placeholder") 35 + assert.True(t, exists) 36 + assert.EqualValues(t, "Search users...", placeholder) 37 37 }
+13 -13
tests/integration/repo_migration_ui_test.go
··· 5 5 6 6 import ( 7 7 "net/http" 8 - "net/url" 9 8 "testing" 9 + 10 + "code.gitea.io/gitea/tests" 10 11 11 12 "github.com/PuerkitoBio/goquery" 12 13 "github.com/stretchr/testify/assert" 13 14 ) 14 15 15 16 func TestRepoMigrationUI(t *testing.T) { 16 - onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) { 17 - sessionUser1 := loginUser(t, "user1") 18 - // Nothing is tested in plain Git migration form right now 19 - testRepoMigrationFormGitHub(t, sessionUser1) 20 - testRepoMigrationFormGitea(t, sessionUser1) 21 - testRepoMigrationFormGitLab(t, sessionUser1) 22 - testRepoMigrationFormGogs(t, sessionUser1) 23 - testRepoMigrationFormOneDev(t, sessionUser1) 24 - testRepoMigrationFormGitBucket(t, sessionUser1) 25 - testRepoMigrationFormCodebase(t, sessionUser1) 26 - testRepoMigrationFormForgejo(t, sessionUser1) 27 - }) 17 + defer tests.PrepareTestEnv(t)() 18 + sessionUser1 := loginUser(t, "user1") 19 + // Nothing is tested in plain Git migration form right now 20 + testRepoMigrationFormGitHub(t, sessionUser1) 21 + testRepoMigrationFormGitea(t, sessionUser1) 22 + testRepoMigrationFormGitLab(t, sessionUser1) 23 + testRepoMigrationFormGogs(t, sessionUser1) 24 + testRepoMigrationFormOneDev(t, sessionUser1) 25 + testRepoMigrationFormGitBucket(t, sessionUser1) 26 + testRepoMigrationFormCodebase(t, sessionUser1) 27 + testRepoMigrationFormForgejo(t, sessionUser1) 28 28 } 29 29 30 30 func testRepoMigrationFormGitHub(t *testing.T, session *TestSession) {
+19 -75
tests/integration/repo_tag_test.go
··· 11 11 "testing" 12 12 13 13 "code.gitea.io/gitea/models" 14 - "code.gitea.io/gitea/models/db" 15 - git_model "code.gitea.io/gitea/models/git" 16 14 repo_model "code.gitea.io/gitea/models/repo" 17 15 "code.gitea.io/gitea/models/unittest" 18 16 user_model "code.gitea.io/gitea/models/user" ··· 31 29 repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) 32 30 owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}) 33 31 34 - defer func() { 35 - releases, err := db.Find[repo_model.Release](db.DefaultContext, repo_model.FindReleasesOptions{ 36 - IncludeTags: true, 37 - TagNames: []string{"no-release"}, 38 - RepoID: repo.ID, 39 - }) 40 - require.NoError(t, err) 41 - 42 - for _, release := range releases { 43 - _, err = db.DeleteByID[repo_model.Release](db.DefaultContext, release.ID) 44 - require.NoError(t, err) 45 - } 46 - }() 47 - 48 32 err := release.CreateNewTag(git.DefaultContext, owner, repo, "master", "no-release", "release-less tag") 49 33 require.NoError(t, err) 50 34 ··· 70 54 } 71 55 72 56 func TestCreateNewTagProtected(t *testing.T) { 73 - defer tests.PrepareTestEnv(t)() 57 + onGiteaRun(t, func(t *testing.T, u *url.URL) { 58 + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) 59 + owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}) 74 60 75 - repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) 76 - owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}) 61 + t.Run("Code", func(t *testing.T) { 62 + defer tests.PrintCurrentTest(t)() 77 63 78 - t.Run("Code", func(t *testing.T) { 79 - defer tests.PrintCurrentTest(t)() 64 + err := release.CreateNewTag(git.DefaultContext, owner, repo, "master", "t-first", "first tag") 65 + require.NoError(t, err) 80 66 81 - err := release.CreateNewTag(git.DefaultContext, owner, repo, "master", "t-first", "first tag") 82 - require.NoError(t, err) 67 + err = release.CreateNewTag(git.DefaultContext, owner, repo, "master", "v-2", "second tag") 68 + require.Error(t, err) 69 + assert.True(t, models.IsErrProtectedTagName(err)) 83 70 84 - err = release.CreateNewTag(git.DefaultContext, owner, repo, "master", "v-2", "second tag") 85 - require.Error(t, err) 86 - assert.True(t, models.IsErrProtectedTagName(err)) 71 + err = release.CreateNewTag(git.DefaultContext, owner, repo, "master", "v-1.1", "third tag") 72 + require.NoError(t, err) 73 + }) 87 74 88 - err = release.CreateNewTag(git.DefaultContext, owner, repo, "master", "v-1.1", "third tag") 89 - require.NoError(t, err) 90 - }) 75 + t.Run("Git", func(t *testing.T) { 76 + defer tests.PrintCurrentTest(t)() 91 77 92 - t.Run("Git", func(t *testing.T) { 93 - onGiteaRun(t, func(t *testing.T, u *url.URL) { 94 78 httpContext := NewAPITestContext(t, owner.Name, repo.Name) 95 79 96 80 dstPath := t.TempDir() ··· 107 91 require.Error(t, err) 108 92 assert.Contains(t, err.Error(), "Tag v-2 is protected") 109 93 }) 110 - }) 111 94 112 - t.Run("GitTagForce", func(t *testing.T) { 113 - onGiteaRun(t, func(t *testing.T, u *url.URL) { 95 + t.Run("GitTagForce", func(t *testing.T) { 96 + defer tests.PrintCurrentTest(t)() 97 + 114 98 httpContext := NewAPITestContext(t, owner.Name, repo.Name) 115 99 116 100 dstPath := t.TempDir() ··· 120 104 121 105 doGitClone(dstPath, u)(t) 122 106 123 - _, _, err := git.NewCommand(git.DefaultContext, "tag", "v-1.1", "-m", "force update", "--force").RunStdString(&git.RunOpts{Dir: dstPath}) 124 - require.NoError(t, err) 125 - 126 - _, _, err = git.NewCommand(git.DefaultContext, "push", "--tags").RunStdString(&git.RunOpts{Dir: dstPath}) 127 - require.NoError(t, err) 128 - 129 - _, _, err = git.NewCommand(git.DefaultContext, "tag", "v-1.1", "-m", "force update v2", "--force").RunStdString(&git.RunOpts{Dir: dstPath}) 107 + _, _, err := git.NewCommand(git.DefaultContext, "tag", "v-1.1", "-m", "force update v2", "--force").RunStdString(&git.RunOpts{Dir: dstPath}) 130 108 require.NoError(t, err) 131 109 132 110 _, _, err = git.NewCommand(git.DefaultContext, "push", "--tags").RunStdString(&git.RunOpts{Dir: dstPath}) ··· 142 120 assert.Contains(t, tagsTab.Text(), "force update v2") 143 121 }) 144 122 }) 145 - 146 - // Cleanup 147 - releases, err := db.Find[repo_model.Release](db.DefaultContext, repo_model.FindReleasesOptions{ 148 - IncludeTags: true, 149 - TagNames: []string{"v-1", "v-1.1"}, 150 - RepoID: repo.ID, 151 - }) 152 - require.NoError(t, err) 153 - 154 - for _, release := range releases { 155 - _, err = db.DeleteByID[repo_model.Release](db.DefaultContext, release.ID) 156 - require.NoError(t, err) 157 - } 158 - 159 - protectedTags, err := git_model.GetProtectedTags(db.DefaultContext, repo.ID) 160 - require.NoError(t, err) 161 - 162 - for _, protectedTag := range protectedTags { 163 - err = git_model.DeleteProtectedTag(db.DefaultContext, protectedTag) 164 - require.NoError(t, err) 165 - } 166 123 } 167 124 168 125 func TestSyncRepoTags(t *testing.T) { ··· 200 157 require.NoError(t, repo_module.SyncRepoTags(git.DefaultContext, repo.ID)) 201 158 testTag(t) 202 159 }) 203 - 204 - // Cleanup 205 - releases, err := db.Find[repo_model.Release](db.DefaultContext, repo_model.FindReleasesOptions{ 206 - IncludeTags: true, 207 - TagNames: []string{"v2"}, 208 - RepoID: repo.ID, 209 - }) 210 - require.NoError(t, err) 211 - 212 - for _, release := range releases { 213 - _, err = db.DeleteByID[repo_model.Release](db.DefaultContext, release.ID) 214 - require.NoError(t, err) 215 - } 216 160 }) 217 161 }
+17 -23
tests/integration/repo_view_test.go
··· 62 62 } 63 63 64 64 func TestRepoView_FindReadme(t *testing.T) { 65 - t.Run("PrioOneLocalizedMdReadme", func(t *testing.T) { 66 - onGiteaRun(t, func(t *testing.T, u *url.URL) { 65 + onGiteaRun(t, func(t *testing.T, u *url.URL) { 66 + t.Run("PrioOneLocalizedMdReadme", func(t *testing.T) { 67 + defer tests.PrintCurrentTest(t)() 67 68 ctx, f := createRepoAndGetContext(t, []string{"README.en.md", "README.en.org", "README.org", "README.txt", "README.tex"}, false) 68 69 defer f() 69 70 ··· 73 74 74 75 assert.Equal(t, "README.en.md", file.Name()) 75 76 }) 76 - }) 77 - t.Run("PrioTwoMdReadme", func(t *testing.T) { 78 - onGiteaRun(t, func(t *testing.T, u *url.URL) { 77 + t.Run("PrioTwoMdReadme", func(t *testing.T) { 78 + defer tests.PrintCurrentTest(t)() 79 79 ctx, f := createRepoAndGetContext(t, []string{"README.en.org", "README.org", "README.txt", "README.tex"}, false) 80 80 defer f() 81 81 ··· 85 85 86 86 assert.Equal(t, "README.md", file.Name()) 87 87 }) 88 - }) 89 - t.Run("PrioThreeLocalizedOrgReadme", func(t *testing.T) { 90 - onGiteaRun(t, func(t *testing.T, u *url.URL) { 88 + t.Run("PrioThreeLocalizedOrgReadme", func(t *testing.T) { 89 + defer tests.PrintCurrentTest(t)() 91 90 ctx, f := createRepoAndGetContext(t, []string{"README.en.org", "README.org", "README.txt", "README.tex"}, true) 92 91 defer f() 93 92 ··· 97 96 98 97 assert.Equal(t, "README.en.org", file.Name()) 99 98 }) 100 - }) 101 - t.Run("PrioFourOrgReadme", func(t *testing.T) { 102 - onGiteaRun(t, func(t *testing.T, u *url.URL) { 99 + t.Run("PrioFourOrgReadme", func(t *testing.T) { 100 + defer tests.PrintCurrentTest(t)() 103 101 ctx, f := createRepoAndGetContext(t, []string{"README.org", "README.txt", "README.tex"}, true) 104 102 defer f() 105 103 ··· 109 107 110 108 assert.Equal(t, "README.org", file.Name()) 111 109 }) 112 - }) 113 - t.Run("PrioFiveTxtReadme", func(t *testing.T) { 114 - onGiteaRun(t, func(t *testing.T, u *url.URL) { 110 + t.Run("PrioFiveTxtReadme", func(t *testing.T) { 111 + defer tests.PrintCurrentTest(t)() 115 112 ctx, f := createRepoAndGetContext(t, []string{"README.txt", "README", "README.tex"}, true) 116 113 defer f() 117 114 ··· 121 118 122 119 assert.Equal(t, "README.txt", file.Name()) 123 120 }) 124 - }) 125 - t.Run("PrioSixWithoutExtensionReadme", func(t *testing.T) { 126 - onGiteaRun(t, func(t *testing.T, u *url.URL) { 121 + t.Run("PrioSixWithoutExtensionReadme", func(t *testing.T) { 122 + defer tests.PrintCurrentTest(t)() 127 123 ctx, f := createRepoAndGetContext(t, []string{"README", "README.tex"}, true) 128 124 defer f() 129 125 ··· 133 129 134 130 assert.Equal(t, "README", file.Name()) 135 131 }) 136 - }) 137 - t.Run("PrioSevenAnyReadme", func(t *testing.T) { 138 - onGiteaRun(t, func(t *testing.T, u *url.URL) { 132 + t.Run("PrioSevenAnyReadme", func(t *testing.T) { 133 + defer tests.PrintCurrentTest(t)() 139 134 ctx, f := createRepoAndGetContext(t, []string{"README.tex"}, true) 140 135 defer f() 141 136 ··· 145 140 146 141 assert.Equal(t, "README.tex", file.Name()) 147 142 }) 148 - }) 149 - t.Run("DoNotPickReadmeIfNonPresent", func(t *testing.T) { 150 - onGiteaRun(t, func(t *testing.T, u *url.URL) { 143 + t.Run("DoNotPickReadmeIfNonPresent", func(t *testing.T) { 144 + defer tests.PrintCurrentTest(t)() 151 145 ctx, f := createRepoAndGetContext(t, []string{}, true) 152 146 defer f() 153 147
+114 -155
tests/integration/repofiles_change_test.go
··· 18 18 "code.gitea.io/gitea/modules/setting" 19 19 api "code.gitea.io/gitea/modules/structs" 20 20 files_service "code.gitea.io/gitea/services/repository/files" 21 + "code.gitea.io/gitea/tests" 21 22 22 23 "github.com/stretchr/testify/assert" 23 24 "github.com/stretchr/testify/require" ··· 63 64 Files: []*files_service.ChangeRepoFile{ 64 65 { 65 66 Operation: "delete", 66 - TreePath: "README.md", 67 - SHA: "4b4851ad51df6a7d9f25c979345979eaeb5b349f", 67 + TreePath: "README_new.md", 68 + SHA: "dbf8d00e022e05b7e5cf7e535de857de57925647", 68 69 }, 69 70 }, 70 71 LastCommitID: "", ··· 244 245 } 245 246 } 246 247 247 - func TestChangeRepoFilesForCreate(t *testing.T) { 248 - // setup 248 + func TestChangeRepoFiles(t *testing.T) { 249 249 onGiteaRun(t, func(t *testing.T, u *url.URL) { 250 250 doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) 251 251 repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) 252 - opts := getCreateRepoFilesOptions(repo) 253 252 254 - // test 255 - filesResponse, err := files_service.ChangeRepoFiles(git.DefaultContext, repo, doer, opts) 256 - 257 - // asserts 253 + gitRepo, err := gitrepo.OpenRepository(git.DefaultContext, repo) 258 254 require.NoError(t, err) 259 - gitRepo, _ := gitrepo.OpenRepository(git.DefaultContext, repo) 260 255 defer gitRepo.Close() 261 256 262 - commitID, _ := gitRepo.GetBranchCommitID(opts.NewBranch) 263 - lastCommit, _ := gitRepo.GetCommitByPath("new/file.txt") 264 - expectedFileResponse := getExpectedFileResponseForRepofilesCreate(commitID, lastCommit.ID.String()) 265 - assert.NotNil(t, expectedFileResponse) 266 - if expectedFileResponse != nil { 257 + t.Run("Create", func(t *testing.T) { 258 + defer tests.PrintCurrentTest(t)() 259 + opts := getCreateRepoFilesOptions(repo) 260 + filesResponse, err := files_service.ChangeRepoFiles(git.DefaultContext, repo, doer, opts) 261 + require.NoError(t, err) 262 + 263 + commitID, err := gitRepo.GetBranchCommitID(opts.NewBranch) 264 + require.NoError(t, err) 265 + lastCommit, err := gitRepo.GetCommitByPath("new/file.txt") 266 + require.NoError(t, err) 267 + expectedFileResponse := getExpectedFileResponseForRepofilesCreate(commitID, lastCommit.ID.String()) 267 268 assert.EqualValues(t, expectedFileResponse.Content, filesResponse.Files[0]) 268 269 assert.EqualValues(t, expectedFileResponse.Commit.SHA, filesResponse.Commit.SHA) 269 270 assert.EqualValues(t, expectedFileResponse.Commit.HTMLURL, filesResponse.Commit.HTMLURL) 270 271 assert.EqualValues(t, expectedFileResponse.Commit.Author.Email, filesResponse.Commit.Author.Email) 271 272 assert.EqualValues(t, expectedFileResponse.Commit.Author.Name, filesResponse.Commit.Author.Name) 272 - } 273 - }) 274 - } 273 + }) 275 274 276 - func TestChangeRepoFilesForUpdate(t *testing.T) { 277 - // setup 278 - onGiteaRun(t, func(t *testing.T, u *url.URL) { 279 - doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) 280 - repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) 281 - opts := getUpdateRepoFilesOptions(repo) 275 + t.Run("Update", func(t *testing.T) { 276 + defer tests.PrintCurrentTest(t)() 277 + opts := getUpdateRepoFilesOptions(repo) 278 + filesResponse, err := files_service.ChangeRepoFiles(git.DefaultContext, repo, doer, opts) 279 + require.NoError(t, err) 282 280 283 - // test 284 - filesResponse, err := files_service.ChangeRepoFiles(git.DefaultContext, repo, doer, opts) 281 + commit, err := gitRepo.GetBranchCommit(opts.NewBranch) 282 + require.NoError(t, err) 283 + lastCommit, err := commit.GetCommitByPath(opts.Files[0].TreePath) 284 + require.NoError(t, err) 285 + expectedFileResponse := getExpectedFileResponseForRepofilesUpdate(commit.ID.String(), opts.Files[0].TreePath, lastCommit.ID.String()) 286 + assert.EqualValues(t, expectedFileResponse.Content, filesResponse.Files[0]) 287 + assert.EqualValues(t, expectedFileResponse.Commit.SHA, filesResponse.Commit.SHA) 288 + assert.EqualValues(t, expectedFileResponse.Commit.HTMLURL, filesResponse.Commit.HTMLURL) 289 + assert.EqualValues(t, expectedFileResponse.Commit.Author.Email, filesResponse.Commit.Author.Email) 290 + assert.EqualValues(t, expectedFileResponse.Commit.Author.Name, filesResponse.Commit.Author.Name) 291 + }) 285 292 286 - // asserts 287 - require.NoError(t, err) 288 - gitRepo, _ := gitrepo.OpenRepository(git.DefaultContext, repo) 289 - defer gitRepo.Close() 293 + t.Run("Update and move", func(t *testing.T) { 294 + defer tests.PrintCurrentTest(t)() 295 + opts := getUpdateRepoFilesOptions(repo) 296 + opts.Files[0].SHA = "dbf8d00e022e05b7e5cf7e535de857de57925647" 297 + opts.Files[0].FromTreePath = "README.md" 298 + opts.Files[0].TreePath = "README_new.md" // new file name, README_new.md 299 + filesResponse, err := files_service.ChangeRepoFiles(git.DefaultContext, repo, doer, opts) 300 + require.NoError(t, err) 290 301 291 - commit, _ := gitRepo.GetBranchCommit(opts.NewBranch) 292 - lastCommit, _ := commit.GetCommitByPath(opts.Files[0].TreePath) 293 - expectedFileResponse := getExpectedFileResponseForRepofilesUpdate(commit.ID.String(), opts.Files[0].TreePath, lastCommit.ID.String()) 294 - assert.EqualValues(t, expectedFileResponse.Content, filesResponse.Files[0]) 295 - assert.EqualValues(t, expectedFileResponse.Commit.SHA, filesResponse.Commit.SHA) 296 - assert.EqualValues(t, expectedFileResponse.Commit.HTMLURL, filesResponse.Commit.HTMLURL) 297 - assert.EqualValues(t, expectedFileResponse.Commit.Author.Email, filesResponse.Commit.Author.Email) 298 - assert.EqualValues(t, expectedFileResponse.Commit.Author.Name, filesResponse.Commit.Author.Name) 299 - }) 300 - } 302 + commit, err := gitRepo.GetBranchCommit(opts.NewBranch) 303 + require.NoError(t, err) 304 + lastCommit, err := commit.GetCommitByPath(opts.Files[0].TreePath) 305 + require.NoError(t, err) 306 + expectedFileResponse := getExpectedFileResponseForRepofilesUpdate(commit.ID.String(), opts.Files[0].TreePath, lastCommit.ID.String()) 301 307 302 - func TestChangeRepoFilesForUpdateWithFileMove(t *testing.T) { 303 - // setup 304 - onGiteaRun(t, func(t *testing.T, u *url.URL) { 305 - doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) 306 - repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) 307 - opts := getUpdateRepoFilesOptions(repo) 308 - opts.Files[0].FromTreePath = "README.md" 309 - opts.Files[0].TreePath = "README_new.md" // new file name, README_new.md 308 + // assert that the old file no longer exists in the last commit of the branch 309 + fromEntry, err := commit.GetTreeEntryByPath(opts.Files[0].FromTreePath) 310 + switch err.(type) { 311 + case git.ErrNotExist: 312 + // correct, continue 313 + default: 314 + t.Fatalf("expected git.ErrNotExist, got:%v", err) 315 + } 316 + toEntry, err := commit.GetTreeEntryByPath(opts.Files[0].TreePath) 317 + require.NoError(t, err) 318 + assert.Nil(t, fromEntry) // Should no longer exist here 319 + assert.NotNil(t, toEntry) // Should exist here 320 + // assert SHA has remained the same but paths use the new file name 321 + assert.EqualValues(t, expectedFileResponse.Content.SHA, filesResponse.Files[0].SHA) 322 + assert.EqualValues(t, expectedFileResponse.Content.Name, filesResponse.Files[0].Name) 323 + assert.EqualValues(t, expectedFileResponse.Content.Path, filesResponse.Files[0].Path) 324 + assert.EqualValues(t, expectedFileResponse.Content.URL, filesResponse.Files[0].URL) 325 + assert.EqualValues(t, expectedFileResponse.Commit.SHA, filesResponse.Commit.SHA) 326 + assert.EqualValues(t, expectedFileResponse.Commit.HTMLURL, filesResponse.Commit.HTMLURL) 327 + }) 310 328 311 - // test 312 - filesResponse, err := files_service.ChangeRepoFiles(git.DefaultContext, repo, doer, opts) 329 + t.Run("Change without branch names", func(t *testing.T) { 330 + defer tests.PrintCurrentTest(t)() 331 + opts := getUpdateRepoFilesOptions(repo) 332 + opts.OldBranch = "" 333 + opts.NewBranch = "" 334 + opts.Files[0].TreePath = "README_new.md" 335 + opts.Files[0].SHA = "dbf8d00e022e05b7e5cf7e535de857de57925647" 313 336 314 - // asserts 315 - require.NoError(t, err) 316 - gitRepo, _ := gitrepo.OpenRepository(git.DefaultContext, repo) 317 - defer gitRepo.Close() 337 + filesResponse, err := files_service.ChangeRepoFiles(git.DefaultContext, repo, doer, opts) 338 + require.NoError(t, err) 318 339 319 - commit, _ := gitRepo.GetBranchCommit(opts.NewBranch) 320 - lastCommit, _ := commit.GetCommitByPath(opts.Files[0].TreePath) 321 - expectedFileResponse := getExpectedFileResponseForRepofilesUpdate(commit.ID.String(), opts.Files[0].TreePath, lastCommit.ID.String()) 322 - // assert that the old file no longer exists in the last commit of the branch 323 - fromEntry, err := commit.GetTreeEntryByPath(opts.Files[0].FromTreePath) 324 - switch err.(type) { 325 - case git.ErrNotExist: 326 - // correct, continue 327 - default: 328 - t.Fatalf("expected git.ErrNotExist, got:%v", err) 329 - } 330 - toEntry, err := commit.GetTreeEntryByPath(opts.Files[0].TreePath) 331 - require.NoError(t, err) 332 - assert.Nil(t, fromEntry) // Should no longer exist here 333 - assert.NotNil(t, toEntry) // Should exist here 334 - // assert SHA has remained the same but paths use the new file name 335 - assert.EqualValues(t, expectedFileResponse.Content.SHA, filesResponse.Files[0].SHA) 336 - assert.EqualValues(t, expectedFileResponse.Content.Name, filesResponse.Files[0].Name) 337 - assert.EqualValues(t, expectedFileResponse.Content.Path, filesResponse.Files[0].Path) 338 - assert.EqualValues(t, expectedFileResponse.Content.URL, filesResponse.Files[0].URL) 339 - assert.EqualValues(t, expectedFileResponse.Commit.SHA, filesResponse.Commit.SHA) 340 - assert.EqualValues(t, expectedFileResponse.Commit.HTMLURL, filesResponse.Commit.HTMLURL) 341 - }) 342 - } 340 + commit, _ := gitRepo.GetBranchCommit(repo.DefaultBranch) 341 + lastCommit, _ := commit.GetCommitByPath(opts.Files[0].TreePath) 342 + expectedFileResponse := getExpectedFileResponseForRepofilesUpdate(commit.ID.String(), opts.Files[0].TreePath, lastCommit.ID.String()) 343 + assert.EqualValues(t, expectedFileResponse.Content, filesResponse.Files[0]) 344 + }) 343 345 344 - // Test opts with branch names removed, should get same results as above test 345 - func TestChangeRepoFilesWithoutBranchNames(t *testing.T) { 346 - // setup 347 - onGiteaRun(t, func(t *testing.T, u *url.URL) { 348 - doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) 349 - repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) 350 - opts := getUpdateRepoFilesOptions(repo) 351 - opts.OldBranch = "" 352 - opts.NewBranch = "" 353 - 354 - // test 355 - filesResponse, err := files_service.ChangeRepoFiles(git.DefaultContext, repo, doer, opts) 356 - 357 - // asserts 358 - require.NoError(t, err) 359 - gitRepo, _ := gitrepo.OpenRepository(git.DefaultContext, repo) 360 - defer gitRepo.Close() 361 - 362 - commit, _ := gitRepo.GetBranchCommit(repo.DefaultBranch) 363 - lastCommit, _ := commit.GetCommitByPath(opts.Files[0].TreePath) 364 - expectedFileResponse := getExpectedFileResponseForRepofilesUpdate(commit.ID.String(), opts.Files[0].TreePath, lastCommit.ID.String()) 365 - assert.EqualValues(t, expectedFileResponse.Content, filesResponse.Files[0]) 366 - }) 367 - } 368 - 369 - func TestChangeRepoFilesForDelete(t *testing.T) { 370 - onGiteaRun(t, testDeleteRepoFiles) 371 - } 372 - 373 - func testDeleteRepoFiles(t *testing.T, u *url.URL) { 374 - // setup 375 - unittest.PrepareTestEnv(t) 376 - doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) 377 - repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) 378 - opts := getDeleteRepoFilesOptions(repo) 379 - 380 - t.Run("Delete README.md file", func(t *testing.T) { 381 - filesResponse, err := files_service.ChangeRepoFiles(git.DefaultContext, repo, doer, opts) 382 - require.NoError(t, err) 383 - expectedFileResponse := getExpectedFileResponseForRepofilesDelete() 384 - assert.NotNil(t, filesResponse) 385 - assert.Nil(t, filesResponse.Files[0]) 386 - assert.EqualValues(t, expectedFileResponse.Commit.Message, filesResponse.Commit.Message) 387 - assert.EqualValues(t, expectedFileResponse.Commit.Author.Identity, filesResponse.Commit.Author.Identity) 388 - assert.EqualValues(t, expectedFileResponse.Commit.Committer.Identity, filesResponse.Commit.Committer.Identity) 389 - assert.EqualValues(t, expectedFileResponse.Verification, filesResponse.Verification) 390 - }) 346 + t.Run("Delete files", func(t *testing.T) { 347 + defer tests.PrintCurrentTest(t)() 348 + opts := getDeleteRepoFilesOptions(repo) 391 349 392 - t.Run("Verify README.md has been deleted", func(t *testing.T) { 393 - filesResponse, err := files_service.ChangeRepoFiles(git.DefaultContext, repo, doer, opts) 394 - assert.Nil(t, filesResponse) 395 - expectedError := "repository file does not exist [path: " + opts.Files[0].TreePath + "]" 396 - assert.EqualError(t, err, expectedError) 397 - }) 398 - } 350 + filesResponse, err := files_service.ChangeRepoFiles(git.DefaultContext, repo, doer, opts) 351 + require.NoError(t, err) 352 + expectedFileResponse := getExpectedFileResponseForRepofilesDelete() 353 + assert.NotNil(t, filesResponse) 354 + assert.Nil(t, filesResponse.Files[0]) 355 + assert.EqualValues(t, expectedFileResponse.Commit.Message, filesResponse.Commit.Message) 356 + assert.EqualValues(t, expectedFileResponse.Commit.Author.Identity, filesResponse.Commit.Author.Identity) 357 + assert.EqualValues(t, expectedFileResponse.Commit.Committer.Identity, filesResponse.Commit.Committer.Identity) 358 + assert.EqualValues(t, expectedFileResponse.Verification, filesResponse.Verification) 399 359 400 - // Test opts with branch names removed, same results 401 - func TestChangeRepoFilesForDeleteWithoutBranchNames(t *testing.T) { 402 - onGiteaRun(t, testDeleteRepoFilesWithoutBranchNames) 403 - } 360 + filesResponse, err = files_service.ChangeRepoFiles(git.DefaultContext, repo, doer, opts) 361 + assert.Nil(t, filesResponse) 362 + expectedError := "repository file does not exist [path: " + opts.Files[0].TreePath + "]" 363 + assert.EqualError(t, err, expectedError) 364 + }) 404 365 405 - func testDeleteRepoFilesWithoutBranchNames(t *testing.T, u *url.URL) { 406 - // setup 407 - unittest.PrepareTestEnv(t) 408 - doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) 409 - repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) 366 + t.Run("Delete without branch name", func(t *testing.T) { 367 + defer tests.PrintCurrentTest(t)() 368 + opts := getDeleteRepoFilesOptions(repo) 369 + opts.OldBranch = "" 370 + opts.NewBranch = "" 371 + opts.Files[0].SHA = "103ff9234cefeee5ec5361d22b49fbb04d385885" 372 + opts.Files[0].TreePath = "new/file.txt" 410 373 411 - opts := getDeleteRepoFilesOptions(repo) 412 - opts.OldBranch = "" 413 - opts.NewBranch = "" 414 - 415 - t.Run("Delete README.md without Branch Name", func(t *testing.T) { 416 - filesResponse, err := files_service.ChangeRepoFiles(git.DefaultContext, repo, doer, opts) 417 - require.NoError(t, err) 418 - expectedFileResponse := getExpectedFileResponseForRepofilesDelete() 419 - assert.NotNil(t, filesResponse) 420 - assert.Nil(t, filesResponse.Files[0]) 421 - assert.EqualValues(t, expectedFileResponse.Commit.Message, filesResponse.Commit.Message) 422 - assert.EqualValues(t, expectedFileResponse.Commit.Author.Identity, filesResponse.Commit.Author.Identity) 423 - assert.EqualValues(t, expectedFileResponse.Commit.Committer.Identity, filesResponse.Commit.Committer.Identity) 424 - assert.EqualValues(t, expectedFileResponse.Verification, filesResponse.Verification) 374 + filesResponse, err := files_service.ChangeRepoFiles(git.DefaultContext, repo, doer, opts) 375 + require.NoError(t, err) 376 + expectedFileResponse := getExpectedFileResponseForRepofilesDelete() 377 + assert.NotNil(t, filesResponse) 378 + assert.Nil(t, filesResponse.Files[0]) 379 + assert.EqualValues(t, expectedFileResponse.Commit.Message, filesResponse.Commit.Message) 380 + assert.EqualValues(t, expectedFileResponse.Commit.Author.Identity, filesResponse.Commit.Author.Identity) 381 + assert.EqualValues(t, expectedFileResponse.Commit.Committer.Identity, filesResponse.Commit.Committer.Identity) 382 + assert.EqualValues(t, expectedFileResponse.Verification, filesResponse.Verification) 383 + }) 425 384 }) 426 385 } 427 386
+47 -54
tests/integration/user_avatar_test.go
··· 10 10 "io" 11 11 "mime/multipart" 12 12 "net/http" 13 - "net/url" 14 13 "testing" 15 14 16 15 "code.gitea.io/gitea/models/db" 17 16 "code.gitea.io/gitea/models/unittest" 18 17 user_model "code.gitea.io/gitea/models/user" 19 18 "code.gitea.io/gitea/modules/avatar" 19 + "code.gitea.io/gitea/tests" 20 20 21 21 "github.com/stretchr/testify/assert" 22 22 "github.com/stretchr/testify/require" 23 23 ) 24 24 25 25 func TestUserAvatar(t *testing.T) { 26 - onGiteaRun(t, func(t *testing.T, u *url.URL) { 27 - user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) // owner of the repo3, is an org 26 + defer tests.PrepareTestEnv(t)() 27 + user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) // owner of the repo3, is an org 28 28 29 - seed := user2.Email 30 - if len(seed) == 0 { 31 - seed = user2.Name 32 - } 29 + seed := user2.Email 30 + if len(seed) == 0 { 31 + seed = user2.Name 32 + } 33 33 34 - img, err := avatar.RandomImage([]byte(seed)) 35 - if err != nil { 36 - require.NoError(t, err) 37 - return 38 - } 34 + img, err := avatar.RandomImage([]byte(seed)) 35 + if err != nil { 36 + require.NoError(t, err) 37 + return 38 + } 39 39 40 - session := loginUser(t, "user2") 41 - csrf := GetCSRF(t, session, "/user/settings") 40 + session := loginUser(t, "user2") 41 + csrf := GetCSRF(t, session, "/user/settings") 42 42 43 - imgData := &bytes.Buffer{} 43 + imgData := &bytes.Buffer{} 44 44 45 - body := &bytes.Buffer{} 45 + body := &bytes.Buffer{} 46 46 47 - // Setup multi-part 48 - writer := multipart.NewWriter(body) 49 - writer.WriteField("source", "local") 50 - part, err := writer.CreateFormFile("avatar", "avatar-for-testuseravatar.png") 51 - if err != nil { 52 - require.NoError(t, err) 53 - return 54 - } 47 + // Setup multi-part 48 + writer := multipart.NewWriter(body) 49 + writer.WriteField("source", "local") 50 + part, err := writer.CreateFormFile("avatar", "avatar-for-testuseravatar.png") 51 + if err != nil { 52 + require.NoError(t, err) 53 + return 54 + } 55 55 56 - if err := png.Encode(imgData, img); err != nil { 57 - require.NoError(t, err) 58 - return 59 - } 56 + if err := png.Encode(imgData, img); err != nil { 57 + require.NoError(t, err) 58 + return 59 + } 60 60 61 - if _, err := io.Copy(part, imgData); err != nil { 62 - require.NoError(t, err) 63 - return 64 - } 61 + if _, err := io.Copy(part, imgData); err != nil { 62 + require.NoError(t, err) 63 + return 64 + } 65 65 66 - if err := writer.Close(); err != nil { 67 - require.NoError(t, err) 68 - return 69 - } 70 - 71 - req := NewRequestWithBody(t, "POST", "/user/settings/avatar", body) 72 - req.Header.Add("X-Csrf-Token", csrf) 73 - req.Header.Add("Content-Type", writer.FormDataContentType()) 66 + if err := writer.Close(); err != nil { 67 + require.NoError(t, err) 68 + return 69 + } 74 70 75 - session.MakeRequest(t, req, http.StatusSeeOther) 71 + req := NewRequestWithBody(t, "POST", "/user/settings/avatar", body) 72 + req.Header.Add("X-Csrf-Token", csrf) 73 + req.Header.Add("Content-Type", writer.FormDataContentType()) 76 74 77 - user2 = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) // owner of the repo3, is an org 75 + session.MakeRequest(t, req, http.StatusSeeOther) 78 76 79 - req = NewRequest(t, "GET", user2.AvatarLinkWithSize(db.DefaultContext, 0)) 80 - _ = session.MakeRequest(t, req, http.StatusOK) 77 + user2 = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) // owner of the repo3, is an org 81 78 82 - testGetAvatarRedirect(t, user2) 79 + req = NewRequest(t, "GET", user2.AvatarLinkWithSize(db.DefaultContext, 0)) 80 + _ = session.MakeRequest(t, req, http.StatusOK) 83 81 84 - // 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. 85 - }) 86 - } 82 + req = NewRequestf(t, "GET", "/%s.png", user2.Name) 83 + resp := MakeRequest(t, req, http.StatusSeeOther) 84 + assert.EqualValues(t, fmt.Sprintf("/avatars/%s", user2.Avatar), resp.Header().Get("location")) 87 85 88 - func testGetAvatarRedirect(t *testing.T, user *user_model.User) { 89 - t.Run(fmt.Sprintf("getAvatarRedirect_%s", user.Name), func(t *testing.T) { 90 - req := NewRequestf(t, "GET", "/%s.png", user.Name) 91 - resp := MakeRequest(t, req, http.StatusSeeOther) 92 - assert.EqualValues(t, fmt.Sprintf("/avatars/%s", user.Avatar), resp.Header().Get("location")) 93 - }) 86 + // 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. 94 87 }
+49 -50
tests/integration/user_profile_activity_test.go
··· 5 5 6 6 import ( 7 7 "net/http" 8 - "net/url" 9 8 "strconv" 10 9 "testing" 11 10 12 11 "code.gitea.io/gitea/modules/structs" 12 + "code.gitea.io/gitea/tests" 13 13 14 14 "github.com/stretchr/testify/assert" 15 15 ) ··· 23 23 // - Profile visibility 24 24 // - Public activity visibility 25 25 func TestUserProfileActivity(t *testing.T) { 26 - onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) { 27 - // This test needs multiple users with different access statuses to check for all possible states 28 - userAdmin := loginUser(t, "user1") 29 - userRegular := loginUser(t, "user2") 30 - // Activity availability should be the same for guest and another non-admin user, so this is not tested separately 31 - userGuest := emptyTestSession(t) 26 + defer tests.PrepareTestEnv(t)() 27 + // This test needs multiple users with different access statuses to check for all possible states 28 + userAdmin := loginUser(t, "user1") 29 + userRegular := loginUser(t, "user2") 30 + // Activity availability should be the same for guest and another non-admin user, so this is not tested separately 31 + userGuest := emptyTestSession(t) 32 32 33 - // = Public profile, public activity = 33 + // = Public profile, public activity = 34 34 35 - // Set activity visibility of user2 to public. This is the default, but won't hurt to set it before testing. 36 - testChangeUserActivityVisibility(t, userRegular, "off") 35 + // Set activity visibility of user2 to public. This is the default, but won't hurt to set it before testing. 36 + testChangeUserActivityVisibility(t, userRegular, "off") 37 37 38 - // Verify availability of RSS button and activity tab 39 - testUser2ActivityButtonsAvailability(t, userAdmin, true) 40 - testUser2ActivityButtonsAvailability(t, userRegular, true) 41 - testUser2ActivityButtonsAvailability(t, userGuest, true) 38 + // Verify availability of RSS button and activity tab 39 + testUser2ActivityButtonsAvailability(t, userAdmin, true) 40 + testUser2ActivityButtonsAvailability(t, userRegular, true) 41 + testUser2ActivityButtonsAvailability(t, userGuest, true) 42 42 43 - // Verify the hint for all types of users: admin, self, guest 44 - testUser2ActivityVisibility(t, userAdmin, "This activity is visible to everyone, but as an administrator you can also see interactions in private spaces.", true) 45 - hintLink := testUser2ActivityVisibility(t, userRegular, "Your activity is visible to everyone, except for interactions in private spaces. Configure.", true) 46 - testUser2ActivityVisibility(t, userGuest, "", true) 43 + // Verify the hint for all types of users: admin, self, guest 44 + testUser2ActivityVisibility(t, userAdmin, "This activity is visible to everyone, but as an administrator you can also see interactions in private spaces.", true) 45 + hintLink := testUser2ActivityVisibility(t, userRegular, "Your activity is visible to everyone, except for interactions in private spaces. Configure.", true) 46 + testUser2ActivityVisibility(t, userGuest, "", true) 47 47 48 - // When viewing own profile, the user is offered to configure activity visibility. Verify that the link is correct and works, also check that it links back to the activity tab. 49 - linkCorrect := assert.EqualValues(t, "/user/settings#keep-activity-private", hintLink) 50 - if linkCorrect { 51 - page := NewHTMLParser(t, userRegular.MakeRequest(t, NewRequest(t, "GET", hintLink), http.StatusOK).Body) 52 - activityLink, exists := page.Find(".field:has(.checkbox#keep-activity-private) .help a").Attr("href") 53 - assert.True(t, exists) 54 - assert.EqualValues(t, "/user2?tab=activity", activityLink) 55 - } 48 + // When viewing own profile, the user is offered to configure activity visibility. Verify that the link is correct and works, also check that it links back to the activity tab. 49 + linkCorrect := assert.EqualValues(t, "/user/settings#keep-activity-private", hintLink) 50 + if linkCorrect { 51 + page := NewHTMLParser(t, userRegular.MakeRequest(t, NewRequest(t, "GET", hintLink), http.StatusOK).Body) 52 + activityLink, exists := page.Find(".field:has(.checkbox#keep-activity-private) .help a").Attr("href") 53 + assert.True(t, exists) 54 + assert.EqualValues(t, "/user2?tab=activity", activityLink) 55 + } 56 56 57 - // = Private profile, but public activity = 57 + // = Private profile, but public activity = 58 58 59 - // Set profile visibility of user2 to private 60 - testChangeUserProfileVisibility(t, userRegular, structs.VisibleTypePrivate) 59 + // Set profile visibility of user2 to private 60 + testChangeUserProfileVisibility(t, userRegular, structs.VisibleTypePrivate) 61 61 62 - // When profile activity is configured as public, but the profile is private, tell the user about this and link to visibility settings. 63 - hintLink = testUser2ActivityVisibility(t, userRegular, "Your activity is only visible to you and the instance administrators because your profile is private. Configure.", true) 64 - assert.EqualValues(t, "/user/settings#visibility-setting", hintLink) 62 + // When profile activity is configured as public, but the profile is private, tell the user about this and link to visibility settings. 63 + hintLink = testUser2ActivityVisibility(t, userRegular, "Your activity is only visible to you and the instance administrators because your profile is private. Configure.", true) 64 + assert.EqualValues(t, "/user/settings#visibility-setting", hintLink) 65 65 66 - // When the profile is private, tell the admin about this. 67 - testUser2ActivityVisibility(t, userAdmin, "This activity is visible to you because you're an administrator, but the user wants it to remain private.", true) 66 + // When the profile is private, tell the admin about this. 67 + testUser2ActivityVisibility(t, userAdmin, "This activity is visible to you because you're an administrator, but the user wants it to remain private.", true) 68 68 69 - // Set profile visibility of user2 back to public 70 - testChangeUserProfileVisibility(t, userRegular, structs.VisibleTypePublic) 69 + // Set profile visibility of user2 back to public 70 + testChangeUserProfileVisibility(t, userRegular, structs.VisibleTypePublic) 71 71 72 - // = Private acitivty = 72 + // = Private acitivty = 73 73 74 - // Set activity visibility of user2 to private 75 - testChangeUserActivityVisibility(t, userRegular, "on") 74 + // Set activity visibility of user2 to private 75 + testChangeUserActivityVisibility(t, userRegular, "on") 76 76 77 - // Verify availability of RSS button and activity tab 78 - testUser2ActivityButtonsAvailability(t, userAdmin, true) 79 - testUser2ActivityButtonsAvailability(t, userRegular, true) 80 - testUser2ActivityButtonsAvailability(t, userGuest, false) 77 + // Verify availability of RSS button and activity tab 78 + testUser2ActivityButtonsAvailability(t, userAdmin, true) 79 + testUser2ActivityButtonsAvailability(t, userRegular, true) 80 + testUser2ActivityButtonsAvailability(t, userGuest, false) 81 81 82 - // Verify the hint for all types of users: admin, self, guest 83 - testUser2ActivityVisibility(t, userAdmin, "This activity is visible to you because you're an administrator, but the user wants it to remain private.", true) 84 - hintLink = testUser2ActivityVisibility(t, userRegular, "Your activity is only visible to you and the instance administrators. Configure.", true) 85 - testUser2ActivityVisibility(t, userGuest, "This user has disabled the public visibility of the activity.", false) 82 + // Verify the hint for all types of users: admin, self, guest 83 + testUser2ActivityVisibility(t, userAdmin, "This activity is visible to you because you're an administrator, but the user wants it to remain private.", true) 84 + hintLink = testUser2ActivityVisibility(t, userRegular, "Your activity is only visible to you and the instance administrators. Configure.", true) 85 + testUser2ActivityVisibility(t, userGuest, "This user has disabled the public visibility of the activity.", false) 86 86 87 - // Verify that Configure link is correct 88 - assert.EqualValues(t, "/user/settings#keep-activity-private", hintLink) 89 - }) 87 + // Verify that Configure link is correct 88 + assert.EqualValues(t, "/user/settings#keep-activity-private", hintLink) 90 89 } 91 90 92 91 // testChangeUserActivityVisibility allows to easily change visibility of public activity for a user
+67 -66
tests/integration/user_profile_follows_test.go
··· 6 6 import ( 7 7 "fmt" 8 8 "net/http" 9 - "net/url" 10 9 "strings" 11 10 "testing" 11 + 12 + "code.gitea.io/gitea/tests" 12 13 13 14 "github.com/stretchr/testify/assert" 14 15 ) ··· 19 20 // - Followers and Following lists have correct amounts of items 20 21 // - %d followers and %following counters are always present and always have correct numbers and use correct plurals 21 22 func TestUserProfileFollows(t *testing.T) { 22 - onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) { 23 - // This test needs 3 users to check for all possible states 24 - // The accounts of user3 and user4 are not functioning 25 - user1 := loginUser(t, "user1") 26 - user2 := loginUser(t, "user2") 27 - user5 := loginUser(t, "user5") 23 + defer tests.PrepareTestEnv(t)() 28 24 29 - followersLink := "#profile-avatar-card a[href='/user1?tab=followers']" 30 - followingLink := "#profile-avatar-card a[href='/user1?tab=following']" 31 - listHeader := ".user-cards h2" 32 - listItems := ".user-cards .list" 25 + // This test needs 3 users to check for all possible states 26 + // The accounts of user3 and user4 are not functioning 27 + user1 := loginUser(t, "user1") 28 + user2 := loginUser(t, "user2") 29 + user5 := loginUser(t, "user5") 33 30 34 - // = No follows = 31 + followersLink := "#profile-avatar-card a[href='/user1?tab=followers']" 32 + followingLink := "#profile-avatar-card a[href='/user1?tab=following']" 33 + listHeader := ".user-cards h2" 34 + listItems := ".user-cards .list" 35 + 36 + // = No follows = 35 37 36 - var followCount int 38 + var followCount int 37 39 38 - // Request the profile of user1, the Followers tab 39 - response := user1.MakeRequest(t, NewRequest(t, "GET", "/user1?tab=followers"), http.StatusOK) 40 - page := NewHTMLParser(t, response.Body) 40 + // Request the profile of user1, the Followers tab 41 + response := user1.MakeRequest(t, NewRequest(t, "GET", "/user1?tab=followers"), http.StatusOK) 42 + page := NewHTMLParser(t, response.Body) 41 43 42 - // Verify that user1 has no followers 43 - testSelectorEquals(t, page, followersLink, "0 followers") 44 - testSelectorEquals(t, page, listHeader, "Followers") 45 - testListCount(t, page, listItems, followCount) 44 + // Verify that user1 has no followers 45 + testSelectorEquals(t, page, followersLink, "0 followers") 46 + testSelectorEquals(t, page, listHeader, "Followers") 47 + testListCount(t, page, listItems, followCount) 46 48 47 - // Request the profile of user1, the Following tab 48 - response = user1.MakeRequest(t, NewRequest(t, "GET", "/user1?tab=following"), http.StatusOK) 49 - page = NewHTMLParser(t, response.Body) 49 + // Request the profile of user1, the Following tab 50 + response = user1.MakeRequest(t, NewRequest(t, "GET", "/user1?tab=following"), http.StatusOK) 51 + page = NewHTMLParser(t, response.Body) 50 52 51 - // Verify that user1 does not follow anyone 52 - testSelectorEquals(t, page, followingLink, "0 following") 53 - testSelectorEquals(t, page, listHeader, "Following") 54 - testListCount(t, page, listItems, followCount) 53 + // Verify that user1 does not follow anyone 54 + testSelectorEquals(t, page, followingLink, "0 following") 55 + testSelectorEquals(t, page, listHeader, "Following") 56 + testListCount(t, page, listItems, followCount) 55 57 56 - // Make user1 and user2 follow each other 57 - testUserFollowUser(t, user1, "user2") 58 - testUserFollowUser(t, user2, "user1") 58 + // Make user1 and user2 follow each other 59 + testUserFollowUser(t, user1, "user2") 60 + testUserFollowUser(t, user2, "user1") 59 61 60 - // = 1 follow each = 62 + // = 1 follow each = 61 63 62 - followCount++ 64 + followCount++ 63 65 64 - // Request the profile of user1, the Followers tab 65 - response = user1.MakeRequest(t, NewRequest(t, "GET", "/user1?tab=followers"), http.StatusOK) 66 - page = NewHTMLParser(t, response.Body) 66 + // Request the profile of user1, the Followers tab 67 + response = user1.MakeRequest(t, NewRequest(t, "GET", "/user1?tab=followers"), http.StatusOK) 68 + page = NewHTMLParser(t, response.Body) 67 69 68 - // Verify it is now followed by 1 user 69 - testSelectorEquals(t, page, followersLink, "1 follower") 70 - testSelectorEquals(t, page, listHeader, "Follower") 71 - testListCount(t, page, listItems, followCount) 70 + // Verify it is now followed by 1 user 71 + testSelectorEquals(t, page, followersLink, "1 follower") 72 + testSelectorEquals(t, page, listHeader, "Follower") 73 + testListCount(t, page, listItems, followCount) 72 74 73 - // Request the profile of user1, the Following tab 74 - response = user1.MakeRequest(t, NewRequest(t, "GET", "/user1?tab=following"), http.StatusOK) 75 - page = NewHTMLParser(t, response.Body) 75 + // Request the profile of user1, the Following tab 76 + response = user1.MakeRequest(t, NewRequest(t, "GET", "/user1?tab=following"), http.StatusOK) 77 + page = NewHTMLParser(t, response.Body) 76 78 77 - // Verify it now follows follows 1 user 78 - testSelectorEquals(t, page, followingLink, "1 following") 79 - testSelectorEquals(t, page, listHeader, "Following") 80 - testListCount(t, page, listItems, followCount) 79 + // Verify it now follows follows 1 user 80 + testSelectorEquals(t, page, followingLink, "1 following") 81 + testSelectorEquals(t, page, listHeader, "Following") 82 + testListCount(t, page, listItems, followCount) 81 83 82 - // Make user1 and user3 follow each other 83 - testUserFollowUser(t, user1, "user5") 84 - testUserFollowUser(t, user5, "user1") 84 + // Make user1 and user3 follow each other 85 + testUserFollowUser(t, user1, "user5") 86 + testUserFollowUser(t, user5, "user1") 85 87 86 - // = 2 follows = 88 + // = 2 follows = 87 89 88 - followCount++ 90 + followCount++ 89 91 90 - // Request the profile of user1, the Followers tab 91 - response = user1.MakeRequest(t, NewRequest(t, "GET", "/user1?tab=followers"), http.StatusOK) 92 - page = NewHTMLParser(t, response.Body) 92 + // Request the profile of user1, the Followers tab 93 + response = user1.MakeRequest(t, NewRequest(t, "GET", "/user1?tab=followers"), http.StatusOK) 94 + page = NewHTMLParser(t, response.Body) 93 95 94 - // Verify it is now followed by 2 users 95 - testSelectorEquals(t, page, followersLink, "2 followers") 96 - testSelectorEquals(t, page, listHeader, "Followers") 97 - testListCount(t, page, listItems, followCount) 96 + // Verify it is now followed by 2 users 97 + testSelectorEquals(t, page, followersLink, "2 followers") 98 + testSelectorEquals(t, page, listHeader, "Followers") 99 + testListCount(t, page, listItems, followCount) 98 100 99 - // Request the profile of user1, the Following tab 100 - response = user1.MakeRequest(t, NewRequest(t, "GET", "/user1?tab=following"), http.StatusOK) 101 - page = NewHTMLParser(t, response.Body) 101 + // Request the profile of user1, the Following tab 102 + response = user1.MakeRequest(t, NewRequest(t, "GET", "/user1?tab=following"), http.StatusOK) 103 + page = NewHTMLParser(t, response.Body) 102 104 103 - // Verify it now follows follows 2 users 104 - testSelectorEquals(t, page, followingLink, "2 following") 105 - testSelectorEquals(t, page, listHeader, "Following") 106 - testListCount(t, page, listItems, followCount) 107 - }) 105 + // Verify it now follows follows 2 users 106 + testSelectorEquals(t, page, followingLink, "2 following") 107 + testSelectorEquals(t, page, listHeader, "Following") 108 + testListCount(t, page, listItems, followCount) 108 109 } 109 110 110 111 // testUserFollowUser simply follows a user `following` by session of user `follower`