Skip to content

Commit

Permalink
fix(ref-imp): fix duplicate service id (#951)
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacJChen authored Dec 2, 2020
1 parent fabf550 commit dae2caf
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 18 deletions.
5 changes: 5 additions & 0 deletions lib/core/versions/latest/DocumentComposer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,13 +296,18 @@ export default class DocumentComposer {
throw new SidetreeError(ErrorCode.DocumentComposerPatchServicesNotArray);
}

const serviceIdSet: Set<string> = new Set();
for (const service of services) {
const serviceProperties = Object.keys(service);
if (serviceProperties.length !== 3) { // type, id, and serviceEndpoint
throw new SidetreeError(ErrorCode.DocumentComposerServiceHasMissingOrUnknownProperty);
}

DocumentComposer.validateId(service.id);
if (serviceIdSet.has(service.id)) {
throw new SidetreeError(ErrorCode.DocumentComposerPatchServiceIdNotUnique, 'Service id has to be unique');
}
serviceIdSet.add(service.id);

if (typeof service.type !== 'string') {
throw new SidetreeError(ErrorCode.DocumentComposerPatchServiceTypeNotString);
Expand Down
1 change: 1 addition & 0 deletions lib/core/versions/latest/ErrorCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export default {
DocumentComposerPatchServiceEndpointCannotBeAnArray: 'document_composer_patch_service_endpoint_cannot_be_an_array',
DocumentComposerPatchServiceEndpointMustBeStringOrNonArrayObject: 'document_composer_patch_service_endpoint_must_be_string_or_non_array_object',
DocumentComposerPatchServiceEndpointStringNotValidUri: 'document_composer_patch_service_endpoint_string_not_valid_uri',
DocumentComposerPatchServiceIdNotUnique: 'document_composer_patch_service_id_not_unique',
DocumentComposerPatchServiceIdsNotArray: 'document_composer_patch_service_ids_not_array',
DocumentComposerPatchServiceTypeMissingOrUnknown: 'document_composer_patch_service_type_missing_or_unknown',
DocumentComposerPatchServiceTypeNotString: 'document_composer_patch_service_type_not_string',
Expand Down
54 changes: 36 additions & 18 deletions tests/core/DocumentComposer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,23 +374,6 @@ describe('DocumentComposer', async () => {
});
});

describe('validateDocument', () => {
it('should throw DocumentComposerDocumentMissing if document is undefined', () => {
const expectedError = new SidetreeError(ErrorCode.DocumentComposerDocumentMissing);
expect(() => { DocumentComposer['validateDocument'](undefined); }).toThrow(expectedError);
});

it('should throw DocumentComposerServiceNotArray if `services` is not an array', () => {
const expectedError = new SidetreeError(ErrorCode.DocumentComposerPatchServicesNotArray);
const document = {
publicKeys: [{ id: 'aRepeatingId', type: 'someType', controller: 'someId' }],
services: 'this is not an array'
};
spyOn(DocumentComposer as any, 'validatePublicKeys').and.returnValue(1);
expect(() => { DocumentComposer['validateDocument'](document); }).toThrow(expectedError);
});
});

describe('validateDocumentPatches()', async () => {
it('should throw error if `patches` is not an array.', async () => {
const patches = 'shouldNotBeAString';
Expand Down Expand Up @@ -564,7 +547,42 @@ describe('DocumentComposer', async () => {
});

describe('validateDocument()', async () => {
it('should throw if document contains 2 keys of with the same ID.', async () => {
it('should throw DocumentComposerDocumentMissing if document is undefined', () => {
const expectedError = new SidetreeError(ErrorCode.DocumentComposerDocumentMissing);
expect(() => { DocumentComposer['validateDocument'](undefined); }).toThrow(expectedError);
});

it('should throw DocumentComposerServiceNotArray if `services` is not an array', () => {
const expectedError = new SidetreeError(ErrorCode.DocumentComposerPatchServicesNotArray);
const document = {
publicKeys: [{ id: 'aRepeatingId', type: 'someType', controller: 'someId' }],
services: 'this is not an array'
};
spyOn(DocumentComposer as any, 'validatePublicKeys').and.returnValue(1);
expect(() => { DocumentComposer['validateDocument'](document); }).toThrow(expectedError);
});

it('should throw if document contains 2 services with the same ID.', async () => {
const document: DocumentModel = {
services: [
{
id: 'key1',
type: 'URL',
serviceEndpoint: 'https://ion.is.cool/'
},
{
id: 'key1', // duplicate to cause failure
type: 'URL',
serviceEndpoint: 'https://ion.is.still.cool/'
}
]
};

const expectedError = new SidetreeError(ErrorCode.DocumentComposerPatchServiceIdNotUnique, 'Service id has to be unique');
expect(() => { DocumentComposer['validateDocument'](document); }).toThrow(expectedError);
});

it('should throw if document contains 2 keys with the same ID.', async () => {
const document = {
publicKeys: [
{
Expand Down

0 comments on commit dae2caf

Please sign in to comment.