code.gitea.io/gitea@v1.21.7/models/organization/org_user_test.go (about) 1 // Copyright 2022 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package organization_test 5 6 import ( 7 "fmt" 8 "testing" 9 10 "code.gitea.io/gitea/models/db" 11 "code.gitea.io/gitea/models/organization" 12 "code.gitea.io/gitea/models/unittest" 13 user_model "code.gitea.io/gitea/models/user" 14 "code.gitea.io/gitea/modules/setting" 15 16 "github.com/stretchr/testify/assert" 17 ) 18 19 func TestUserIsPublicMember(t *testing.T) { 20 assert.NoError(t, unittest.PrepareTestDatabase()) 21 22 tt := []struct { 23 uid int64 24 orgid int64 25 expected bool 26 }{ 27 {2, 3, true}, 28 {4, 3, false}, 29 {5, 6, true}, 30 {5, 7, false}, 31 } 32 for _, v := range tt { 33 t.Run(fmt.Sprintf("UserId%dIsPublicMemberOf%d", v.uid, v.orgid), func(t *testing.T) { 34 testUserIsPublicMember(t, v.uid, v.orgid, v.expected) 35 }) 36 } 37 } 38 39 func testUserIsPublicMember(t *testing.T, uid, orgID int64, expected bool) { 40 user, err := user_model.GetUserByID(db.DefaultContext, uid) 41 assert.NoError(t, err) 42 is, err := organization.IsPublicMembership(orgID, user.ID) 43 assert.NoError(t, err) 44 assert.Equal(t, expected, is) 45 } 46 47 func TestIsUserOrgOwner(t *testing.T) { 48 assert.NoError(t, unittest.PrepareTestDatabase()) 49 50 tt := []struct { 51 uid int64 52 orgid int64 53 expected bool 54 }{ 55 {2, 3, true}, 56 {4, 3, false}, 57 {5, 6, true}, 58 {5, 7, true}, 59 } 60 for _, v := range tt { 61 t.Run(fmt.Sprintf("UserId%dIsOrgOwnerOf%d", v.uid, v.orgid), func(t *testing.T) { 62 testIsUserOrgOwner(t, v.uid, v.orgid, v.expected) 63 }) 64 } 65 } 66 67 func testIsUserOrgOwner(t *testing.T, uid, orgID int64, expected bool) { 68 user, err := user_model.GetUserByID(db.DefaultContext, uid) 69 assert.NoError(t, err) 70 is, err := organization.IsOrganizationOwner(db.DefaultContext, orgID, user.ID) 71 assert.NoError(t, err) 72 assert.Equal(t, expected, is) 73 } 74 75 func TestUserListIsPublicMember(t *testing.T) { 76 assert.NoError(t, unittest.PrepareTestDatabase()) 77 tt := []struct { 78 orgid int64 79 expected map[int64]bool 80 }{ 81 {3, map[int64]bool{2: true, 4: false, 28: true}}, 82 {6, map[int64]bool{5: true, 28: true}}, 83 {7, map[int64]bool{5: false}}, 84 {25, map[int64]bool{24: true}}, 85 {22, map[int64]bool{}}, 86 } 87 for _, v := range tt { 88 t.Run(fmt.Sprintf("IsPublicMemberOfOrgId%d", v.orgid), func(t *testing.T) { 89 testUserListIsPublicMember(t, v.orgid, v.expected) 90 }) 91 } 92 } 93 94 func testUserListIsPublicMember(t *testing.T, orgID int64, expected map[int64]bool) { 95 org, err := organization.GetOrgByID(db.DefaultContext, orgID) 96 assert.NoError(t, err) 97 _, membersIsPublic, err := org.GetMembers(db.DefaultContext) 98 assert.NoError(t, err) 99 assert.Equal(t, expected, membersIsPublic) 100 } 101 102 func TestUserListIsUserOrgOwner(t *testing.T) { 103 assert.NoError(t, unittest.PrepareTestDatabase()) 104 tt := []struct { 105 orgid int64 106 expected map[int64]bool 107 }{ 108 {3, map[int64]bool{2: true, 4: false, 28: false}}, 109 {6, map[int64]bool{5: true, 28: false}}, 110 {7, map[int64]bool{5: true}}, 111 {25, map[int64]bool{24: false}}, // ErrTeamNotExist 112 {22, map[int64]bool{}}, // No member 113 } 114 for _, v := range tt { 115 t.Run(fmt.Sprintf("IsUserOrgOwnerOfOrgId%d", v.orgid), func(t *testing.T) { 116 testUserListIsUserOrgOwner(t, v.orgid, v.expected) 117 }) 118 } 119 } 120 121 func testUserListIsUserOrgOwner(t *testing.T, orgID int64, expected map[int64]bool) { 122 org, err := organization.GetOrgByID(db.DefaultContext, orgID) 123 assert.NoError(t, err) 124 members, _, err := org.GetMembers(db.DefaultContext) 125 assert.NoError(t, err) 126 assert.Equal(t, expected, organization.IsUserOrgOwner(members, orgID)) 127 } 128 129 func TestAddOrgUser(t *testing.T) { 130 assert.NoError(t, unittest.PrepareTestDatabase()) 131 testSuccess := func(orgID, userID int64, isPublic bool) { 132 org := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: orgID}) 133 expectedNumMembers := org.NumMembers 134 if !unittest.BeanExists(t, &organization.OrgUser{OrgID: orgID, UID: userID}) { 135 expectedNumMembers++ 136 } 137 assert.NoError(t, organization.AddOrgUser(orgID, userID)) 138 ou := &organization.OrgUser{OrgID: orgID, UID: userID} 139 unittest.AssertExistsAndLoadBean(t, ou) 140 assert.Equal(t, isPublic, ou.IsPublic) 141 org = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: orgID}) 142 assert.EqualValues(t, expectedNumMembers, org.NumMembers) 143 } 144 145 setting.Service.DefaultOrgMemberVisible = false 146 testSuccess(3, 5, false) 147 testSuccess(3, 5, false) 148 testSuccess(6, 2, false) 149 150 setting.Service.DefaultOrgMemberVisible = true 151 testSuccess(6, 3, true) 152 153 unittest.CheckConsistencyFor(t, &user_model.User{}, &organization.Team{}) 154 }