2020-10-14 19:04:22 +02:00
|
|
|
'use strict';
|
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
const { createTestBuilder } = require('../../../test/helpers/builder');
|
|
|
|
const { createStrapiInstance } = require('../../../test/helpers/strapi');
|
2020-10-14 19:04:22 +02:00
|
|
|
const { createAuthRequest } = require('../../../test/helpers/request');
|
2020-11-17 15:38:41 +01:00
|
|
|
const modelsUtils = require('../../../test/helpers/models');
|
2020-10-14 19:04:22 +02:00
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
const builder = createTestBuilder();
|
|
|
|
let strapi;
|
2020-10-14 19:04:22 +02:00
|
|
|
let rq;
|
|
|
|
let data = {
|
|
|
|
dogs: [],
|
|
|
|
};
|
|
|
|
|
|
|
|
const dogModel = {
|
|
|
|
draftAndPublish: false,
|
|
|
|
attributes: {
|
|
|
|
name: {
|
|
|
|
type: 'string',
|
|
|
|
required: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
connection: 'default',
|
|
|
|
name: 'dog',
|
|
|
|
description: '',
|
|
|
|
collectionName: '',
|
|
|
|
};
|
|
|
|
|
|
|
|
const dogs = [
|
|
|
|
{
|
|
|
|
name: null,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Atos',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
const restart = async () => {
|
|
|
|
await strapi.destroy();
|
2020-11-30 20:20:36 +01:00
|
|
|
strapi = await createStrapiInstance();
|
2020-11-17 15:38:41 +01:00
|
|
|
rq = await createAuthRequest({ strapi });
|
|
|
|
};
|
|
|
|
|
2020-10-14 19:04:22 +02:00
|
|
|
describe('Migration - required attribute', () => {
|
|
|
|
beforeAll(async () => {
|
2020-11-17 15:38:41 +01:00
|
|
|
await builder
|
|
|
|
.addContentType(dogModel)
|
|
|
|
.addFixtures(dogModel.name, dogs)
|
|
|
|
.build();
|
2020-11-02 17:19:42 +01:00
|
|
|
|
2020-11-30 20:20:36 +01:00
|
|
|
strapi = await createStrapiInstance();
|
2020-11-17 15:38:41 +01:00
|
|
|
rq = await createAuthRequest({ strapi });
|
|
|
|
|
|
|
|
data.dogs = builder.sanitizedFixturesFor(dogModel.name, strapi);
|
2020-10-14 19:04:22 +02:00
|
|
|
}, 60000);
|
|
|
|
|
|
|
|
afterAll(async () => {
|
2020-11-17 15:38:41 +01:00
|
|
|
await strapi.destroy();
|
|
|
|
await builder.cleanup();
|
2021-03-25 14:59:44 +01:00
|
|
|
}, 60000);
|
2020-10-14 19:04:22 +02:00
|
|
|
|
|
|
|
describe('Required: false -> true', () => {
|
|
|
|
test('Can be null before migration', async () => {
|
|
|
|
let { body } = await rq({
|
|
|
|
method: 'GET',
|
2020-11-03 13:42:01 +01:00
|
|
|
url: '/content-manager/collection-types/application::dog.dog',
|
2020-10-14 19:04:22 +02:00
|
|
|
});
|
2020-11-03 13:42:01 +01:00
|
|
|
expect(body.results.length).toBe(2);
|
|
|
|
const dogWithNameNull = body.results.find(dog => dog.name === null);
|
2020-10-14 19:04:22 +02:00
|
|
|
expect(dogWithNameNull).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Cannot create an entry with null after migration', async () => {
|
|
|
|
// remove null values otherwise the migration would fail
|
2020-11-23 14:43:07 +01:00
|
|
|
|
2020-11-06 14:27:57 +01:00
|
|
|
const { body } = await rq({
|
2020-10-14 19:04:22 +02:00
|
|
|
method: 'PUT',
|
2020-11-03 13:42:01 +01:00
|
|
|
url: `/content-manager/collection-types/application::dog.dog/${data.dogs[0].id}`,
|
2020-10-14 19:04:22 +02:00
|
|
|
body: { name: 'Nelson' },
|
|
|
|
});
|
2020-11-06 14:27:57 +01:00
|
|
|
data.dogs[0] = body;
|
2020-10-14 19:04:22 +02:00
|
|
|
|
|
|
|
// migration
|
2020-11-17 15:38:41 +01:00
|
|
|
const schema = await modelsUtils.getContentTypeSchema(dogModel.name, { strapi });
|
2020-10-14 19:04:22 +02:00
|
|
|
schema.attributes.name.required = true;
|
2020-11-17 15:38:41 +01:00
|
|
|
|
|
|
|
await modelsUtils.modifyContentType(schema, { strapi });
|
|
|
|
await restart();
|
2020-10-14 19:04:22 +02:00
|
|
|
|
|
|
|
// Try to create an entry with null
|
|
|
|
const res = await rq({
|
|
|
|
method: 'POST',
|
2020-11-02 17:19:42 +01:00
|
|
|
url: '/content-manager/collection-types/application::dog.dog',
|
2020-10-14 19:04:22 +02:00
|
|
|
body: { name: null },
|
|
|
|
});
|
|
|
|
expect(res.body.message).toBe('ValidationError');
|
2020-11-20 18:21:43 +01:00
|
|
|
}, 60000);
|
2020-10-14 19:04:22 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('Required: true -> false', () => {
|
|
|
|
test('Can create an entry with null after migration', async () => {
|
|
|
|
// migration
|
2020-11-17 15:38:41 +01:00
|
|
|
const schema = await modelsUtils.getContentTypeSchema(dogModel.name, { strapi });
|
2020-10-14 19:04:22 +02:00
|
|
|
schema.attributes.name.required = false;
|
2020-11-17 15:38:41 +01:00
|
|
|
|
|
|
|
await modelsUtils.modifyContentType(schema, { strapi });
|
|
|
|
await restart();
|
2020-10-14 19:04:22 +02:00
|
|
|
|
|
|
|
// Try to create an entry with null
|
|
|
|
const res = await rq({
|
2020-11-02 17:19:42 +01:00
|
|
|
url: `/content-manager/collection-types/application::dog.dog`,
|
2020-10-14 19:04:22 +02:00
|
|
|
method: 'POST',
|
|
|
|
body: { name: null },
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.body).toMatchObject({ name: null });
|
2020-11-06 14:27:57 +01:00
|
|
|
data.dogs.push(res.body);
|
2020-11-20 18:21:43 +01:00
|
|
|
}, 60000);
|
2020-10-14 19:04:22 +02:00
|
|
|
});
|
|
|
|
});
|