-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Delete project member when delete project
Signed-off-by: stonezdj <[email protected]>
- Loading branch information
stonezdj
committed
Nov 4, 2023
1 parent
064a109
commit 4749983
Showing
6 changed files
with
112 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,19 +21,24 @@ import ( | |
beegoorm "github.com/beego/beego/v2/client/orm" | ||
"github.com/stretchr/testify/suite" | ||
|
||
"github.com/goharbor/harbor/src/common" | ||
common_dao "github.com/goharbor/harbor/src/common/dao" | ||
commonmodels "github.com/goharbor/harbor/src/common/models" | ||
"github.com/goharbor/harbor/src/controller/event" | ||
"github.com/goharbor/harbor/src/controller/immutable" | ||
"github.com/goharbor/harbor/src/lib/config" | ||
"github.com/goharbor/harbor/src/lib/orm" | ||
"github.com/goharbor/harbor/src/pkg" | ||
"github.com/goharbor/harbor/src/pkg/artifact" | ||
immutableModel "github.com/goharbor/harbor/src/pkg/immutable/model" | ||
"github.com/goharbor/harbor/src/pkg/member" | ||
memberModels "github.com/goharbor/harbor/src/pkg/member/models" | ||
"github.com/goharbor/harbor/src/pkg/project" | ||
"github.com/goharbor/harbor/src/pkg/project/models" | ||
"github.com/goharbor/harbor/src/pkg/repository/model" | ||
"github.com/goharbor/harbor/src/pkg/tag" | ||
tagmodel "github.com/goharbor/harbor/src/pkg/tag/model/tag" | ||
"github.com/goharbor/harbor/src/pkg/user" | ||
) | ||
|
||
// ProjectHandlerTestSuite is test suite for artifact handler. | ||
|
@@ -81,6 +86,18 @@ func (suite *ProjectHandlerTestSuite) TestOnProjectDelete() { | |
projID, err := project.New().Create(suite.ctx, &models.Project{Name: "test-project", OwnerID: 1}) | ||
suite.Nil(err) | ||
|
||
userID, err := user.Mgr.Create(suite.ctx, &commonmodels.User{Username: "test-user-event", Email: "[email protected]"}) | ||
defer user.Mgr.Delete(suite.ctx, userID) | ||
|
||
// create project member | ||
_, err = member.Mgr.AddProjectMember(suite.ctx, memberModels.Member{ProjectID: projID, EntityType: common.UserMember, EntityID: userID, Role: 1}) | ||
suite.Nil(err) | ||
|
||
// verify project member | ||
members, err := member.Mgr.SearchMemberByName(suite.ctx, projID, "test-user-event") | ||
suite.Nil(err) | ||
suite.Equal(1, len(members)) | ||
|
||
defer project.New().Delete(suite.ctx, projID) | ||
immutableRule := &immutableModel.Metadata{ | ||
ProjectID: projID, | ||
|
@@ -116,6 +133,11 @@ func (suite *ProjectHandlerTestSuite) TestOnProjectDelete() { | |
// check if immutable rule is deleted | ||
_, err = immutable.Ctr.GetImmutableRule(suite.ctx, immutableID) | ||
suite.NotNil(err) | ||
|
||
// check if project member is deleted | ||
mbs, err := member.Mgr.SearchMemberByName(suite.ctx, projID, "test-user-event") | ||
suite.Nil(err) | ||
suite.Equal(0, len(mbs)) | ||
} | ||
|
||
// TestArtifactHandler tests ArtifactHandler. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ package dao | |
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/suite" | ||
|
@@ -293,6 +294,56 @@ func (s *DaoTestSuite) TestDeleteProjectMemberByUserId() { | |
s.Nil(err) | ||
} | ||
|
||
func (s *DaoTestSuite) TestDeleteProjectMemberByProjectID() { | ||
s.WithUser(func(userID int64, username string) { | ||
var addMember = models.Member{ | ||
ProjectID: s.projectID, | ||
EntityID: int(userID), | ||
EntityType: common.UserMember, | ||
Role: common.RoleDeveloper, | ||
} | ||
pmid, err := s.dao.AddProjectMember(s.Context(), addMember) | ||
s.Nil(err) | ||
s.True(pmid > 0) | ||
|
||
err = s.dao.DeleteProjectMemberByProjectID(s.Context(), s.projectID) | ||
s.Nil(err) | ||
|
||
queryMember := models.Member{ProjectID: s.projectID, EntityID: int(userID), EntityType: common.UserMember} | ||
|
||
// not exist | ||
members, err := s.dao.GetProjectMember(s.Context(), queryMember, nil) | ||
s.True(len(members) == 0) | ||
s.Nil(err) | ||
}, "test_project_member_delete") | ||
} | ||
|
||
func (s *DaoTestSuite) WithUser(f func(int64, string), usernames ...string) { | ||
var username string | ||
if len(usernames) > 0 { | ||
username = usernames[0] | ||
} else { | ||
username = s.RandString(5) | ||
} | ||
|
||
o, err := orm.FromContext(orm.Context()) | ||
if err != nil { | ||
s.Fail("got error %v", err) | ||
} | ||
|
||
var userID int64 | ||
|
||
email := fmt.Sprintf("%[email protected]", username) | ||
sql := "INSERT INTO harbor_user (username, realname, email, password) VALUES (?, ?, ?, 'Harbor12345') RETURNING user_id" | ||
s.Nil(o.Raw(sql, username, username, email).QueryRow(&userID)) | ||
|
||
defer func() { | ||
o.Raw("UPDATE harbor_user SET deleted=True, username=concat_ws('#', username, user_id), email=concat_ws('#', email, user_id) WHERE user_id = ?", userID).Exec() | ||
}() | ||
|
||
f(userID, username) | ||
} | ||
|
||
func TestDaoTestSuite(t *testing.T) { | ||
suite.Run(t, &DaoTestSuite{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.