Skip to content

Commit

Permalink
validation fix for editing files (hyperledger-archives#2910)
Browse files Browse the repository at this point in the history
Signed-off-by: awjh-ibm <[email protected]>
  • Loading branch information
awjh-ibm authored and cazfletch committed Nov 30, 2017
1 parent 54120cf commit a7034fb
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,7 @@ export class EditorFileComponent {

let updatedFile = this.fileService.updateFile(this._editorFile.id, this.editorContent, type);

if (!this.currentError) {
// update the stored business network
this.fileService.updateBusinessNetwork(this._editorFile.id, updatedFile);
}
this.fileService.updateBusinessNetwork(this._editorFile.id, updatedFile);

this.fileService.businessNetworkChanged$.next(true);
} catch (e) {
Expand Down
129 changes: 106 additions & 23 deletions packages/composer-playground/src/app/services/file.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -816,29 +816,6 @@ describe('FileService', () => {
fileService['dirty'].should.equal(false);
})));

it('should update the correct script file', fakeAsync(inject([FileService], (fileService: FileService) => {
let file = new EditorFile('1', '1', 'this is the script', 'script');
let testScripts = new Map<string, EditorFile>();

let id = '1';
let content = 'this is the NEW script';
let type = 'script';

testScripts.set('1', file);

fileService['scriptFiles'] = testScripts;

fileService.updateFile(id, content, type);

let testFile = fileService.getFile('1', 'script');

testFile.getId().should.equal('1');
testFile.getContent().should.equal('this is the NEW script');
testFile.getType().should.equal('script');

fileService['dirty'].should.equal(true);
})));

