You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

org_test.go 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "testing"
  7. "code.gitea.io/gitea/models/db"
  8. "code.gitea.io/gitea/models/organization"
  9. repo_model "code.gitea.io/gitea/models/repo"
  10. "code.gitea.io/gitea/models/unittest"
  11. user_model "code.gitea.io/gitea/models/user"
  12. "code.gitea.io/gitea/modules/setting"
  13. "github.com/stretchr/testify/assert"
  14. )
  15. func TestUser_RemoveMember(t *testing.T) {
  16. assert.NoError(t, unittest.PrepareTestDatabase())
  17. org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3}).(*organization.Organization)
  18. // remove a user that is a member
  19. unittest.AssertExistsAndLoadBean(t, &organization.OrgUser{UID: 4, OrgID: 3})
  20. prevNumMembers := org.NumMembers
  21. assert.NoError(t, RemoveOrgUser(org.ID, 4))
  22. unittest.AssertNotExistsBean(t, &organization.OrgUser{UID: 4, OrgID: 3})
  23. org = unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3}).(*organization.Organization)
  24. assert.Equal(t, prevNumMembers-1, org.NumMembers)
  25. // remove a user that is not a member
  26. unittest.AssertNotExistsBean(t, &organization.OrgUser{UID: 5, OrgID: 3})
  27. prevNumMembers = org.NumMembers
  28. assert.NoError(t, RemoveOrgUser(org.ID, 5))
  29. unittest.AssertNotExistsBean(t, &organization.OrgUser{UID: 5, OrgID: 3})
  30. org = unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3}).(*organization.Organization)
  31. assert.Equal(t, prevNumMembers, org.NumMembers)
  32. unittest.CheckConsistencyFor(t, &user_model.User{}, &organization.Team{})
  33. }
  34. func TestRemoveOrgUser(t *testing.T) {
  35. assert.NoError(t, unittest.PrepareTestDatabase())
  36. testSuccess := func(orgID, userID int64) {
  37. org := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: orgID}).(*user_model.User)
  38. expectedNumMembers := org.NumMembers
  39. if unittest.BeanExists(t, &organization.OrgUser{OrgID: orgID, UID: userID}) {
  40. expectedNumMembers--
  41. }
  42. assert.NoError(t, RemoveOrgUser(orgID, userID))
  43. unittest.AssertNotExistsBean(t, &organization.OrgUser{OrgID: orgID, UID: userID})
  44. org = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: orgID}).(*user_model.User)
  45. assert.EqualValues(t, expectedNumMembers, org.NumMembers)
  46. }
  47. testSuccess(3, 4)
  48. testSuccess(3, 4)
  49. err := RemoveOrgUser(7, 5)
  50. assert.Error(t, err)
  51. assert.True(t, organization.IsErrLastOrgOwner(err))
  52. unittest.AssertExistsAndLoadBean(t, &organization.OrgUser{OrgID: 7, UID: 5})
  53. unittest.CheckConsistencyFor(t, &user_model.User{}, &organization.Team{})
  54. }
  55. func TestUser_RemoveOrgRepo(t *testing.T) {
  56. assert.NoError(t, unittest.PrepareTestDatabase())
  57. org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3}).(*organization.Organization)
  58. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerID: org.ID}).(*repo_model.Repository)
  59. // remove a repo that does belong to org
  60. unittest.AssertExistsAndLoadBean(t, &organization.TeamRepo{RepoID: repo.ID, OrgID: org.ID})
  61. assert.NoError(t, organization.RemoveOrgRepo(db.DefaultContext, org.ID, repo.ID))
  62. unittest.AssertNotExistsBean(t, &organization.TeamRepo{RepoID: repo.ID, OrgID: org.ID})
  63. unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: repo.ID}) // repo should still exist
  64. // remove a repo that does not belong to org
  65. assert.NoError(t, organization.RemoveOrgRepo(db.DefaultContext, org.ID, repo.ID))
  66. unittest.AssertNotExistsBean(t, &organization.TeamRepo{RepoID: repo.ID, OrgID: org.ID})
  67. assert.NoError(t, organization.RemoveOrgRepo(db.DefaultContext, org.ID, unittest.NonexistentID))
  68. unittest.CheckConsistencyFor(t,
  69. &user_model.User{ID: org.ID},
  70. &organization.Team{OrgID: org.ID},
  71. &repo_model.Repository{ID: repo.ID})
  72. }
  73. func TestCreateOrganization(t *testing.T) {
  74. // successful creation of org
  75. assert.NoError(t, unittest.PrepareTestDatabase())
  76. owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
  77. const newOrgName = "neworg"
  78. org := &organization.Organization{
  79. Name: newOrgName,
  80. }
  81. unittest.AssertNotExistsBean(t, &user_model.User{Name: newOrgName, Type: user_model.UserTypeOrganization})
  82. assert.NoError(t, organization.CreateOrganization(org, owner))
  83. org = unittest.AssertExistsAndLoadBean(t,
  84. &organization.Organization{Name: newOrgName, Type: user_model.UserTypeOrganization}).(*organization.Organization)
  85. ownerTeam := unittest.AssertExistsAndLoadBean(t,
  86. &organization.Team{Name: organization.OwnerTeamName, OrgID: org.ID}).(*organization.Team)
  87. unittest.AssertExistsAndLoadBean(t, &organization.TeamUser{UID: owner.ID, TeamID: ownerTeam.ID})
  88. unittest.CheckConsistencyFor(t, &user_model.User{}, &organization.Team{})
  89. }
  90. func TestCreateOrganization2(t *testing.T) {
  91. // unauthorized creation of org
  92. assert.NoError(t, unittest.PrepareTestDatabase())
  93. owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 5}).(*user_model.User)
  94. const newOrgName = "neworg"
  95. org := &organization.Organization{
  96. Name: newOrgName,
  97. }
  98. unittest.AssertNotExistsBean(t, &organization.Organization{Name: newOrgName, Type: user_model.UserTypeOrganization})
  99. err := organization.CreateOrganization(org, owner)
  100. assert.Error(t, err)
  101. assert.True(t, organization.IsErrUserNotAllowedCreateOrg(err))
  102. unittest.AssertNotExistsBean(t, &organization.Organization{Name: newOrgName, Type: user_model.UserTypeOrganization})
  103. unittest.CheckConsistencyFor(t, &organization.Organization{}, &organization.Team{})
  104. }
  105. func TestCreateOrganization3(t *testing.T) {
  106. // create org with same name as existent org
  107. assert.NoError(t, unittest.PrepareTestDatabase())
  108. owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
  109. org := &organization.Organization{Name: "user3"} // should already exist
  110. unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: org.Name}) // sanity check
  111. err := organization.CreateOrganization(org, owner)
  112. assert.Error(t, err)
  113. assert.True(t, user_model.IsErrUserAlreadyExist(err))
  114. unittest.CheckConsistencyFor(t, &user_model.User{}, &organization.Team{})
  115. }
  116. func TestCreateOrganization4(t *testing.T) {
  117. // create org with unusable name
  118. assert.NoError(t, unittest.PrepareTestDatabase())
  119. owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
  120. err := organization.CreateOrganization(&organization.Organization{Name: "assets"}, owner)
  121. assert.Error(t, err)
  122. assert.True(t, db.IsErrNameReserved(err))
  123. unittest.CheckConsistencyFor(t, &organization.Organization{}, &organization.Team{})
  124. }
  125. func TestAddOrgUser(t *testing.T) {
  126. assert.NoError(t, unittest.PrepareTestDatabase())
  127. testSuccess := func(orgID, userID int64, isPublic bool) {
  128. org := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: orgID}).(*user_model.User)
  129. expectedNumMembers := org.NumMembers
  130. if !unittest.BeanExists(t, &organization.OrgUser{OrgID: orgID, UID: userID}) {
  131. expectedNumMembers++
  132. }
  133. assert.NoError(t, organization.AddOrgUser(orgID, userID))
  134. ou := &organization.OrgUser{OrgID: orgID, UID: userID}
  135. unittest.AssertExistsAndLoadBean(t, ou)
  136. assert.Equal(t, isPublic, ou.IsPublic)
  137. org = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: orgID}).(*user_model.User)
  138. assert.EqualValues(t, expectedNumMembers, org.NumMembers)
  139. }
  140. setting.Service.DefaultOrgMemberVisible = false
  141. testSuccess(3, 5, false)
  142. testSuccess(3, 5, false)
  143. testSuccess(6, 2, false)
  144. setting.Service.DefaultOrgMemberVisible = true
  145. testSuccess(6, 3, true)
  146. unittest.CheckConsistencyFor(t, &user_model.User{}, &organization.Team{})
  147. }