Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ref-imp): fix duplicate service id #951

Merged
merged 3 commits into from
Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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