-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(schema-diff): compare two environments api map (#476)
- Loading branch information
Showing
11 changed files
with
367 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
const EnvironmentManager = require('../../services/environment-manager'); | ||
const AbstractAuthenticatedCommand = require('../../abstract-authenticated-command'); | ||
|
||
class DiffCommand extends AbstractAuthenticatedCommand { | ||
init(plan) { | ||
super.init(plan); | ||
const { | ||
assertPresent, | ||
chalk, | ||
env, | ||
environmentRenderer, | ||
errorHandler, | ||
} = this.context; | ||
assertPresent({ chalk, env }); | ||
this.chalk = chalk; | ||
this.env = env; | ||
this.environmentRenderer = environmentRenderer; | ||
this.errorHandler = errorHandler; | ||
} | ||
|
||
async runIfAuthenticated() { | ||
const parsed = this.parse(DiffCommand); | ||
const config = { ...this.env, ...parsed.flags, ...parsed.args }; | ||
const manager = new EnvironmentManager(config); | ||
|
||
const { environmentIdFrom, environmentIdTo } = config; | ||
try { | ||
const [apimapFrom, apimapTo] = await Promise.all([ | ||
manager.getEnvironmentApimap(environmentIdFrom), | ||
manager.getEnvironmentApimap(environmentIdTo), | ||
]); | ||
|
||
this.environmentRenderer.renderApimapDiff(apimapFrom, apimapTo); | ||
} catch (error) { | ||
this.logger.error( | ||
`Cannot fetch the environments ${this.chalk.bold(environmentIdFrom)} and ${this.chalk.bold(environmentIdTo)}.`, | ||
); | ||
this.logger.error(manager.handleEnvironmentError(error)); | ||
} | ||
} | ||
} | ||
|
||
DiffCommand.description = 'Allow to compare two environment schemas'; | ||
|
||
DiffCommand.flags = { | ||
help: AbstractAuthenticatedCommand.flags.boolean({ | ||
description: 'Display usage information.', | ||
}), | ||
}; | ||
|
||
DiffCommand.args = [ | ||
{ name: 'environmentIdFrom', required: true, description: 'ID of an environment to compare.' }, | ||
{ name: 'environmentIdTo', required: true, description: 'ID of an environment to compare.' }, | ||
]; | ||
module.exports = DiffCommand; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
const jsonDiff = require('json-diff'); | ||
const testCli = require('../test-cli-helper/test-cli'); | ||
const DiffSchemaCommand = require('../../../src/commands/schema/diff'); | ||
const { testEnvWithSecret } = require('../../fixtures/env'); | ||
const { | ||
loginValidOidc, getEnvironmentApimap, | ||
getEnvironmentApimapForbidden, | ||
} = require('../../fixtures/api'); | ||
|
||
describe('schema:diff', () => { | ||
describe('when the user is not logged in', () => { | ||
it('should login the user and does the diff', () => testCli({ | ||
env: testEnvWithSecret, | ||
api: [ | ||
() => loginValidOidc(), | ||
() => getEnvironmentApimap(10), | ||
() => getEnvironmentApimap(11), | ||
], | ||
commandClass: DiffSchemaCommand, | ||
commandArgs: ['10', '11'], | ||
std: [ | ||
{ out: '> Login required.' }, | ||
{ out: 'Click on "Log in" on the browser tab which opened automatically or open this link: http://app.localhost/device/check?code=ABCD' }, | ||
{ out: 'Your confirmation code: USER-CODE' }, | ||
{ out: '> Login successful' }, | ||
{ out: '√ The schemas are identical.' }, | ||
], | ||
})); | ||
}); | ||
|
||
describe('when the user is logged in', () => { | ||
describe('when schemas are identical', () => { | ||
it('display "identical" message', () => testCli({ | ||
env: testEnvWithSecret, | ||
token: 'any', | ||
api: [ | ||
() => getEnvironmentApimap(10), | ||
() => getEnvironmentApimap(11), | ||
], | ||
commandClass: DiffSchemaCommand, | ||
commandArgs: ['10', '11'], | ||
std: [ | ||
{ out: '√ The schemas are identical.' }, | ||
], | ||
})); | ||
}); | ||
|
||
describe('when schemas are not identical', () => { | ||
const apiMapA = { collections: [{ name: 'Users' }] }; | ||
const apiMapB = { collections: [{ name: 'Users' }, { name: 'Posts' }] }; | ||
|
||
it('display the diff message', () => testCli({ | ||
env: testEnvWithSecret, | ||
token: 'any', | ||
api: [ | ||
() => getEnvironmentApimap(10, apiMapA), | ||
() => getEnvironmentApimap(11, apiMapB), | ||
], | ||
commandClass: DiffSchemaCommand, | ||
commandArgs: ['10', '11'], | ||
std: [ | ||
{ out: '⚠ The schemas have differences.' }, | ||
{ in: jsonDiff.diffString(apiMapA, apiMapB) }, | ||
], | ||
})); | ||
}); | ||
|
||
describe('when there is an error', () => { | ||
it('should display an error message', () => testCli({ | ||
env: testEnvWithSecret, | ||
token: 'any', | ||
api: [ | ||
() => getEnvironmentApimap(10), | ||
() => getEnvironmentApimapForbidden(99999), | ||
], | ||
commandClass: DiffSchemaCommand, | ||
commandArgs: ['10', '99999'], | ||
std: [ | ||
{ err: '× Cannot fetch the environments 10 and 99999.' }, | ||
{ err: '× Oops something went wrong.' }, | ||
], | ||
})); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const Context = require('@forestadmin/context'); | ||
const EnvironmentManager = require('../../src/services/environment-manager'); | ||
const defaultPlan = require('../../src/context/plan'); | ||
|
||
describe('services > EnvironmentManager', () => { | ||
const buildManager = () => { | ||
// we must init the context for enable the dependency injection | ||
Context.init(defaultPlan); | ||
|
||
return new EnvironmentManager({}); | ||
}; | ||
describe('handleEnvironmentError', () => { | ||
describe('when the error is unknown', () => { | ||
it('should return the receives error', () => { | ||
expect.assertions(1); | ||
const error = new Error('error'); | ||
const result = buildManager().handleEnvironmentError(error); | ||
expect(result).toBe('error'); | ||
}); | ||
}); | ||
|
||
describe('when the error is a 403 Forbidden', () => { | ||
it('should return a forbidden error', () => { | ||
expect.assertions(1); | ||
const error = new Error('Forbidden'); | ||
const result = buildManager().handleEnvironmentError(error); | ||
expect(result).toBe('You do not have the permission to perform this action on the given environments.'); | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.