@microsoft/useragent-sdk
Version:
SDK for building decentralized identity wallets and enterprise agents.
120 lines • 6.78 kB
JavaScript
"use strict";
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
Object.defineProperty(exports, "__esModule", { value: true });
const base64url_1 = require("base64url");
const ProtectedCommit_1 = require("../../src/hubSession/ProtectedCommit");
const JoseToken_1 = require("../../src/crypto/protocols/jose/JoseToken");
const ProtocolTest_1 = require("../crypto/protocols/jose/ProtocolTest");
const JoseConstants_1 = require("../../src/crypto/protocols/jose/JoseConstants");
const ProtectionFormat_1 = require("../../src/crypto/keyStore/ProtectionFormat");
const createHeaders = {
interface: 'Collections',
context: 'schema.org',
type: 'MusicPlaylist',
operation: 'create',
committed_at: '2019-01-01',
commit_strategy: 'basic',
sub: 'did:example:sub.id',
kid: 'did:example:client.id#key-1',
iss: 'did:example:client.id',
};
describe('ProtectedCommit', () => {
describe('getProtectedHeaders()', () => {
it('should return the headers', async () => {
const token = new JoseToken_1.default({}, new ProtocolTest_1.default(), [
[JoseConstants_1.default.tokenFormat, ProtectionFormat_1.ProtectionFormat.JwsFlatJson],
[JoseConstants_1.default.tokenPayload, base64url_1.default(JSON.stringify({ name: 'test' }))],
[JoseConstants_1.default.tokenProtected, base64url_1.default(JSON.stringify(createHeaders))],
[JoseConstants_1.default.tokenSignatures, ['abc']]
]);
const signedCommit = new ProtectedCommit_1.default(token);
expect(signedCommit.getProtectedHeaders()).toEqual(createHeaders);
});
it('should throw if protected headers are missing', async () => {
const token = new JoseToken_1.default({}, new ProtocolTest_1.default(), [
[JoseConstants_1.default.tokenFormat, ProtectionFormat_1.ProtectionFormat.JwsFlatJson],
[JoseConstants_1.default.tokenPayload, base64url_1.default(JSON.stringify({ name: 'test' }))],
[JoseConstants_1.default.tokenSignatures, ['abc']]
]);
const signedCommit = new ProtectedCommit_1.default(token);
try {
signedCommit.getProtectedHeaders();
fail('Should not reach this point.');
}
catch (e) {
// Expected
}
});
});
describe('getPayload()', () => {
it('should return a json payload', async () => {
const payload = {
name: 'test'
};
const token = new JoseToken_1.default({}, new ProtocolTest_1.default(), [
[JoseConstants_1.default.tokenFormat, ProtectionFormat_1.ProtectionFormat.JwsFlatJson],
[JoseConstants_1.default.tokenPayload, base64url_1.default(JSON.stringify(payload))],
[JoseConstants_1.default.tokenProtected, base64url_1.default(JSON.stringify(createHeaders))],
[JoseConstants_1.default.tokenSignatures, ['abc']]
]);
const signedCommit = new ProtectedCommit_1.default(token);
expect(signedCommit.getPayload()).toEqual(payload);
});
it('should return a non-json payload', async () => {
const payload = 'test';
const token = new JoseToken_1.default({}, new ProtocolTest_1.default(), [
[JoseConstants_1.default.tokenFormat, ProtectionFormat_1.ProtectionFormat.JwsFlatJson],
[JoseConstants_1.default.tokenPayload, base64url_1.default(payload)],
[JoseConstants_1.default.tokenProtected, base64url_1.default(JSON.stringify(createHeaders))],
[JoseConstants_1.default.tokenSignatures, ['abc']]
]);
const signedCommit = new ProtectedCommit_1.default(token);
expect(signedCommit.getPayload()).toEqual(payload);
});
it('should throw if a payload is missing', async () => {
const token = new JoseToken_1.default({}, new ProtocolTest_1.default(), [
[JoseConstants_1.default.tokenFormat, ProtectionFormat_1.ProtectionFormat.JwsFlatJson],
[JoseConstants_1.default.tokenProtected, base64url_1.default(JSON.stringify(createHeaders))],
[JoseConstants_1.default.tokenSignatures, ['abc']]
]);
const signedCommit = new ProtectedCommit_1.default(token);
try {
signedCommit.getPayload();
fail('Should not reach this point.');
}
catch (e) {
// Expected
}
});
});
describe('getObjectId()', () => {
it('should return the revision for a create commit', async () => {
const token = new JoseToken_1.default({}, new ProtocolTest_1.default(), [
[JoseConstants_1.default.tokenFormat, ProtectionFormat_1.ProtectionFormat.JwsFlatJson],
[JoseConstants_1.default.tokenPayload, base64url_1.default(JSON.stringify({ name: 'test ' }))],
[JoseConstants_1.default.tokenProtected, base64url_1.default(JSON.stringify(createHeaders))],
[JoseConstants_1.default.tokenSignatures, ['abc']]
]);
const signedCommit = new ProtectedCommit_1.default(token);
expect(signedCommit.getObjectId()).toEqual(signedCommit.getRevision());
});
it('should return the revision for an update commit', async () => {
const updateHeaders = Object.assign({}, createHeaders, {
operation: 'update',
object_id: 'abc123'
});
const token = new JoseToken_1.default({}, new ProtocolTest_1.default(), [
[JoseConstants_1.default.tokenFormat, ProtectionFormat_1.ProtectionFormat.JwsFlatJson],
[JoseConstants_1.default.tokenPayload, base64url_1.default(JSON.stringify({ name: 'test' }))],
[JoseConstants_1.default.tokenProtected, base64url_1.default(JSON.stringify(updateHeaders))],
[JoseConstants_1.default.tokenSignatures, ['abc']]
]);
const signedCommit = new ProtectedCommit_1.default(token);
expect(signedCommit.getObjectId()).toEqual('abc123');
});
});
});
//# sourceMappingURL=SignedCommit.spec.js.map