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
1 merge request!28feat(ssi): add method to fetch offers and requests and delete credentials
......@@ -17,6 +17,7 @@ export default {
},
],
},
testPathIgnorePatterns: ['<rootDir>/dist'],
extensionsToTreatAsEsm: ['.ts'],
moduleNameMapper: {
// ESM modules require `.js` extension to be specified, but Jest doesn't work with them
......
......@@ -24,75 +24,101 @@ describe('AnonCredsCredentialsController', () => {
credentialsController = moduleRef.get(AnonCredsCredentialsController);
});
describe('get all', () => {
it('should get all the credential records of the agent', async () => {
const result: Array<CredentialExchangeRecord> = [];
jest.spyOn(credentialsService, 'getAll').mockResolvedValue(result);
it('get all', async () => {
const result: Array<CredentialExchangeRecord> = [];
jest.spyOn(credentialsService, 'getAll').mockResolvedValue(result);
const event = await credentialsController.getAll({
tenantId: 'some-id',
});
const event = await credentialsController.getAll({
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('should get a credential record by id', async () => {
const result: CredentialExchangeRecord | null = null;
jest.spyOn(credentialsService, 'getById').mockResolvedValue(result);
it('get all requests', async () => {
const result: Array<CredentialExchangeRecord> = [];
jest.spyOn(credentialsService, 'getAllRequests').mockResolvedValue(result);
const event = await credentialsController.getById({
tenantId: 'some-id',
credentialRecordId: 'some-id',
});
const event = await credentialsController.getAllRequests({
tenantId: '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('should offer a credential', async () => {
const result: CredentialExchangeRecord = new CredentialExchangeRecord({
state: CredentialState.Done,
threadId: 'some-id',
protocolVersion: 'v2',
});
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);
it('delete by id', async () => {
const result = {};
jest.spyOn(credentialsService, 'deleteById').mockResolvedValue(result);
const event = await credentialsController.deleteById({
tenantId: 'some-id',
credentialRecordId: 'some-id-that-does-not-exist',
});
expect(event.data).toStrictEqual(result);
});
describe('offer to self', () => {
it('should offer a credential 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);
it('offer', async () => {
const result: CredentialExchangeRecord = new CredentialExchangeRecord({
state: CredentialState.Done,
threadId: 'some-id',
protocolVersion: 'v2',
});
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);
});
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 {
EventAnonCredsCredentialRequestGetAllInput,
EventAnonCredsCredentialsDeleteById,
EventAnonCredsCredentialsDeleteByIdInput,
EventAnonCredsCredentialOfferGetById,
EventAnonCredsCredentialOfferGetByIdInput,
EventAnonCredsCredentialRequestGetById,
EventAnonCredsCredentialRequestGetByIdInput,
} from '@ocm/shared';
import { AnonCredsCredentialsService } from './anoncredsCredentials.service.js';
......@@ -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)
public async deleteById(
options: EventAnonCredsCredentialsDeleteByIdInput,
......
import type { INestApplication } from '@nestjs/common';
import type { ClientProxy } from '@nestjs/microservices';
import type {
EventDidcommAnonCredsCredentialsGetAllInput,
EventDidcommAnonCredsCredentialsGetByIdInput,
EventAnonCredsCredentialRequestGetAllInput,
EventAnonCredsCredentialsGetAllInput,
EventAnonCredsCredentialsGetByIdInput,
EventDidcommAnonCredsCredentialsOfferToSelfInput,
EventAnonCredsCredentialOfferGetAllInput,
EventAnonCredsCredentialOfferGetByIdInput,
EventAnonCredsCredentialRequestGetByIdInput,
EventAnonCredsCredentialsDeleteByIdInput,
} from '@ocm/shared';
import { AutoAcceptCredential } from '@aries-framework/core';
import {
AutoAcceptCredential,
CredentialExchangeRecord,
} from '@aries-framework/core';
import { ClientsModule, Transport } from '@nestjs/microservices';
import { Test } from '@nestjs/testing';
import {
EventDidcommAnonCredsCredentialsGetAll,
EventDidcommAnonCredsCredentialsGetById,
EventAnonCredsCredentialsDeleteById,
EventAnonCredsCredentialOfferGetAll,
EventAnonCredsCredentialOfferGetById,
EventAnonCredsCredentialRequestGetAll,
EventAnonCredsCredentialRequestGetById,
EventAnonCredsCredentialsGetAll,
EventAnonCredsCredentialsGetById,
EventAnonCredsProofsDeleteById,
EventDidcommAnonCredsCredentialsOfferToSelf,
} from '@ocm/shared';
import { firstValueFrom } from 'rxjs';
......@@ -104,29 +118,81 @@ describe('Credentials', () => {
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<
EventDidcommAnonCredsCredentialsGetAll,
EventDidcommAnonCredsCredentialsGetAllInput
>(EventDidcommAnonCredsCredentialsGetAll.token, { tenantId });
EventAnonCredsCredentialOfferGetAll,
EventAnonCredsCredentialOfferGetAllInput
>(EventAnonCredsCredentialOfferGetAll.token, { tenantId });
const response = await firstValueFrom(response$);
const eventInstance =
EventDidcommAnonCredsCredentialsGetAll.fromEvent(response);
EventAnonCredsCredentialOfferGetAll.fromEvent(response);
expect(eventInstance.instance).toEqual(expect.arrayContaining([]));
});
it(EventDidcommAnonCredsCredentialsGetById.token, async () => {
it(EventAnonCredsCredentialOfferGetById.token, async () => {
const response$ = client.send<
EventDidcommAnonCredsCredentialsGetById,
EventDidcommAnonCredsCredentialsGetByIdInput
>(EventDidcommAnonCredsCredentialsGetById.token, {
EventAnonCredsCredentialOfferGetById,
EventAnonCredsCredentialOfferGetByIdInput
>(EventAnonCredsCredentialOfferGetById.token, {
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 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);
});
......@@ -154,4 +220,97 @@ describe('Credentials', () => {
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