Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: add integration tests for deleting user (#15645)
ecmadao22f7b6ca0513
diff --git a/backend/api/v1/user_service.go b/backend/api/v1/user_service.goindex 6c1e19849f..3fc86c23cb 100644--- a/backend/api/v1/user_service.go+++ b/backend/api/v1/user_service.go@@ -705,7 +705,6 @@ func (s *UserService) getActiveUserCount(ctx context.Context) (int, error) {return activeEndUserCount, nil}-// TODO(ed): move to integration test.func (s *UserService) hasExtraWorkspaceAdmin(ctx context.Context, policy *storepb.IamPolicy, userID int) (bool, error) {workspaceAdminRole := common.FormatRole(api.WorkspaceAdmin.String())userMember := common.FormatUserUID(userID)diff --git a/backend/tests/tests.go b/backend/tests/tests.goindex 94325c1f3c..9967e894cd 100644--- a/backend/tests/tests.go+++ b/backend/tests/tests.go@@ -79,13 +79,15 @@ type controller struct {sqlServiceClient v1pb.SQLServiceClientsubscriptionServiceClient v1pb.SubscriptionServiceClientactuatorServiceClient v1pb.ActuatorServiceClient+ workspaceServiceClient v1pb.WorkspaceServiceClientcookie stringproject *v1pb.Project- rootURL string- apiURL string- v1APIURL string+ rootURL string+ apiURL string+ v1APIURL string+ principalName string}var (@@ -261,6 +263,7 @@ func (ctl *controller) start(ctx context.Context, port int) (context.Context, erctl.sqlServiceClient = v1pb.NewSQLServiceClient(ctl.grpcConn)ctl.subscriptionServiceClient = v1pb.NewSubscriptionServiceClient(ctl.grpcConn)ctl.actuatorServiceClient = v1pb.NewActuatorServiceClient(ctl.grpcConn)+ ctl.workspaceServiceClient = v1pb.NewWorkspaceServiceClient(ctl.grpcConn)if err := ctl.waitForHealthz(ctx); err != nil {return nil, errors.Wrap(err, "failed to wait for healthz")@@ -327,14 +330,15 @@ func (*controller) provisionSQLiteInstance(rootDir, name string) (string, error)// signupAndLogin will signup and login as user [email protected].func (ctl *controller) signupAndLogin(ctx context.Context) (string, error) {- if _, err := ctl.userServiceClient.CreateUser(ctx, &v1pb.CreateUserRequest{+ principal, err := ctl.userServiceClient.CreateUser(ctx, &v1pb.CreateUserRequest{User: &v1pb.User{Email: "[email protected]",Password: "1024bytebase",Title: "demo",UserType: v1pb.UserType_USER,},- }); err != nil && !strings.Contains(err.Error(), "exist") {+ })+ if err != nil && !strings.Contains(err.Error(), "exist") {return "", err}resp, err := ctl.authServiceClient.Login(ctx, &v1pb.LoginRequest{@@ -344,6 +348,7 @@ func (ctl *controller) signupAndLogin(ctx context.Context) (string, error) {if err != nil {return "", err}+ ctl.principalName = principal.Namectl.cookie = fmt.Sprintf("access-token=%s", resp.Token)return resp.Token, nil}diff --git a/backend/tests/user_test.go b/backend/tests/user_test.gonew file mode 100644index 0000000000..d2a2feb297--- /dev/null+++ b/backend/tests/user_test.go@@ -0,0 +1,142 @@+package tests++import (+ "context"+ "fmt"+ "testing"++ "github.com/stretchr/testify/require"+ "google.golang.org/grpc/metadata"++ api "github.com/bytebase/bytebase/backend/legacyapi"++ v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"+)++func TestDeleteUser(t *testing.T) {+ a := require.New(t)+ ctx := context.Background()+ ctl := &controller{}+ ctx, err := ctl.StartServerWithExternalPg(ctx)+ a.NoError(err)+ defer ctl.Close(ctx)++ expectErrorMsg := "rpc error: code = InvalidArgument desc = workspace must have at least one admin"++ member, err := ctl.userServiceClient.CreateUser(ctx, &v1pb.CreateUserRequest{+ User: &v1pb.User{+ Title: "member",+ UserType: v1pb.UserType_USER,+ Email: "[email protected]",+ Password: "1024bytebase",+ },+ })+ a.NoError(err)++ _, err = ctl.userServiceClient.DeleteUser(ctx, &v1pb.DeleteUserRequest{+ Name: member.Name,+ })+ a.NoError(err)++ // Test: cannot delete the last admin.+ _, err = ctl.userServiceClient.DeleteUser(ctx, &v1pb.DeleteUserRequest{+ Name: ctl.principalName,+ })+ a.Error(err)+ a.EqualError(err, expectErrorMsg)++ serviceAccount, err := ctl.userServiceClient.CreateUser(ctx, &v1pb.CreateUserRequest{+ User: &v1pb.User{+ Title: "bot",+ UserType: v1pb.UserType_SERVICE_ACCOUNT,+ Email: "[email protected]",+ },+ })+ a.NoError(err)++ policy, err := ctl.workspaceServiceClient.GetIamPolicy(ctx, &v1pb.GetIamPolicyRequest{})+ a.NoError(err)++ // Test: only count the end user.+ for _, binding := range policy.Bindings {+ if binding.Role == "roles/workspaceAdmin" {+ binding.Members = append(binding.Members, fmt.Sprintf("user:%s", serviceAccount.Email))+ break+ }+ }+ updatedPolicy, err := ctl.workspaceServiceClient.SetIamPolicy(ctx, &v1pb.SetIamPolicyRequest{+ Etag: policy.Etag,+ Policy: policy,+ })+ a.NoError(err)++ _, err = ctl.userServiceClient.DeleteUser(ctx, &v1pb.DeleteUserRequest{+ Name: ctl.principalName,+ })+ a.Error(err)+ a.EqualError(err, expectErrorMsg)++ _, err = ctl.userServiceClient.UndeleteUser(ctx, &v1pb.UndeleteUserRequest{+ Name: member.Name,+ })+ a.NoError(err)++ // Test: can delete the admin if member count > 1+ for _, binding := range updatedPolicy.Bindings {+ if binding.Role == "roles/workspaceAdmin" {+ binding.Members = append(binding.Members, fmt.Sprintf("user:%s", member.Email))+ break+ }+ }+ newPolicy, err := ctl.workspaceServiceClient.SetIamPolicy(ctx, &v1pb.SetIamPolicyRequest{+ Etag: updatedPolicy.Etag,+ Policy: updatedPolicy,+ })+ a.NoError(err)++ _, err = ctl.userServiceClient.DeleteUser(ctx, &v1pb.DeleteUserRequest{+ Name: ctl.principalName,+ })+ a.NoError(err)++ // Switch context.+ resp, err := ctl.authServiceClient.Login(ctx, &v1pb.LoginRequest{+ Email: member.Email,+ Password: "1024bytebase",+ })+ a.NoError(err)+ ctl.cookie = fmt.Sprintf("access-token=%s", resp.Token)+ ctx = metadata.NewOutgoingContext(ctx, metadata.Pairs(+ "Authorization",+ fmt.Sprintf("Bearer %s", resp.Token),+ ))++ // Test: check allUser in the binding+ for _, binding := range newPolicy.Bindings {+ if binding.Role == "roles/workspaceAdmin" {+ binding.Members = []string{api.AllUsers}+ break+ }+ }+ _, err = ctl.workspaceServiceClient.SetIamPolicy(ctx, &v1pb.SetIamPolicyRequest{+ Etag: newPolicy.Etag,+ Policy: newPolicy,+ })+ a.NoError(err)++ _, err = ctl.userServiceClient.DeleteUser(ctx, &v1pb.DeleteUserRequest{+ Name: member.Name,+ })+ a.Error(err)+ a.EqualError(err, expectErrorMsg)++ _, err = ctl.userServiceClient.UndeleteUser(ctx, &v1pb.UndeleteUserRequest{+ Name: ctl.principalName,+ })+ a.NoError(err)++ _, err = ctl.userServiceClient.DeleteUser(ctx, &v1pb.DeleteUserRequest{+ Name: member.Name,+ })+ a.NoError(err)+}