strapi/packages/strapi-database/test/migration-draft-publish.test.e2e.js

173 lines
4.9 KiB
JavaScript
Raw Normal View History

2020-09-07 15:04:20 +02:00
'use strict';
const _ = require('lodash');
const { registerAndLogin } = require('../../../test/helpers/auth');
const { createAuthRequest } = require('../../../test/helpers/request');
const createModelsUtils = require('../../../test/helpers/models');
let rq;
let modelsUtils;
let data = {
dogs: [],
};
const dogModel = {
draftAndPublish: false,
attributes: {
name: {
type: 'string',
},
code: {
type: 'string',
unique: true,
},
2020-09-07 15:04:20 +02:00
},
connection: 'default',
name: 'dog',
description: '',
collectionName: '',
};
const dogs = [
{
name: 'Nelson',
code: '1',
2020-09-07 15:04:20 +02:00
},
{
name: 'Atos',
code: '2',
2020-09-07 15:04:20 +02:00
},
];
const sortDogs = dogs => _.sortBy(dogs, 'name');
2020-09-07 15:04:20 +02:00
describe('Migration - draft and publish', () => {
describe.each([
['without table modifications', {}, {}],
['with table modifications', { town: { type: 'string' } }, { color: { type: 'string' } }],
])('%p', (testName, tableModification1, tableModification2) => {
beforeAll(async () => {
data.dogs = [];
const token = await registerAndLogin();
rq = createAuthRequest(token);
modelsUtils = createModelsUtils({ rq });
await modelsUtils.createContentTypes([dogModel]);
const createdDogs = [];
for (const dog of dogs) {
const res = await rq({
method: 'POST',
url: '/content-manager/collection-types/application::dog.dog',
body: dog,
});
createdDogs.push(res.body);
}
data.dogs = sortDogs(createdDogs);
}, 60000);
2020-09-07 15:04:20 +02:00
afterAll(async () => {
await rq({
2020-09-07 15:04:20 +02:00
method: 'POST',
url: `/content-manager/collection-types/application::dog.dog/actions/bulkDelete`,
body: {
ids: data.dogs.map(({ id }) => id),
},
2020-09-07 15:04:20 +02:00
});
await modelsUtils.deleteContentTypes(['dog']);
}, 60000);
2020-09-07 15:04:20 +02:00
describe('Enabling D&P on a content-type', () => {
test('No published_at before enabling the feature', async () => {
let { body } = await rq({
url: '/content-manager/collection-types/application::dog.dog',
method: 'GET',
});
expect(body.results.length).toBe(2);
2020-09-07 15:04:20 +02:00
const sortedBody = sortDogs(body.results);
sortedBody.forEach((dog, index) => {
expect(dog).toMatchObject(data.dogs[index]);
expect(dog.published_at).toBeUndefined();
});
2020-09-07 15:04:20 +02:00
});
test('Published_at is equal to created_at after enabling the feature', async () => {
const schema = await modelsUtils.getContentTypeSchema('dog');
await modelsUtils.modifyContentType({
...schema,
attributes: _.merge(schema.attributes, tableModification1),
draftAndPublish: true,
});
2020-09-07 15:04:20 +02:00
let { body } = await rq({
method: 'GET',
url: '/content-manager/collection-types/application::dog.dog',
});
2020-09-07 15:04:20 +02:00
expect(body.results.length).toBe(2);
const sortedBody = sortDogs(body.results);
sortedBody.forEach((dog, index) => {
expect(dog).toMatchObject(data.dogs[index]);
expect(dog.published_at).toBe(dog.createdAt || dog.created_at);
expect(!isNaN(new Date(dog.published_at).valueOf())).toBe(true);
});
data.dogs = sortedBody;
2020-09-07 15:04:20 +02:00
});
});
describe('Disabling D&P on a content-type', () => {
test('No published_at after disabling the feature + draft removed', async () => {
const res = await rq({
method: 'POST',
url: `/content-manager/collection-types/application::dog.dog/${data.dogs[1].id}/actions/unpublish`,
});
2020-09-07 15:04:20 +02:00
data.dogs[1] = res.body;
2020-09-07 15:04:20 +02:00
const schema = await modelsUtils.getContentTypeSchema('dog');
await modelsUtils.modifyContentType({
...schema,
draftAndPublish: false,
attributes: _.merge(schema.attributes, tableModification2),
});
// drafts should have been deleted with the migration, so we remove them
data.dogs = data.dogs.filter(dog => !_.isNil(dog.published_at));
2020-09-07 15:04:20 +02:00
let { body } = await rq({
url: '/content-manager/collection-types/application::dog.dog',
method: 'GET',
});
2020-09-07 15:04:20 +02:00
expect(body.results.length).toBe(1);
expect(body.results[0]).toMatchObject(_.pick(data.dogs[0], ['name']));
expect(body.results[0].published_at).toBeUndefined();
2020-09-07 15:04:20 +02:00
});
test('Unique constraint is kept after disabling the feature', async () => {
const dogToCreate = { code: 'sameCode' };
let res = await rq({
method: 'POST',
url: `/content-manager/explorer/application::dog.dog/`,
body: dogToCreate,
});
expect(res.statusCode).toBe(200);
expect(res.body).toMatchObject(dogToCreate);
data.dogs.push(res.body);
res = await rq({
method: 'POST',
url: `/content-manager/explorer/application::dog.dog/`,
body: dogToCreate,
});
expect(res.statusCode).toBe(400);
});
2020-09-07 15:04:20 +02:00
});
});
});