mirror of
https://github.com/strapi/strapi.git
synced 2025-11-13 16:52:18 +00:00
Merge pull request #14599 from strapi/relations-main-view/validation-test-findExisting-findAvailable
add validation tests on findAvailable and findExisting relations
This commit is contained in:
commit
987987e2df
@ -65,6 +65,15 @@ module.exports = {
|
|||||||
return ctx.forbidden();
|
return ctx.forbidden();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// eslint-disable-next-line no-lonely-if
|
||||||
|
if (entityId) {
|
||||||
|
const entity = await strapi.entityService.findOne(model, entityId);
|
||||||
|
|
||||||
|
if (!entity) {
|
||||||
|
return ctx.notFound();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const targetedModel = strapi.getModel(attribute.target);
|
const targetedModel = strapi.getModel(attribute.target);
|
||||||
@ -161,6 +170,12 @@ module.exports = {
|
|||||||
if (permissionChecker.cannot.read(entity, targetField)) {
|
if (permissionChecker.cannot.read(entity, targetField)) {
|
||||||
return ctx.forbidden();
|
return ctx.forbidden();
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
const entity = await strapi.entityService.findOne(model, id);
|
||||||
|
|
||||||
|
if (!entity) {
|
||||||
|
return ctx.notFound();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const targetedModel = strapi.getModel(attribute.target);
|
const targetedModel = strapi.getModel(attribute.target);
|
||||||
|
|||||||
@ -171,6 +171,124 @@ describe.each([[false], [true]])('Relations, with d&p: %p', (withDraftAndPublish
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('findAvailable', () => {
|
describe('findAvailable', () => {
|
||||||
|
describe('On a content-type', () => {
|
||||||
|
test('Fail when entity is not found', async () => {
|
||||||
|
const res = await rq({
|
||||||
|
method: 'GET',
|
||||||
|
url: '/content-manager/relations/api::shop.shop/products_ow',
|
||||||
|
qs: {
|
||||||
|
entityId: 99999,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(res.status).toBe(404);
|
||||||
|
expect(res.body).toMatchObject({
|
||||||
|
data: null,
|
||||||
|
error: {
|
||||||
|
details: {},
|
||||||
|
message: 'Not Found',
|
||||||
|
name: 'NotFoundError',
|
||||||
|
status: 404,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Fail when the field doesn't exist", async () => {
|
||||||
|
const res = await rq({
|
||||||
|
method: 'GET',
|
||||||
|
url: '/content-manager/relations/api::shop.shop/unkown',
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(res.status).toBe(400);
|
||||||
|
expect(res.body).toMatchObject({
|
||||||
|
data: null,
|
||||||
|
error: {
|
||||||
|
details: {},
|
||||||
|
message: "This relational field doesn't exist",
|
||||||
|
name: 'BadRequestError',
|
||||||
|
status: 400,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Fail when the field exists but is not a relational field', async () => {
|
||||||
|
const res = await rq({
|
||||||
|
method: 'GET',
|
||||||
|
url: '/content-manager/relations/api::shop.shop/name',
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(res.status).toBe(400);
|
||||||
|
expect(res.body).toMatchObject({
|
||||||
|
data: null,
|
||||||
|
error: {
|
||||||
|
details: {},
|
||||||
|
message: "This relational field doesn't exist",
|
||||||
|
name: 'BadRequestError',
|
||||||
|
status: 400,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('On a component', () => {
|
||||||
|
test('Fail when the component is not found', async () => {
|
||||||
|
const res = await rq({
|
||||||
|
method: 'GET',
|
||||||
|
url: '/content-manager/relations/default.compo/compo_products_ow',
|
||||||
|
qs: {
|
||||||
|
entityId: 99999,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(res.status).toBe(404);
|
||||||
|
expect(res.body).toMatchObject({
|
||||||
|
data: null,
|
||||||
|
error: {
|
||||||
|
details: {},
|
||||||
|
message: 'Not Found',
|
||||||
|
name: 'NotFoundError',
|
||||||
|
status: 404,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Fail when the field doesn't exist", async () => {
|
||||||
|
const res = await rq({
|
||||||
|
method: 'GET',
|
||||||
|
url: '/content-manager/relations/default.compo/unknown',
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(res.status).toBe(400);
|
||||||
|
expect(res.body).toMatchObject({
|
||||||
|
data: null,
|
||||||
|
error: {
|
||||||
|
details: {},
|
||||||
|
message: "This relational field doesn't exist",
|
||||||
|
name: 'BadRequestError',
|
||||||
|
status: 400,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Fail when the field exists but is not a relational field', async () => {
|
||||||
|
const res = await rq({
|
||||||
|
method: 'GET',
|
||||||
|
url: '/content-manager/relations/default.compo/name',
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(res.status).toBe(400);
|
||||||
|
expect(res.body).toMatchObject({
|
||||||
|
data: null,
|
||||||
|
error: {
|
||||||
|
details: {},
|
||||||
|
message: "This relational field doesn't exist",
|
||||||
|
name: 'BadRequestError',
|
||||||
|
status: 400,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe.each([
|
describe.each([
|
||||||
['products_ow', false],
|
['products_ow', false],
|
||||||
['products_oo', false],
|
['products_oo', false],
|
||||||
|
|||||||
@ -173,117 +173,229 @@ describe.each([false, true])('Relations, with d&p: %s', (withDraftAndPublish) =>
|
|||||||
await builder.cleanup();
|
await builder.cleanup();
|
||||||
});
|
});
|
||||||
describe('findExisting', () => {
|
describe('findExisting', () => {
|
||||||
describe.each([
|
describe('On a content-type', () => {
|
||||||
['products_ow', false],
|
test('Fail when entity is not found', async () => {
|
||||||
['products_oo', false],
|
|
||||||
['products_mo', false],
|
|
||||||
['products_om', true],
|
|
||||||
['products_mm', true],
|
|
||||||
['products_mw', true],
|
|
||||||
])('Relation not in a component (%s)', (fieldName, isManyRelation) => {
|
|
||||||
test('Can retrieve the relation(s) for an entity that have some relations', async () => {
|
|
||||||
const res = await rq({
|
const res = await rq({
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
url: `/content-manager/relations/api::shop.shop/${data.shops[0].id}/${fieldName}`,
|
url: `/content-manager/relations/api::shop.shop/999999/products_ow`,
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(res.status).toBe(200);
|
expect(res.status).toBe(404);
|
||||||
|
expect(res.body).toMatchObject({
|
||||||
if (isManyRelation) {
|
data: null,
|
||||||
expect(res.body.results).toMatchObject([
|
error: {
|
||||||
{
|
details: {},
|
||||||
id: expect.any(Number),
|
message: 'Not Found',
|
||||||
name: 'Candle',
|
name: 'NotFoundError',
|
||||||
...addPublishedAtCheck(null),
|
status: 404,
|
||||||
},
|
},
|
||||||
{
|
});
|
||||||
id: expect.any(Number),
|
|
||||||
name: 'Skate',
|
|
||||||
...addPublishedAtCheck(expect.any(String)),
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
} else {
|
|
||||||
expect(res.body.data).toMatchObject({
|
|
||||||
id: expect.any(Number),
|
|
||||||
name: 'Skate',
|
|
||||||
...addPublishedAtCheck(expect.any(String)),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test("Can retrieve the relation(s) for an entity that don't have relations yet", async () => {
|
test("Fail when the field doesn't exist", async () => {
|
||||||
const res = await rq({
|
const res = await rq({
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
url: `/content-manager/relations/api::shop.shop/${data.shops[1].id}/${fieldName}`,
|
url: `/content-manager/relations/api::shop.shop/${data.shops[0].id}/unkown`,
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(res.status).toBe(200);
|
expect(res.status).toBe(400);
|
||||||
if (isManyRelation) {
|
expect(res.body).toMatchObject({
|
||||||
expect(res.body.results).toHaveLength(0);
|
data: null,
|
||||||
} else {
|
error: {
|
||||||
expect(res.body.data).toBe(null);
|
details: {},
|
||||||
}
|
message: "This relational field doesn't exist",
|
||||||
|
name: 'BadRequestError',
|
||||||
|
status: 400,
|
||||||
|
},
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
if (isManyRelation) {
|
test('Fail when the field exists but is not a relational field', async () => {
|
||||||
test("Can search ''", async () => {
|
const res = await rq({
|
||||||
|
method: 'GET',
|
||||||
|
url: `/content-manager/relations/api::shop.shop/${data.shops[0].id}/name`,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(res.status).toBe(400);
|
||||||
|
expect(res.body).toMatchObject({
|
||||||
|
data: null,
|
||||||
|
error: {
|
||||||
|
details: {},
|
||||||
|
message: "This relational field doesn't exist",
|
||||||
|
name: 'BadRequestError',
|
||||||
|
status: 400,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe.each([
|
||||||
|
['one-way', 'products_ow', false],
|
||||||
|
['one-one', 'products_oo', false],
|
||||||
|
['many-to-one', 'products_mo', false],
|
||||||
|
['one-to-many', 'products_om', true],
|
||||||
|
['many-many', 'products_mm', true],
|
||||||
|
['many-way', 'products_mw', true],
|
||||||
|
])('%s relation (%s)', (relationType, fieldName, isManyRelation) => {
|
||||||
|
test('Can retrieve the relation(s) for an entity that have some relations', async () => {
|
||||||
const res = await rq({
|
const res = await rq({
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
url: `/content-manager/relations/api::shop.shop/${data.shops[0].id}/${fieldName}`,
|
url: `/content-manager/relations/api::shop.shop/${data.shops[0].id}/${fieldName}`,
|
||||||
qs: {
|
|
||||||
_q: '',
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(res.status).toBe(200);
|
expect(res.status).toBe(200);
|
||||||
expect(res.body.results).toMatchObject([
|
|
||||||
{
|
if (isManyRelation) {
|
||||||
id: expect.any(Number),
|
expect(res.body.results).toMatchObject([
|
||||||
name: 'Candle',
|
{
|
||||||
...addPublishedAtCheck(null),
|
id: expect.any(Number),
|
||||||
},
|
name: 'Candle',
|
||||||
{
|
...addPublishedAtCheck(null),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: expect.any(Number),
|
||||||
|
name: 'Skate',
|
||||||
|
...addPublishedAtCheck(expect.any(String)),
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
} else {
|
||||||
|
expect(res.body.data).toMatchObject({
|
||||||
id: expect.any(Number),
|
id: expect.any(Number),
|
||||||
name: 'Skate',
|
name: 'Skate',
|
||||||
...addPublishedAtCheck(expect.any(String)),
|
...addPublishedAtCheck(expect.any(String)),
|
||||||
},
|
});
|
||||||
]);
|
}
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
describe.each([
|
|
||||||
['compo_products_ow', false],
|
|
||||||
['compo_products_mw', true],
|
|
||||||
])('Relation in a component (%s)', (fieldName, isManyRelation) => {
|
|
||||||
test('Can retrieve the relation(s)', async () => {
|
|
||||||
const res = await rq({
|
|
||||||
method: 'GET',
|
|
||||||
url: `/content-manager/relations/default.compo/${data.shops[0].myCompo.id}/${fieldName}`,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(res.status).toBe(200);
|
test("Can retrieve the relation(s) for an entity that don't have relations yet", async () => {
|
||||||
|
const res = await rq({
|
||||||
|
method: 'GET',
|
||||||
|
url: `/content-manager/relations/api::shop.shop/${data.shops[1].id}/${fieldName}`,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
if (isManyRelation) {
|
||||||
|
expect(res.body.results).toHaveLength(0);
|
||||||
|
} else {
|
||||||
|
expect(res.body.data).toBe(null);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
if (isManyRelation) {
|
if (isManyRelation) {
|
||||||
expect(res.body.results).toMatchObject([
|
test("Can search ''", async () => {
|
||||||
{
|
const res = await rq({
|
||||||
id: expect.any(Number),
|
method: 'GET',
|
||||||
name: 'Candle',
|
url: `/content-manager/relations/api::shop.shop/${data.shops[0].id}/${fieldName}`,
|
||||||
...addPublishedAtCheck(null),
|
qs: {
|
||||||
},
|
_q: '',
|
||||||
{
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(res.body.results).toMatchObject([
|
||||||
|
{
|
||||||
|
id: expect.any(Number),
|
||||||
|
name: 'Candle',
|
||||||
|
...addPublishedAtCheck(null),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: expect.any(Number),
|
||||||
|
name: 'Skate',
|
||||||
|
...addPublishedAtCheck(expect.any(String)),
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('On a component', () => {
|
||||||
|
test('Fail when the component is not found', async () => {
|
||||||
|
const res = await rq({
|
||||||
|
method: 'GET',
|
||||||
|
url: `/content-manager/relations/default.compo/999999/compo_products_ow`,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(res.status).toBe(404);
|
||||||
|
expect(res.body).toMatchObject({
|
||||||
|
data: null,
|
||||||
|
error: {
|
||||||
|
details: {},
|
||||||
|
message: 'Not Found',
|
||||||
|
name: 'NotFoundError',
|
||||||
|
status: 404,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Fail when the field doesn't exist", async () => {
|
||||||
|
const res = await rq({
|
||||||
|
method: 'GET',
|
||||||
|
url: `/content-manager/relations/default.compo/${data.shops[0].myCompo.id}/unknown`,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(res.status).toBe(400);
|
||||||
|
expect(res.body).toMatchObject({
|
||||||
|
data: null,
|
||||||
|
error: {
|
||||||
|
details: {},
|
||||||
|
message: "This relational field doesn't exist",
|
||||||
|
name: 'BadRequestError',
|
||||||
|
status: 400,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Fail when the field exists but is not a relational field', async () => {
|
||||||
|
const res = await rq({
|
||||||
|
method: 'GET',
|
||||||
|
url: `/content-manager/relations/default.compo/${data.shops[0].myCompo.id}/name`,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(res.status).toBe(400);
|
||||||
|
expect(res.body).toMatchObject({
|
||||||
|
data: null,
|
||||||
|
error: {
|
||||||
|
details: {},
|
||||||
|
message: "This relational field doesn't exist",
|
||||||
|
name: 'BadRequestError',
|
||||||
|
status: 400,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe.each([
|
||||||
|
['one-way', 'compo_products_ow', false],
|
||||||
|
['many-way', 'compo_products_mw', true],
|
||||||
|
])('%s relation (%s)', (relationType, fieldName, isManyRelation) => {
|
||||||
|
test('Can retrieve the relation(s)', async () => {
|
||||||
|
const res = await rq({
|
||||||
|
method: 'GET',
|
||||||
|
url: `/content-manager/relations/default.compo/${data.shops[0].myCompo.id}/${fieldName}`,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
|
||||||
|
if (isManyRelation) {
|
||||||
|
expect(res.body.results).toMatchObject([
|
||||||
|
{
|
||||||
|
id: expect.any(Number),
|
||||||
|
name: 'Candle',
|
||||||
|
...addPublishedAtCheck(null),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: expect.any(Number),
|
||||||
|
name: 'Skate',
|
||||||
|
...addPublishedAtCheck(expect.any(String)),
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
} else {
|
||||||
|
expect(res.body.data).toMatchObject({
|
||||||
id: expect.any(Number),
|
id: expect.any(Number),
|
||||||
name: 'Skate',
|
name: 'Skate',
|
||||||
...addPublishedAtCheck(expect.any(String)),
|
...addPublishedAtCheck(expect.any(String)),
|
||||||
},
|
});
|
||||||
]);
|
}
|
||||||
} else {
|
});
|
||||||
expect(res.body.data).toMatchObject({
|
|
||||||
id: expect.any(Number),
|
|
||||||
name: 'Skate',
|
|
||||||
...addPublishedAtCheck(expect.any(String)),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user