diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-11-19 12:41:40 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-19 12:41:40 +0100 |
commit | 7a0347315995b25bcb2dca4786504fb699b5f004 (patch) | |
tree | 803dfd39286216fd0521ad16539ffd9fc5f87fc0 /models/org_test.go | |
parent | Prevent double sanitize (#16386) (diff) | |
download | forgejo-7a0347315995b25bcb2dca4786504fb699b5f004.tar.xz forgejo-7a0347315995b25bcb2dca4786504fb699b5f004.zip |
Use a standalone struct name for Organization (#17632)
* Use a standalone struct name for Organization
* recover unnecessary change
* make the code readable
* Fix template failure
* Fix template failure
* Move HasMemberWithUserID to org
* Fix test
* Remove unnecessary user type check
* Fix test
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Diffstat (limited to 'models/org_test.go')
-rw-r--r-- | models/org_test.go | 112 |
1 files changed, 57 insertions, 55 deletions
diff --git a/models/org_test.go b/models/org_test.go index 8580702a06..10ae877458 100644 --- a/models/org_test.go +++ b/models/org_test.go @@ -29,7 +29,7 @@ func TestUser_IsOwnedBy(t *testing.T) { {2, 2, false}, // user2 is not an organization {2, 3, false}, } { - org := unittest.AssertExistsAndLoadBean(t, &User{ID: testCase.OrgID}).(*User) + org := unittest.AssertExistsAndLoadBean(t, &Organization{ID: testCase.OrgID}).(*Organization) isOwner, err := org.IsOwnedBy(testCase.UserID) assert.NoError(t, err) assert.Equal(t, testCase.ExpectedOwner, isOwner) @@ -50,7 +50,7 @@ func TestUser_IsOrgMember(t *testing.T) { {2, 2, false}, // user2 is not an organization {2, 3, false}, } { - org := unittest.AssertExistsAndLoadBean(t, &User{ID: testCase.OrgID}).(*User) + org := unittest.AssertExistsAndLoadBean(t, &Organization{ID: testCase.OrgID}).(*Organization) isMember, err := org.IsOrgMember(testCase.UserID) assert.NoError(t, err) assert.Equal(t, testCase.ExpectedMember, isMember) @@ -59,7 +59,7 @@ func TestUser_IsOrgMember(t *testing.T) { func TestUser_GetTeam(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - org := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + org := unittest.AssertExistsAndLoadBean(t, &Organization{ID: 3}).(*Organization) team, err := org.GetTeam("team1") assert.NoError(t, err) assert.Equal(t, org.ID, team.OrgID) @@ -68,56 +68,58 @@ func TestUser_GetTeam(t *testing.T) { _, err = org.GetTeam("does not exist") assert.True(t, IsErrTeamNotExist(err)) - nonOrg := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + nonOrg := unittest.AssertExistsAndLoadBean(t, &Organization{ID: 2}).(*Organization) _, err = nonOrg.GetTeam("team") assert.True(t, IsErrTeamNotExist(err)) } func TestUser_GetOwnerTeam(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - org := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + org := unittest.AssertExistsAndLoadBean(t, &Organization{ID: 3}).(*Organization) team, err := org.GetOwnerTeam() assert.NoError(t, err) assert.Equal(t, org.ID, team.OrgID) - nonOrg := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + nonOrg := unittest.AssertExistsAndLoadBean(t, &Organization{ID: 2}).(*Organization) _, err = nonOrg.GetOwnerTeam() assert.True(t, IsErrTeamNotExist(err)) } func TestUser_GetTeams(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - org := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) - assert.NoError(t, org.LoadTeams()) - if assert.Len(t, org.Teams, 4) { - assert.Equal(t, int64(1), org.Teams[0].ID) - assert.Equal(t, int64(2), org.Teams[1].ID) - assert.Equal(t, int64(12), org.Teams[2].ID) - assert.Equal(t, int64(7), org.Teams[3].ID) + org := unittest.AssertExistsAndLoadBean(t, &Organization{ID: 3}).(*Organization) + teams, err := org.LoadTeams() + assert.NoError(t, err) + if assert.Len(t, teams, 4) { + assert.Equal(t, int64(1), teams[0].ID) + assert.Equal(t, int64(2), teams[1].ID) + assert.Equal(t, int64(12), teams[2].ID) + assert.Equal(t, int64(7), teams[3].ID) } } func TestUser_GetMembers(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - org := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) - assert.NoError(t, org.GetMembers()) - if assert.Len(t, org.Members, 3) { - assert.Equal(t, int64(2), org.Members[0].ID) - assert.Equal(t, int64(28), org.Members[1].ID) - assert.Equal(t, int64(4), org.Members[2].ID) + org := unittest.AssertExistsAndLoadBean(t, &Organization{ID: 3}).(*Organization) + members, _, err := org.GetMembers() + assert.NoError(t, err) + if assert.Len(t, members, 3) { + assert.Equal(t, int64(2), members[0].ID) + assert.Equal(t, int64(28), members[1].ID) + assert.Equal(t, int64(4), members[2].ID) } } func TestUser_AddMember(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - org := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + org := unittest.AssertExistsAndLoadBean(t, &Organization{ID: 3}).(*Organization) // add a user that is not a member unittest.AssertNotExistsBean(t, &OrgUser{UID: 5, OrgID: 3}) prevNumMembers := org.NumMembers assert.NoError(t, org.AddMember(5)) unittest.AssertExistsAndLoadBean(t, &OrgUser{UID: 5, OrgID: 3}) - org = unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + org = unittest.AssertExistsAndLoadBean(t, &Organization{ID: 3}).(*Organization) assert.Equal(t, prevNumMembers+1, org.NumMembers) // add a user that is already a member @@ -125,7 +127,7 @@ func TestUser_AddMember(t *testing.T) { prevNumMembers = org.NumMembers assert.NoError(t, org.AddMember(4)) unittest.AssertExistsAndLoadBean(t, &OrgUser{UID: 4, OrgID: 3}) - org = unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + org = unittest.AssertExistsAndLoadBean(t, &Organization{ID: 3}).(*Organization) assert.Equal(t, prevNumMembers, org.NumMembers) unittest.CheckConsistencyFor(t, &User{}) @@ -133,14 +135,14 @@ func TestUser_AddMember(t *testing.T) { func TestUser_RemoveMember(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - org := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + org := unittest.AssertExistsAndLoadBean(t, &Organization{ID: 3}).(*Organization) // remove a user that is a member unittest.AssertExistsAndLoadBean(t, &OrgUser{UID: 4, OrgID: 3}) prevNumMembers := org.NumMembers assert.NoError(t, org.RemoveMember(4)) unittest.AssertNotExistsBean(t, &OrgUser{UID: 4, OrgID: 3}) - org = unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + org = unittest.AssertExistsAndLoadBean(t, &Organization{ID: 3}).(*Organization) assert.Equal(t, prevNumMembers-1, org.NumMembers) // remove a user that is not a member @@ -148,7 +150,7 @@ func TestUser_RemoveMember(t *testing.T) { prevNumMembers = org.NumMembers assert.NoError(t, org.RemoveMember(5)) unittest.AssertNotExistsBean(t, &OrgUser{UID: 5, OrgID: 3}) - org = unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + org = unittest.AssertExistsAndLoadBean(t, &Organization{ID: 3}).(*Organization) assert.Equal(t, prevNumMembers, org.NumMembers) unittest.CheckConsistencyFor(t, &User{}, &Team{}) @@ -156,7 +158,7 @@ func TestUser_RemoveMember(t *testing.T) { func TestUser_RemoveOrgRepo(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - org := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + org := unittest.AssertExistsAndLoadBean(t, &Organization{ID: 3}).(*Organization) repo := unittest.AssertExistsAndLoadBean(t, &Repository{OwnerID: org.ID}).(*Repository) // remove a repo that does belong to org @@ -183,14 +185,14 @@ func TestCreateOrganization(t *testing.T) { owner := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) const newOrgName = "neworg" - org := &User{ + org := &Organization{ Name: newOrgName, } unittest.AssertNotExistsBean(t, &User{Name: newOrgName, Type: UserTypeOrganization}) assert.NoError(t, CreateOrganization(org, owner)) org = unittest.AssertExistsAndLoadBean(t, - &User{Name: newOrgName, Type: UserTypeOrganization}).(*User) + &Organization{Name: newOrgName, Type: UserTypeOrganization}).(*Organization) ownerTeam := unittest.AssertExistsAndLoadBean(t, &Team{Name: ownerTeamName, OrgID: org.ID}).(*Team) unittest.AssertExistsAndLoadBean(t, &TeamUser{UID: owner.ID, TeamID: ownerTeam.ID}) @@ -203,16 +205,16 @@ func TestCreateOrganization2(t *testing.T) { owner := unittest.AssertExistsAndLoadBean(t, &User{ID: 5}).(*User) const newOrgName = "neworg" - org := &User{ + org := &Organization{ Name: newOrgName, } - unittest.AssertNotExistsBean(t, &User{Name: newOrgName, Type: UserTypeOrganization}) + unittest.AssertNotExistsBean(t, &Organization{Name: newOrgName, Type: UserTypeOrganization}) err := CreateOrganization(org, owner) assert.Error(t, err) assert.True(t, IsErrUserNotAllowedCreateOrg(err)) - unittest.AssertNotExistsBean(t, &User{Name: newOrgName, Type: UserTypeOrganization}) - unittest.CheckConsistencyFor(t, &User{}, &Team{}) + unittest.AssertNotExistsBean(t, &Organization{Name: newOrgName, Type: UserTypeOrganization}) + unittest.CheckConsistencyFor(t, &Organization{}, &Team{}) } func TestCreateOrganization3(t *testing.T) { @@ -220,7 +222,7 @@ func TestCreateOrganization3(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) owner := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) - org := &User{Name: "user3"} // should already exist + org := &Organization{Name: "user3"} // should already exist unittest.AssertExistsAndLoadBean(t, &User{Name: org.Name}) // sanity check err := CreateOrganization(org, owner) assert.Error(t, err) @@ -233,10 +235,10 @@ func TestCreateOrganization4(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) owner := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) - err := CreateOrganization(&User{Name: "assets"}, owner) + err := CreateOrganization(&Organization{Name: "assets"}, owner) assert.Error(t, err) assert.True(t, IsErrNameReserved(err)) - unittest.CheckConsistencyFor(t, &User{}, &Team{}) + unittest.CheckConsistencyFor(t, &Organization{}, &Team{}) } func TestGetOrgByName(t *testing.T) { @@ -478,7 +480,7 @@ func TestRemoveOrgUser(t *testing.T) { func TestUser_GetUserTeamIDs(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - org := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + org := unittest.AssertExistsAndLoadBean(t, &Organization{ID: 3}).(*Organization) testSuccess := func(userID int64, expected []int64) { teamIDs, err := org.GetUserTeamIDs(userID) assert.NoError(t, err) @@ -491,7 +493,7 @@ func TestUser_GetUserTeamIDs(t *testing.T) { func TestAccessibleReposEnv_CountRepos(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - org := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + org := unittest.AssertExistsAndLoadBean(t, &Organization{ID: 3}).(*Organization) testSuccess := func(userID, expectedCount int64) { env, err := org.AccessibleReposEnv(userID) assert.NoError(t, err) @@ -505,7 +507,7 @@ func TestAccessibleReposEnv_CountRepos(t *testing.T) { func TestAccessibleReposEnv_RepoIDs(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - org := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + org := unittest.AssertExistsAndLoadBean(t, &Organization{ID: 3}).(*Organization) testSuccess := func(userID, _, pageSize int64, expectedRepoIDs []int64) { env, err := org.AccessibleReposEnv(userID) assert.NoError(t, err) @@ -519,7 +521,7 @@ func TestAccessibleReposEnv_RepoIDs(t *testing.T) { func TestAccessibleReposEnv_Repos(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - org := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + org := unittest.AssertExistsAndLoadBean(t, &Organization{ID: 3}).(*Organization) testSuccess := func(userID int64, expectedRepoIDs []int64) { env, err := org.AccessibleReposEnv(userID) assert.NoError(t, err) @@ -538,7 +540,7 @@ func TestAccessibleReposEnv_Repos(t *testing.T) { func TestAccessibleReposEnv_MirrorRepos(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - org := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + org := unittest.AssertExistsAndLoadBean(t, &Organization{ID: 3}).(*Organization) testSuccess := func(userID int64, expectedRepoIDs []int64) { env, err := org.AccessibleReposEnv(userID) assert.NoError(t, err) @@ -561,7 +563,7 @@ func TestHasOrgVisibleTypePublic(t *testing.T) { user3 := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) const newOrgName = "test-org-public" - org := &User{ + org := &Organization{ Name: newOrgName, Visibility: structs.VisibleTypePublic, } @@ -569,10 +571,10 @@ func TestHasOrgVisibleTypePublic(t *testing.T) { unittest.AssertNotExistsBean(t, &User{Name: org.Name, Type: UserTypeOrganization}) assert.NoError(t, CreateOrganization(org, owner)) org = unittest.AssertExistsAndLoadBean(t, - &User{Name: org.Name, Type: UserTypeOrganization}).(*User) - test1 := HasOrgOrUserVisible(org, owner) - test2 := HasOrgOrUserVisible(org, user3) - test3 := HasOrgOrUserVisible(org, nil) + &Organization{Name: org.Name, Type: UserTypeOrganization}).(*Organization) + test1 := HasOrgOrUserVisible(org.AsUser(), owner) + test2 := HasOrgOrUserVisible(org.AsUser(), user3) + test3 := HasOrgOrUserVisible(org.AsUser(), nil) assert.True(t, test1) // owner of org assert.True(t, test2) // user not a part of org assert.True(t, test3) // logged out user @@ -584,7 +586,7 @@ func TestHasOrgVisibleTypeLimited(t *testing.T) { user3 := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) const newOrgName = "test-org-limited" - org := &User{ + org := &Organization{ Name: newOrgName, Visibility: structs.VisibleTypeLimited, } @@ -592,10 +594,10 @@ func TestHasOrgVisibleTypeLimited(t *testing.T) { unittest.AssertNotExistsBean(t, &User{Name: org.Name, Type: UserTypeOrganization}) assert.NoError(t, CreateOrganization(org, owner)) org = unittest.AssertExistsAndLoadBean(t, - &User{Name: org.Name, Type: UserTypeOrganization}).(*User) - test1 := HasOrgOrUserVisible(org, owner) - test2 := HasOrgOrUserVisible(org, user3) - test3 := HasOrgOrUserVisible(org, nil) + &Organization{Name: org.Name, Type: UserTypeOrganization}).(*Organization) + test1 := HasOrgOrUserVisible(org.AsUser(), owner) + test2 := HasOrgOrUserVisible(org.AsUser(), user3) + test3 := HasOrgOrUserVisible(org.AsUser(), nil) assert.True(t, test1) // owner of org assert.True(t, test2) // user not a part of org assert.False(t, test3) // logged out user @@ -607,7 +609,7 @@ func TestHasOrgVisibleTypePrivate(t *testing.T) { user3 := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) const newOrgName = "test-org-private" - org := &User{ + org := &Organization{ Name: newOrgName, Visibility: structs.VisibleTypePrivate, } @@ -615,10 +617,10 @@ func TestHasOrgVisibleTypePrivate(t *testing.T) { unittest.AssertNotExistsBean(t, &User{Name: org.Name, Type: UserTypeOrganization}) assert.NoError(t, CreateOrganization(org, owner)) org = unittest.AssertExistsAndLoadBean(t, - &User{Name: org.Name, Type: UserTypeOrganization}).(*User) - test1 := HasOrgOrUserVisible(org, owner) - test2 := HasOrgOrUserVisible(org, user3) - test3 := HasOrgOrUserVisible(org, nil) + &Organization{Name: org.Name, Type: UserTypeOrganization}).(*Organization) + test1 := HasOrgOrUserVisible(org.AsUser(), owner) + test2 := HasOrgOrUserVisible(org.AsUser(), user3) + test3 := HasOrgOrUserVisible(org.AsUser(), nil) assert.True(t, test1) // owner of org assert.False(t, test2) // user not a part of org assert.False(t, test3) // logged out user |