code.gitea.io/gitea@v1.21.7/models/organization/org_test.go (about) 1 // Copyright 2017 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package organization_test 5 6 import ( 7 "testing" 8 9 "code.gitea.io/gitea/models/db" 10 "code.gitea.io/gitea/models/organization" 11 repo_model "code.gitea.io/gitea/models/repo" 12 "code.gitea.io/gitea/models/unittest" 13 user_model "code.gitea.io/gitea/models/user" 14 "code.gitea.io/gitea/modules/structs" 15 16 "github.com/stretchr/testify/assert" 17 ) 18 19 func TestUser_IsOwnedBy(t *testing.T) { 20 assert.NoError(t, unittest.PrepareTestDatabase()) 21 for _, testCase := range []struct { 22 OrgID int64 23 UserID int64 24 ExpectedOwner bool 25 }{ 26 {3, 2, true}, 27 {3, 1, false}, 28 {3, 3, false}, 29 {3, 4, false}, 30 {2, 2, false}, // user2 is not an organization 31 {2, 3, false}, 32 } { 33 org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: testCase.OrgID}) 34 isOwner, err := org.IsOwnedBy(testCase.UserID) 35 assert.NoError(t, err) 36 assert.Equal(t, testCase.ExpectedOwner, isOwner) 37 } 38 } 39 40 func TestUser_IsOrgMember(t *testing.T) { 41 assert.NoError(t, unittest.PrepareTestDatabase()) 42 for _, testCase := range []struct { 43 OrgID int64 44 UserID int64 45 ExpectedMember bool 46 }{ 47 {3, 2, true}, 48 {3, 4, true}, 49 {3, 1, false}, 50 {3, 3, false}, 51 {2, 2, false}, // user2 is not an organization 52 {2, 3, false}, 53 } { 54 org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: testCase.OrgID}) 55 isMember, err := org.IsOrgMember(testCase.UserID) 56 assert.NoError(t, err) 57 assert.Equal(t, testCase.ExpectedMember, isMember) 58 } 59 } 60 61 func TestUser_GetTeam(t *testing.T) { 62 assert.NoError(t, unittest.PrepareTestDatabase()) 63 org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3}) 64 team, err := org.GetTeam(db.DefaultContext, "team1") 65 assert.NoError(t, err) 66 assert.Equal(t, org.ID, team.OrgID) 67 assert.Equal(t, "team1", team.LowerName) 68 69 _, err = org.GetTeam(db.DefaultContext, "does not exist") 70 assert.True(t, organization.IsErrTeamNotExist(err)) 71 72 nonOrg := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 2}) 73 _, err = nonOrg.GetTeam(db.DefaultContext, "team") 74 assert.True(t, organization.IsErrTeamNotExist(err)) 75 } 76 77 func TestUser_GetOwnerTeam(t *testing.T) { 78 assert.NoError(t, unittest.PrepareTestDatabase()) 79 org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3}) 80 team, err := org.GetOwnerTeam(db.DefaultContext) 81 assert.NoError(t, err) 82 assert.Equal(t, org.ID, team.OrgID) 83 84 nonOrg := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 2}) 85 _, err = nonOrg.GetOwnerTeam(db.DefaultContext) 86 assert.True(t, organization.IsErrTeamNotExist(err)) 87 } 88 89 func TestUser_GetTeams(t *testing.T) { 90 assert.NoError(t, unittest.PrepareTestDatabase()) 91 org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3}) 92 teams, err := org.LoadTeams() 93 assert.NoError(t, err) 94 if assert.Len(t, teams, 5) { 95 assert.Equal(t, int64(1), teams[0].ID) 96 assert.Equal(t, int64(2), teams[1].ID) 97 assert.Equal(t, int64(12), teams[2].ID) 98 assert.Equal(t, int64(14), teams[3].ID) 99 assert.Equal(t, int64(7), teams[4].ID) 100 } 101 } 102 103 func TestUser_GetMembers(t *testing.T) { 104 assert.NoError(t, unittest.PrepareTestDatabase()) 105 org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3}) 106 members, _, err := org.GetMembers(db.DefaultContext) 107 assert.NoError(t, err) 108 if assert.Len(t, members, 3) { 109 assert.Equal(t, int64(2), members[0].ID) 110 assert.Equal(t, int64(28), members[1].ID) 111 assert.Equal(t, int64(4), members[2].ID) 112 } 113 } 114 115 func TestGetOrgByName(t *testing.T) { 116 assert.NoError(t, unittest.PrepareTestDatabase()) 117 118 org, err := organization.GetOrgByName(db.DefaultContext, "org3") 119 assert.NoError(t, err) 120 assert.EqualValues(t, 3, org.ID) 121 assert.Equal(t, "org3", org.Name) 122 123 _, err = organization.GetOrgByName(db.DefaultContext, "user2") // user2 is an individual 124 assert.True(t, organization.IsErrOrgNotExist(err)) 125 126 _, err = organization.GetOrgByName(db.DefaultContext, "") // corner case 127 assert.True(t, organization.IsErrOrgNotExist(err)) 128 } 129 130 func TestCountOrganizations(t *testing.T) { 131 assert.NoError(t, unittest.PrepareTestDatabase()) 132 expected, err := db.GetEngine(db.DefaultContext).Where("type=?", user_model.UserTypeOrganization).Count(&organization.Organization{}) 133 assert.NoError(t, err) 134 cnt, err := organization.CountOrgs(organization.FindOrgOptions{IncludePrivate: true}) 135 assert.NoError(t, err) 136 assert.Equal(t, expected, cnt) 137 } 138 139 func TestIsOrganizationOwner(t *testing.T) { 140 assert.NoError(t, unittest.PrepareTestDatabase()) 141 test := func(orgID, userID int64, expected bool) { 142 isOwner, err := organization.IsOrganizationOwner(db.DefaultContext, orgID, userID) 143 assert.NoError(t, err) 144 assert.EqualValues(t, expected, isOwner) 145 } 146 test(3, 2, true) 147 test(3, 3, false) 148 test(6, 5, true) 149 test(6, 4, false) 150 test(unittest.NonexistentID, unittest.NonexistentID, false) 151 } 152 153 func TestIsOrganizationMember(t *testing.T) { 154 assert.NoError(t, unittest.PrepareTestDatabase()) 155 test := func(orgID, userID int64, expected bool) { 156 isMember, err := organization.IsOrganizationMember(db.DefaultContext, orgID, userID) 157 assert.NoError(t, err) 158 assert.EqualValues(t, expected, isMember) 159 } 160 test(3, 2, true) 161 test(3, 3, false) 162 test(3, 4, true) 163 test(6, 5, true) 164 test(6, 4, false) 165 test(unittest.NonexistentID, unittest.NonexistentID, false) 166 } 167 168 func TestIsPublicMembership(t *testing.T) { 169 assert.NoError(t, unittest.PrepareTestDatabase()) 170 test := func(orgID, userID int64, expected bool) { 171 isMember, err := organization.IsPublicMembership(orgID, userID) 172 assert.NoError(t, err) 173 assert.EqualValues(t, expected, isMember) 174 } 175 test(3, 2, true) 176 test(3, 3, false) 177 test(3, 4, false) 178 test(6, 5, true) 179 test(6, 4, false) 180 test(unittest.NonexistentID, unittest.NonexistentID, false) 181 } 182 183 func TestFindOrgs(t *testing.T) { 184 assert.NoError(t, unittest.PrepareTestDatabase()) 185 186 orgs, err := organization.FindOrgs(organization.FindOrgOptions{ 187 UserID: 4, 188 IncludePrivate: true, 189 }) 190 assert.NoError(t, err) 191 if assert.Len(t, orgs, 1) { 192 assert.EqualValues(t, 3, orgs[0].ID) 193 } 194 195 orgs, err = organization.FindOrgs(organization.FindOrgOptions{ 196 UserID: 4, 197 IncludePrivate: false, 198 }) 199 assert.NoError(t, err) 200 assert.Len(t, orgs, 0) 201 202 total, err := organization.CountOrgs(organization.FindOrgOptions{ 203 UserID: 4, 204 IncludePrivate: true, 205 }) 206 assert.NoError(t, err) 207 assert.EqualValues(t, 1, total) 208 } 209 210 func TestGetOrgUsersByOrgID(t *testing.T) { 211 assert.NoError(t, unittest.PrepareTestDatabase()) 212 213 orgUsers, err := organization.GetOrgUsersByOrgID(db.DefaultContext, &organization.FindOrgMembersOpts{ 214 ListOptions: db.ListOptions{}, 215 OrgID: 3, 216 PublicOnly: false, 217 }) 218 assert.NoError(t, err) 219 if assert.Len(t, orgUsers, 3) { 220 assert.Equal(t, organization.OrgUser{ 221 ID: orgUsers[0].ID, 222 OrgID: 3, 223 UID: 2, 224 IsPublic: true, 225 }, *orgUsers[0]) 226 assert.Equal(t, organization.OrgUser{ 227 ID: orgUsers[1].ID, 228 OrgID: 3, 229 UID: 4, 230 IsPublic: false, 231 }, *orgUsers[1]) 232 assert.Equal(t, organization.OrgUser{ 233 ID: orgUsers[2].ID, 234 OrgID: 3, 235 UID: 28, 236 IsPublic: true, 237 }, *orgUsers[2]) 238 } 239 240 orgUsers, err = organization.GetOrgUsersByOrgID(db.DefaultContext, &organization.FindOrgMembersOpts{ 241 ListOptions: db.ListOptions{}, 242 OrgID: unittest.NonexistentID, 243 PublicOnly: false, 244 }) 245 assert.NoError(t, err) 246 assert.Len(t, orgUsers, 0) 247 } 248 249 func TestChangeOrgUserStatus(t *testing.T) { 250 assert.NoError(t, unittest.PrepareTestDatabase()) 251 252 testSuccess := func(orgID, userID int64, public bool) { 253 assert.NoError(t, organization.ChangeOrgUserStatus(orgID, userID, public)) 254 orgUser := unittest.AssertExistsAndLoadBean(t, &organization.OrgUser{OrgID: orgID, UID: userID}) 255 assert.Equal(t, public, orgUser.IsPublic) 256 } 257 258 testSuccess(3, 2, false) 259 testSuccess(3, 2, false) 260 testSuccess(3, 4, true) 261 assert.NoError(t, organization.ChangeOrgUserStatus(unittest.NonexistentID, unittest.NonexistentID, true)) 262 } 263 264 func TestUser_GetUserTeamIDs(t *testing.T) { 265 assert.NoError(t, unittest.PrepareTestDatabase()) 266 org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3}) 267 testSuccess := func(userID int64, expected []int64) { 268 teamIDs, err := org.GetUserTeamIDs(userID) 269 assert.NoError(t, err) 270 assert.Equal(t, expected, teamIDs) 271 } 272 testSuccess(2, []int64{1, 2, 14}) 273 testSuccess(4, []int64{2}) 274 testSuccess(unittest.NonexistentID, []int64{}) 275 } 276 277 func TestAccessibleReposEnv_CountRepos(t *testing.T) { 278 assert.NoError(t, unittest.PrepareTestDatabase()) 279 org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3}) 280 testSuccess := func(userID, expectedCount int64) { 281 env, err := organization.AccessibleReposEnv(db.DefaultContext, org, userID) 282 assert.NoError(t, err) 283 count, err := env.CountRepos() 284 assert.NoError(t, err) 285 assert.EqualValues(t, expectedCount, count) 286 } 287 testSuccess(2, 3) 288 testSuccess(4, 2) 289 } 290 291 func TestAccessibleReposEnv_RepoIDs(t *testing.T) { 292 assert.NoError(t, unittest.PrepareTestDatabase()) 293 org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3}) 294 testSuccess := func(userID, _, pageSize int64, expectedRepoIDs []int64) { 295 env, err := organization.AccessibleReposEnv(db.DefaultContext, org, userID) 296 assert.NoError(t, err) 297 repoIDs, err := env.RepoIDs(1, 100) 298 assert.NoError(t, err) 299 assert.Equal(t, expectedRepoIDs, repoIDs) 300 } 301 testSuccess(2, 1, 100, []int64{3, 5, 32}) 302 testSuccess(4, 0, 100, []int64{3, 32}) 303 } 304 305 func TestAccessibleReposEnv_Repos(t *testing.T) { 306 assert.NoError(t, unittest.PrepareTestDatabase()) 307 org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3}) 308 testSuccess := func(userID int64, expectedRepoIDs []int64) { 309 env, err := organization.AccessibleReposEnv(db.DefaultContext, org, userID) 310 assert.NoError(t, err) 311 repos, err := env.Repos(1, 100) 312 assert.NoError(t, err) 313 expectedRepos := make(repo_model.RepositoryList, len(expectedRepoIDs)) 314 for i, repoID := range expectedRepoIDs { 315 expectedRepos[i] = unittest.AssertExistsAndLoadBean(t, 316 &repo_model.Repository{ID: repoID}) 317 } 318 assert.Equal(t, expectedRepos, repos) 319 } 320 testSuccess(2, []int64{3, 5, 32}) 321 testSuccess(4, []int64{3, 32}) 322 } 323 324 func TestAccessibleReposEnv_MirrorRepos(t *testing.T) { 325 assert.NoError(t, unittest.PrepareTestDatabase()) 326 org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3}) 327 testSuccess := func(userID int64, expectedRepoIDs []int64) { 328 env, err := organization.AccessibleReposEnv(db.DefaultContext, org, userID) 329 assert.NoError(t, err) 330 repos, err := env.MirrorRepos() 331 assert.NoError(t, err) 332 expectedRepos := make(repo_model.RepositoryList, len(expectedRepoIDs)) 333 for i, repoID := range expectedRepoIDs { 334 expectedRepos[i] = unittest.AssertExistsAndLoadBean(t, 335 &repo_model.Repository{ID: repoID}) 336 } 337 assert.Equal(t, expectedRepos, repos) 338 } 339 testSuccess(2, []int64{5}) 340 testSuccess(4, []int64{}) 341 } 342 343 func TestHasOrgVisibleTypePublic(t *testing.T) { 344 assert.NoError(t, unittest.PrepareTestDatabase()) 345 owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) 346 org3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3}) 347 348 const newOrgName = "test-org-public" 349 org := &organization.Organization{ 350 Name: newOrgName, 351 Visibility: structs.VisibleTypePublic, 352 } 353 354 unittest.AssertNotExistsBean(t, &user_model.User{Name: org.Name, Type: user_model.UserTypeOrganization}) 355 assert.NoError(t, organization.CreateOrganization(org, owner)) 356 org = unittest.AssertExistsAndLoadBean(t, 357 &organization.Organization{Name: org.Name, Type: user_model.UserTypeOrganization}) 358 test1 := organization.HasOrgOrUserVisible(db.DefaultContext, org.AsUser(), owner) 359 test2 := organization.HasOrgOrUserVisible(db.DefaultContext, org.AsUser(), org3) 360 test3 := organization.HasOrgOrUserVisible(db.DefaultContext, org.AsUser(), nil) 361 assert.True(t, test1) // owner of org 362 assert.True(t, test2) // user not a part of org 363 assert.True(t, test3) // logged out user 364 } 365 366 func TestHasOrgVisibleTypeLimited(t *testing.T) { 367 assert.NoError(t, unittest.PrepareTestDatabase()) 368 owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) 369 org3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3}) 370 371 const newOrgName = "test-org-limited" 372 org := &organization.Organization{ 373 Name: newOrgName, 374 Visibility: structs.VisibleTypeLimited, 375 } 376 377 unittest.AssertNotExistsBean(t, &user_model.User{Name: org.Name, Type: user_model.UserTypeOrganization}) 378 assert.NoError(t, organization.CreateOrganization(org, owner)) 379 org = unittest.AssertExistsAndLoadBean(t, 380 &organization.Organization{Name: org.Name, Type: user_model.UserTypeOrganization}) 381 test1 := organization.HasOrgOrUserVisible(db.DefaultContext, org.AsUser(), owner) 382 test2 := organization.HasOrgOrUserVisible(db.DefaultContext, org.AsUser(), org3) 383 test3 := organization.HasOrgOrUserVisible(db.DefaultContext, org.AsUser(), nil) 384 assert.True(t, test1) // owner of org 385 assert.True(t, test2) // user not a part of org 386 assert.False(t, test3) // logged out user 387 } 388 389 func TestHasOrgVisibleTypePrivate(t *testing.T) { 390 assert.NoError(t, unittest.PrepareTestDatabase()) 391 owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) 392 org3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3}) 393 394 const newOrgName = "test-org-private" 395 org := &organization.Organization{ 396 Name: newOrgName, 397 Visibility: structs.VisibleTypePrivate, 398 } 399 400 unittest.AssertNotExistsBean(t, &user_model.User{Name: org.Name, Type: user_model.UserTypeOrganization}) 401 assert.NoError(t, organization.CreateOrganization(org, owner)) 402 org = unittest.AssertExistsAndLoadBean(t, 403 &organization.Organization{Name: org.Name, Type: user_model.UserTypeOrganization}) 404 test1 := organization.HasOrgOrUserVisible(db.DefaultContext, org.AsUser(), owner) 405 test2 := organization.HasOrgOrUserVisible(db.DefaultContext, org.AsUser(), org3) 406 test3 := organization.HasOrgOrUserVisible(db.DefaultContext, org.AsUser(), nil) 407 assert.True(t, test1) // owner of org 408 assert.False(t, test2) // user not a part of org 409 assert.False(t, test3) // logged out user 410 } 411 412 func TestGetUsersWhoCanCreateOrgRepo(t *testing.T) { 413 assert.NoError(t, unittest.PrepareTestDatabase()) 414 415 users, err := organization.GetUsersWhoCanCreateOrgRepo(db.DefaultContext, 3) 416 assert.NoError(t, err) 417 assert.Len(t, users, 2) 418 var ids []int64 419 for i := range users { 420 ids = append(ids, users[i].ID) 421 } 422 assert.ElementsMatch(t, ids, []int64{2, 28}) 423 424 users, err = organization.GetUsersWhoCanCreateOrgRepo(db.DefaultContext, 7) 425 assert.NoError(t, err) 426 assert.Len(t, users, 1) 427 assert.NotNil(t, users[5]) 428 } 429 430 func TestUser_RemoveOrgRepo(t *testing.T) { 431 assert.NoError(t, unittest.PrepareTestDatabase()) 432 org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3}) 433 repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerID: org.ID}) 434 435 // remove a repo that does belong to org 436 unittest.AssertExistsAndLoadBean(t, &organization.TeamRepo{RepoID: repo.ID, OrgID: org.ID}) 437 assert.NoError(t, organization.RemoveOrgRepo(db.DefaultContext, org.ID, repo.ID)) 438 unittest.AssertNotExistsBean(t, &organization.TeamRepo{RepoID: repo.ID, OrgID: org.ID}) 439 unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: repo.ID}) // repo should still exist 440 441 // remove a repo that does not belong to org 442 assert.NoError(t, organization.RemoveOrgRepo(db.DefaultContext, org.ID, repo.ID)) 443 unittest.AssertNotExistsBean(t, &organization.TeamRepo{RepoID: repo.ID, OrgID: org.ID}) 444 445 assert.NoError(t, organization.RemoveOrgRepo(db.DefaultContext, org.ID, unittest.NonexistentID)) 446 447 unittest.CheckConsistencyFor(t, 448 &user_model.User{ID: org.ID}, 449 &organization.Team{OrgID: org.ID}, 450 &repo_model.Repository{ID: repo.ID}) 451 } 452 453 func TestCreateOrganization(t *testing.T) { 454 // successful creation of org 455 assert.NoError(t, unittest.PrepareTestDatabase()) 456 457 owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) 458 const newOrgName = "neworg" 459 org := &organization.Organization{ 460 Name: newOrgName, 461 } 462 463 unittest.AssertNotExistsBean(t, &user_model.User{Name: newOrgName, Type: user_model.UserTypeOrganization}) 464 assert.NoError(t, organization.CreateOrganization(org, owner)) 465 org = unittest.AssertExistsAndLoadBean(t, 466 &organization.Organization{Name: newOrgName, Type: user_model.UserTypeOrganization}) 467 ownerTeam := unittest.AssertExistsAndLoadBean(t, 468 &organization.Team{Name: organization.OwnerTeamName, OrgID: org.ID}) 469 unittest.AssertExistsAndLoadBean(t, &organization.TeamUser{UID: owner.ID, TeamID: ownerTeam.ID}) 470 unittest.CheckConsistencyFor(t, &user_model.User{}, &organization.Team{}) 471 } 472 473 func TestCreateOrganization2(t *testing.T) { 474 // unauthorized creation of org 475 assert.NoError(t, unittest.PrepareTestDatabase()) 476 477 owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 5}) 478 const newOrgName = "neworg" 479 org := &organization.Organization{ 480 Name: newOrgName, 481 } 482 483 unittest.AssertNotExistsBean(t, &organization.Organization{Name: newOrgName, Type: user_model.UserTypeOrganization}) 484 err := organization.CreateOrganization(org, owner) 485 assert.Error(t, err) 486 assert.True(t, organization.IsErrUserNotAllowedCreateOrg(err)) 487 unittest.AssertNotExistsBean(t, &organization.Organization{Name: newOrgName, Type: user_model.UserTypeOrganization}) 488 unittest.CheckConsistencyFor(t, &organization.Organization{}, &organization.Team{}) 489 } 490 491 func TestCreateOrganization3(t *testing.T) { 492 // create org with same name as existent org 493 assert.NoError(t, unittest.PrepareTestDatabase()) 494 495 owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) 496 org := &organization.Organization{Name: "org3"} // should already exist 497 unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: org.Name}) // sanity check 498 err := organization.CreateOrganization(org, owner) 499 assert.Error(t, err) 500 assert.True(t, user_model.IsErrUserAlreadyExist(err)) 501 unittest.CheckConsistencyFor(t, &user_model.User{}, &organization.Team{}) 502 } 503 504 func TestCreateOrganization4(t *testing.T) { 505 // create org with unusable name 506 assert.NoError(t, unittest.PrepareTestDatabase()) 507 508 owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) 509 err := organization.CreateOrganization(&organization.Organization{Name: "assets"}, owner) 510 assert.Error(t, err) 511 assert.True(t, db.IsErrNameReserved(err)) 512 unittest.CheckConsistencyFor(t, &organization.Organization{}, &organization.Team{}) 513 }