Skip to content
Snippets Groups Projects
Commit 3142487d authored by Berend Sliedrecht's avatar Berend Sliedrecht
Browse files

test(ssi): include unit test for credential functionality


Signed-off-by: default avatarBerend Sliedrecht <berend@animo.id>
parent a0d13e75
No related branches found
No related tags found
No related merge requests found
...@@ -17,6 +17,7 @@ export default { ...@@ -17,6 +17,7 @@ export default {
}, },
], ],
}, },
testPathIgnorePatterns: ['<rootDir>/dist'],
extensionsToTreatAsEsm: ['.ts'], extensionsToTreatAsEsm: ['.ts'],
moduleNameMapper: { moduleNameMapper: {
// ESM modules require `.js` extension to be specified, but Jest doesn't work with them // ESM modules require `.js` extension to be specified, but Jest doesn't work with them
......
...@@ -24,75 +24,101 @@ describe('AnonCredsCredentialsController', () => { ...@@ -24,75 +24,101 @@ describe('AnonCredsCredentialsController', () => {
credentialsController = moduleRef.get(AnonCredsCredentialsController); credentialsController = moduleRef.get(AnonCredsCredentialsController);
}); });
describe('get all', () => { it('get all', async () => {
it('should get all the credential records of the agent', async () => { const result: Array<CredentialExchangeRecord> = [];
const result: Array<CredentialExchangeRecord> = []; jest.spyOn(credentialsService, 'getAll').mockResolvedValue(result);
jest.spyOn(credentialsService, 'getAll').mockResolvedValue(result);
const event = await credentialsController.getAll({ const event = await credentialsController.getAll({
tenantId: 'some-id', tenantId: 'some-id',
}); });
expect(event.data).toStrictEqual(result);
});
expect(event.data).toStrictEqual(result); it('get all offers', async () => {
const result: Array<CredentialExchangeRecord> = [];
jest.spyOn(credentialsService, 'getAllOffers').mockResolvedValue(result);
const event = await credentialsController.getAllOffers({
tenantId: 'some-id',
}); });
expect(event.data).toStrictEqual(result);
}); });
describe('get by id', () => { it('get all requests', async () => {
it('should get a credential record by id', async () => { const result: Array<CredentialExchangeRecord> = [];
const result: CredentialExchangeRecord | null = null; jest.spyOn(credentialsService, 'getAllRequests').mockResolvedValue(result);
jest.spyOn(credentialsService, 'getById').mockResolvedValue(result);
const event = await credentialsController.getById({ const event = await credentialsController.getAllRequests({
tenantId: 'some-id', tenantId: 'some-id',
credentialRecordId: 'some-id', });
});
expect(event.data).toStrictEqual(result);
});
expect(event.data).toStrictEqual(result); it('get by id', async () => {
const result: CredentialExchangeRecord | null = null;
jest.spyOn(credentialsService, 'getById').mockResolvedValue(result);
const event = await credentialsController.getById({
tenantId: 'some-id',
credentialRecordId: 'some-id',
}); });
expect(event.data).toStrictEqual(result);
}); });
describe('offer', () => { it('delete by id', async () => {
it('should offer a credential', async () => { const result = {};
const result: CredentialExchangeRecord = new CredentialExchangeRecord({ jest.spyOn(credentialsService, 'deleteById').mockResolvedValue(result);
state: CredentialState.Done,
threadId: 'some-id', const event = await credentialsController.deleteById({
protocolVersion: 'v2', tenantId: 'some-id',
}); credentialRecordId: 'some-id-that-does-not-exist',
jest.spyOn(credentialsService, 'offer').mockResolvedValue(result);
const event = await credentialsController.offer({
tenantId: 'some-id',
connectionId: 'some-id',
credentialDefinitionId: 'some-id',
attributes: [
{ name: 'Name', value: 'Berend', mimeType: 'application/text' },
{ name: 'Age', value: '25' },
],
});
expect(event.data).toStrictEqual(result);
}); });
expect(event.data).toStrictEqual(result);
}); });
describe('offer to self', () => { it('offer', async () => {
it('should offer a credential to self', async () => { const result: CredentialExchangeRecord = new CredentialExchangeRecord({
const result: CredentialExchangeRecord = new CredentialExchangeRecord({ state: CredentialState.Done,
state: CredentialState.Done, threadId: 'some-id',
threadId: 'some-id', protocolVersion: 'v2',
protocolVersion: 'v2', });
}); jest.spyOn(credentialsService, 'offer').mockResolvedValue(result);
jest.spyOn(credentialsService, 'offerToSelf').mockResolvedValue(result);
const event = await credentialsController.offer({
const event = await credentialsController.offerToSelf({ tenantId: 'some-id',
tenantId: 'some-id', connectionId: 'some-id',
credentialDefinitionId: 'some-id', credentialDefinitionId: 'some-id',
attributes: [ attributes: [
{ name: 'Name', value: 'Berend', mimeType: 'application/text' }, { name: 'Name', value: 'Berend', mimeType: 'application/text' },
{ name: 'Age', value: '25' }, { name: 'Age', value: '25' },
], ],
});
expect(event.data).toStrictEqual(result);
}); });
expect(event.data).toStrictEqual(result);
});
it('offer to self', async () => {
const result: CredentialExchangeRecord = new CredentialExchangeRecord({
state: CredentialState.Done,
threadId: 'some-id',
protocolVersion: 'v2',
});
jest.spyOn(credentialsService, 'offerToSelf').mockResolvedValue(result);
const event = await credentialsController.offerToSelf({
tenantId: 'some-id',
credentialDefinitionId: 'some-id',
attributes: [
{ name: 'Name', value: 'Berend', mimeType: 'application/text' },
{ name: 'Age', value: '25' },
],
});
expect(event.data).toStrictEqual(result);
}); });
}); });
...@@ -15,6 +15,10 @@ import { ...@@ -15,6 +15,10 @@ import {
EventAnonCredsCredentialRequestGetAllInput, EventAnonCredsCredentialRequestGetAllInput,
EventAnonCredsCredentialsDeleteById, EventAnonCredsCredentialsDeleteById,
EventAnonCredsCredentialsDeleteByIdInput, EventAnonCredsCredentialsDeleteByIdInput,
EventAnonCredsCredentialOfferGetById,
EventAnonCredsCredentialOfferGetByIdInput,
EventAnonCredsCredentialRequestGetById,
EventAnonCredsCredentialRequestGetByIdInput,
} from '@ocm/shared'; } from '@ocm/shared';
import { AnonCredsCredentialsService } from './anoncredsCredentials.service.js'; import { AnonCredsCredentialsService } from './anoncredsCredentials.service.js';
...@@ -63,6 +67,26 @@ export class AnonCredsCredentialsController { ...@@ -63,6 +67,26 @@ export class AnonCredsCredentialsController {
); );
} }
@MessagePattern(EventAnonCredsCredentialOfferGetById.token)
public async getOfferById(
options: EventAnonCredsCredentialOfferGetByIdInput,
): Promise<EventAnonCredsCredentialOfferGetById> {
return new EventAnonCredsCredentialOfferGetById(
await this.credentialsService.getOfferById(options),
options.tenantId,
);
}
@MessagePattern(EventAnonCredsCredentialRequestGetById.token)
public async getRequestById(
options: EventAnonCredsCredentialRequestGetByIdInput,
): Promise<EventAnonCredsCredentialRequestGetById> {
return new EventAnonCredsCredentialRequestGetById(
await this.credentialsService.getRequestById(options),
options.tenantId,
);
}
@MessagePattern(EventAnonCredsCredentialsDeleteById.token) @MessagePattern(EventAnonCredsCredentialsDeleteById.token)
public async deleteById( public async deleteById(
options: EventAnonCredsCredentialsDeleteByIdInput, options: EventAnonCredsCredentialsDeleteByIdInput,
......
import type { INestApplication } from '@nestjs/common'; import type { INestApplication } from '@nestjs/common';
import type { ClientProxy } from '@nestjs/microservices'; import type { ClientProxy } from '@nestjs/microservices';
import type { import type {
EventDidcommAnonCredsCredentialsGetAllInput, EventAnonCredsCredentialRequestGetAllInput,
EventDidcommAnonCredsCredentialsGetByIdInput, EventAnonCredsCredentialsGetAllInput,
EventAnonCredsCredentialsGetByIdInput,
EventDidcommAnonCredsCredentialsOfferToSelfInput, EventDidcommAnonCredsCredentialsOfferToSelfInput,
EventAnonCredsCredentialOfferGetAllInput,
EventAnonCredsCredentialOfferGetByIdInput,
EventAnonCredsCredentialRequestGetByIdInput,
EventAnonCredsCredentialsDeleteByIdInput,
} from '@ocm/shared'; } from '@ocm/shared';
import { AutoAcceptCredential } from '@aries-framework/core'; import {
AutoAcceptCredential,
CredentialExchangeRecord,
} from '@aries-framework/core';
import { ClientsModule, Transport } from '@nestjs/microservices'; import { ClientsModule, Transport } from '@nestjs/microservices';
import { Test } from '@nestjs/testing'; import { Test } from '@nestjs/testing';
import { import {
EventDidcommAnonCredsCredentialsGetAll, EventAnonCredsCredentialsDeleteById,
EventDidcommAnonCredsCredentialsGetById, EventAnonCredsCredentialOfferGetAll,
EventAnonCredsCredentialOfferGetById,
EventAnonCredsCredentialRequestGetAll,
EventAnonCredsCredentialRequestGetById,
EventAnonCredsCredentialsGetAll,
EventAnonCredsCredentialsGetById,
EventAnonCredsProofsDeleteById,
EventDidcommAnonCredsCredentialsOfferToSelf, EventDidcommAnonCredsCredentialsOfferToSelf,
} from '@ocm/shared'; } from '@ocm/shared';
import { firstValueFrom } from 'rxjs'; import { firstValueFrom } from 'rxjs';
...@@ -104,29 +118,81 @@ describe('Credentials', () => { ...@@ -104,29 +118,81 @@ describe('Credentials', () => {
client.close(); client.close();
}); });
it(EventDidcommAnonCredsCredentialsGetAll.token, async () => { it(EventAnonCredsCredentialsGetAll.token, async () => {
const response$ = client.send<
EventAnonCredsCredentialsGetAll,
EventAnonCredsCredentialsGetAllInput
>(EventAnonCredsCredentialsGetAll.token, { tenantId });
const response = await firstValueFrom(response$);
const eventInstance = EventAnonCredsCredentialsGetAll.fromEvent(response);
expect(eventInstance.instance).toEqual(expect.arrayContaining([]));
});
it(EventAnonCredsCredentialOfferGetAll.token, async () => {
const response$ = client.send< const response$ = client.send<
EventDidcommAnonCredsCredentialsGetAll, EventAnonCredsCredentialOfferGetAll,
EventDidcommAnonCredsCredentialsGetAllInput EventAnonCredsCredentialOfferGetAllInput
>(EventDidcommAnonCredsCredentialsGetAll.token, { tenantId }); >(EventAnonCredsCredentialOfferGetAll.token, { tenantId });
const response = await firstValueFrom(response$); const response = await firstValueFrom(response$);
const eventInstance = const eventInstance =
EventDidcommAnonCredsCredentialsGetAll.fromEvent(response); EventAnonCredsCredentialOfferGetAll.fromEvent(response);
expect(eventInstance.instance).toEqual(expect.arrayContaining([])); expect(eventInstance.instance).toEqual(expect.arrayContaining([]));
}); });
it(EventDidcommAnonCredsCredentialsGetById.token, async () => { it(EventAnonCredsCredentialOfferGetById.token, async () => {
const response$ = client.send< const response$ = client.send<
EventDidcommAnonCredsCredentialsGetById, EventAnonCredsCredentialOfferGetById,
EventDidcommAnonCredsCredentialsGetByIdInput EventAnonCredsCredentialOfferGetByIdInput
>(EventDidcommAnonCredsCredentialsGetById.token, { >(EventAnonCredsCredentialOfferGetById.token, {
tenantId, tenantId,
credentialRecordId: 'some-id', credentialOfferId: 'some-id-that-does-not-exist',
});
const response = await firstValueFrom(response$);
const eventInstance =
EventAnonCredsCredentialOfferGetById.fromEvent(response);
expect(eventInstance.instance).toBeNull();
});
it(EventAnonCredsCredentialRequestGetAll.token, async () => {
const response$ = client.send<
EventAnonCredsCredentialRequestGetAll,
EventAnonCredsCredentialRequestGetAllInput
>(EventAnonCredsCredentialRequestGetAll.token, { tenantId });
const response = await firstValueFrom(response$);
const eventInstance =
EventAnonCredsCredentialRequestGetAll.fromEvent(response);
expect(eventInstance.instance).toEqual(expect.arrayContaining([]));
});
it(EventAnonCredsCredentialRequestGetById.token, async () => {
const response$ = client.send<
EventAnonCredsCredentialRequestGetById,
EventAnonCredsCredentialRequestGetByIdInput
>(EventAnonCredsCredentialRequestGetById.token, {
tenantId,
credentialRequestId: 'some-id-that-does-not-exist',
}); });
const response = await firstValueFrom(response$); const response = await firstValueFrom(response$);
const eventInstance = const eventInstance =
EventDidcommAnonCredsCredentialsGetById.fromEvent(response); EventAnonCredsCredentialRequestGetById.fromEvent(response);
expect(eventInstance.instance).toBeNull();
});
it(EventAnonCredsCredentialsGetById.token, async () => {
const response$ = client.send<
EventAnonCredsCredentialsGetById,
EventAnonCredsCredentialsGetByIdInput
>(EventAnonCredsCredentialsGetById.token, {
tenantId,
credentialRecordId: 'some-id',
});
const response = await firstValueFrom(response$);
const eventInstance = EventAnonCredsCredentialsGetById.fromEvent(response);
expect(eventInstance.instance).toEqual(null); expect(eventInstance.instance).toEqual(null);
}); });
...@@ -154,4 +220,97 @@ describe('Credentials', () => { ...@@ -154,4 +220,97 @@ describe('Credentials', () => {
autoAcceptCredential: AutoAcceptCredential.Always, autoAcceptCredential: AutoAcceptCredential.Always,
}); });
}); });
it(EventAnonCredsProofsDeleteById.token, async () => {
let credentialExchangeRecord: CredentialExchangeRecord | undefined =
undefined;
// GET ALL
{
const response$ = client.send<
EventAnonCredsCredentialsGetAll,
EventAnonCredsCredentialsGetAllInput
>(EventAnonCredsCredentialsGetAll.token, { tenantId });
const response = await firstValueFrom(response$);
const eventInstance = EventAnonCredsCredentialsGetAll.fromEvent(response);
expect(eventInstance.instance).toEqual(expect.arrayContaining([]));
}
// Offer a credential
{
const attributes = [
{ name: 'Name', value: 'Berend' },
{ name: 'Age', value: '25' },
];
const response$ = client.send<
EventDidcommAnonCredsCredentialsOfferToSelf,
EventDidcommAnonCredsCredentialsOfferToSelfInput
>(EventDidcommAnonCredsCredentialsOfferToSelf.token, {
tenantId,
credentialDefinitionId,
attributes,
});
const response = await firstValueFrom(response$);
const eventInstance =
EventDidcommAnonCredsCredentialsOfferToSelf.fromEvent(response);
expect(eventInstance.instance).toMatchObject({
autoAcceptCredential: AutoAcceptCredential.Always,
});
credentialExchangeRecord = eventInstance.instance;
}
// GET THE CREDENTIAL BY ID
{
const response$ = client.send<
EventAnonCredsCredentialsGetById,
EventAnonCredsCredentialsGetByIdInput
>(EventAnonCredsCredentialsGetById.token, {
tenantId,
credentialRecordId: credentialExchangeRecord.id,
});
const response = await firstValueFrom(response$);
const eventInstance =
EventAnonCredsCredentialsGetById.fromEvent(response);
expect(eventInstance.instance).toBeInstanceOf(CredentialExchangeRecord);
}
// DELETE THE CREDENTIAL
{
const response$ = client.send<
EventAnonCredsCredentialsDeleteById,
EventAnonCredsCredentialsDeleteByIdInput
>(EventAnonCredsCredentialsDeleteById.token, {
tenantId,
credentialRecordId: credentialExchangeRecord.id,
});
const response = await firstValueFrom(response$);
const eventInstance =
EventAnonCredsCredentialsDeleteById.fromEvent(response);
expect(eventInstance).toMatchObject({});
}
// GET THE CREDENTIAL BY ID
{
const response$ = client.send<
EventAnonCredsCredentialsGetById,
EventAnonCredsCredentialsGetByIdInput
>(EventAnonCredsCredentialsGetById.token, {
tenantId,
credentialRecordId: credentialExchangeRecord.id,
});
const response = await firstValueFrom(response$);
const eventInstance =
EventAnonCredsCredentialsGetById.fromEvent(response);
expect(eventInstance.instance).toBeNull();
}
});
}); });
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment