Skip to content

Commit

Permalink
Add GQL API for deleteUser (#1443)
Browse files Browse the repository at this point in the history
Summary: Previous changes added deleteUser to the profile and API gRPC
services. This PR adds it to the graphql layer.

Relevant Issues: #655

Type of change: /kind feature

Test Plan: Unit tests

Changelog Message:
```release-note
Add API to delete user in GQL and gRPC. If the user being deleted is the only user in the org, the org will also be deleted.
```

---------

Signed-off-by: Michelle Nguyen <michellenguyen@pixielabs.ai>
  • Loading branch information
aimichelle authored Jun 6, 2023
1 parent 0ba4a1b commit f3741ba
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/cloud/api/controllers/schema/03_auth_schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ extend type Mutation {
DeleteAPIKey(id: ID!): Boolean!
UpdateUserSettings(settings: EditableUserSettings!): UserSettings!
SetUserAttributes(attributes: EditableUserAttributes!): UserAttributes!
DeleteUser: Boolean!
InviteUser(email: String!, firstName: String!, lastName: String!): UserInvite!
UpdateUserPermissions(userID: ID!, userPermissions: EditableUserPermissions!): UserInfo!
CreateOrg(orgName: String!): ID!
Expand Down
4 changes: 2 additions & 2 deletions src/cloud/api/controllers/schema/complete/bindata.gen.go

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/cloud/api/controllers/schema/noauth/bindata.gen.go

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions src/cloud/api/controllers/user_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,23 @@ func (q *QueryResolver) SetUserAttributes(ctx context.Context, args *setUserAttr

return resp, nil
}

// DeleteUser deletes the user with the current credentials.
func (q *QueryResolver) DeleteUser(ctx context.Context) (bool, error) {
sCtx, err := authcontext.FromContext(ctx)
if err != nil {
return false, err
}

id := sCtx.Claims.GetUserClaims().UserID
req := &cloudpb.DeleteUserRequest{
ID: utils.ProtoFromUUIDStrOrNil(id),
}

_, err = q.Env.UserServer.DeleteUser(ctx, req)
if err != nil {
return false, rpcErrorHelper(err)
}

return true, nil
}
30 changes: 30 additions & 0 deletions src/cloud/api/controllers/user_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,3 +420,33 @@ func TestUserSettingsResolver_SetUserAttributes(t *testing.T) {
})
}
}

func TestUserSettingsResolver_DeleteUser(t *testing.T) {
t.Run("deletion succeeds", func(t *testing.T) {
gqlEnv, mockClients, cleanup := gqltestutils.CreateTestGraphQLEnv(t)
defer cleanup()
ctx := CreateTestContext()

mockClients.MockUser.EXPECT().DeleteUser(gomock.Any(), &cloudpb.DeleteUserRequest{
ID: utils.ProtoFromUUIDStrOrNil("6ba7b810-9dad-11d1-80b4-00c04fd430c9"),
}).Return(&cloudpb.DeleteUserResponse{}, nil)

gqlSchema := LoadSchema(gqlEnv)
gqltesting.RunTests(t, []*gqltesting.Test{
{
Schema: gqlSchema,
Context: ctx,
Query: `
mutation {
DeleteUser()
}
`,
ExpectedResult: `
{
"DeleteUser": true
}
`,
},
})
})
}
6 changes: 6 additions & 0 deletions src/ui/src/types/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export interface GQLMutation {
DeleteAPIKey: boolean;
UpdateUserSettings: GQLUserSettings;
SetUserAttributes: GQLUserAttributes;
DeleteUser: boolean;
InviteUser: GQLUserInvite;
UpdateUserPermissions: GQLUserInfo;
CreateOrg: string;
Expand Down Expand Up @@ -640,6 +641,7 @@ export interface GQLMutationTypeResolver<TParent = any> {
DeleteAPIKey?: MutationToDeleteAPIKeyResolver<TParent>;
UpdateUserSettings?: MutationToUpdateUserSettingsResolver<TParent>;
SetUserAttributes?: MutationToSetUserAttributesResolver<TParent>;
DeleteUser?: MutationToDeleteUserResolver<TParent>;
InviteUser?: MutationToInviteUserResolver<TParent>;
UpdateUserPermissions?: MutationToUpdateUserPermissionsResolver<TParent>;
CreateOrg?: MutationToCreateOrgResolver<TParent>;
Expand Down Expand Up @@ -697,6 +699,10 @@ export interface MutationToSetUserAttributesResolver<TParent = any, TResult = an
(parent: TParent, args: MutationToSetUserAttributesArgs, context: any, info: GraphQLResolveInfo): TResult;
}

export interface MutationToDeleteUserResolver<TParent = any, TResult = any> {
(parent: TParent, args: {}, context: any, info: GraphQLResolveInfo): TResult;
}

export interface MutationToInviteUserArgs {
email: string;
firstName: string;
Expand Down

0 comments on commit f3741ba

Please sign in to comment.