Skip to content

Commit

Permalink
fix: api tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc-Roig committed Apr 5, 2024
1 parent 3d7d106 commit aeb3324
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,11 @@ export default {

// Load entity
const locale = await validateLocale(query?.locale);
const entity = await strapi
.documents(modelUID)
.findOne(documentId, { locale, populate: [ENTITY_STAGE_ATTRIBUTE] });
const entity = await strapi.documents(modelUID).findOne(documentId, {
// @ts-expect-error - locale should be also null in the doc service types
locale,
populate: [ENTITY_STAGE_ATTRIBUTE],
});

if (!entity) {
ctx.throw(404, 'Entity not found');
Expand Down Expand Up @@ -152,9 +154,11 @@ export default {

// Load entity
const locale = await validateLocale(query?.locale);
const entity = await strapi
.documents(modelUID)
.findOne(documentId, { locale, populate: [ENTITY_STAGE_ATTRIBUTE] });
const entity = await strapi.documents(modelUID).findOne(documentId, {
// @ts-expect-error - locale should be also null in the doc service types
locale,
populate: [ENTITY_STAGE_ATTRIBUTE],
});

if (!entity) {
ctx.throw(404, 'Entity not found');
Expand Down
12 changes: 6 additions & 6 deletions packages/core/review-workflows/server/src/services/assignees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,22 @@ export default ({ strapi }: { strapi: Core.Strapi }) => {

metrics.sendDidEditAssignee(await this.findEntityAssigneeId(documentId, model), assigneeId);

return strapi.db.query(model).update({
where: { documentId, locale, publishedAt: null },
return strapi.documents(model).update(documentId, {
locale,
data: { [ENTITY_ASSIGNEE_ATTRIBUTE]: assigneeId },
populate: [ENTITY_ASSIGNEE_ATTRIBUTE],
select: [],
fields: [],
});
},

async deleteEntityAssignee(documentId: string, locale: string, model: UID.ContentType) {
metrics.sendDidEditAssignee(await this.findEntityAssigneeId(documentId, model), null);

return strapi.db.query(model).update({
where: { documentId, locale, publishedAt: null },
return strapi.documents(model).update(documentId, {
locale,
data: { [ENTITY_ASSIGNEE_ATTRIBUTE]: null },
populate: [ENTITY_ASSIGNEE_ATTRIBUTE],
select: [],
fields: [],
});
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const validateUpdateAssigneeOnEntitySchema = yup
})
.required();

const validateLocaleSchema = yup.string();
const validateLocaleSchema = yup.string().nullable();

export const validateWorkflowCreate = validateYupSchema(validateWorkflowCreateSchema);
export const validateUpdateStageOnEntity = validateYupSchema(validateUpdateStageOnEntitySchema);
Expand Down
28 changes: 18 additions & 10 deletions tests/api/core/review-workflows/review-workflows.test.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -526,10 +526,12 @@ describeOnCondition(edition === 'EE')('Review workflows', () => {

const response = await requests.admin({
method: 'PUT',
url: `/review-workflows/content-manager/collection-types/${productUID}/${entry.id}/assignee`,
url: `/review-workflows/content-manager/collection-types/${productUID}/${entry.documentId}/assignee`,
body: {
data: { id: user.id },
},
// TODO: Test with other locales
qs: { locale: undefined },
});
expect(response.status).toBe(200);
const assignee = response.body.data[ENTITY_ASSIGNEE_ATTRIBUTE];
Expand Down Expand Up @@ -571,7 +573,7 @@ describeOnCondition(edition === 'EE')('Review workflows', () => {

const response = await requests.admin({
method: 'PUT',
url: `/review-workflows/content-manager/collection-types/${productUID}/${entry.id}/assignee`,
url: `/review-workflows/content-manager/collection-types/${productUID}/${entry.documentId}/assignee`,
body: {
data: { id: 1234 },
},
Expand All @@ -598,7 +600,7 @@ describeOnCondition(edition === 'EE')('Review workflows', () => {

const response = await requests.admin({
method: 'PUT',
url: `/review-workflows/content-manager/collection-types/${productUID}/${entry.id}/assignee`,
url: `/review-workflows/content-manager/collection-types/${productUID}/${entry.documentId}/assignee`,
body: {
data: { id: user.id },
},
Expand All @@ -625,10 +627,12 @@ describeOnCondition(edition === 'EE')('Review workflows', () => {

const response = await requests.admin({
method: 'PUT',
url: `/review-workflows/content-manager/collection-types/${productUID}/${entry.id}/stage`,
url: `/review-workflows/content-manager/collection-types/${productUID}/${entry.documentId}/stage`,
body: {
data: { id: secondStage.id },
},
// TODO: Test with other locales
qs: { locale: entry.locale },
});

expect(response.status).toBe(200);
Expand All @@ -642,10 +646,12 @@ describeOnCondition(edition === 'EE')('Review workflows', () => {

const response = await requests.admin({
method: 'PUT',
url: `/review-workflows/content-manager/collection-types/${productUID}/${entry.id}/stage`,
url: `/review-workflows/content-manager/collection-types/${productUID}/${entry.documentId}/stage`,
body: {
data: { id: 1234 },
},
// TODO: Test with other locales
qs: { locale: entry.locale },
});

expect(response.status).toBe(400);
Expand Down Expand Up @@ -684,10 +690,12 @@ describeOnCondition(edition === 'EE')('Review workflows', () => {

const response = await requests.admin({
method: 'PUT',
url: `/review-workflows/content-manager/collection-types/${productUID}/${entry.id}/stage`,
url: `/review-workflows/content-manager/collection-types/${productUID}/${entry.documentId}/stage`,
body: {
data: { id: secondStage.id },
},
// TODO: Test with other locales
qs: { locale: entry.locale },
});

expect(response.status).toBe(400);
Expand Down Expand Up @@ -746,7 +754,7 @@ describeOnCondition(edition === 'EE')('Review workflows', () => {
});

test("It shouldn't be available for public", async () => {
const res = await requests.public.get(endpoint(entry.id));
const res = await requests.public.get(endpoint(entry.documentId));

if (hasRW) {
expect(res.status).toBe(401);
Expand All @@ -757,14 +765,14 @@ describeOnCondition(edition === 'EE')('Review workflows', () => {
});

test('It should return available stages for an admin user', async () => {
const res = await requests.admin.get(endpoint(entry.id));
const res = await requests.admin.get(endpoint(entry.documentId));

expect(res.body.data).toHaveLength(1);
expect(res.body.data[0]).toMatchObject(secondStage);
});

test('It should be forbidden when the user cannot read the content type', async () => {
const res = await restrictedRequest.get(endpoint(entry.id));
const res = await restrictedRequest.get(endpoint(entry.documentId));

expect(res.status).toBe(403);
});
Expand All @@ -778,7 +786,7 @@ describeOnCondition(edition === 'EE')('Review workflows', () => {
};
await utils.assignPermissionsToRole(restrictedRole.id, [permission]);

const res = await restrictedRequest.get(endpoint(entry.id));
const res = await restrictedRequest.get(endpoint(entry.documentId));

expect(res.body.data).toHaveLength(0);
});
Expand Down

0 comments on commit aeb3324

Please sign in to comment.