it('should update the correct model file with a not namespace', fakeAsync(inject([FileService], (fileService: FileService) => {
let file = new EditorFile('1', '1', 'this is the model', 'model');
let testModels = new Map<string, EditorFile>();
Expand Down Expand Up @@ -877,6 +854,52 @@ describe('FileService', () => {
fileService['dirty'].should.equal(true);
})));

it('should update the correct script file', fakeAsync(inject([FileService], (fileService: FileService) => {
let file = new EditorFile('1', '1', 'this is the script', 'script');
let testScripts = new Map<string, EditorFile>();

sinon.stub(fileService, 'validateFile').returns(null);

let id = '1';
let content = 'this is the NEW script';
let type = 'script';

testScripts.set('1', file);

fileService['scriptFiles'] = testScripts;

fileService.updateFile(id, content, type);

let testFile = fileService.getFile('1', 'script');

testFile.getId().should.equal('1');
testFile.getContent().should.equal('this is the NEW script');
testFile.getType().should.equal('script');

fileService['dirty'].should.equal(true);
})));

it('should through an error if validate file on the script returns something', fakeAsync(inject([FileService], (fileService: FileService) => {
let file = new EditorFile('1', '1', 'this is the script', 'script');
let testScripts = new Map<string, EditorFile>();

sinon.stub(fileService, 'validateFile').returns('Validator error message');

let id = '1';
let content = 'this is the NEW script';
let type = 'script';

testScripts.set('1', file);

fileService['scriptFiles'] = testScripts;

(() => {
fileService.updateFile(id, content, type);
}).should.throw(/Validator error message/);

fileService['dirty'].should.equal(false);
})));

it('should throw an error if there is no script file with the given ID', fakeAsync(inject([FileService], (fileService: FileService) => {
let id = '1';
let content = 'this is the NEW script';
Expand All @@ -898,6 +921,8 @@ describe('FileService', () => {

fileService['queryFile'] = file;

sinon.stub(fileService, 'validateFile').returns(null);

fileService.updateFile(id, content, type);

let testFile = fileService.getFile('1', 'query');
Expand All @@ -909,6 +934,24 @@ describe('FileService', () => {
fileService['dirty'].should.equal(true);
})));

it('should through an error if validate file on the query returns something', fakeAsync(inject([FileService], (fileService: FileService) => {
let file = new EditorFile('1', '1', 'this is the query', 'query');

let id = '1';
let content = 'this is the NEW query';
let type = 'query';

fileService['queryFile'] = file;

sinon.stub(fileService, 'validateFile').returns('Validator error message');

(() => {
fileService.updateFile(id, content, type);
}).should.throw(/Validator error message/);

fileService['dirty'].should.equal(false);
})));

it('should throw an error if there is no query file with the given ID', fakeAsync(inject([FileService], (fileService: FileService) => {
let id = '1';
let content = 'this is the NEW query';
Expand All @@ -924,6 +967,8 @@ describe('FileService', () => {
it('should update the correct acl file', fakeAsync(inject([FileService], (fileService: FileService) => {
let file = new EditorFile('1', '1', 'this is the acl', 'acl');

sinon.stub(fileService, 'validateFile').returns(null);

let id = '1';
let content = 'this is the NEW acl';
let type = 'acl';
Expand All @@ -941,6 +986,24 @@ describe('FileService', () => {
fileService['dirty'].should.equal(true);
})));

it('should through an error if validate file on the acl returns something', fakeAsync(inject([FileService], (fileService: FileService) => {
let file = new EditorFile('1', '1', 'this is the acl', 'acl');

sinon.stub(fileService, 'validateFile').returns('Validator error message');

let id = '1';
let content = 'this is the NEW acl';
let type = 'acl';

fileService['aclFile'] = file;

(() => {
fileService.updateFile(id, content, type);
}).should.throw(/Validator error message/);

fileService['dirty'].should.equal(false);
})));

it('should throw an error if there is no acl file with the given ID', fakeAsync(inject([FileService], (fileService: FileService) => {
let id = '1';
let content = 'this is the NEW acl';
Expand Down Expand Up @@ -994,6 +1057,8 @@ describe('FileService', () => {

fileService['packageJson'] = file;

sinon.stub(fileService, 'validateFile').returns(null);

fileService.updateFile(id, content, type);

let testFile = fileService.getFile('1', 'package');
Expand All @@ -1005,6 +1070,24 @@ describe('FileService', () => {
fileService['dirty'].should.equal(true);
})));

it('should through an error if validate file on the packageJSON returns something', fakeAsync(inject([FileService], (fileService: FileService) => {
let file = new EditorFile('1', '1', '{"name" : "this is the NEW packageJson"}', 'package');

let id = '1';
let content = '{"name" : "this is the NEW packageJson"}';
let type = 'package';

fileService['packageJson'] = file;

sinon.stub(fileService, 'validateFile').returns('Validator error message');

(() => {
fileService.updateFile(id, content, type);
}).should.throw(/Validator error message/);

fileService['dirty'].should.equal(false);
})));

it('should throw an error if there is no packageJson file with the given ID', fakeAsync(inject([FileService], (fileService: FileService) => {
let id = '1';
let content = 'this is the NEW packageJson';
Expand Down
19 changes: 18 additions & 1 deletion packages/composer-playground/src/app/services/file.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ export class FileService {
// Handle the update of a file.
updateFile(id: string, content: string, type: string): EditorFile {
let updatedFile: EditorFile;
let validationError;
switch (type) {
// Deal with the update of a model file.
case 'model':
Expand All @@ -273,7 +274,7 @@ export class FileService {
let original: ModelFile = this.getModelFile(id);
if (original) {
let modelFile = this.createModelFile(content, original.getName());
let validationError = this.validateFile(id, type);
validationError = this.validateFile(id, type);
if (validationError) {
throw new Error(validationError);
}
Expand All @@ -297,6 +298,10 @@ export class FileService {
let updatedScriptFile = this.scriptFiles.get(id);
updatedScriptFile.setContent(content);
updatedFile = updatedScriptFile;
validationError = this.validateFile(id, type);
if (validationError) {
throw new Error(validationError);
}
break;
// Deal with the update of a query file.
case 'query':
Expand All @@ -305,6 +310,10 @@ export class FileService {
}
this.queryFile.setContent(content);
updatedFile = this.queryFile;
validationError = this.validateFile(id, type);
if (validationError) {
throw new Error(validationError);
}
break;
// Deal with the update of an acl file.
case 'acl':
Expand All @@ -313,6 +322,10 @@ export class FileService {
}
this.aclFile.setContent(content);
updatedFile = this.aclFile;
validationError = this.validateFile(id, type);
if (validationError) {
throw new Error(validationError);
}
break;
// Deal with the update of a readme file.
case 'readme':
Expand All @@ -328,6 +341,10 @@ export class FileService {
}
this.packageJson.setContent(JSON.parse(content));
updatedFile = this.packageJson;
validationError = this.validateFile(id, type);
if (validationError) {
throw new Error(validationError);
}
break;
default:
throw new Error('Attempted update of unknown file type: ' + type);
Expand Down

0 comments on commit a7034fb

Please sign in to comment.