Skip to content

test: services unit & integration tests #879

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

Merged
merged 4 commits into from
Jul 17, 2024
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
1 change: 1 addition & 0 deletions packages/sdk-js/src/DidHelpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
124 changes: 124 additions & 0 deletions packages/sdk-js/src/DidHelpers/service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/**
* 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, DidUrl, 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.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: serviceId as DidUrl,
type: ['http://schema.org/EmailService'],
serviceEndpoint: ['mailto:info@kilt.io'],
},
})

expect(mockedTransact).toHaveBeenLastCalledWith(
expect.objectContaining<Partial<Parameters<typeof transact>[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: serviceId as DidUrl,
})

expect(mockedTransact).toHaveBeenLastCalledWith(
expect.objectContaining<Partial<Parameters<typeof transact>[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',
},
})
})
}
)
57 changes: 57 additions & 0 deletions tests/integration/didHelpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
DidDocument,
KeyringPair,
KiltKeyringPair,
Service,
} from '@kiltprotocol/types'
import { Crypto } from '@kiltprotocol/utils'

Expand Down Expand Up @@ -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<Service>([
{
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()
})