From 184e11689d097e9db69e294165c684e17921cd71 Mon Sep 17 00:00:00 2001 From: Raphael Flechtner Date: Tue, 16 Jul 2024 18:05:22 +0200 Subject: [PATCH 1/4] test: add unit tests for addService & removeService --- .../sdk-js/src/DidHelpers/service.spec.ts | 121 ++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 packages/sdk-js/src/DidHelpers/service.spec.ts diff --git a/packages/sdk-js/src/DidHelpers/service.spec.ts b/packages/sdk-js/src/DidHelpers/service.spec.ts new file mode 100644 index 000000000..79738ad0d --- /dev/null +++ b/packages/sdk-js/src/DidHelpers/service.spec.ts @@ -0,0 +1,121 @@ +/** + * Copyright (c) 2018-2024, BOTLabs GmbH. + * + * This source code is licensed under the BSD 4-Clause "Original" license + * found in the LICENSE file in the root directory of this source tree. + */ + +import type { DidDocument, KiltKeyringPair } from '@kiltprotocol/types' +import { Crypto } from '@kiltprotocol/utils' +import { + ApiMocks, + createLocalDemoFullDidFromKeypair, +} from '../../../../tests/testUtils/index.js' +import { ConfigService } from '../index.js' +import { transact } from './index.js' +import { addService, removeService } from './service.js' + +jest.mock('./transact.js') + +const mockedTransact = jest.mocked(transact) +const mockedApi = ApiMocks.createAugmentedApi() + +describe('service management', () => { + let didDocument: DidDocument + let keypair: KiltKeyringPair + beforeAll(async () => { + ConfigService.set({ api: mockedApi }) + + keypair = Crypto.makeKeypairFromUri('//Alice') + const { id, verificationMethod, authentication } = + await createLocalDemoFullDidFromKeypair(keypair, { + verificationRelationships: new Set(['assertionMethod']), + }) + didDocument = { + id, + authentication, + assertionMethod: authentication, + verificationMethod: verificationMethod?.filter( + (vm) => vm.id === authentication![0] + ), + } + }) + + it('creates an add service tx', async () => { + addService({ + didDocument, + api: mockedApi, + submitter: keypair, + signers: [keypair], + service: { + id: '#my_service', + type: ['http://schema.org/EmailService'], + serviceEndpoint: ['mailto:info@kilt.io'], + }, + }) + + expect(mockedTransact).toHaveBeenLastCalledWith( + expect.objectContaining[0]>>({ + call: expect.any(Object), + expectedEvents: expect.arrayContaining([ + { + section: 'did', + method: 'DidUpdated', + }, + ]), + didDocument, + api: mockedApi, + submitter: keypair, + signers: [keypair], + }) + ) + expect(mockedTransact.mock.lastCall?.[0].call.toHuman()).toMatchObject({ + method: { + args: { + service_endpoint: { + id: 'my_service', + serviceTypes: ['http://schema.org/EmailService'], + urls: ['mailto:info@kilt.io'], + }, + }, + section: 'did', + method: 'addServiceEndpoint', + }, + }) + }) + + it('creates a remove service tx', async () => { + removeService({ + didDocument, + api: mockedApi, + submitter: keypair, + signers: [keypair], + id: '#my_service', + }) + + expect(mockedTransact).toHaveBeenLastCalledWith( + expect.objectContaining[0]>>({ + call: expect.any(Object), + expectedEvents: expect.arrayContaining([ + { + section: 'did', + method: 'DidUpdated', + }, + ]), + didDocument, + api: mockedApi, + submitter: keypair, + signers: [keypair], + }) + ) + expect(mockedTransact.mock.lastCall?.[0].call.toHuman()).toMatchObject({ + method: { + args: { + service_id: 'my_service', + }, + section: 'did', + method: 'removeServiceEndpoint', + }, + }) + }) +}) From a85b5b15e37cd013ba59fcf6eb42ea6f5e703d74 Mon Sep 17 00:00:00 2001 From: Raphael Flechtner Date: Tue, 16 Jul 2024 18:20:00 +0200 Subject: [PATCH 2/4] test: run service unit tests for fragment- and full ids --- .../sdk-js/src/DidHelpers/service.spec.ts | 165 +++++++++--------- 1 file changed, 84 insertions(+), 81 deletions(-) diff --git a/packages/sdk-js/src/DidHelpers/service.spec.ts b/packages/sdk-js/src/DidHelpers/service.spec.ts index 79738ad0d..0e5b7f91c 100644 --- a/packages/sdk-js/src/DidHelpers/service.spec.ts +++ b/packages/sdk-js/src/DidHelpers/service.spec.ts @@ -5,7 +5,7 @@ * found in the LICENSE file in the root directory of this source tree. */ -import type { DidDocument, KiltKeyringPair } from '@kiltprotocol/types' +import type { DidDocument, DidUrl, KiltKeyringPair } from '@kiltprotocol/types' import { Crypto } from '@kiltprotocol/utils' import { ApiMocks, @@ -20,102 +20,105 @@ jest.mock('./transact.js') const mockedTransact = jest.mocked(transact) const mockedApi = ApiMocks.createAugmentedApi() -describe('service management', () => { - let didDocument: DidDocument - let keypair: KiltKeyringPair - beforeAll(async () => { - ConfigService.set({ api: mockedApi }) +describe.each(['#my_service', 'did:kilt:4abctest#my_service'])( + 'service management with id %s', + (serviceId) => { + let didDocument: DidDocument + let keypair: KiltKeyringPair + beforeAll(async () => { + ConfigService.set({ api: mockedApi }) - keypair = Crypto.makeKeypairFromUri('//Alice') - const { id, verificationMethod, authentication } = - await createLocalDemoFullDidFromKeypair(keypair, { - verificationRelationships: new Set(['assertionMethod']), - }) - didDocument = { - id, - authentication, - assertionMethod: authentication, - verificationMethod: verificationMethod?.filter( - (vm) => vm.id === authentication![0] - ), - } - }) - - it('creates an add service tx', async () => { - addService({ - didDocument, - api: mockedApi, - submitter: keypair, - signers: [keypair], - service: { - id: '#my_service', - type: ['http://schema.org/EmailService'], - serviceEndpoint: ['mailto:info@kilt.io'], - }, + keypair = Crypto.makeKeypairFromUri('//Alice') + const { id, verificationMethod, authentication } = + await createLocalDemoFullDidFromKeypair(keypair, { + verificationRelationships: new Set(['assertionMethod']), + }) + didDocument = { + id, + authentication, + assertionMethod: authentication, + verificationMethod: verificationMethod?.filter( + (vm) => vm.id === authentication![0] + ), + } }) - expect(mockedTransact).toHaveBeenLastCalledWith( - expect.objectContaining[0]>>({ - call: expect.any(Object), - expectedEvents: expect.arrayContaining([ - { - section: 'did', - method: 'DidUpdated', - }, - ]), + it('creates an add service tx', async () => { + addService({ didDocument, api: mockedApi, submitter: keypair, signers: [keypair], + service: { + id: serviceId as DidUrl, + type: ['http://schema.org/EmailService'], + serviceEndpoint: ['mailto:info@kilt.io'], + }, }) - ) - expect(mockedTransact.mock.lastCall?.[0].call.toHuman()).toMatchObject({ - method: { - args: { - service_endpoint: { - id: 'my_service', - serviceTypes: ['http://schema.org/EmailService'], - urls: ['mailto:info@kilt.io'], + + expect(mockedTransact).toHaveBeenLastCalledWith( + expect.objectContaining[0]>>({ + call: expect.any(Object), + expectedEvents: expect.arrayContaining([ + { + section: 'did', + method: 'DidUpdated', + }, + ]), + didDocument, + api: mockedApi, + submitter: keypair, + signers: [keypair], + }) + ) + expect(mockedTransact.mock.lastCall?.[0].call.toHuman()).toMatchObject({ + method: { + args: { + service_endpoint: { + id: 'my_service', + serviceTypes: ['http://schema.org/EmailService'], + urls: ['mailto:info@kilt.io'], + }, }, + section: 'did', + method: 'addServiceEndpoint', }, - section: 'did', - method: 'addServiceEndpoint', - }, - }) - }) - - it('creates a remove service tx', async () => { - removeService({ - didDocument, - api: mockedApi, - submitter: keypair, - signers: [keypair], - id: '#my_service', + }) }) - expect(mockedTransact).toHaveBeenLastCalledWith( - expect.objectContaining[0]>>({ - call: expect.any(Object), - expectedEvents: expect.arrayContaining([ - { - section: 'did', - method: 'DidUpdated', - }, - ]), + it('creates a remove service tx', async () => { + removeService({ didDocument, api: mockedApi, submitter: keypair, signers: [keypair], + id: serviceId as DidUrl, }) - ) - expect(mockedTransact.mock.lastCall?.[0].call.toHuman()).toMatchObject({ - method: { - args: { - service_id: 'my_service', + + expect(mockedTransact).toHaveBeenLastCalledWith( + expect.objectContaining[0]>>({ + call: expect.any(Object), + expectedEvents: expect.arrayContaining([ + { + section: 'did', + method: 'DidUpdated', + }, + ]), + didDocument, + api: mockedApi, + submitter: keypair, + signers: [keypair], + }) + ) + expect(mockedTransact.mock.lastCall?.[0].call.toHuman()).toMatchObject({ + method: { + args: { + service_id: 'my_service', + }, + section: 'did', + method: 'removeServiceEndpoint', }, - section: 'did', - method: 'removeServiceEndpoint', - }, + }) }) - }) -}) + } +) From 5b76934d09d1cf3936c39c4e9c0bd9b783ec9ea0 Mon Sep 17 00:00:00 2001 From: Raphael Flechtner Date: Tue, 16 Jul 2024 18:20:38 +0200 Subject: [PATCH 3/4] fix: export service did helpers --- packages/sdk-js/src/DidHelpers/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/sdk-js/src/DidHelpers/index.ts b/packages/sdk-js/src/DidHelpers/index.ts index d54f51fdd..5d978add1 100644 --- a/packages/sdk-js/src/DidHelpers/index.ts +++ b/packages/sdk-js/src/DidHelpers/index.ts @@ -12,6 +12,7 @@ import { Signers } from '@kiltprotocol/utils' import type { SharedArguments } from './interfaces.js' export { createDid } from './createDid.js' +export { addService, removeService } from './service.js' export { setVerificationMethod } from './setVerificationMethod.js' export { transact } from './transact.js' export { claimWeb3Name, releaseWeb3Name } from './w3names.js' From 86678ba8bdba4445dfe9297ba42d48d336946e08 Mon Sep 17 00:00:00 2001 From: Raphael Flechtner Date: Tue, 16 Jul 2024 18:22:14 +0200 Subject: [PATCH 4/4] test: service did helpers integration tests --- tests/integration/didHelpers.spec.ts | 57 ++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/tests/integration/didHelpers.spec.ts b/tests/integration/didHelpers.spec.ts index 3da557372..b5c2a5436 100644 --- a/tests/integration/didHelpers.spec.ts +++ b/tests/integration/didHelpers.spec.ts @@ -12,6 +12,7 @@ import type { DidDocument, KeyringPair, KiltKeyringPair, + Service, } from '@kiltprotocol/types' import { Crypto } from '@kiltprotocol/utils' @@ -109,6 +110,62 @@ describe('w3ns', () => { }, 30_000) }) +describe('services', () => { + let keypair: KeyringPair + let didDocument: DidDocument + beforeAll(async () => { + keypair = Crypto.makeKeypairFromUri('//Services') + const result = await DidHelpers.createDid({ + api, + signers: [keypair], + submitter: paymentAccount, + fromPublicKey: keypair, + }).submit() + didDocument = result.asConfirmed.didDocument + }) + + it('adds a service', async () => { + const result = await DidHelpers.addService({ + api, + signers: [keypair], + submitter: paymentAccount, + didDocument, + service: { + id: '#my_service', + type: ['http://schema.org/EmailService'], + serviceEndpoint: ['mailto:info@kilt.io'], + }, + }).submit() + expect(result.status).toStrictEqual('confirmed') + didDocument = result.asConfirmed.didDocument + expect(didDocument).toHaveProperty( + 'service', + expect.arrayContaining([ + { + id: `${didDocument.id}#my_service`, + type: ['http://schema.org/EmailService'], + serviceEndpoint: ['mailto:info@kilt.io'], + }, + ]) + ) + expect(didDocument.service).toHaveLength(1) + }, 30_000) + + it('removes a service', async () => { + const result = await DidHelpers.removeService({ + api, + signers: [keypair], + submitter: paymentAccount, + didDocument, + id: '#my_service', + }).submit() + + expect(result.status).toStrictEqual('confirmed') + didDocument = result.asConfirmed.didDocument + expect(didDocument).not.toHaveProperty('service') + }, 30_000) +}) + afterAll(async () => { await disconnect() })