860 lines
26 KiB
JavaScript
Raw Normal View History

'use strict';
2019-03-09 01:06:39 +01:00
// Helpers.
2021-04-29 11:11:46 +02:00
const { createTestBuilder } = require('../../../../test/helpers/builder');
const { createStrapiInstance } = require('../../../../test/helpers/strapi');
const form = require('../../../../test/helpers/generators');
const { createAuthRequest } = require('../../../../test/helpers/request');
2019-03-09 01:06:39 +01:00
const cleanDate = entry => {
delete entry.updatedAt;
delete entry.createdAt;
};
const builder = createTestBuilder();
let strapi;
2019-03-09 01:06:39 +01:00
let data;
let rq;
const deleteFixtures = async () => {
for (const [name, modelName] of [
['references', 'reference'],
['tags', 'tag'],
['categories', 'category'],
['articles', 'article'],
['articlesWithTag', 'articlewithtag'],
]) {
2021-08-06 18:09:49 +02:00
const uid = `api::${modelName}.${modelName}`;
2021-08-11 09:34:55 +02:00
if (data[name] && data[name].length > 0) {
await rq({
method: 'POST',
url: `/content-manager/collection-types/${uid}/actions/bulkDelete`,
body: {
ids: (data[name] || []).map(({ id }) => id),
},
});
}
}
};
2019-03-09 01:06:39 +01:00
describe('Content Manager End to End', () => {
beforeAll(async () => {
await builder
.addContentTypes(
[form.article, form.tag, form.category, form.reference, form.articlewithtag],
{ batch: true }
)
.build();
strapi = await createStrapiInstance();
rq = await createAuthRequest({ strapi });
});
2019-03-09 01:06:39 +01:00
afterAll(async () => {
await strapi.destroy();
await builder.cleanup();
});
2019-03-09 01:06:39 +01:00
describe('Content Types api', () => {
test('Label is pluralized', async () => {
const res = await rq({
url: `/content-manager/content-types`,
method: 'GET',
});
expect(res.statusCode).toBe(200);
expect(res.body.data).toEqual(
expect.arrayContaining([
expect.objectContaining({
info: expect.objectContaining({
label: 'Articles',
}),
}),
expect.objectContaining({
info: expect.objectContaining({
label: 'Tags',
}),
}),
expect.objectContaining({
info: expect.objectContaining({
label: 'Categories',
}),
}),
])
);
});
});
2019-03-09 01:06:39 +01:00
describe('Test manyToMany relation (article - tag) with Content Manager', () => {
beforeAll(async () => {
data = {
articles: [],
tags: [],
};
});
afterAll(async () => {
await deleteFixtures();
});
2019-03-09 01:06:39 +01:00
test('Create tag1', async () => {
let { body } = await rq({
2021-08-06 18:09:49 +02:00
url: '/content-manager/collection-types/api::tag.tag',
2019-03-09 01:06:39 +01:00
method: 'POST',
body: {
2019-03-09 01:06:39 +01:00
name: 'tag1',
},
});
data.tags.push(body);
2021-07-05 23:31:23 +02:00
expect(body.id).toBeDefined();
// expect(Array.isArray(body.articles)).toBeTruthy();
2019-03-09 01:06:39 +01:00
expect(body.name).toBe('tag1');
2021-07-05 23:31:23 +02:00
// expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
// expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.published_at).toBeUndefined();
2019-03-09 01:06:39 +01:00
});
test('Create tag2', async () => {
let { body } = await rq({
2021-08-06 18:09:49 +02:00
url: '/content-manager/collection-types/api::tag.tag',
2019-03-09 01:06:39 +01:00
method: 'POST',
body: {
2019-03-09 01:06:39 +01:00
name: 'tag2',
},
});
data.tags.push(body);
2021-07-05 23:31:23 +02:00
expect(body.id).toBeDefined();
// expect(Array.isArray(body.articles)).toBeTruthy();
2019-03-09 01:06:39 +01:00
expect(body.name).toBe('tag2');
2021-07-05 23:31:23 +02:00
// expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
// expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.published_at).toBeUndefined();
2019-03-09 01:06:39 +01:00
});
test('Create tag3', async () => {
let { body } = await rq({
2021-08-06 18:09:49 +02:00
url: '/content-manager/collection-types/api::tag.tag',
2019-03-09 01:06:39 +01:00
method: 'POST',
body: {
2019-03-09 01:06:39 +01:00
name: 'tag3',
},
});
data.tags.push(body);
2021-07-05 23:31:23 +02:00
expect(body.id).toBeDefined();
// expect(Array.isArray(body.articles)).toBeTruthy();
2019-03-09 01:06:39 +01:00
expect(body.name).toBe('tag3');
2021-07-05 23:31:23 +02:00
// expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
// expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.published_at).toBeUndefined();
2019-03-09 01:06:39 +01:00
});
test('Create article1 without relation', async () => {
const entry = {
title: 'Article 1',
content: 'My super content 1',
date: '2019-08-13T00:00:00.000Z',
2019-03-09 01:06:39 +01:00
};
let { body } = await rq({
2021-08-06 18:09:49 +02:00
url: '/content-manager/collection-types/api::article.article',
2019-03-09 01:06:39 +01:00
method: 'POST',
body: entry,
2019-03-09 01:06:39 +01:00
});
data.articles.push(body);
2021-07-05 23:31:23 +02:00
expect(body.id).toBeDefined();
2019-03-09 01:06:39 +01:00
expect(body.title).toBe(entry.title);
expect(body.content).toBe(entry.content);
2021-07-05 23:31:23 +02:00
// expect(Array.isArray(body.tags)).toBeTruthy();
// expect(body.tags.length).toBe(0);
// expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
// expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.published_at).toBeUndefined();
2019-03-09 01:06:39 +01:00
});
test('Create article2 with tag1', async () => {
const entry = {
title: 'Article 2',
content: 'Content 2',
2021-08-11 09:34:55 +02:00
tags: [data.tags[0].id],
2019-03-09 01:06:39 +01:00
};
let { body } = await rq({
2021-08-06 18:09:49 +02:00
url: '/content-manager/collection-types/api::article.article',
2019-03-09 01:06:39 +01:00
method: 'POST',
body: entry,
2019-03-09 01:06:39 +01:00
});
data.articles.push(body);
2021-07-05 23:31:23 +02:00
expect(body.id).toBeDefined();
2019-03-09 01:06:39 +01:00
expect(body.title).toBe(entry.title);
expect(body.content).toBe(entry.content);
2021-08-11 09:34:55 +02:00
expect(Array.isArray(body.tags)).toBeTruthy();
expect(body.tags.length).toBe(1);
expect(body.tags[0].id).toBe(data.tags[0].id);
expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.published_at).toBeUndefined();
2019-03-09 01:06:39 +01:00
});
test('Update article1 add tag2', async () => {
const entry = Object.assign({}, data.articles[0], {
2021-08-11 09:34:55 +02:00
tags: [data.tags[1].id],
2019-03-09 01:06:39 +01:00
});
cleanDate(entry);
let { body } = await rq({
2021-08-06 18:09:49 +02:00
url: `/content-manager/collection-types/api::article.article/${entry.id}`,
2019-03-09 01:06:39 +01:00
method: 'PUT',
body: entry,
2019-03-09 01:06:39 +01:00
});
data.articles[0] = body;
2021-07-05 23:31:23 +02:00
expect(body.id).toBeDefined();
2019-03-09 01:06:39 +01:00
expect(body.title).toBe(entry.title);
expect(body.content).toBe(entry.content);
2021-08-11 09:34:55 +02:00
expect(Array.isArray(body.tags)).toBeTruthy();
expect(body.tags.length).toBe(1);
expect(body.tags[0].id).toBe(data.tags[1].id);
expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.published_at).toBeUndefined();
2019-03-09 01:06:39 +01:00
});
test('Update article1 add tag1 and tag3', async () => {
const entry = Object.assign({}, data.articles[0]);
2021-08-11 09:34:55 +02:00
entry.tags = entry.tags.map(tag => tag.id);
entry.tags.push(data.tags[0].id);
entry.tags.push(data.tags[2].id);
2019-03-09 01:06:39 +01:00
cleanDate(entry);
let { body } = await rq({
2021-08-06 18:09:49 +02:00
url: `/content-manager/collection-types/api::article.article/${entry.id}`,
2019-03-09 01:06:39 +01:00
method: 'PUT',
body: entry,
2019-03-09 01:06:39 +01:00
});
data.articles[0] = body;
2021-07-05 23:31:23 +02:00
expect(body.id).toBeDefined();
2019-03-09 01:06:39 +01:00
expect(body.title).toBe(entry.title);
expect(body.content).toBe(entry.content);
2021-08-11 09:34:55 +02:00
expect(Array.isArray(body.tags)).toBeTruthy();
expect(body.tags.length).toBe(3);
expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.published_at).toBeUndefined();
2019-03-09 01:06:39 +01:00
});
test('Update article1 remove one tag', async () => {
const entry = Object.assign({}, data.articles[0]);
2021-08-11 09:34:55 +02:00
entry.tags = entry.tags.slice(1).map(tag => tag.id);
2019-03-09 01:06:39 +01:00
cleanDate(entry);
let { body } = await rq({
2021-08-06 18:09:49 +02:00
url: `/content-manager/collection-types/api::article.article/${entry.id}`,
2019-03-09 01:06:39 +01:00
method: 'PUT',
body: entry,
2019-03-09 01:06:39 +01:00
});
data.articles[0] = body;
2021-07-05 23:31:23 +02:00
expect(body.id).toBeDefined();
2019-03-09 01:06:39 +01:00
expect(body.title).toBe(entry.title);
expect(body.content).toBe(entry.content);
2021-08-11 09:34:55 +02:00
expect(Array.isArray(body.tags)).toBeTruthy();
expect(body.tags.length).toBe(2);
expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.published_at).toBeUndefined();
2019-03-09 01:06:39 +01:00
});
test('Update article1 remove all tag', async () => {
const entry = Object.assign({}, data.articles[0], {
tags: [],
});
cleanDate(entry);
let { body } = await rq({
2021-08-06 18:09:49 +02:00
url: `/content-manager/collection-types/api::article.article/${entry.id}`,
2019-03-09 01:06:39 +01:00
method: 'PUT',
body: entry,
2019-03-09 01:06:39 +01:00
});
data.articles[0] = body;
2021-07-05 23:31:23 +02:00
expect(body.id).toBeDefined();
2019-03-09 01:06:39 +01:00
expect(body.title).toBe(entry.title);
expect(body.content).toBe(entry.content);
2021-08-11 09:34:55 +02:00
expect(Array.isArray(body.tags)).toBeTruthy();
expect(body.tags.length).toBe(0);
expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.published_at).toBeUndefined();
});
2019-03-09 01:06:39 +01:00
test('Delete all articles should remove the association in each tags related to them', async () => {
2019-03-12 11:58:30 +01:00
const { body: createdTag } = await rq({
2021-08-06 18:09:49 +02:00
url: '/content-manager/collection-types/api::tag.tag',
2019-03-09 01:06:39 +01:00
method: 'POST',
body: {
2019-03-09 01:06:39 +01:00
name: 'tag11',
},
});
const { body: article12 } = await rq({
2021-08-06 18:09:49 +02:00
url: '/content-manager/collection-types/api::article.article',
2019-03-09 01:06:39 +01:00
method: 'POST',
body: {
2019-03-09 01:06:39 +01:00
title: 'article12',
content: 'Content',
2021-07-05 23:31:23 +02:00
tags: [createdTag.id],
2019-03-09 01:06:39 +01:00
},
});
2019-03-12 11:58:30 +01:00
const { body: updatedTag } = await rq({
2021-08-06 18:09:49 +02:00
url: `/content-manager/collection-types/api::tag.tag/${createdTag.id}`,
2019-03-12 11:58:30 +01:00
method: 'GET',
});
2019-03-09 01:06:39 +01:00
const { body: article13 } = await rq({
2021-08-06 18:09:49 +02:00
url: '/content-manager/collection-types/api::article.article',
2019-03-09 01:06:39 +01:00
method: 'POST',
body: {
2019-03-09 01:06:39 +01:00
title: 'article13',
content: 'Content',
2021-07-05 23:31:23 +02:00
tags: [updatedTag.id],
2019-03-09 01:06:39 +01:00
},
});
const articles = [article12, article13];
2021-08-04 19:39:40 +02:00
expect(Array.isArray(articles[0].tags)).toBeTruthy();
expect(articles[0].tags.length).toBe(1);
expect(Array.isArray(articles[1].tags)).toBeTruthy();
expect(articles[1].tags.length).toBe(1);
2019-03-09 01:06:39 +01:00
2021-07-05 23:31:23 +02:00
let { body: foundTag } = await rq({
2021-08-06 18:09:49 +02:00
url: `/content-manager/collection-types/api::tag.tag/${createdTag.id}`,
2019-03-09 01:06:39 +01:00
method: 'GET',
});
2021-08-04 19:39:40 +02:00
expect(Array.isArray(foundTag.articles)).toBeTruthy();
expect(foundTag.articles.length).toBe(2);
2019-03-09 01:06:39 +01:00
await rq({
2021-08-06 18:09:49 +02:00
url: '/content-manager/collection-types/api::article.article/actions/bulkDelete',
method: 'POST',
body: {
ids: articles.map(article => article.id),
},
2019-03-09 01:06:39 +01:00
});
2021-07-05 23:31:23 +02:00
let { body: foundTag2 } = await rq({
2021-08-06 18:09:49 +02:00
url: `/content-manager/collection-types/api::tag.tag/${createdTag.id}`,
2019-03-09 01:06:39 +01:00
method: 'GET',
});
2021-08-04 19:39:40 +02:00
expect(Array.isArray(foundTag2.articles)).toBeTruthy();
expect(foundTag2.articles.length).toBe(0);
2019-03-09 01:06:39 +01:00
});
});
2019-07-08 17:34:56 +02:00
describe('Test manyWay articlesWithTags and tags', () => {
beforeAll(() => {
data = {
tags: [],
articlesWithTag: [],
};
});
afterAll(async () => {
await deleteFixtures();
});
2019-07-08 17:34:56 +02:00
test('Creating an article with some many way tags', async () => {
const { body: createdTag } = await rq({
2021-08-06 18:09:49 +02:00
url: '/content-manager/collection-types/api::tag.tag',
method: 'POST',
body: {
name: 'tag11',
},
});
data.tags.push(createdTag);
2019-07-08 17:34:56 +02:00
const entry = {
tags: [createdTag.id],
2019-07-08 17:34:56 +02:00
};
let { body } = await rq({
2021-08-06 18:09:49 +02:00
url: '/content-manager/collection-types/api::articlewithtag.articlewithtag',
2019-07-08 17:34:56 +02:00
method: 'POST',
body: entry,
2019-07-08 17:34:56 +02:00
});
data.articlesWithTag.push(body);
2021-07-05 23:31:23 +02:00
expect(body.id).toBeDefined();
2021-08-11 09:34:55 +02:00
expect(Array.isArray(body.tags)).toBeTruthy();
expect(body.tags.length).toBe(1);
expect(body.tags[0].id).toBe(data.tags[0].id);
expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
2019-07-08 17:34:56 +02:00
});
});
2019-03-09 01:06:39 +01:00
describe('Test oneToMany - manyToOne relation (article - category) with Content Manager', () => {
beforeAll(() => {
data = {
articles: [],
categories: [],
};
});
afterAll(async () => {
await deleteFixtures();
});
2019-03-09 01:06:39 +01:00
test('Create cat1', async () => {
let { body } = await rq({
2021-08-06 18:09:49 +02:00
url: '/content-manager/collection-types/api::category.category',
2019-03-09 01:06:39 +01:00
method: 'POST',
body: {
2019-03-09 01:06:39 +01:00
name: 'cat1',
},
});
data.categories.push(body);
2021-07-05 23:31:23 +02:00
expect(body.id).toBeDefined();
2021-08-11 09:34:55 +02:00
expect(Array.isArray(body.articles)).toBeTruthy();
2019-03-09 01:06:39 +01:00
expect(body.name).toBe('cat1');
2021-08-11 09:34:55 +02:00
expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.published_at).toBeUndefined();
2019-03-09 01:06:39 +01:00
});
test('Create cat2', async () => {
let { body } = await rq({
2021-08-06 18:09:49 +02:00
url: '/content-manager/collection-types/api::category.category',
2019-03-09 01:06:39 +01:00
method: 'POST',
body: {
2019-03-09 01:06:39 +01:00
name: 'cat2',
},
});
data.categories.push(body);
2021-07-05 23:31:23 +02:00
expect(body.id).toBeDefined();
2021-08-11 09:34:55 +02:00
expect(Array.isArray(body.articles)).toBeTruthy();
2019-03-09 01:06:39 +01:00
expect(body.name).toBe('cat2');
2021-08-11 09:34:55 +02:00
expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.published_at).toBeUndefined();
2019-03-09 01:06:39 +01:00
});
test('Create article1 with cat1', async () => {
const entry = {
title: 'Article 1',
content: 'Content 1',
2021-08-11 09:34:55 +02:00
category: data.categories[0].id,
2019-03-09 01:06:39 +01:00
};
let { body } = await rq({
2021-08-06 18:09:49 +02:00
url: '/content-manager/collection-types/api::article.article',
2019-03-09 01:06:39 +01:00
method: 'POST',
body: entry,
2019-03-09 01:06:39 +01:00
});
data.articles.push(body);
2021-07-05 23:31:23 +02:00
expect(body.id).toBeDefined();
2019-03-09 01:06:39 +01:00
expect(body.title).toBe(entry.title);
expect(body.content).toBe(entry.content);
2021-08-11 09:34:55 +02:00
expect(body.category.name).toBe(data.categories[0].name);
expect(Array.isArray(body.tags)).toBeTruthy();
expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
2019-03-09 01:06:39 +01:00
});
test('Update article1 with cat2', async () => {
const entry = Object.assign({}, data.articles[0], {
2021-08-11 09:34:55 +02:00
category: data.categories[1].id,
2019-03-09 01:06:39 +01:00
});
cleanDate(entry);
let { body } = await rq({
2021-08-06 18:09:49 +02:00
url: `/content-manager/collection-types/api::article.article/${entry.id}`,
2019-03-09 01:06:39 +01:00
method: 'PUT',
body: entry,
2019-03-09 01:06:39 +01:00
});
data.articles[0] = body;
2021-07-05 23:31:23 +02:00
expect(body.id).toBeDefined();
2019-03-09 01:06:39 +01:00
expect(body.title).toBe(entry.title);
expect(body.content).toBe(entry.content);
2021-08-11 09:34:55 +02:00
expect(body.category.name).toBe(data.categories[1].name);
expect(Array.isArray(body.tags)).toBeTruthy();
expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
2019-03-09 01:06:39 +01:00
});
test('Create article2', async () => {
const entry = {
title: 'Article 2',
content: 'Content 2',
};
let { body } = await rq({
2021-08-06 18:09:49 +02:00
url: '/content-manager/collection-types/api::article.article',
2019-03-09 01:06:39 +01:00
method: 'POST',
body: entry,
2019-03-09 01:06:39 +01:00
});
data.articles.push(body);
2021-07-05 23:31:23 +02:00
expect(body.id).toBeDefined();
2019-03-09 01:06:39 +01:00
expect(body.title).toBe(entry.title);
expect(body.content).toBe(entry.content);
2021-08-11 09:34:55 +02:00
expect(Array.isArray(body.tags)).toBeTruthy();
expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
2019-03-09 01:06:39 +01:00
});
test('Update article2 with cat2', async () => {
const entry = Object.assign({}, data.articles[1], {
2021-08-11 09:34:55 +02:00
category: data.categories[1].id,
2019-03-09 01:06:39 +01:00
});
cleanDate(entry);
let { body } = await rq({
2021-08-06 18:09:49 +02:00
url: `/content-manager/collection-types/api::article.article/${entry.id}`,
2019-03-09 01:06:39 +01:00
method: 'PUT',
body: entry,
2019-03-09 01:06:39 +01:00
});
data.articles[1] = body;
2021-07-05 23:31:23 +02:00
expect(body.id).toBeDefined();
2019-03-09 01:06:39 +01:00
expect(body.title).toBe(entry.title);
expect(body.content).toBe(entry.content);
2021-08-11 09:34:55 +02:00
expect(body.category.name).toBe(data.categories[1].name);
expect(Array.isArray(body.tags)).toBeTruthy();
expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
2019-03-09 01:06:39 +01:00
});
test('Update cat1 with article1', async () => {
const entry = Object.assign({}, data.categories[0]);
2021-08-11 09:34:55 +02:00
entry.articles = entry.articles.map(article => article.id);
entry.articles.push(data.articles[0].id);
2019-03-09 01:06:39 +01:00
cleanDate(entry);
let { body } = await rq({
2021-08-06 18:09:49 +02:00
url: `/content-manager/collection-types/api::category.category/${entry.id}`,
2019-03-09 01:06:39 +01:00
method: 'PUT',
body: entry,
2019-03-09 01:06:39 +01:00
});
data.categories[0] = body;
2021-07-05 23:31:23 +02:00
expect(body.id).toBeDefined();
2021-08-11 09:34:55 +02:00
expect(Array.isArray(body.articles)).toBeTruthy();
expect(body.articles.length).toBe(1);
2019-03-09 01:06:39 +01:00
expect(body.name).toBe(entry.name);
2021-08-11 09:34:55 +02:00
expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
2019-03-09 01:06:39 +01:00
});
test('Create cat3 with article1', async () => {
const entry = {
name: 'cat3',
2021-08-11 09:34:55 +02:00
articles: [data.articles[0].id],
2019-03-09 01:06:39 +01:00
};
let { body } = await rq({
2021-08-06 18:09:49 +02:00
url: '/content-manager/collection-types/api::category.category',
2019-03-09 01:06:39 +01:00
method: 'POST',
body: entry,
2019-03-09 01:06:39 +01:00
});
data.categories.push(body);
2021-07-05 23:31:23 +02:00
expect(body.id).toBeDefined();
2021-08-11 09:34:55 +02:00
expect(Array.isArray(body.articles)).toBeTruthy();
expect(body.articles.length).toBe(1);
2019-03-09 01:06:39 +01:00
expect(body.name).toBe(entry.name);
2021-08-11 09:34:55 +02:00
expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
2019-03-09 01:06:39 +01:00
});
test('Get article1 with cat3', async () => {
let { body } = await rq({
2021-08-06 18:09:49 +02:00
url: `/content-manager/collection-types/api::article.article/${data.articles[0].id}`,
2019-03-09 01:06:39 +01:00
method: 'GET',
});
2021-07-05 23:31:23 +02:00
expect(body.id).toBeDefined();
2021-08-11 09:34:55 +02:00
expect(body.category.id).toBe(data.categories[2].id);
expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
2019-03-09 01:06:39 +01:00
});
test('Get article2 with cat2', async () => {
let { body } = await rq({
2021-08-06 18:09:49 +02:00
url: `/content-manager/collection-types/api::article.article/${data.articles[1].id}`,
2019-03-09 01:06:39 +01:00
method: 'GET',
});
2021-07-05 23:31:23 +02:00
expect(body.id).toBeDefined();
2021-08-11 09:34:55 +02:00
expect(body.category.id).toBe(data.categories[1].id);
expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
2019-03-09 01:06:39 +01:00
});
test('Get cat1 without relations', async () => {
let { body } = await rq({
2021-08-06 18:09:49 +02:00
url: `/content-manager/collection-types/api::category.category/${data.categories[0].id}`,
2019-03-09 01:06:39 +01:00
method: 'GET',
});
2021-07-05 23:31:23 +02:00
expect(body.id).toBeDefined();
2021-08-11 09:34:55 +02:00
expect(body.articles.length).toBe(0);
expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
2019-03-09 01:06:39 +01:00
});
test('Get cat2 with article2', async () => {
let { body } = await rq({
2021-08-06 18:09:49 +02:00
url: `/content-manager/collection-types/api::category.category/${data.categories[1].id}`,
2019-03-09 01:06:39 +01:00
method: 'GET',
});
2021-07-05 23:31:23 +02:00
expect(body.id).toBeDefined();
2021-08-11 09:34:55 +02:00
expect(body.articles.length).toBe(1);
expect(body.articles[0].id).toBe(data.articles[1].id);
expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
2019-03-09 01:06:39 +01:00
});
test('Get cat3 with article1', async () => {
let { body } = await rq({
2021-08-06 18:09:49 +02:00
url: `/content-manager/collection-types/api::category.category/${data.categories[2].id}`,
2019-03-09 01:06:39 +01:00
method: 'GET',
});
2021-07-05 23:31:23 +02:00
expect(body.id).toBeDefined();
2021-08-11 09:34:55 +02:00
expect(body.articles.length).toBe(1);
expect(body.articles[0].id).toBe(data.articles[0].id);
expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
2019-03-09 01:06:39 +01:00
});
});
describe('Test oneToOne relation (article - reference) with Content Manager', () => {
beforeAll(() => {
data = {
articles: [],
references: [],
};
});
afterAll(async () => {
await deleteFixtures();
});
2019-03-09 01:06:39 +01:00
test('Create ref1', async () => {
let { body } = await rq({
2021-08-06 18:09:49 +02:00
url: '/content-manager/collection-types/api::reference.reference',
2019-03-09 01:06:39 +01:00
method: 'POST',
body: {
2019-03-09 01:06:39 +01:00
name: 'ref1',
},
});
data.references.push(body);
2021-07-05 23:31:23 +02:00
expect(body.id).toBeDefined();
2019-03-09 01:06:39 +01:00
expect(body.name).toBe('ref1');
2021-08-11 09:34:55 +02:00
expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
2019-03-09 01:06:39 +01:00
});
test('Create article1', async () => {
const entry = {
title: 'Article 1',
content: 'Content 1',
};
let { body } = await rq({
2021-08-06 18:09:49 +02:00
url: '/content-manager/collection-types/api::article.article',
2019-03-09 01:06:39 +01:00
method: 'POST',
body: entry,
2019-03-09 01:06:39 +01:00
});
data.articles.push(body);
2021-07-05 23:31:23 +02:00
expect(body.id).toBeDefined();
2019-03-09 01:06:39 +01:00
expect(body.title).toBe(entry.title);
expect(body.content).toBe(entry.content);
2021-08-11 09:34:55 +02:00
expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.published_at).toBeUndefined();
2019-03-09 01:06:39 +01:00
});
test('Update article1 with ref1', async () => {
const entry = Object.assign({}, data.articles[0], {
reference: data.references[0].id,
});
cleanDate(entry);
let { body } = await rq({
2021-08-06 18:09:49 +02:00
url: `/content-manager/collection-types/api::article.article/${entry.id}`,
2019-03-09 01:06:39 +01:00
method: 'PUT',
body: entry,
2019-03-09 01:06:39 +01:00
});
data.articles[0] = body;
2021-07-05 23:31:23 +02:00
expect(body.id).toBeDefined();
2019-03-09 01:06:39 +01:00
expect(body.title).toBe(entry.title);
expect(body.content).toBe(entry.content);
2021-08-11 09:34:55 +02:00
expect(body.reference.id).toBe(entry.reference);
expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
2019-03-09 01:06:39 +01:00
});
test('Create article2 with ref1', async () => {
const entry = {
title: 'Article 2',
content: 'Content 2',
reference: data.references[0].id,
};
let { body } = await rq({
2021-08-06 18:09:49 +02:00
url: '/content-manager/collection-types/api::article.article',
2019-03-09 01:06:39 +01:00
method: 'POST',
body: entry,
2019-03-09 01:06:39 +01:00
});
data.articles.push(body);
2021-07-05 23:31:23 +02:00
expect(body.id).toBeDefined();
2019-03-09 01:06:39 +01:00
expect(body.title).toBe(entry.title);
expect(body.content).toBe(entry.content);
2021-08-11 09:34:55 +02:00
expect(body.reference.id).toBe(entry.reference);
expect(body.created_by).toMatchObject({ email: 'admin@strapi.io' });
expect(body.updated_by).toMatchObject({ email: 'admin@strapi.io' });
2019-03-09 01:06:39 +01:00
});
});
describe('Test oneWay relation (reference - tag) with Content Manager', () => {
test('Attach Tag to a Reference', async () => {
2021-07-05 23:31:23 +02:00
const { body: createdTag } = await rq({
2021-08-06 18:09:49 +02:00
url: '/content-manager/collection-types/api::tag.tag',
2019-03-09 01:06:39 +01:00
method: 'POST',
body: {
2019-03-09 01:06:39 +01:00
name: 'tag111',
},
});
2021-07-05 23:31:23 +02:00
const { body: createdReference } = await rq({
2021-08-06 18:09:49 +02:00
url: '/content-manager/collection-types/api::reference.reference',
2019-03-09 01:06:39 +01:00
method: 'POST',
body: {
2019-03-09 01:06:39 +01:00
name: 'cat111',
2021-08-11 09:34:55 +02:00
tag: createdTag.id,
2019-03-09 01:06:39 +01:00
},
});
2021-07-05 23:31:23 +02:00
expect(createdReference.id).toBeDefined();
2021-08-11 09:34:55 +02:00
expect(createdReference.tag.id).toBe(createdTag.id);
2019-03-09 01:06:39 +01:00
});
test('Detach Tag to a Reference', async () => {
2021-07-05 23:31:23 +02:00
const { body: createdTag } = await rq({
2021-08-06 18:09:49 +02:00
url: '/content-manager/collection-types/api::tag.tag',
2019-03-09 01:06:39 +01:00
method: 'POST',
body: {
2019-03-09 01:06:39 +01:00
name: 'tag111',
},
});
2021-07-05 23:31:23 +02:00
const { body: createdReference } = await rq({
2021-08-06 18:09:49 +02:00
url: '/content-manager/collection-types/api::reference.reference',
2019-03-09 01:06:39 +01:00
method: 'POST',
body: {
2019-03-09 01:06:39 +01:00
name: 'cat111',
2021-08-04 19:39:40 +02:00
tag: createdTag.id,
2019-03-09 01:06:39 +01:00
},
});
2021-08-04 19:39:40 +02:00
expect(createdReference.tag.id).toBe(createdTag.id);
2019-03-09 01:06:39 +01:00
const { body: referenceToUpdate } = await rq({
2021-08-06 18:09:49 +02:00
url: `/content-manager/collection-types/api::reference.reference/${createdReference.id}`,
2019-03-09 01:06:39 +01:00
method: 'PUT',
body: {
2019-03-09 01:06:39 +01:00
tag: null,
},
});
2021-08-04 19:39:40 +02:00
expect(referenceToUpdate.tag).toBe(null);
2019-03-09 01:06:39 +01:00
});
test('Delete Tag so the relation in the Reference side should be removed', async () => {
2021-07-05 23:31:23 +02:00
const { body: createdTag } = await rq({
2021-08-06 18:09:49 +02:00
url: '/content-manager/collection-types/api::tag.tag',
2019-03-09 01:06:39 +01:00
method: 'POST',
body: {
2019-03-09 01:06:39 +01:00
name: 'tag111',
},
});
2021-07-05 23:31:23 +02:00
const { body: createdReference } = await rq({
2021-08-06 18:09:49 +02:00
url: '/content-manager/collection-types/api::reference.reference',
2019-03-09 01:06:39 +01:00
method: 'POST',
body: {
2019-03-09 01:06:39 +01:00
name: 'cat111',
2021-08-11 09:34:55 +02:00
tag: createdTag.id,
2019-03-09 01:06:39 +01:00
},
});
await rq({
2021-08-06 18:09:49 +02:00
url: `/content-manager/collection-types/api::tag.tag/${createdTag.id}`,
2019-03-09 01:06:39 +01:00
method: 'DELETE',
});
2021-07-05 23:31:23 +02:00
const { body: foundReference } = await rq({
2021-08-06 18:09:49 +02:00
url: `/content-manager/collection-types/api::reference.reference/${createdReference.id}`,
2019-03-09 01:06:39 +01:00
method: 'GET',
});
2021-07-05 23:31:23 +02:00
if (!foundReference.tag || Object.keys(foundReference.tag).length === 0) return;
expect(foundReference.tag).toBe(null);
2019-03-09 01:06:39 +01:00
});
});
});