Skip to content

Commit

Permalink
test(data-store): use multiple DB connection options in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mirceanis committed Aug 30, 2021
1 parent d3ceca1 commit 39b27f4
Show file tree
Hide file tree
Showing 2 changed files with 209 additions and 17 deletions.
24 changes: 7 additions & 17 deletions __tests__/localAgent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -77,34 +77,23 @@ let agent: TAgent<
IDIDDiscovery
>
let dbConnection: Promise<Connection>
let databaseFile: string

const setup = async (options?: IAgentOptions): Promise<boolean> => {
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,
migrations: migrations,
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 &
Expand Down Expand Up @@ -222,4 +211,5 @@ describe('Local integration tests', () => {
messageHandler(testContext)
didComm(testContext)
didDiscovery(testContext)
dbInitOptions(testContext)
})
202 changes: 202 additions & 0 deletions __tests__/shared/dbInitOptions.ts
Original file line number Diff line number Diff line change
@@ -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<boolean>
tearDown: () => Promise<boolean>
}) => {
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)
})
})
}
})
}

0 comments on commit 39b27f4

Please sign in to comment.