Skip to content

Commit

Permalink
Merge pull request #90 from uport-project/test/w3c-action-handler
Browse files Browse the repository at this point in the history
W3c action handler tests
  • Loading branch information
simonas-notcat authored Feb 25, 2020
2 parents 3d3cbf3 + 7e2229a commit 5a15d51
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Message, Core } from 'daf-core'
import { MessageValidator } from '../message-validator'
import { MessageValidator } from '../index'
describe('daf-url', () => {
const validator = new MessageValidator()

Expand Down
80 changes: 80 additions & 0 deletions packages/daf-w3c/src/__tests__/action-handler.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
const mockCreateVerifiableCredential = jest.fn()
const mockCreatePresentation = jest.fn()

jest.mock('did-jwt-vc', () => ({
createVerifiableCredential: mockCreateVerifiableCredential,
createPresentation: mockCreatePresentation,
}))

import { ActionHandler, ActionTypes, ActionSignW3cVc, ActionSignW3cVp } from '../index'

const mockDid = 'did:example:123'

const mockSigner = jest.fn()

const mockCore = {
identityManager: {
getIdentity: async (did: string) => ({
did: mockDid,
keyByType: async (type: string) => ({
signer: () => mockSigner,
}),
}),
},
}

describe('daf-w3c', () => {
it('handles action.sign.w3c.vc', async () => {
expect.assertions(1)

const actionHandler = new ActionHandler()

const data = {
sub: 'did:web:uport.me',
vc: {
'@context': ['https://www.w3.org/2018/credentials/v1'],
type: ['VerifiableCredential'],
credentialSubject: {
you: 'Rock',
},
},
}

await actionHandler.handleAction(
{
type: ActionTypes.signVc,
did: mockDid,
data,
} as ActionSignW3cVc,
mockCore as any,
)

expect(mockCreateVerifiableCredential).toBeCalledWith(data, { did: mockDid, signer: mockSigner })
})

it('handles action.sign.w3c.vp', async () => {
expect.assertions(1)

const actionHandler = new ActionHandler()

const data = {
sub: 'did:web:uport.me',
vp: {
'@context': ['https://www.w3.org/2018/credentials/v1'],
type: ['VerifiablePresentation'],
verifiableCredential: ['JWT'],
},
}

await actionHandler.handleAction(
{
type: ActionTypes.signVp,
did: mockDid,
data,
} as ActionSignW3cVp,
mockCore as any,
)

expect(mockCreatePresentation).toBeCalledWith(data, { did: mockDid, signer: mockSigner })
})
})
68 changes: 68 additions & 0 deletions packages/daf-w3c/src/__tests__/graphql.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { resolvers } from '../graphql'

const mockDid = 'did:example:123'

describe('daf-w3c:graphql', () => {
it('handles Mutation.actionSignVc', async () => {
const data = {
sub: 'did:web:uport.me',
vc: {
context: ['https://www.w3.org/2018/credentials/v1'],
type: ['VerifiableCredential'],
credentialSubject: {
you: 'Rock',
},
},
}

const transformedData = {
sub: 'did:web:uport.me',
vc: {
'@context': ['https://www.w3.org/2018/credentials/v1'],
type: ['VerifiableCredential'],
credentialSubject: {
you: 'Rock',
},
},
}
const mockHandleAction = jest.fn()
const core = { handleAction: mockHandleAction }

await resolvers.Mutation.actionSignVc(null, { data, did: mockDid }, { core: core as any })
expect(mockHandleAction).toBeCalledWith({
type: 'action.sign.w3c.vc',
did: mockDid,
data: transformedData,
})
})

it('handles Mutation.actionSignVp', async () => {
const data = {
aud: 'did:web:uport.me',
vp: {
context: ['https://www.w3.org/2018/credentials/v1'],
type: ['VerifiablePresentation'],
verifiableCredential: ['JWT'],
},
}

const transformedData = {
aud: 'did:web:uport.me',
vp: {
'@context': ['https://www.w3.org/2018/credentials/v1'],
type: ['VerifiablePresentation'],
verifiableCredential: ['JWT'],
},
}

const mockHandleAction = jest.fn()
const core = { handleAction: mockHandleAction }

await resolvers.Mutation.actionSignVp(null, { data, did: mockDid }, { core: core as any })
expect(mockHandleAction).toBeCalledWith({
type: 'action.sign.w3c.vp',
did: mockDid,
data: transformedData,
})
})
})
2 changes: 1 addition & 1 deletion packages/daf-w3c/src/__tests__/message-validator.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Message, Core } from 'daf-core'
import { MessageValidator, MessageTypes } from '../message-validator'
import { MessageValidator, MessageTypes } from '../index'

describe('daf-w3c', () => {
const vcJwt =
Expand Down
83 changes: 67 additions & 16 deletions packages/daf-w3c/src/graphql.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,74 @@
import { Core } from 'daf-core'
import { ActionTypes, ActionSignW3cVc, ActionSignW3cVp } from './action-handler'
import { PresentationPayload, VerifiableCredentialPayload, VC, VP } from 'did-jwt-vc/src/types'

interface Context {
core: Core
}
export interface CredentialSubject {
[x: string]: any
}

export interface VC {
'@context': string[]
type: string[]
credentialSubject: CredentialSubject
}

export interface VerifiableCredentialPayload {
sub: string
vc: VC
nbf?: number
aud?: string
exp?: number
jti?: string
[x: string]: any
}

export interface VP {
'@context': string[]
type: string[]
verifiableCredential: string[]
}

export interface PresentationPayload {
vp: VP
aud?: string
nbf?: number
exp?: number
jti?: string
[x: string]: any
}

interface VCInput extends VC {
context: [string]
interface VCInput {
context: string[]
type: string[]
credentialSubject: CredentialSubject
}

interface VerifiableCredentialInput extends VerifiableCredentialPayload {
interface VerifiableCredentialInput {
vc: VCInput
tag: string
tag?: string
sub: string
nbf?: number
aud?: string
exp?: number
jti?: string
[x: string]: any
}

interface VPInput extends VP {
context: [string]
interface VPInput {
context: string[]
type: string[]
verifiableCredential: string[]
}

interface VerifiablePresentationInput extends PresentationPayload {
interface VerifiablePresentationInput {
vp: VPInput
tag: string
aud?: string
nbf?: number
exp?: number
jti?: string
[x: string]: any
}

const actionSignVc = async (
Expand All @@ -36,16 +83,18 @@ const actionSignVc = async (
// This is needed because it is not possible to pass '@context' as gql input
const payload: VerifiableCredentialPayload = {
sub: data.sub,
nbf: data.nbf,
jti: data.jti,
aud: data.aud,
tag: data.tag,
vc: {
type: data.vc.type,
'@context': data.vc.context,
credentialSubject: data.vc.credentialSubject,
},
}
if (data.iat) payload['iat'] = data.iat
if (data.nbf) payload['nbf'] = data.nbf
if (data.aud) payload['aud'] = data.aud
if (data.jit) payload['jit'] = data.jit
if (data.tag) payload['tag'] = data.tag

return await ctx.core.handleAction({
type: ActionTypes.signVc,
did: args.did,
Expand All @@ -64,16 +113,18 @@ const actionSignVp = async (
const { data } = args
// This is needed because it is not possible to pass '@context' as gql input
const payload: PresentationPayload = {
nbf: data.nbf,
jti: data.jti,
aud: data.aud,
tag: data.tag,
vp: {
type: data.vp.type,
'@context': data.vp.context,
verifiableCredential: data.vp.verifiableCredential,
},
}
if (data.iat) payload['iat'] = data.iat
if (data.nbf) payload['nbf'] = data.nbf
if (data.jit) payload['jit'] = data.jit
if (data.tag) payload['tag'] = data.tag

return await ctx.core.handleAction({
type: ActionTypes.signVp,
did: args.did,
Expand Down

0 comments on commit 5a15d51

Please sign in to comment.