loading up the forgejo repo on tangled to test page performance
at forgejo 297 lines 9.2 kB view raw
1// Copyright 2018 The Gitea Authors. All rights reserved. 2// SPDX-License-Identifier: MIT 3 4package integration 5 6import ( 7 "fmt" 8 "net/http" 9 "strings" 10 "testing" 11 12 auth_model "forgejo.org/models/auth" 13 "forgejo.org/models/db" 14 org_model "forgejo.org/models/organization" 15 "forgejo.org/models/perm" 16 unit_model "forgejo.org/models/unit" 17 "forgejo.org/models/unittest" 18 user_model "forgejo.org/models/user" 19 "forgejo.org/modules/setting" 20 api "forgejo.org/modules/structs" 21 "forgejo.org/modules/test" 22 "forgejo.org/tests" 23 24 "github.com/stretchr/testify/assert" 25) 26 27func TestAPIOrgCreate(t *testing.T) { 28 defer tests.PrepareTestEnv(t)() 29 token := getUserToken(t, "user1", auth_model.AccessTokenScopeWriteOrganization) 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) 42 43 var apiOrg api.Organization 44 DecodeJSON(t, resp, &apiOrg) 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) 52 53 unittest.AssertExistsAndLoadBean(t, &user_model.User{ 54 Name: org.UserName, 55 LowerName: strings.ToLower(org.UserName), 56 FullName: org.FullName, 57 }) 58 59 // Check owner team permission 60 ownerTeam, _ := org_model.GetOwnerTeam(db.DefaultContext, apiOrg.ID) 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 }) 73 } 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.Equal(t, org.UserName, apiOrg.Name) 80 81 req = NewRequestf(t, "GET", "/api/v1/orgs/%s/repos", org.UserName). 82 AddTokenAuth(token) 83 resp = MakeRequest(t, req, http.StatusOK) 84 85 var repos []*api.Repository 86 DecodeJSON(t, resp, &repos) 87 for _, repo := range repos { 88 assert.False(t, repo.Private) 89 } 90 91 req = NewRequestf(t, "GET", "/api/v1/orgs/%s/members", org.UserName). 92 AddTokenAuth(token) 93 resp = MakeRequest(t, req, http.StatusOK) 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.Equal(t, "user1", users[0].UserName) 100} 101 102func TestAPIOrgRename(t *testing.T) { 103 defer tests.PrepareTestEnv(t)() 104 token := getUserToken(t, "user1", auth_model.AccessTokenScopeWriteOrganization) 105 106 org := api.CreateOrgOption{ 107 UserName: "user1_org", 108 FullName: "User1's organization", 109 Description: "This organization created by user1", 110 Website: "https://try.gitea.io", 111 Location: "Shanghai", 112 Visibility: "limited", 113 } 114 req := NewRequestWithJSON(t, "POST", "/api/v1/orgs", &org). 115 AddTokenAuth(token) 116 MakeRequest(t, req, http.StatusCreated) 117 118 req = NewRequestWithJSON(t, "POST", "/api/v1/orgs/user1_org/rename", &api.RenameOrgOption{ 119 NewName: "renamed_org", 120 }).AddTokenAuth(token) 121 MakeRequest(t, req, http.StatusNoContent) 122 unittest.AssertExistsAndLoadBean(t, &org_model.Organization{Name: "renamed_org"}) 123} 124 125func TestAPIOrgEdit(t *testing.T) { 126 defer tests.PrepareTestEnv(t)() 127 session := loginUser(t, "user1") 128 129 token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteOrganization) 130 org := api.EditOrgOption{ 131 FullName: "Org3 organization new full name", 132 Description: "A new description", 133 Website: "https://try.gitea.io/new", 134 Location: "Beijing", 135 Visibility: "private", 136 } 137 req := NewRequestWithJSON(t, "PATCH", "/api/v1/orgs/org3", &org). 138 AddTokenAuth(token) 139 resp := MakeRequest(t, req, http.StatusOK) 140 141 var apiOrg api.Organization 142 DecodeJSON(t, resp, &apiOrg) 143 144 assert.Equal(t, "org3", apiOrg.Name) 145 assert.Equal(t, org.FullName, apiOrg.FullName) 146 assert.Equal(t, org.Description, apiOrg.Description) 147 assert.Equal(t, org.Website, apiOrg.Website) 148 assert.Equal(t, org.Location, apiOrg.Location) 149 assert.Equal(t, org.Visibility, apiOrg.Visibility) 150} 151 152func TestAPIOrgEditBadVisibility(t *testing.T) { 153 defer tests.PrepareTestEnv(t)() 154 session := loginUser(t, "user1") 155 156 token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteOrganization) 157 org := api.EditOrgOption{ 158 FullName: "Org3 organization new full name", 159 Description: "A new description", 160 Website: "https://try.gitea.io/new", 161 Location: "Beijing", 162 Visibility: "badvisibility", 163 } 164 req := NewRequestWithJSON(t, "PATCH", "/api/v1/orgs/org3", &org). 165 AddTokenAuth(token) 166 MakeRequest(t, req, http.StatusUnprocessableEntity) 167} 168 169func TestAPIOrgDeny(t *testing.T) { 170 defer tests.PrepareTestEnv(t)() 171 defer test.MockVariableValue(&setting.Service.RequireSignInView, true)() 172 173 orgName := "user1_org" 174 req := NewRequestf(t, "GET", "/api/v1/orgs/%s", orgName) 175 MakeRequest(t, req, http.StatusNotFound) 176 177 req = NewRequestf(t, "GET", "/api/v1/orgs/%s/repos", orgName) 178 MakeRequest(t, req, http.StatusNotFound) 179 180 req = NewRequestf(t, "GET", "/api/v1/orgs/%s/members", orgName) 181 MakeRequest(t, req, http.StatusNotFound) 182} 183 184func TestAPIGetAll(t *testing.T) { 185 defer tests.PrepareTestEnv(t)() 186 187 token := getUserToken(t, "user1", auth_model.AccessTokenScopeReadOrganization) 188 189 // accessing with a token will return all orgs 190 req := NewRequest(t, "GET", "/api/v1/orgs"). 191 AddTokenAuth(token) 192 resp := MakeRequest(t, req, http.StatusOK) 193 var apiOrgList []*api.Organization 194 195 DecodeJSON(t, resp, &apiOrgList) 196 assert.Len(t, apiOrgList, 12) 197 assert.Equal(t, "Limited Org 36", apiOrgList[1].FullName) 198 assert.Equal(t, "limited", apiOrgList[1].Visibility) 199 200 // accessing without a token will return only public orgs 201 req = NewRequest(t, "GET", "/api/v1/orgs") 202 resp = MakeRequest(t, req, http.StatusOK) 203 204 DecodeJSON(t, resp, &apiOrgList) 205 assert.Len(t, apiOrgList, 8) 206 assert.Equal(t, "org 17", apiOrgList[0].FullName) 207 assert.Equal(t, "public", apiOrgList[0].Visibility) 208} 209 210func TestAPIOrgSearchEmptyTeam(t *testing.T) { 211 defer tests.PrepareTestEnv(t)() 212 token := getUserToken(t, "user1", auth_model.AccessTokenScopeWriteOrganization) 213 orgName := "org_with_empty_team" 214 215 // create org 216 req := NewRequestWithJSON(t, "POST", "/api/v1/orgs", &api.CreateOrgOption{ 217 UserName: orgName, 218 }).AddTokenAuth(token) 219 MakeRequest(t, req, http.StatusCreated) 220 221 // create team with no member 222 req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/orgs/%s/teams", orgName), &api.CreateTeamOption{ 223 Name: "Empty", 224 IncludesAllRepositories: true, 225 Permission: "read", 226 Units: []string{"repo.code", "repo.issues", "repo.ext_issues", "repo.wiki", "repo.pulls"}, 227 }).AddTokenAuth(token) 228 MakeRequest(t, req, http.StatusCreated) 229 230 // case-insensitive search for teams that have no members 231 req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/orgs/%s/teams/search?q=%s", orgName, "empty")). 232 AddTokenAuth(token) 233 resp := MakeRequest(t, req, http.StatusOK) 234 data := struct { 235 Ok bool 236 Data []*api.Team 237 }{} 238 DecodeJSON(t, resp, &data) 239 assert.True(t, data.Ok) 240 if assert.Len(t, data.Data, 1) { 241 assert.Equal(t, "Empty", data.Data[0].Name) 242 } 243} 244 245func TestAPIOrgChangeEmail(t *testing.T) { 246 defer tests.PrepareTestEnv(t)() 247 248 session := loginUser(t, "user1") 249 token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteOrganization) 250 251 t.Run("Invalid", func(t *testing.T) { 252 newMail := "invalid" 253 settings := api.EditOrgOption{Email: &newMail} 254 255 resp := MakeRequest(t, NewRequestWithJSON(t, "PATCH", "/api/v1/orgs/org3", &settings).AddTokenAuth(token), http.StatusUnprocessableEntity) 256 257 var org *api.Organization 258 DecodeJSON(t, resp, &org) 259 260 assert.Empty(t, org.Email) 261 }) 262 263 t.Run("Valid", func(t *testing.T) { 264 newMail := "example@example.com" 265 settings := api.EditOrgOption{Email: &newMail} 266 267 resp := MakeRequest(t, NewRequestWithJSON(t, "PATCH", "/api/v1/orgs/org3", &settings).AddTokenAuth(token), http.StatusOK) 268 269 var org *api.Organization 270 DecodeJSON(t, resp, &org) 271 272 assert.Equal(t, "example@example.com", org.Email) 273 }) 274 275 t.Run("NoChange", func(t *testing.T) { 276 settings := api.EditOrgOption{} 277 278 resp := MakeRequest(t, NewRequestWithJSON(t, "PATCH", "/api/v1/orgs/org3", &settings).AddTokenAuth(token), http.StatusOK) 279 280 var org *api.Organization 281 DecodeJSON(t, resp, &org) 282 283 assert.Equal(t, "example@example.com", org.Email) 284 }) 285 286 t.Run("Empty", func(t *testing.T) { 287 newMail := "" 288 settings := api.EditOrgOption{Email: &newMail} 289 290 resp := MakeRequest(t, NewRequestWithJSON(t, "PATCH", "/api/v1/orgs/org3", &settings).AddTokenAuth(token), http.StatusOK) 291 292 var org *api.Organization 293 DecodeJSON(t, resp, &org) 294 295 assert.Empty(t, org.Email) 296 }) 297}