From 379d19821aaecc347000f377568589efa59dd6fa Mon Sep 17 00:00:00 2001 From: Tushar <30565750+tushar5526@users.noreply.github.com> Date: Tue, 11 Jul 2023 11:14:48 +0530 Subject: [PATCH] feat: add tests for rendering templates --- .../rendering-templates.fixtures.ts | 14 +++ .../rendering-templates.service.spec.ts | 107 ++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 services/credential-schema/src/rendering-templates/rendering-templates.fixtures.ts diff --git a/services/credential-schema/src/rendering-templates/rendering-templates.fixtures.ts b/services/credential-schema/src/rendering-templates/rendering-templates.fixtures.ts new file mode 100644 index 000000000..87b2c882c --- /dev/null +++ b/services/credential-schema/src/rendering-templates/rendering-templates.fixtures.ts @@ -0,0 +1,14 @@ +import { AddTemplateDTO } from './dto/addTemplate.dto'; + +export const templatePayloadGenerator = ( + schemaID: string, + schemaVersion: string, +): AddTemplateDTO => { + return { + schemaId: schemaID, + schemaVersion: schemaVersion, + template: + "Certificate

CERTIFICATE OF COMPLETION

is hereby awarded to

Daniel Vitorrie

for successfully completing the

diploma in Java Developer

Thank you for demonstrating the type of character and integrity that inspire others
", + type: 'Handlebar', + }; +}; diff --git a/services/credential-schema/src/rendering-templates/rendering-templates.service.spec.ts b/services/credential-schema/src/rendering-templates/rendering-templates.service.spec.ts index 94d015bed..e30b7b350 100644 --- a/services/credential-schema/src/rendering-templates/rendering-templates.service.spec.ts +++ b/services/credential-schema/src/rendering-templates/rendering-templates.service.spec.ts @@ -5,8 +5,16 @@ import { SchemaService } from '../schema/schema.service'; import { PrismaClient } from '@prisma/client'; import { UtilsService } from '../utils/utils.service'; import { HttpModule } from '@nestjs/axios'; +import { templatePayloadGenerator } from './rendering-templates.fixtures'; +import { + generateCredentialSchemaTestBody, + generateTestDIDBody, +} from '../schema/schema.fixtures'; + describe('RenderingTemplatesService', () => { let service: RenderingTemplatesService; + let schemaService: SchemaService; + let utilsService: UtilsService; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ @@ -21,9 +29,108 @@ describe('RenderingTemplatesService', () => { }).compile(); service = module.get(RenderingTemplatesService); + schemaService = module.get(SchemaService); + utilsService = module.get(UtilsService); }); it('should be defined', () => { expect(service).toBeDefined(); }); + + it('should create a template and get it by templateID', async () => { + const didBody = generateTestDIDBody(); + const did = await utilsService.generateDID(didBody); + const credSchemaPayload = generateCredentialSchemaTestBody(); + credSchemaPayload.schema.author = did.id; + const schema = await schemaService.createCredentialSchema( + credSchemaPayload, + ); + const templatePayload = templatePayloadGenerator( + schema.schema.id, + schema.schema.version, + ); + const template = await service.addTemplate(templatePayload); + expect(template).toBeDefined(); + const getTemplate = await service.getTemplateById( + template.template.templateId, + ); + expect(getTemplate.schemaId).toEqual(template.template.schemaId); + expect(getTemplate.template).toEqual(template.template.template); + }); + + it('should create a template with wrong schemaID', async () => { + const templatePayload = templatePayloadGenerator('randomID', '1.0.0'); + await expect(service.addTemplate(templatePayload)).rejects.toThrowError(); + }); + + it('should get a template by schemaID', async () => { + const didBody = generateTestDIDBody(); + const did = await utilsService.generateDID(didBody); + const credSchemaPayload = generateCredentialSchemaTestBody(); + credSchemaPayload.schema.author = did.id; + const schema = await schemaService.createCredentialSchema( + credSchemaPayload, + ); + const templatePayload = templatePayloadGenerator( + schema.schema.id, + schema.schema.version, + ); + const template = await service.addTemplate(templatePayload); + expect(template).toBeDefined(); + + const getTemplate = await service.getTemplateBySchemaID( + templatePayload.schemaId, + ); + expect(getTemplate).toBeDefined(); + expect(getTemplate[0].template).toEqual(template.template.template); + }); + + it('should update the template by templateID', async () => { + const didBody = generateTestDIDBody(); + const did = await utilsService.generateDID(didBody); + const credSchemaPayload = generateCredentialSchemaTestBody(); + credSchemaPayload.schema.author = did.id; + const schema = await schemaService.createCredentialSchema( + credSchemaPayload, + ); + const templatePayload = templatePayloadGenerator( + schema.schema.id, + schema.schema.version, + ); + const template = await service.addTemplate(templatePayload); + expect(template).toBeDefined(); + + const newTemplatePayload = templatePayloadGenerator( + schema.schema.id, + schema.schema.version, + ); + newTemplatePayload.template = 'TEST'; + + const ntemplate = await service.updateTemplate( + template.template.templateId, + newTemplatePayload, + ); + expect(ntemplate.template).toEqual(newTemplatePayload.template); + }); + + it('should delete the template by templateID', async () => { + const didBody = generateTestDIDBody(); + const did = await utilsService.generateDID(didBody); + const credSchemaPayload = generateCredentialSchemaTestBody(); + credSchemaPayload.schema.author = did.id; + const schema = await schemaService.createCredentialSchema( + credSchemaPayload, + ); + const templatePayload = templatePayloadGenerator( + schema.schema.id, + schema.schema.version, + ); + const template = await service.addTemplate(templatePayload); + expect(template).toBeDefined(); + + await service.deleteTemplate(template.template.templateId); + await expect( + service.getTemplateById(template.template.templateId), + ).rejects.toThrowError(); + }); });