From 297c8d41a67eb37a2884d8f077feb9004f677c1c Mon Sep 17 00:00:00 2001 From: Simonas Karuzas Date: Mon, 5 Oct 2020 14:19:21 +0300 Subject: [PATCH] test: Integration tests --- __tests__/graphqlAgent.test.ts | 2 + __tests__/localAgent.test.ts | 2 + __tests__/restAgent.test.ts | 3 ++ __tests__/shared/handleSdrMessage.ts | 32 +++++++++++++-- __tests__/shared/messageHandler.ts | 59 ++++++++++++++++++++++++++++ __tests__/shared/saveClaims.ts | 4 ++ 6 files changed, 99 insertions(+), 3 deletions(-) create mode 100644 __tests__/shared/messageHandler.ts diff --git a/__tests__/graphqlAgent.test.ts b/__tests__/graphqlAgent.test.ts index a600e2a8c..8bb8afb69 100644 --- a/__tests__/graphqlAgent.test.ts +++ b/__tests__/graphqlAgent.test.ts @@ -47,6 +47,7 @@ import webDidFlow from './shared/webDidFlow' import documentationExamples from './shared/documentationExamples' import keyManager from './shared/keyManager' import identityManager from './shared/identityManager' +import messageHandler from './shared/messageHandler' const databaseFile = 'graphql-database.sqlite' const infuraProjectId = '5ffc47f65c4042ce847ef66a3fa70d4c' @@ -171,4 +172,5 @@ describe('GraphQL integration tests', () => { documentationExamples(testContext) keyManager(testContext) identityManager(testContext) + messageHandler(testContext) }) diff --git a/__tests__/localAgent.test.ts b/__tests__/localAgent.test.ts index dd2922092..0670639ce 100644 --- a/__tests__/localAgent.test.ts +++ b/__tests__/localAgent.test.ts @@ -42,6 +42,7 @@ import saveClaims from './shared/saveClaims' import documentationExamples from './shared/documentationExamples' import keyManager from './shared/keyManager' import identityManager from './shared/identityManager' +import messageHandler from './shared/messageHandler' const databaseFile = 'local-database.sqlite' const infuraProjectId = '5ffc47f65c4042ce847ef66a3fa70d4c' @@ -151,4 +152,5 @@ describe('Local integration tests', () => { documentationExamples(testContext) keyManager(testContext) identityManager(testContext) + messageHandler(testContext) }) diff --git a/__tests__/restAgent.test.ts b/__tests__/restAgent.test.ts index 94ba1dae5..263b2cea6 100644 --- a/__tests__/restAgent.test.ts +++ b/__tests__/restAgent.test.ts @@ -46,6 +46,8 @@ import webDidFlow from './shared/webDidFlow' import documentationExamples from './shared/documentationExamples' import keyManager from './shared/keyManager' import identityManager from './shared/identityManager' +import messageHandler from './shared/messageHandler' + const databaseFile = 'rest-database.sqlite' const infuraProjectId = '5ffc47f65c4042ce847ef66a3fa70d4c' @@ -164,4 +166,5 @@ describe('REST integration tests', () => { documentationExamples(testContext) keyManager(testContext) identityManager(testContext) + messageHandler(testContext) }) diff --git a/__tests__/shared/handleSdrMessage.ts b/__tests__/shared/handleSdrMessage.ts index 0f9d47ba7..d08375e74 100644 --- a/__tests__/shared/handleSdrMessage.ts +++ b/__tests__/shared/handleSdrMessage.ts @@ -1,6 +1,6 @@ import { TAgent, IIdentityManager, IIdentity, IDataStore, IMessageHandler } from '../../packages/daf-core/src' import { ICredentialIssuer } from '../../packages/daf-w3c/src' -import { ISelectiveDisclosure } from '../../packages/daf-selective-disclosure/src' +import { ISelectiveDisclosure, SelectiveDisclosure } from '../../packages/daf-selective-disclosure/src' import { IDataStoreORM } from '../../packages/daf-typeorm/src' type ConfiguredAgent = TAgent< @@ -15,9 +15,9 @@ export default (testContext: { describe('handling sdr message', () => { let agent: ConfiguredAgent let identity: IIdentity - const JWT = - 'eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NkstUiJ9.eyJpYXQiOjE1OTM0NTE3MDAsInR5cGUiOiJzZHIiLCJzdWJqZWN0IjoiZGlkOmV0aHI6cmlua2VieToweDM2MjQ2M2NiZTUyMjhjZTUwMGJlOGUwMzVjZGIyMWI3NzQ1ZjZkYjAiLCJ0YWciOiJzZHItb25lIiwiY2xhaW1zIjpbeyJyZWFzb24iOiJXZSBuZWVkIGl0IiwiY2xhaW1UeXBlIjoibmFtZSIsImVzc2VudGlhbCI6dHJ1ZX1dLCJpc3MiOiJkaWQ6ZXRocjpyaW5rZWJ5OjB4MTM4NGMxZmNlM2Y3MWQ3NjU5NzcwOGY1NGM0ZDEyOGMyNDFkMDBkMiJ9.L-j-gREAuN7DAxDCe1vXJWtMIdmn88HTuTFp2PasTTo_aqvIdGcFtv-rSfvRHkauNq5C3PkXkQWY01VGqpJ-QwE' + let JWT: string let originalRequestSender: string + let sdr: SelectiveDisclosure beforeAll(() => { testContext.setup() @@ -49,6 +49,18 @@ export default (testContext: { }) it('should save an SDR message', async () => { + JWT = await agent.createSelectiveDisclosureRequest({ + data: { + issuer: identity.did, + tag: 'sdr-one', + claims: [{ + reason: 'We need it', + claimType: 'name', + essential: true + }] + } + }) + const message = await agent.handleMessage({ raw: JWT, save: true, @@ -131,6 +143,20 @@ export default (testContext: { }) expect(verifiablePresentation).toHaveProperty('proof.jwt') + + + const validated = await agent.validatePresentationAgainstSdr({ + presentation: verifiablePresentation, + sdr: { + issuer: '', + claims: [ + { + claimType: 'name', + }, + ], + }, + }) + expect(validated.valid).toEqual(true) }) }) } diff --git a/__tests__/shared/messageHandler.ts b/__tests__/shared/messageHandler.ts new file mode 100644 index 000000000..f7fd7eebb --- /dev/null +++ b/__tests__/shared/messageHandler.ts @@ -0,0 +1,59 @@ +import { TAgent, IDataStore, IMessageHandler, IMessage } from '../../packages/daf-core/src' +import { IDataStoreORM } from '../../packages/daf-typeorm/src' + +type ConfiguredAgent = TAgent + +export default (testContext: { + getAgent: () => ConfiguredAgent + setup: () => Promise + tearDown: () => Promise +}) => { + describe('message handler', () => { + let agent: ConfiguredAgent + + beforeAll(async () => { + await testContext.setup() + agent = testContext.getAgent() + return true + }) + afterAll(testContext.tearDown) + + let parsedMessage: IMessage + + it('should parse raw message', async () => { + const raw = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NkstUiJ9.eyJpYXQiOjE1ODg2NzY3MzksInZwIjp7IkBjb250ZXh0IjpbImh0dHBzOi8vd3d3LnczLm9yZy8yMDE4L2NyZWRlbnRpYWxzL3YxIl0sInR5cGUiOlsiVmVyaWZpYWJsZVByZXNlbnRhdGlvbiJdLCJ2ZXJpZmlhYmxlQ3JlZGVudGlhbCI6WyJleUowZVhBaU9pSktWMVFpTENKaGJHY2lPaUpGVXpJMU5rc3RVaUo5LmV5SnBZWFFpT2pFMU9ESTJNVGsyTnpZc0luTjFZaUk2SW1ScFpEcGxkR2h5T25KcGJtdGxZbms2TUhnell6TTFOMkpoTkRVNE9UTXpZVEU1WXpGa1pqRmpOMlkyWWpRM00ySXpNekF5WW1KaVpUWXhJaXdpZG1NaU9uc2lRR052Ym5SbGVIUWlPbHNpYUhSMGNITTZMeTkzZDNjdWR6TXViM0puTHpJd01UZ3ZZM0psWkdWdWRHbGhiSE12ZGpFaVhTd2lkSGx3WlNJNld5SldaWEpwWm1saFlteGxRM0psWkdWdWRHbGhiQ0pkTENKamNtVmtaVzUwYVdGc1UzVmlhbVZqZENJNmV5SnVZVzFsSWpvaVFXeHBZMlVpZlgwc0ltbHpjeUk2SW1ScFpEcGxkR2h5T25KcGJtdGxZbms2TUhnell6TTFOMkpoTkRVNE9UTXpZVEU1WXpGa1pqRmpOMlkyWWpRM00ySXpNekF5WW1KaVpUWXhJbjAuSUdGMUxGT2M0X1BjR1ZlcTdZdzdPR3o0R2o3eFhaSzZwOGJQOUNTRUlYejdtTkZQTTB2MG51ZXZUWjQ3YTBJOFhnTGZDRk5rVXJJSXNjakg4TUZ4X3dFIl19LCJ0YWciOiJ0YWcxMjMiLCJhdWQiOlsiZGlkOmV4YW1wbGU6MzQ1NiIsImRpZDp3ZWI6dXBvcnQubWUiXSwiaXNzIjoiZGlkOmV0aHI6cmlua2VieToweGIwOWI2NjAyNmJhNTkwOWE3Y2ZlOTliNzY4NzU0MzFkMmI4ZDUxOTAifQ.4SWpp8siCBHP47KrOT_28IJIQPZLCWO9VS0Ir-VVYOGUAVj7vHtXLxl3Y6lLAxYeNqWrRPCAVkDArBFCNRjYUgA' + parsedMessage = await agent.handleMessage({ + raw, + save: false, + metaData: [{type: 'test'}], + }) + expect(typeof parsedMessage.id).toEqual('string') + + }) + + it('should save message', async () => { + const id = await agent.dataStoreSaveMessage({ message: parsedMessage }) + expect(id).toEqual(parsedMessage.id) + }) + + it('should get message from db', async () => { + const message = await agent.dataStoreGetMessage({ id: parsedMessage.id }) + expect(message).toEqual({...parsedMessage, ...{replyTo: null, replyUrl: null}}) + }) + + it('should throw error for non existing message', async () => { + await expect( + agent.dataStoreGetMessage({ + id: 'foobar', + }), + ).rejects.toThrow('Message not found') + }) + + it('should count messages', async () => { + const allMessages = await agent.dataStoreORMGetMessages() + const count = await agent.dataStoreORMGetMessagesCount() + expect(allMessages.length).toEqual(count) + }) + + }) +} diff --git a/__tests__/shared/saveClaims.ts b/__tests__/shared/saveClaims.ts index f0eea425b..d10ec0425 100644 --- a/__tests__/shared/saveClaims.ts +++ b/__tests__/shared/saveClaims.ts @@ -86,6 +86,10 @@ export default (testContext: { where: [{ column: 'type', value: ['topic'] }], }) expect(credentials).toHaveLength(3) + const count = await agent.dataStoreORMGetVerifiableCredentialsByClaimsCount({ + where: [{ column: 'type', value: ['topic'] }], + }) + expect(count).toEqual(credentials.length) }) it('should be able to find all the credentials when query by claim type and value', async () => {