mirror of
https://github.com/strapi/strapi.git
synced 2025-11-15 09:39:15 +00:00
Add small fix and adapt tests
This commit is contained in:
parent
41a5c79994
commit
e1b78e05c4
@ -4,21 +4,21 @@ import { useIntl } from 'react-intl';
|
|||||||
|
|
||||||
import { Select, Option } from '@strapi/design-system';
|
import { Select, Option } from '@strapi/design-system';
|
||||||
|
|
||||||
const TokenTypeSelect = ({ errors, values, onChange, canEditInputs, options, label }) => {
|
const TokenTypeSelect = ({ name, errors, values, onChange, canEditInputs, options, label }) => {
|
||||||
const { formatMessage } = useIntl();
|
const { formatMessage } = useIntl();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Select
|
<Select
|
||||||
name="type"
|
name={name}
|
||||||
label={formatMessage({
|
label={formatMessage({
|
||||||
id: label.id,
|
id: label.id,
|
||||||
defaultMessage: label.defaultMessage,
|
defaultMessage: label.defaultMessage,
|
||||||
})}
|
})}
|
||||||
value={values?.type}
|
value={values && values[name]}
|
||||||
error={
|
error={
|
||||||
errors.type
|
errors[name]
|
||||||
? formatMessage(
|
? formatMessage(
|
||||||
errors.type?.id ? errors.type : { id: errors.type, defaultMessage: errors.type }
|
errors[name]?.id ? errors[name] : { id: errors[name], defaultMessage: errors[name] }
|
||||||
)
|
)
|
||||||
: null
|
: null
|
||||||
}
|
}
|
||||||
@ -38,6 +38,7 @@ const TokenTypeSelect = ({ errors, values, onChange, canEditInputs, options, lab
|
|||||||
};
|
};
|
||||||
|
|
||||||
TokenTypeSelect.propTypes = {
|
TokenTypeSelect.propTypes = {
|
||||||
|
name: PropTypes.string,
|
||||||
options: PropTypes.arrayOf(
|
options: PropTypes.arrayOf(
|
||||||
PropTypes.shape({
|
PropTypes.shape({
|
||||||
label: PropTypes.shape({
|
label: PropTypes.shape({
|
||||||
@ -62,6 +63,7 @@ TokenTypeSelect.propTypes = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
TokenTypeSelect.defaultProps = {
|
TokenTypeSelect.defaultProps = {
|
||||||
|
name: 'type',
|
||||||
errors: {},
|
errors: {},
|
||||||
options: [],
|
options: [],
|
||||||
};
|
};
|
||||||
|
|||||||
@ -84,8 +84,9 @@ const FormTransferTokenContainer = ({
|
|||||||
token={transferToken}
|
token={transferToken}
|
||||||
/>
|
/>
|
||||||
</GridItem>
|
</GridItem>
|
||||||
<GridItem key="type" col={6} xs={12}>
|
<GridItem key="permissions" col={6} xs={12}>
|
||||||
<TokenTypeSelect
|
<TokenTypeSelect
|
||||||
|
name="permissions"
|
||||||
values={values}
|
values={values}
|
||||||
errors={errors}
|
errors={errors}
|
||||||
label={{
|
label={{
|
||||||
@ -93,7 +94,7 @@ const FormTransferTokenContainer = ({
|
|||||||
defaultMessage: 'Token type',
|
defaultMessage: 'Token type',
|
||||||
}}
|
}}
|
||||||
onChange={(value) => {
|
onChange={(value) => {
|
||||||
onChange({ target: { name: 'type', value } });
|
onChange({ target: { name: 'permissions', value } });
|
||||||
}}
|
}}
|
||||||
options={typeOptions}
|
options={typeOptions}
|
||||||
canEditInputs={canEditInputs}
|
canEditInputs={canEditInputs}
|
||||||
|
|||||||
@ -92,7 +92,7 @@ const TransferTokenCreateView = () => {
|
|||||||
? parseInt(body.lifespan, 10)
|
? parseInt(body.lifespan, 10)
|
||||||
: null;
|
: null;
|
||||||
|
|
||||||
const permissions = body.type.split('-');
|
const permissions = body.permissions.split('-');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const {
|
const {
|
||||||
@ -106,7 +106,6 @@ const TransferTokenCreateView = () => {
|
|||||||
: await put(`/admin/transfer/tokens/${id}`, {
|
: await put(`/admin/transfer/tokens/${id}`, {
|
||||||
name: body.name,
|
name: body.name,
|
||||||
description: body.description,
|
description: body.description,
|
||||||
type: body.type,
|
|
||||||
permissions,
|
permissions,
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -132,9 +131,9 @@ const TransferTokenCreateView = () => {
|
|||||||
defaultMessage: 'Transfer Token successfully edited',
|
defaultMessage: 'Transfer Token successfully edited',
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
console.log('transferToken', transferToken);
|
||||||
trackUsageRef.current(isCreating ? 'didCreateToken' : 'didEditToken', {
|
trackUsageRef.current(isCreating ? 'didCreateToken' : 'didEditToken', {
|
||||||
type: transferToken?.type,
|
type: transferToken?.permissions,
|
||||||
tokenType: TRANSFER_TOKEN_TYPE,
|
tokenType: TRANSFER_TOKEN_TYPE,
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@ -175,7 +174,7 @@ const TransferTokenCreateView = () => {
|
|||||||
lifespan: transferToken?.lifespan
|
lifespan: transferToken?.lifespan
|
||||||
? transferToken.lifespan.toString()
|
? transferToken.lifespan.toString()
|
||||||
: transferToken?.lifespan,
|
: transferToken?.lifespan,
|
||||||
type: transferToken?.permissions.join('-'),
|
permissions: transferToken?.permissions.join('-'),
|
||||||
}}
|
}}
|
||||||
enableReinitialize
|
enableReinitialize
|
||||||
onSubmit={(body, actions) => handleSubmit(body, actions)}
|
onSubmit={(body, actions) => handleSubmit(body, actions)}
|
||||||
|
|||||||
@ -5,7 +5,7 @@ const schema = yup.object().shape({
|
|||||||
name: yup.string(translatedErrors.string).required(translatedErrors.required),
|
name: yup.string(translatedErrors.string).required(translatedErrors.required),
|
||||||
description: yup.string().nullable(),
|
description: yup.string().nullable(),
|
||||||
lifespan: yup.number().integer().min(0).nullable().defined(translatedErrors.required),
|
lifespan: yup.number().integer().min(0).nullable().defined(translatedErrors.required),
|
||||||
type: yup.string(translatedErrors.string).required(translatedErrors.required),
|
permissions: yup.string(translatedErrors.string).required(translatedErrors.required),
|
||||||
});
|
});
|
||||||
|
|
||||||
export default schema;
|
export default schema;
|
||||||
|
|||||||
@ -149,6 +149,7 @@ const update = async (id, attributes) => {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (attributes.permissions) {
|
||||||
const currentPermissionsResult = await strapi.entityService.load(
|
const currentPermissionsResult = await strapi.entityService.load(
|
||||||
TRANSFER_TOKEN_UID,
|
TRANSFER_TOKEN_UID,
|
||||||
updatedToken,
|
updatedToken,
|
||||||
@ -180,6 +181,7 @@ const update = async (id, attributes) => {
|
|||||||
})
|
})
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// retrieve permissions
|
// retrieve permissions
|
||||||
const permissionsFromDb = await strapi.entityService.load(
|
const permissionsFromDb = await strapi.entityService.load(
|
||||||
|
|||||||
@ -40,6 +40,7 @@ describe('Admin Transfer Token CRUD (api)', () => {
|
|||||||
const body = {
|
const body = {
|
||||||
name: `transfer_token_${String(currentTokens)}`,
|
name: `transfer_token_${String(currentTokens)}`,
|
||||||
description: 'generic description',
|
description: 'generic description',
|
||||||
|
permissions: ['push', 'pull'],
|
||||||
...token,
|
...token,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -69,7 +70,7 @@ describe('Admin Transfer Token CRUD (api)', () => {
|
|||||||
error: {
|
error: {
|
||||||
status: 400,
|
status: 400,
|
||||||
name: 'ValidationError',
|
name: 'ValidationError',
|
||||||
message: '2 errors occurred',
|
message: '3 errors occurred',
|
||||||
details: {
|
details: {
|
||||||
errors: [
|
errors: [
|
||||||
{
|
{
|
||||||
@ -82,6 +83,11 @@ describe('Admin Transfer Token CRUD (api)', () => {
|
|||||||
name: 'ValidationError',
|
name: 'ValidationError',
|
||||||
message: 'name is a required field',
|
message: 'name is a required field',
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
path: ['permissions'],
|
||||||
|
name: 'ValidationError',
|
||||||
|
message: 'permissions is a required field',
|
||||||
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -92,6 +98,7 @@ describe('Admin Transfer Token CRUD (api)', () => {
|
|||||||
const body = {
|
const body = {
|
||||||
name: 'transfer-token_tests-no-lifespan',
|
name: 'transfer-token_tests-no-lifespan',
|
||||||
description: 'transfer-token_tests-description',
|
description: 'transfer-token_tests-description',
|
||||||
|
permissions: ['push'],
|
||||||
};
|
};
|
||||||
|
|
||||||
const res = await rq({
|
const res = await rq({
|
||||||
@ -104,7 +111,7 @@ describe('Admin Transfer Token CRUD (api)', () => {
|
|||||||
expect(res.body.data).toStrictEqual({
|
expect(res.body.data).toStrictEqual({
|
||||||
accessKey: expect.any(String),
|
accessKey: expect.any(String),
|
||||||
name: body.name,
|
name: body.name,
|
||||||
permissions: [],
|
permissions: body.permissions,
|
||||||
description: body.description,
|
description: body.description,
|
||||||
id: expect.any(Number),
|
id: expect.any(Number),
|
||||||
createdAt: expect.toBeISODate(),
|
createdAt: expect.toBeISODate(),
|
||||||
@ -123,6 +130,7 @@ describe('Admin Transfer Token CRUD (api)', () => {
|
|||||||
name: 'transfer-token_tests-lifespan7',
|
name: 'transfer-token_tests-lifespan7',
|
||||||
description: 'transfer-token_tests-description',
|
description: 'transfer-token_tests-description',
|
||||||
lifespan: 7 * 24 * 60 * 60 * 1000, // 7 days
|
lifespan: 7 * 24 * 60 * 60 * 1000, // 7 days
|
||||||
|
permissions: ['pull'],
|
||||||
};
|
};
|
||||||
|
|
||||||
const res = await rq({
|
const res = await rq({
|
||||||
@ -135,7 +143,7 @@ describe('Admin Transfer Token CRUD (api)', () => {
|
|||||||
expect(res.body.data).toStrictEqual({
|
expect(res.body.data).toStrictEqual({
|
||||||
accessKey: expect.any(String),
|
accessKey: expect.any(String),
|
||||||
name: body.name,
|
name: body.name,
|
||||||
permissions: [],
|
permissions: body.permissions,
|
||||||
description: body.description,
|
description: body.description,
|
||||||
id: expect.any(Number),
|
id: expect.any(Number),
|
||||||
createdAt: expect.toBeISODate(),
|
createdAt: expect.toBeISODate(),
|
||||||
@ -160,6 +168,7 @@ describe('Admin Transfer Token CRUD (api)', () => {
|
|||||||
name: 'transfer-token_tests-lifespan30',
|
name: 'transfer-token_tests-lifespan30',
|
||||||
description: 'transfer-token_tests-description',
|
description: 'transfer-token_tests-description',
|
||||||
lifespan: 30 * 24 * 60 * 60 * 1000, // 30 days
|
lifespan: 30 * 24 * 60 * 60 * 1000, // 30 days
|
||||||
|
permissions: ['push'],
|
||||||
};
|
};
|
||||||
|
|
||||||
const res = await rq({
|
const res = await rq({
|
||||||
@ -172,7 +181,7 @@ describe('Admin Transfer Token CRUD (api)', () => {
|
|||||||
expect(res.body.data).toStrictEqual({
|
expect(res.body.data).toStrictEqual({
|
||||||
accessKey: expect.any(String),
|
accessKey: expect.any(String),
|
||||||
name: body.name,
|
name: body.name,
|
||||||
permissions: [],
|
permissions: body.permissions,
|
||||||
description: body.description,
|
description: body.description,
|
||||||
id: expect.any(Number),
|
id: expect.any(Number),
|
||||||
createdAt: expect.toBeISODate(),
|
createdAt: expect.toBeISODate(),
|
||||||
@ -197,6 +206,7 @@ describe('Admin Transfer Token CRUD (api)', () => {
|
|||||||
name: 'transfer-token_tests-lifespan90',
|
name: 'transfer-token_tests-lifespan90',
|
||||||
description: 'transfer-token_tests-description',
|
description: 'transfer-token_tests-description',
|
||||||
lifespan: 90 * 24 * 60 * 60 * 1000, // 90 days
|
lifespan: 90 * 24 * 60 * 60 * 1000, // 90 days
|
||||||
|
permissions: ['push', 'pull'],
|
||||||
};
|
};
|
||||||
|
|
||||||
const res = await rq({
|
const res = await rq({
|
||||||
@ -209,7 +219,7 @@ describe('Admin Transfer Token CRUD (api)', () => {
|
|||||||
expect(res.body.data).toStrictEqual({
|
expect(res.body.data).toStrictEqual({
|
||||||
accessKey: expect.any(String),
|
accessKey: expect.any(String),
|
||||||
name: body.name,
|
name: body.name,
|
||||||
permissions: [],
|
permissions: body.permissions,
|
||||||
description: body.description,
|
description: body.description,
|
||||||
id: expect.any(Number),
|
id: expect.any(Number),
|
||||||
createdAt: expect.toBeISODate(),
|
createdAt: expect.toBeISODate(),
|
||||||
@ -231,6 +241,7 @@ describe('Admin Transfer Token CRUD (api)', () => {
|
|||||||
name: 'transfer-token_tests-nulllifespan',
|
name: 'transfer-token_tests-nulllifespan',
|
||||||
description: 'transfer-token_tests-description',
|
description: 'transfer-token_tests-description',
|
||||||
lifespan: null,
|
lifespan: null,
|
||||||
|
permissions: ['push', 'pull'],
|
||||||
};
|
};
|
||||||
|
|
||||||
const res = await rq({
|
const res = await rq({
|
||||||
@ -243,7 +254,7 @@ describe('Admin Transfer Token CRUD (api)', () => {
|
|||||||
expect(res.body.data).toStrictEqual({
|
expect(res.body.data).toStrictEqual({
|
||||||
accessKey: expect.any(String),
|
accessKey: expect.any(String),
|
||||||
name: body.name,
|
name: body.name,
|
||||||
permissions: [],
|
permissions: body.permissions,
|
||||||
description: body.description,
|
description: body.description,
|
||||||
id: expect.any(Number),
|
id: expect.any(Number),
|
||||||
createdAt: expect.toBeISODate(),
|
createdAt: expect.toBeISODate(),
|
||||||
@ -259,6 +270,7 @@ describe('Admin Transfer Token CRUD (api)', () => {
|
|||||||
name: 'transfer-token_tests-lifespan',
|
name: 'transfer-token_tests-lifespan',
|
||||||
description: 'transfer-token_tests-description',
|
description: 'transfer-token_tests-description',
|
||||||
lifespan: -1,
|
lifespan: -1,
|
||||||
|
permissions: ['push', 'pull'],
|
||||||
};
|
};
|
||||||
|
|
||||||
const res = await rq({
|
const res = await rq({
|
||||||
@ -286,9 +298,10 @@ describe('Admin Transfer Token CRUD (api)', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Creates an transfer token without a description (successfully)', async () => {
|
test('Creates a transfer token without a description (successfully)', async () => {
|
||||||
const body = {
|
const body = {
|
||||||
name: 'transfer-token_tests-without-description',
|
name: 'transfer-token_tests-without-description',
|
||||||
|
permissions: ['push', 'pull'],
|
||||||
};
|
};
|
||||||
|
|
||||||
const res = await rq({
|
const res = await rq({
|
||||||
@ -301,7 +314,7 @@ describe('Admin Transfer Token CRUD (api)', () => {
|
|||||||
expect(res.body.data).toMatchObject({
|
expect(res.body.data).toMatchObject({
|
||||||
accessKey: expect.any(String),
|
accessKey: expect.any(String),
|
||||||
name: body.name,
|
name: body.name,
|
||||||
permissions: [],
|
permissions: body.permissions,
|
||||||
description: '',
|
description: '',
|
||||||
id: expect.any(Number),
|
id: expect.any(Number),
|
||||||
createdAt: expect.any(String),
|
createdAt: expect.any(String),
|
||||||
@ -312,10 +325,11 @@ describe('Admin Transfer Token CRUD (api)', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Creates an transfer token with trimmed description and name (successfully)', async () => {
|
test('Creates a transfer token with trimmed description and name (successfully)', async () => {
|
||||||
const body = {
|
const body = {
|
||||||
name: ' transfer-token_tests-spaces-at-the-end ',
|
name: ' transfer-token_tests-spaces-at-the-end ',
|
||||||
description: ' transfer-token_tests-description-with-spaces-at-the-end ',
|
description: ' transfer-token_tests-description-with-spaces-at-the-end ',
|
||||||
|
permissions: ['push', 'pull'],
|
||||||
};
|
};
|
||||||
|
|
||||||
const res = await rq({
|
const res = await rq({
|
||||||
@ -328,7 +342,7 @@ describe('Admin Transfer Token CRUD (api)', () => {
|
|||||||
expect(res.body.data).toMatchObject({
|
expect(res.body.data).toMatchObject({
|
||||||
accessKey: expect.any(String),
|
accessKey: expect.any(String),
|
||||||
name: 'transfer-token_tests-spaces-at-the-end',
|
name: 'transfer-token_tests-spaces-at-the-end',
|
||||||
permissions: [],
|
permissions: body.permissions,
|
||||||
description: 'transfer-token_tests-description-with-spaces-at-the-end',
|
description: 'transfer-token_tests-description-with-spaces-at-the-end',
|
||||||
id: expect.any(Number),
|
id: expect.any(Number),
|
||||||
createdAt: expect.any(String),
|
createdAt: expect.any(String),
|
||||||
@ -377,9 +391,9 @@ describe('Admin Transfer Token CRUD (api)', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
expect(res.statusCode).toBe(200);
|
expect(res.statusCode).toBe(200);
|
||||||
expect(res.body.data).toMatchObject({
|
expect(res.body.data).toEqual(
|
||||||
|
expect.objectContaining({
|
||||||
name: token.name,
|
name: token.name,
|
||||||
permissions: token.permissions,
|
|
||||||
description: token.description,
|
description: token.description,
|
||||||
id: token.id,
|
id: token.id,
|
||||||
createdAt: token.createdAt,
|
createdAt: token.createdAt,
|
||||||
@ -387,7 +401,8 @@ describe('Admin Transfer Token CRUD (api)', () => {
|
|||||||
updatedAt: expect.any(String),
|
updatedAt: expect.any(String),
|
||||||
expiresAt: null,
|
expiresAt: null,
|
||||||
lifespan: null,
|
lifespan: null,
|
||||||
});
|
})
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Does not return an error if the resource to delete does not exist', async () => {
|
test('Does not return an error if the resource to delete does not exist', async () => {
|
||||||
@ -451,6 +466,7 @@ describe('Admin Transfer Token CRUD (api)', () => {
|
|||||||
const updatedBody = {
|
const updatedBody = {
|
||||||
name: 'transfer-token_tests-updated-name',
|
name: 'transfer-token_tests-updated-name',
|
||||||
description: 'transfer-token_tests-description',
|
description: 'transfer-token_tests-description',
|
||||||
|
permissions: ['push'],
|
||||||
};
|
};
|
||||||
|
|
||||||
const updatedRes = await rq({
|
const updatedRes = await rq({
|
||||||
@ -462,7 +478,7 @@ describe('Admin Transfer Token CRUD (api)', () => {
|
|||||||
expect(updatedRes.statusCode).toBe(200);
|
expect(updatedRes.statusCode).toBe(200);
|
||||||
expect(updatedRes.body.data).toMatchObject({
|
expect(updatedRes.body.data).toMatchObject({
|
||||||
name: updatedBody.name,
|
name: updatedBody.name,
|
||||||
permissions: [],
|
permissions: updatedBody.permissions,
|
||||||
description: updatedBody.description,
|
description: updatedBody.description,
|
||||||
id: token.id,
|
id: token.id,
|
||||||
createdAt: token.createdAt,
|
createdAt: token.createdAt,
|
||||||
@ -477,6 +493,7 @@ describe('Admin Transfer Token CRUD (api)', () => {
|
|||||||
const body = {
|
const body = {
|
||||||
name: 'transfer-token_tests-updated-name',
|
name: 'transfer-token_tests-updated-name',
|
||||||
description: 'transfer-token_tests-updated-description',
|
description: 'transfer-token_tests-updated-description',
|
||||||
|
permissions: ['push'],
|
||||||
};
|
};
|
||||||
|
|
||||||
const res = await rq({
|
const res = await rq({
|
||||||
|
|||||||
@ -31,7 +31,7 @@ const transferTokenUpdateSchema = yup
|
|||||||
.array()
|
.array()
|
||||||
.min(1)
|
.min(1)
|
||||||
.of(yup.string().oneOf(Object.values(constants.TRANSFER_TOKEN_TYPE)))
|
.of(yup.string().oneOf(Object.values(constants.TRANSFER_TOKEN_TYPE)))
|
||||||
.required(),
|
.nullable(),
|
||||||
})
|
})
|
||||||
.noUnknown()
|
.noUnknown()
|
||||||
.strict();
|
.strict();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user