Skip to content

Commit

Permalink
Add validation checks
Browse files Browse the repository at this point in the history
  • Loading branch information
rebeccahum committed Dec 7, 2023
1 parent edc0bad commit 78db9ae
Show file tree
Hide file tree
Showing 4 changed files with 348 additions and 173 deletions.
106 changes: 106 additions & 0 deletions __tests__/bin/vip-deploy-app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { deployAppCmd, renameFile, gates, promptToContinue } from '../../src/bin/vip-deploy-app';
import * as exit from '../../src/lib/cli/exit';
import { validateDeployFileExt, validateFilename } from '../../src/lib/validations/manual-deploy';

jest.mock( '../../src/lib/client-file-uploader', () => ( {
...jest.requireActual( '../../src/lib/client-file-uploader' ),
getFileMeta: jest
.fn()
.mockResolvedValue( { fileName: '/vip/skeleton.zip', basename: 'skeleton.zip' } ),
uploadImportSqlFileToS3: jest.fn(),
} ) );

jest.mock( 'fs', () => ( {
...jest.requireActual( 'fs' ),
existsSync: jest.fn().mockReturnValue( true ),
} ) );

jest.mock( '../../src/bin/vip-deploy-app', () => ( {
...jest.requireActual( '../../src/bin/vip-deploy-app' ),
gates: jest.fn(),
renameFile: jest.fn(),
promptToContinue: jest.fn(),
} ) );

jest.mock( '../../src/lib/cli/command', () => {
const commandMock = {
argv: () => commandMock,
examples: () => commandMock,
option: () => commandMock,
};
return jest.fn( () => commandMock );
} );

const exitSpy = jest.spyOn( exit, 'withError' );
jest.spyOn( process, 'exit' ).mockImplementation( () => {} );

const args = [ '/vip/skeleton.zip' ];
const opts = {
app: {
id: 1,
organization: {
id: 2,
},
},
env: {
id: 3,
type: 'develop',
},
};

describe( 'vip-deploy-app', () => {
describe( 'validations', () => {
beforeEach( async () => {
exitSpy.mockClear();
} );

it.each( [ '$t2.tar.gz', 'vip-go!!skeleton.zip', '1-(vip).tgz' ] )(
'fails if the deploy file has has invalid characters',
async basename => {
validateFilename( basename );
expect( exitSpy ).toHaveBeenCalledWith(
'Error: The characters used in the name of a file for manual deploys are limited to [0-9,a-z,A-Z,-,_,.]'
);
}
);

it.each( [ 'test-repo.rar', 'vip-go-skeleton.sql', 'vip.test' ] )(
'fails if the deploy file has an invalid extension',
async basename => {
validateDeployFileExt( basename );
expect( exitSpy ).toHaveBeenCalledWith(
'Invalid file extension. Please provide a .zip, .tar.gz, or a .tgz file.'
);
}
);

it.each( [ 'test-repo.tar.gz', 'vip-go-skeleton.zip', 'vip.tgz' ] )(
'passes if the deploy file has a valid extension',
async basename => {
validateDeployFileExt( basename );
expect( exitSpy ).not.toHaveBeenCalled();
}
);
} );

describe( 'deployAppCmd', () => {
it( 'should call validation functions', async () => {
await deployAppCmd( args, opts );

expect( gates ).toHaveBeenCalledTimes( 1 );
} );

it( 'should not prompt if --force is passed in', async () => {
opts.force = true;
await deployAppCmd( args, opts );

expect( promptToContinue ).not.toHaveBeenCalled();
} );

it( 'should rename file', async () => {
await deployAppCmd( args, opts );

expect( renameFile ).toHaveBeenCalledTimes( 1 );
} );
} );
} );
Loading

0 comments on commit 78db9ae

Please sign in to comment.