Skip to content

Commit

Permalink
fix: accept new meta key format for forestadmin schema (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
olesyakorovina authored Jun 10, 2021
1 parent 6d0e494 commit 360b1a7
Show file tree
Hide file tree
Showing 3 changed files with 219 additions and 63 deletions.
29 changes: 26 additions & 3 deletions src/commands/schema/apply.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,37 @@ class ApplyCommand extends AbstractAuthenticatedCommand {
this.exit(1);
}

const stack = this.joi.object().keys({
orm_version: this.joi.string().required(),
database_type: this.joi.string().required(),
framework_version: this.joi.string().allow(null),
engine: this.joi.string().allow(null),
engine_version: this.joi.string().allow(null),
});

const validateRequiredWithStackPresence = this.joi.when('stack', {
is: this.joi.object().required(),
then: this.joi.forbidden(),
otherwise: this.joi.string().required(),
});

const allowNullWithStackPresence = this.joi.when('stack', {
is: this.joi.object().required(),
then: this.joi.forbidden(),
otherwise: this.joi.string().allow(null),
});

const { error } = this.joi.validate(schema, this.joi.object().keys({
collections: this.joi.array().items(this.joi.object()).required(),
meta: this.joi.object().keys({
liana: this.joi.string().required(),
orm_version: this.joi.string().required(),
database_type: this.joi.string().required(),
liana_version: this.joi.string().required(),
framework_version: this.joi.string().allow(null),
stack: stack.optional(),
orm_version: validateRequiredWithStackPresence,
database_type: validateRequiredWithStackPresence,
framework_version: allowNullWithStackPresence,
engine: allowNullWithStackPresence,
engine_version: allowNullWithStackPresence,
}).unknown().required(),
}), { convert: false });

Expand Down
63 changes: 63 additions & 0 deletions test/commands/schema/apply.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const {
const {
forestadminSchema,
forestadminSchemaSnake,
forestadminNewMetaFormat,
forestadminWrongMetaFormat,
} = require('../../fixtures/files');

function postSchemaMatch(body) {
Expand All @@ -35,6 +37,29 @@ function postSchemaMatch(body) {
return true;
}

function postSchemaNewMetaFormatMatch(body) {
expect(body).toMatchObject({
meta: {
liana: 'forest-express-sequelize',
liana_version: '2.16.9',
stack: {
orm_version: '3.24.8',
database_type: 'postgres',
},
},
data: [
{
type: 'collections',
id: 'Users',
attributes: {
name: 'Users',
},
},
],
});
return true;
}

describe('schema:apply', () => {
describe('when the user is not logged in', () => {
it('should login the user and then send the schema', () => testCli({
Expand Down Expand Up @@ -151,6 +176,44 @@ describe('schema:apply', () => {
],
}));
});

describe('with a schema with new meta format keys', () => {
it('should send the schema', () => testCli({
file: {
name: '.forestadmin-schema.json',
content: forestadminNewMetaFormat,
},
env: testEnv2,
token: 'any',
api: [postSchema(postSchemaNewMetaFormatMatch)],
command: () => ApplySchemaCommand.run([]),
std: [
{ out: 'Reading "./.forestadmin-schema.json"...' },
{
out: 'Using the forest environment secret found in the environment variable "FOREST_ENV_SECRET"',
},
{ out: 'Sending "./.forestadmin-schema.json"...' },
{ out: 'The schema is the same as before, nothing changed.' },
],
}));
});

describe('with a schema with new and old meta format keys', () => {
it('should exit with code 20', () => testCli({
file: {
name: '.forestadmin-schema.json',
content: forestadminWrongMetaFormat,
},
env: testEnv2,
token: 'any',
command: () => ApplySchemaCommand.run([]),
std: [{
err: '> Cannot properly read the ".forestadmin-schema.json" file:\n'
+ ' - "orm_version" is not allowed \n',
}],
exitCode: 20,
}));
});
});
});
});
Expand Down
190 changes: 130 additions & 60 deletions test/fixtures/files.js
Original file line number Diff line number Diff line change
@@ -1,66 +1,136 @@
module.exports = {
forestadminSchema: `{
"meta": {
"liana": "forest-express-sequelize",
"orm_version": "3.24.8",
"database_type": "postgres",
"liana_version": "2.16.9"
},
"collections": [
{
"name": "Users",
"idField": "id",
"primaryKeys": [
"id"
],
"isCompositePrimary": false,
"fields": [
{
"field": "id",
"type": "Number",
"columnName": "id",
"primaryKey": true
},
{
"field": "createdAt",
"type": "Date",
"columnName": "createdAt"
}
],
"isSearchable": true
"meta": {
"liana": "forest-express-sequelize",
"orm_version": "3.24.8",
"database_type": "postgres",
"liana_version": "2.16.9"
},
"collections": [
{
"name": "Users",
"idField": "id",
"primaryKeys": [
"id"
],
"isCompositePrimary": false,
"fields": [
{
"field": "id",
"type": "Number",
"columnName": "id",
"primaryKey": true
},
{
"field": "createdAt",
"type": "Date",
"columnName": "createdAt"
}
]
}`,
],
"isSearchable": true
}
]
}`,
forestadminSchemaSnake: `{
"meta": {
"liana": "forest-express-sequelize",
"orm_version": "3.24.8",
"database_type": "postgres",
"liana_version": "2.16.9"
"meta": {
"liana": "forest-express-sequelize",
"orm_version": "3.24.8",
"database_type": "postgres",
"liana_version": "2.16.9"
},
"collections": [
{
"name": "Users",
"id_field": "id",
"primary_keys": [
"id"
],
"is_composite_primary": false,
"fields": [
{
"field": "id",
"type": "Number",
"column_name": "id",
"primary_key": true
},
{
"field": "createdAt",
"type": "Date",
"column_name": "createdAt"
}
],
"is_searchable": true
}
]
}`,
forestadminNewMetaFormat: `{
"meta": {
"liana": "forest-express-sequelize",
"liana_version": "2.16.9",
"stack": {
"orm_version": "3.24.8",
"database_type": "postgres"
}
},
"collections": [
{
"name": "Users",
"id_field": "id",
"primary_keys": [
"id"
],
"is_composite_primary": false,
"fields": [
{
"field": "id",
"type": "Number",
"column_name": "id",
"primary_key": true
},
"collections": [
{
"name": "Users",
"id_field": "id",
"primary_keys": [
"id"
],
"is_composite_primary": false,
"fields": [
{
"field": "id",
"type": "Number",
"column_name": "id",
"primary_key": true
},
{
"field": "createdAt",
"type": "Date",
"column_name": "createdAt"
}
],
"is_searchable": true
}
]
}`,
{
"field": "createdAt",
"type": "Date",
"column_name": "createdAt"
}
],
"is_searchable": true
}
]
}`,
forestadminWrongMetaFormat: `{
"meta": {
"liana": "forest-express-sequelize",
"liana_version": "2.16.9",
"orm_version": "3.24.8",
"database_type": "postgres",
"stack": {
"orm_version": "3.24.8",
"database_type": "postgres"
}
},
"collections": [
{
"name": "Users",
"id_field": "id",
"primary_keys": [
"id"
],
"is_composite_primary": false,
"fields": [
{
"field": "id",
"type": "Number",
"column_name": "id",
"primary_key": true
},
{
"field": "createdAt",
"type": "Date",
"column_name": "createdAt"
}
],
"is_searchable": true
}
]
}`,
};

0 comments on commit 360b1a7

Please sign in to comment.