diff --git a/__tests__/localAgent.test.ts b/__tests__/localAgent.test.ts index 0cad32c11..70d657f2c 100644 --- a/__tests__/localAgent.test.ts +++ b/__tests__/localAgent.test.ts @@ -59,8 +59,8 @@ import didManager from './shared/didManager' import didComm from './shared/didcomm' import messageHandler from './shared/messageHandler' import didDiscovery from './shared/didDiscovery' +import dbInitOptions from './shared/dbInitOptions' -const databaseFile = 'local-database.sqlite' const infuraProjectId = '5ffc47f65c4042ce847ef66a3fa70d4c' const secretKey = '29739248cad1bd1a0fc4d9b75cd4d2990de535baf5caadfdf8d8f86664aa830c' @@ -77,10 +77,12 @@ let agent: TAgent< IDIDDiscovery > let dbConnection: Promise +let databaseFile: string const setup = async (options?: IAgentOptions): Promise => { + databaseFile = options?.context?.databaseFile || 'local-database.sqlite' dbConnection = createConnection({ - name: options?.context?.['dbName'] || 'sqlite-test', + name: options?.context?.['dbName'] || 'test', type: 'sqlite', database: databaseFile, synchronize: false, @@ -88,23 +90,10 @@ const setup = async (options?: IAgentOptions): Promise => { migrationsRun: true, logging: false, entities: Entities, + // allow shared tests to override connection options + ...options?.context?.dbConnectionOptions }) - // //docker run -p 5432:5432 -it --rm -e POSTGRES_PASSWORD=test123 postgres - // dbConnection = createConnection({ - // name: options?.context?.['dbName'] || 'postgres-test', - // type: 'postgres', - // host: 'localhost', - // port: 5432, - // password: 'test123', - // username: 'postgres', - // synchronize: true, - // // migrations: migrations, - // // migrationsRun: true, - // logging: false, - // entities: Entities, - // }) - agent = createAgent< IDIDManager & IKeyManager & @@ -222,4 +211,5 @@ describe('Local integration tests', () => { messageHandler(testContext) didComm(testContext) didDiscovery(testContext) + dbInitOptions(testContext) }) diff --git a/__tests__/shared/dbInitOptions.ts b/__tests__/shared/dbInitOptions.ts new file mode 100644 index 000000000..4635d52e3 --- /dev/null +++ b/__tests__/shared/dbInitOptions.ts @@ -0,0 +1,202 @@ +import { + TAgent, + IDIDManager, + IKeyManager, + IIdentifier, + IAgentOptions, + IKey, + IDataStore, + IMessageHandler, + IResolver, +} from '@veramo/core/src' +import { IDataStoreORM } from '@veramo/data-store/src' +import { ICredentialIssuer } from '@veramo/credential-w3c/src' +import { IDIDComm, IPackedDIDCommMessage } from '../../packages/did-comm/src' + +type ConfiguredAgent = TAgent< + IDataStoreORM & + IDataStore & + IDIDManager & + IKeyManager & + ICredentialIssuer & + IDIDComm & + IMessageHandler & + IResolver +> + +export default (testContext: { + getAgent: () => ConfiguredAgent + setup: (agentOptions: IAgentOptions) => Promise + tearDown: () => Promise +}) => { + describe('when database is initialized', () => { + describe('using sqlite and synchronize=true', () => { + createTestsUsingOptions({ + context: { + databaseFile: 'sqlite-sync-init-test.sqlite', + dbConnectionOptions: { + name: 'sqlite-sync-init-test', + type: 'sqlite', + synchronize: true, + migrationsRun: false, + }, + }, + }) + }) + describe('using sqlite and migrations', () => { + createTestsUsingOptions({ + context: { + databaseFile: 'sqlite-migration-init-test.sqlite', + dbConnectionOptions: { + name: 'sqlite-migration-init-test', + type: 'sqlite', + synchronize: false, + migrationsRun: true, + }, + }, + }) + }) + + if (process.env.INCLUDE_POSTGRES_TESTS === 'true') { + // //docker run -p 5432:5432 -it --rm -e POSTGRES_PASSWORD=test123 postgres + describe('using postgres and migrations', () => { + createTestsUsingOptions({ + context: { + dbConnectionOptions: { + name: 'postgres-migration-init-test', + type: 'postgres', + database: undefined, + synchronize: false, + migrationsRun: true, + host: 'localhost', + port: 5432, + password: 'test123', + username: 'postgres', + }, + }, + }) + }) + + describe('using postgres and sync', () => { + createTestsUsingOptions({ + context: { + dbConnectionOptions: { + name: 'postgres-sync-init-test', + type: 'postgres', + database: undefined, + synchronize: true, + migrationsRun: false, + host: 'localhost', + port: 5432, + password: 'test123', + username: 'postgres', + }, + }, + }) + }) + } + + function createTestsUsingOptions(options: IAgentOptions) { + describe('agent', () => { + let agent: ConfiguredAgent + beforeAll(async () => { + await testContext.setup(options) + agent = testContext.getAgent() + return true + }) + afterAll(testContext.tearDown) + + let identifier: IIdentifier + it('should create DID', async () => { + identifier = await agent.didManagerGetOrCreate({ provider: 'did:fake', alias: 'migrationDID' }) + expect(identifier.did).toMatch(/did:fake:.*/) + }) + it('should create and add key', async () => { + const key: IKey = await agent.keyManagerCreate({ + type: 'Ed25519', + kms: 'local', + }) + + await agent.didManagerAddKey({ + did: identifier.did, + key: key, + }) + + identifier = await agent.didManagerGet({ did: identifier.did }) + expect(identifier.keys.length).toBeGreaterThanOrEqual(2) + }) + + it('should add service', async () => { + await agent.didManagerAddService({ + did: identifier.did, + service: { + id: 'fake-service', + type: 'DIDCommMessaging', + serviceEndpoint: 'http://localhost:6123', + }, + }) + identifier = await agent.didManagerGet({ did: identifier.did }) + expect(identifier.services.length).toBe(1) + }) + + let credentialRaw: string + it('should sign and save credential', async () => { + const credential = await agent.createVerifiableCredential({ + proofFormat: 'jwt', + credential: { + credentialSubject: { id: identifier.did, pseudonym: 'FakeAlice' }, + issuer: identifier.did, + }, + }) + const credentialId = await agent.dataStoreSaveVerifiableCredential({ + verifiableCredential: credential, + }) + const retrieved = await agent.dataStoreGetVerifiableCredential({ + hash: credentialId, + }) + credentialRaw = retrieved.proof.jwt + expect(retrieved.issuer.id).toEqual(identifier.did) + }) + + let packedMessage: IPackedDIDCommMessage + it('should pack anon message', async () => { + packedMessage = await agent.packDIDCommMessage({ + packing: 'authcrypt', + message: { + to: identifier.did, + from: identifier.did, + id: 'test-message-123', + type: 'w3c.vc', + body: credentialRaw, + }, + }) + expect(packedMessage.message.length).toBeGreaterThan(0) + }) + + it('should unpack anon message', async () => { + const msg = await agent.handleMessage({ raw: packedMessage.message }) + expect(msg.type).toBe('w3c.vc') + }) + + it('should get credentials from message by claim', async () => { + const incomingCredential = await agent.createVerifiableCredential({ + proofFormat: 'jwt', + credential: { + credentialSubject: { + incoming: 'yes', + }, + issuer: identifier.did, + }, + save: false, + }) + const message = await agent.handleMessage({ raw: incomingCredential.proof.jwt, save: false }) + const msgId = await agent.dataStoreSaveMessage({ message }) + const retrievedCredential = await agent.dataStoreORMGetVerifiableCredentialsByClaims({ + where: [{ column: 'type', value: ['incoming'] }], + }) + expect(retrievedCredential.length).toBeGreaterThan(0) + }) + }) + } + }) +}