Skip to content

Commit

Permalink
feat(ref-imp): #781 - add even more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacJChen committed Sep 22, 2020
1 parent d10ac4a commit 6921d8b
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/core/versions/latest/CreateOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ export default class CreateOperation implements OperationModel {
* @param anchorFileMode If set to true, then `delta` and `type` properties are expected to be absent.
*/
public static async parseObject (operationObject: any, operationBuffer: Buffer, anchorFileMode: boolean): Promise<CreateOperation> {
// TODO SIP 2 #781 deprecates this. Should be deleted when fully switched over
let expectedPropertyCount = 3;
if (anchorFileMode) {
expectedPropertyCount = 1;
Expand Down
1 change: 1 addition & 0 deletions lib/core/versions/latest/Did.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ export default class Did {
}

private static async constructCreateOperationFromInitialState (initialState: string): Promise<CreateOperation> {
// TODO SIP 2 #781 deprecates this. Should be deleted when fully switched over
// Initial state should be in the format: <suffix-data>.<delta>
const firstIndexOfDot = initialState.indexOf('.');
if (firstIndexOfDot === -1) {
Expand Down
42 changes: 42 additions & 0 deletions tests/core/CreateOperation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,36 @@ import OperationType from '../../lib/core/enums/OperationType';
import SidetreeError from '../../lib/common/SidetreeError';

describe('CreateOperation', async () => {
describe('parseJcsObject', () => {
it('should throw sidetree error if object contains more or less than 3 properties', () => {
const twoProperties = { one: 1, two: 2 };
const fourProperties = { one: 1, two: 2, three: 3, four: 4 };

try {
CreateOperation.parseJcsObject(twoProperties, Buffer.from(JSON.stringify(twoProperties)));
fail('expect to throw sidetree error but did not');
} catch (e) {
expect(e).toEqual(new SidetreeError(ErrorCode.CreateOperationMissingOrUnknownProperty));
}

try {
CreateOperation.parseJcsObject(fourProperties, Buffer.from(JSON.stringify(fourProperties)));
fail('expect to throw sidetree error but did not');
} catch (e) {
expect(e).toEqual(new SidetreeError(ErrorCode.CreateOperationMissingOrUnknownProperty));
}
})

it('should throw sidetree error if type is not create', () => {
const testObject = {type: 'notCreate', two: 2, three: 3};
try {
CreateOperation.parseJcsObject(testObject, Buffer.from(JSON.stringify(testObject)));
fail('expect to throw sidetree error but did not');
} catch (e) {
expect(e).toEqual(new SidetreeError(ErrorCode.CreateOperationTypeIncorrect));
}
})
})
describe('computeDidUniqueSuffix()', async () => {
it('should pass test vector.', async (done) => {
const suffixDataString = 'AStringActingAsTheSuffixData';
Expand Down Expand Up @@ -56,7 +86,19 @@ describe('CreateOperation', async () => {
});
});

describe('validateSuffixData', () => {
it('should throw if the input is not an object', () => {
const input = 'this is not an object, this is a string';
try {
CreateOperation['validateSuffixData'](input);
} catch (e) {
expect(e).toEqual(new SidetreeError(ErrorCode.CreateOperationSuffixDataIsNotObject));
}
});
})

describe('parseSuffixData()', async () => {
// TODO SIP 2 #781 deprecates this. These tests can be siwtched over to validateSuffixData
it('should function as expected with type', async () => {
const suffixData = {
delta_hash: Encoder.encode(Multihash.hash(Buffer.from('some data'))),
Expand Down
24 changes: 24 additions & 0 deletions tests/core/Did.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,34 @@
import * as crypto from 'crypto';
import Did from '../../lib/core/versions/latest/Did';
import Encoder from '../../lib/core/versions/latest/Encoder';
import ErrorCode from '../../lib/core/versions/latest/ErrorCode';
import JasmineSidetreeErrorValidator from '../JasmineSidetreeErrorValidator';
import OperationGenerator from '../generators/OperationGenerator';
import SidetreeError from '../../lib/common/SidetreeError';

describe('DID', async () => {
describe('constructCreateOperationFromEncodedJCS', () => {
it('should throw sidetree error if initial state is not an json', () => {
const testInitialState = Encoder.encode('notJson');
try {
Did['constructCreateOperationFromEncodedJCS'](testInitialState)
fail('expect to throw sidetree error but did not');
} catch (e) {
expect(e).toEqual(new SidetreeError(ErrorCode.DidInitialStateJcsIsNotJosn, 'long form initial state should be encoded jcs'));
}
})

it('should throw sidetree error if initial state is not jcs', () => {
const testInitialState = Encoder.encode(JSON.stringify({z: 1, a: 2, b: 1}));
try {
Did['constructCreateOperationFromEncodedJCS'](testInitialState)
fail('expect to throw sidetree error but did not');
} catch (e) {
expect(e).toEqual(new SidetreeError(ErrorCode.DidInitialStateJcsIsNotJcs, 'make sure to jcs then encode the initial state'));
}
})
})

describe('create()', async () => {
it('should create a short-form DID successfully.', async () => {
const expectedDidMethodName = 'sidetree';
Expand Down
11 changes: 11 additions & 0 deletions tests/core/Operation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ describe('Operation', async () => {
});
});

describe('validateDelta', () => {
it('should throw sidetree error if input is not an object', () => {
const input = 'this is not an object, this is a string';
try {
Operation.validateDelta(input)
} catch (e) {
expect(e).toEqual(new SidetreeError(ErrorCode.DeltaIsNotObject));
}
})
})

describe('parseDelta()', async () => {
it('should throw if delta is not string', async () => {
await expectAsync(Operation.parseDelta(123)).toBeRejectedWith(new SidetreeError(ErrorCode.DeltaMissingOrNotString));
Expand Down

0 comments on commit 6921d8b

Please sign in to comment.