Merge pull request #1733 from automatisch/AUT-844
refactor: rewrite useUser with RQ
This commit is contained in:
@@ -1,17 +0,0 @@
|
|||||||
import User from '../../models/user.js';
|
|
||||||
|
|
||||||
const getUser = async (_parent, params, context) => {
|
|
||||||
context.currentUser.can('read', 'User');
|
|
||||||
|
|
||||||
return await User.query()
|
|
||||||
.leftJoinRelated({
|
|
||||||
role: true,
|
|
||||||
})
|
|
||||||
.withGraphFetched({
|
|
||||||
role: true,
|
|
||||||
})
|
|
||||||
.findById(params.id)
|
|
||||||
.throwIfNotFound();
|
|
||||||
};
|
|
||||||
|
|
||||||
export default getUser;
|
|
@@ -1,146 +0,0 @@
|
|||||||
import { describe, it, expect, beforeEach } from 'vitest';
|
|
||||||
import request from 'supertest';
|
|
||||||
import app from '../../app';
|
|
||||||
import createAuthTokenByUserId from '../../helpers/create-auth-token-by-user-id';
|
|
||||||
import Crypto from 'crypto';
|
|
||||||
import { createRole } from '../../../test/factories/role';
|
|
||||||
import { createPermission } from '../../../test/factories/permission';
|
|
||||||
import { createUser } from '../../../test/factories/user';
|
|
||||||
|
|
||||||
describe('graphQL getUser query', () => {
|
|
||||||
describe('and without permissions', () => {
|
|
||||||
it('should throw not authorized error', async () => {
|
|
||||||
const userWithoutPermissions = await createUser();
|
|
||||||
const anotherUser = await createUser();
|
|
||||||
|
|
||||||
const query = `
|
|
||||||
query {
|
|
||||||
getUser(id: "${anotherUser.id}") {
|
|
||||||
id
|
|
||||||
email
|
|
||||||
}
|
|
||||||
}
|
|
||||||
`;
|
|
||||||
|
|
||||||
const token = createAuthTokenByUserId(userWithoutPermissions.id);
|
|
||||||
|
|
||||||
const response = await request(app)
|
|
||||||
.post('/graphql')
|
|
||||||
.set('Authorization', token)
|
|
||||||
.send({ query })
|
|
||||||
.expect(200);
|
|
||||||
|
|
||||||
expect(response.body.errors).toBeDefined();
|
|
||||||
expect(response.body.errors[0].message).toEqual('Not authorized!');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('and correct permissions', () => {
|
|
||||||
let role, currentUser, anotherUser, token, requestObject;
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
|
||||||
role = await createRole({
|
|
||||||
key: 'sample',
|
|
||||||
name: 'sample',
|
|
||||||
});
|
|
||||||
|
|
||||||
await createPermission({
|
|
||||||
action: 'read',
|
|
||||||
subject: 'User',
|
|
||||||
roleId: role.id,
|
|
||||||
});
|
|
||||||
|
|
||||||
currentUser = await createUser({
|
|
||||||
roleId: role.id,
|
|
||||||
});
|
|
||||||
|
|
||||||
anotherUser = await createUser({
|
|
||||||
roleId: role.id,
|
|
||||||
});
|
|
||||||
|
|
||||||
token = createAuthTokenByUserId(currentUser.id);
|
|
||||||
requestObject = request(app).post('/graphql').set('Authorization', token);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should return user data for a valid user id', async () => {
|
|
||||||
const query = `
|
|
||||||
query {
|
|
||||||
getUser(id: "${anotherUser.id}") {
|
|
||||||
id
|
|
||||||
email
|
|
||||||
fullName
|
|
||||||
email
|
|
||||||
createdAt
|
|
||||||
updatedAt
|
|
||||||
role {
|
|
||||||
id
|
|
||||||
name
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
`;
|
|
||||||
|
|
||||||
const response = await requestObject.send({ query }).expect(200);
|
|
||||||
|
|
||||||
const expectedResponsePayload = {
|
|
||||||
data: {
|
|
||||||
getUser: {
|
|
||||||
createdAt: anotherUser.createdAt.getTime().toString(),
|
|
||||||
email: anotherUser.email,
|
|
||||||
fullName: anotherUser.fullName,
|
|
||||||
id: anotherUser.id,
|
|
||||||
role: { id: role.id, name: role.name },
|
|
||||||
updatedAt: anotherUser.updatedAt.getTime().toString(),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
expect(response.body).toEqual(expectedResponsePayload);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should not return user password for a valid user id', async () => {
|
|
||||||
const query = `
|
|
||||||
query {
|
|
||||||
getUser(id: "${anotherUser.id}") {
|
|
||||||
id
|
|
||||||
email
|
|
||||||
password
|
|
||||||
}
|
|
||||||
}
|
|
||||||
`;
|
|
||||||
|
|
||||||
const response = await requestObject.send({ query }).expect(400);
|
|
||||||
|
|
||||||
expect(response.body.errors).toBeDefined();
|
|
||||||
expect(response.body.errors[0].message).toEqual(
|
|
||||||
'Cannot query field "password" on type "User".'
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should return not found for invalid user id', async () => {
|
|
||||||
const invalidUserId = Crypto.randomUUID();
|
|
||||||
|
|
||||||
const query = `
|
|
||||||
query {
|
|
||||||
getUser(id: "${invalidUserId}") {
|
|
||||||
id
|
|
||||||
email
|
|
||||||
fullName
|
|
||||||
email
|
|
||||||
createdAt
|
|
||||||
updatedAt
|
|
||||||
role {
|
|
||||||
id
|
|
||||||
name
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
`;
|
|
||||||
|
|
||||||
const response = await requestObject.send({ query }).expect(200);
|
|
||||||
|
|
||||||
expect(response.body.errors).toBeDefined();
|
|
||||||
expect(response.body.errors[0].message).toEqual('NotFoundError');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
@@ -13,7 +13,6 @@ import getPermissionCatalog from './queries/get-permission-catalog.ee.js';
|
|||||||
import getSamlAuthProviderRoleMappings from './queries/get-saml-auth-provider-role-mappings.ee.js';
|
import getSamlAuthProviderRoleMappings from './queries/get-saml-auth-provider-role-mappings.ee.js';
|
||||||
import getStepWithTestExecutions from './queries/get-step-with-test-executions.js';
|
import getStepWithTestExecutions from './queries/get-step-with-test-executions.js';
|
||||||
import getTrialStatus from './queries/get-trial-status.ee.js';
|
import getTrialStatus from './queries/get-trial-status.ee.js';
|
||||||
import getUser from './queries/get-user.js';
|
|
||||||
import getUsers from './queries/get-users.js';
|
import getUsers from './queries/get-users.js';
|
||||||
import listSamlAuthProviders from './queries/list-saml-auth-providers.ee.js';
|
import listSamlAuthProviders from './queries/list-saml-auth-providers.ee.js';
|
||||||
import testConnection from './queries/test-connection.js';
|
import testConnection from './queries/test-connection.js';
|
||||||
@@ -34,7 +33,6 @@ const queryResolvers = {
|
|||||||
getSamlAuthProviderRoleMappings,
|
getSamlAuthProviderRoleMappings,
|
||||||
getStepWithTestExecutions,
|
getStepWithTestExecutions,
|
||||||
getTrialStatus,
|
getTrialStatus,
|
||||||
getUser,
|
|
||||||
getUsers,
|
getUsers,
|
||||||
listSamlAuthProviders,
|
listSamlAuthProviders,
|
||||||
testConnection,
|
testConnection,
|
||||||
|
@@ -29,7 +29,6 @@ type Query {
|
|||||||
getNotifications: [Notification]
|
getNotifications: [Notification]
|
||||||
getSamlAuthProviderRoleMappings(id: String!): [SamlAuthProvidersRoleMapping]
|
getSamlAuthProviderRoleMappings(id: String!): [SamlAuthProvidersRoleMapping]
|
||||||
getTrialStatus: GetTrialStatus
|
getTrialStatus: GetTrialStatus
|
||||||
getUser(id: String!): User
|
|
||||||
getUsers(limit: Int!, offset: Int!): UserConnection
|
getUsers(limit: Int!, offset: Int!): UserConnection
|
||||||
listSamlAuthProviders: [ListSamlAuthProvider]
|
listSamlAuthProviders: [ListSamlAuthProvider]
|
||||||
}
|
}
|
||||||
|
@@ -1,18 +0,0 @@
|
|||||||
import { gql } from '@apollo/client';
|
|
||||||
export const GET_USER = gql`
|
|
||||||
query GetUser($id: String!) {
|
|
||||||
getUser(id: $id) {
|
|
||||||
id
|
|
||||||
fullName
|
|
||||||
email
|
|
||||||
role {
|
|
||||||
id
|
|
||||||
key
|
|
||||||
name
|
|
||||||
isAdmin
|
|
||||||
}
|
|
||||||
createdAt
|
|
||||||
updatedAt
|
|
||||||
}
|
|
||||||
}
|
|
||||||
`;
|
|
@@ -1,19 +1,17 @@
|
|||||||
import * as React from 'react';
|
import { useQuery } from '@tanstack/react-query';
|
||||||
import { useLazyQuery } from '@apollo/client';
|
import api from 'helpers/api';
|
||||||
import { GET_USER } from 'graphql/queries/get-user';
|
|
||||||
export default function useUser(userId) {
|
export default function useUser({ userId }) {
|
||||||
const [getUser, { data, loading }] = useLazyQuery(GET_USER);
|
const query = useQuery({
|
||||||
React.useEffect(() => {
|
queryKey: ['user', userId],
|
||||||
if (userId) {
|
queryFn: async ({ signal }) => {
|
||||||
getUser({
|
const { data } = await api.get(`/v1/admin/users/${userId}`, {
|
||||||
variables: {
|
signal,
|
||||||
id: userId,
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
}
|
return data;
|
||||||
}, [userId]);
|
},
|
||||||
return {
|
enabled: !!userId,
|
||||||
user: data?.getUser,
|
});
|
||||||
loading,
|
|
||||||
};
|
return query;
|
||||||
}
|
}
|
||||||
|
@@ -28,7 +28,8 @@ export default function EditUser() {
|
|||||||
const formatMessage = useFormatMessage();
|
const formatMessage = useFormatMessage();
|
||||||
const [updateUser, { loading }] = useMutation(UPDATE_USER);
|
const [updateUser, { loading }] = useMutation(UPDATE_USER);
|
||||||
const { userId } = useParams();
|
const { userId } = useParams();
|
||||||
const { user, loading: userLoading } = useUser(userId);
|
const { data: userData, loading: isUserLoading } = useUser({ userId });
|
||||||
|
const user = userData?.data;
|
||||||
const { data, loading: isRolesLoading } = useRoles();
|
const { data, loading: isRolesLoading } = useRoles();
|
||||||
const roles = data?.data;
|
const roles = data?.data;
|
||||||
const enqueueSnackbar = useEnqueueSnackbar();
|
const enqueueSnackbar = useEnqueueSnackbar();
|
||||||
@@ -73,7 +74,7 @@ export default function EditUser() {
|
|||||||
</Grid>
|
</Grid>
|
||||||
|
|
||||||
<Grid item xs={12} justifyContent="flex-end" sx={{ pt: 5 }}>
|
<Grid item xs={12} justifyContent="flex-end" sx={{ pt: 5 }}>
|
||||||
{userLoading && (
|
{isUserLoading && (
|
||||||
<Stack direction="column" gap={2}>
|
<Stack direction="column" gap={2}>
|
||||||
<Skeleton variant="rounded" height={55} />
|
<Skeleton variant="rounded" height={55} />
|
||||||
<Skeleton variant="rounded" height={55} />
|
<Skeleton variant="rounded" height={55} />
|
||||||
@@ -82,7 +83,7 @@ export default function EditUser() {
|
|||||||
</Stack>
|
</Stack>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{!userLoading && (
|
{!isUserLoading && (
|
||||||
<Form defaultValues={user} onSubmit={handleUserUpdate}>
|
<Form defaultValues={user} onSubmit={handleUserUpdate}>
|
||||||
<Stack direction="column" gap={2}>
|
<Stack direction="column" gap={2}>
|
||||||
<TextField
|
<TextField
|
||||||
|
Reference in New Issue
Block a user