Skip to content

Commit

Permalink
feat: changed the test structure and few other pr notes addressed
Browse files Browse the repository at this point in the history
  • Loading branch information
sksadjad committed Jun 15, 2023
1 parent d0fd5f1 commit 6520fbe
Show file tree
Hide file tree
Showing 17 changed files with 286 additions and 112 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
44 changes: 44 additions & 0 deletions packages/oid4vci-issuer-rest-client/__tests__/localAgent.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { createObjects, getConfig } from '../../agent-config/dist'

jest.setTimeout(30000)

import issuanceRestClientAgentLogic from './shared/issuanceRestClientAgentLogic'
import nock from 'nock'

let agent: any

const setup = async (): Promise<boolean> => {
const config = await getConfig('packages/oid4vci-issuer-rest-client/agent.yml')
const { localAgent } = await createObjects(config, { localAgent: '/agent' })
agent = localAgent
nock('https://ssi-backend.sphereon.com/webapp/')
.post(`/credential-offers`, {
grants: {
'urn:ietf:params:oauth:grant-type:pre-authorized_code': {
'pre-authorized_code': '1234',
user_pin_required: false,
},
},
credentials: ['dbc2023'],
})
.times(4)
.reply(200, {
uri: 'openid-credential-offer://?credential_offer=%7B%22grants%22%3A%7B%22urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Apre-authorized_code%22%3A%7B%22pre-authorized_code%22%3A%221234%22%2C%22user_pin_required%22%3Afalse%7D%7D%2C%22credentials%22%3A%5B%22dbc2023%22%5D%2C%22credential_issuer%22%3A%22https%3A%2F%2Fdbc2023.test.sphereon.com%2Fissuer%2Fdbc2023%22%7D',
})
return true
}

const tearDown = async (): Promise<boolean> => {
return true
}

const getAgent = () => agent
const testContext = {
getAgent,
setup,
tearDown,
}

describe('Local integration tests', () => {
issuanceRestClientAgentLogic(testContext)
})
67 changes: 67 additions & 0 deletions packages/oid4vci-issuer-rest-client/__tests__/restAgent.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import 'cross-fetch/polyfill'
// @ts-ignore
import express, { Router } from 'express'
import { Server } from 'http'
import { IAgent, createAgent, IAgentOptions } from '@veramo/core'
import { AgentRestClient } from '@veramo/remote-client'
import { AgentRouter, RequestWithAgentRouter } from '@veramo/remote-server'
import { createObjects, getConfig } from '../../agent-config/dist'
import { IOID4VCIRestClient } from '../src'
import issuanceRestClientAgentLogic from './shared/issuanceRestClientAgentLogic'

jest.setTimeout(30000)

const port = 3002
const basePath = '/agent'

let serverAgent: IAgent
let restServer: Server

const getAgent = (options?: IAgentOptions) =>
createAgent<IOID4VCIRestClient>({
...options,
plugins: [
new AgentRestClient({
url: 'http://localhost:' + port + basePath,
enabledMethods: serverAgent.availableMethods(),
schema: serverAgent.getSchema(),
}),
],
})

const setup = async (): Promise<boolean> => {
const config = await getConfig('packages/oid4vci-issuer-rest-client/agent.yml')
const { agent } = await createObjects(config, { agent: '/agent' })
serverAgent = agent

const agentRouter: Router = AgentRouter({
exposedMethods: serverAgent.availableMethods(),
})

const requestWithAgent: Router = RequestWithAgentRouter({
agent: serverAgent,
})

return new Promise((resolve): void => {
const app = express()
app.use(basePath, requestWithAgent, agentRouter)
restServer = app.listen(port, () => {
resolve(true)
})
})
}

const tearDown = async (): Promise<boolean> => {
restServer.close()
return true
}

const testContext = {
getAgent,
setup,
tearDown,
}

describe('REST integration tests', () => {
issuanceRestClientAgentLogic(testContext)
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { TAgent } from '@veramo/core'
import { IOID4VCIRestClient } from '../../src'

type ConfiguredAgent = TAgent<IOID4VCIRestClient>

export default (testContext: { getAgent: () => ConfiguredAgent; setup: () => Promise<boolean>; tearDown: () => Promise<boolean> }) => {
describe('ssi-sdk.oid4vci-issuer-rest-client', () => {
let agent: ConfiguredAgent

beforeAll(async () => {
await testContext.setup()
agent = testContext.getAgent()
})
afterAll(async () => {
await testContext.tearDown()
})

it('should create the url Offer Url with baseUrl', async () => {
const result = await agent.vciClientCreateOfferUri({
baseUrl: 'https://ssi-backend.sphereon.com',
grants: {
'urn:ietf:params:oauth:grant-type:pre-authorized_code': {
'pre-authorized_code': '1234',
user_pin_required: false,
},
},
credentials: ['dbc2023'],
})
expect(result).toEqual({
uri: 'openid-credential-offer://?credential_offer=%7B%22grants%22%3A%7B%22urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Apre-authorized_code%22%3A%7B%22pre-authorized_code%22%3A%221234%22%2C%22user_pin_required%22%3Afalse%7D%7D%2C%22credentials%22%3A%5B%22dbc2023%22%5D%2C%22credential_issuer%22%3A%22https%3A%2F%2Fdbc2023.test.sphereon.com%2Fissuer%2Fdbc2023%22%7D',
})
})

it('should create the url Offer Url without baseUrl', async () => {
const result = await agent.vciClientCreateOfferUri({
grants: {
'urn:ietf:params:oauth:grant-type:pre-authorized_code': {
'pre-authorized_code': '1234',
user_pin_required: false,
},
},
credentials: ['dbc2023'],
})
expect(result).toEqual({
uri: 'openid-credential-offer://?credential_offer=%7B%22grants%22%3A%7B%22urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Apre-authorized_code%22%3A%7B%22pre-authorized_code%22%3A%221234%22%2C%22user_pin_required%22%3Afalse%7D%7D%2C%22credentials%22%3A%5B%22dbc2023%22%5D%2C%22credential_issuer%22%3A%22https%3A%2F%2Fdbc2023.test.sphereon.com%2Fissuer%2Fdbc2023%22%7D',
})
})
})
}
73 changes: 73 additions & 0 deletions packages/oid4vci-issuer-rest-client/agent.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
version: 3.0

constants:
baseUrl: http://localhost:3335
port: 3335
methods:
- vciClientCreateOfferUri

server:
baseUrl:
$ref: /constants/baseUrl
port:
$ref: /constants/port
use:
# CORS
- - $require: 'cors'

# Add agent to the request object
- - $require: '@veramo/remote-server?t=function#RequestWithAgentRouter'
$args:
- agent:
$ref: /agent

# API base path
- - /agent
- $require: '@veramo/remote-server?t=function#apiKeyAuth'
$args:
# Please configure your own API key. This is used when executing agent methods through ${baseUrl}/agent or ${baseUrl}/api-docs
- apiKey: test123
- $require: '@veramo/remote-server?t=function#AgentRouter'
$args:
- exposedMethods:
$ref: /constants/methods

# Open API schema
- - /open-api.json
- $require: '@veramo/remote-server?t=function#ApiSchemaRouter'
$args:
- basePath: :3335/agent
securityScheme: bearer
apiName: Agent
apiVersion: '1.0.0'
exposedMethods:
$ref: /constants/methods

# Swagger docs
- - /api-docs
- $require: swagger-ui-express?t=object#serve
- $require: swagger-ui-express?t=function#setup
$args:
- null
- swaggerOptions:
url: '/open-api.json'

# Execute during server initialization
init:
- $require: '@veramo/remote-server?t=function#createDefaultDid'
$args:
- agent:
$ref: /agent
baseUrl:
$ref: /constants/baseUrl
messagingServiceEndpoint: /messaging

# Agent
agent:
$require: '@veramo/core#Agent'
$args:
- schemaValidation: false
plugins:
- $require: ./packages/oid4vci-issuer-rest-client/dist#OID4VCIRestClient
$args:
- baseUrl: 'https://ssi-backend.sphereon.com'
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@sphereon/ssi-sdk.oid4vci-rest-client",
"name": "@sphereon/ssi-sdk.oid4vci-issuer-rest-client",
"description": "contains the client side to call REST endpoints of a Verifiable Credential Issuer",
"version": "0.11.0",
"source": "src/index.ts",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import Debug from 'debug'
import { IAgentPlugin } from '@veramo/core'

const debug = Debug('sphereon:ssi-sdk.oid4vci-rest-client')
const debug = Debug('sphereon:ssi-sdk.oid4vci-issuer-rest-client')

/**
* @beta
Expand All @@ -30,36 +30,42 @@ export class OID4VCIRestClient implements IAgentPlugin {
if (!args.credentials || !args.grants) {
throw new Error("Can't generate the credential offer url without credentials and grants params present.")
}
const baseUrl = this.checkBaseUrlParameter(args.baseUri)
this.assertBaseUrl(args.baseUrl)
const request: IVCIClientCreateOfferUriRequest = {
credentials: args.credentials,
grants: args.grants,
}
if (args.grants) {
request['grants'] = args.grants
}
const baseUrl = args.baseUrl || this.baseUrl
const url = this.urlWithBase(`webapp/credential-offers`, baseUrl)
debug(`OID4VCIRestClient is going to send request: ${JSON.stringify(request)} to ${url}`)
const origResponse = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(request),
})
return await origResponse.json()
try {
const origResponse = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(request),
})
if (origResponse.status >= 400) {
return Promise.reject(Error(`request to ${url} returned ${origResponse.status}`));
}
return await origResponse.json()
} catch (e) {
debug(`Error on posting to url ${url}: ${e}`)
throw e
}
}

private urlWithBase(path: string, baseUrl?: string): string {
if (!this.baseUrl && !baseUrl) {
throw new Error('You have to provide baseUrl')
}
return baseUrl ? `${baseUrl}${path.startsWith('/') ? path : '/' + path}` : `${this.baseUrl}${path.startsWith('/') ? path : '/' + path}`
return baseUrl ? `${baseUrl}${path.startsWith('/') ? path : `/${path}`}` : `${this.baseUrl}${path.startsWith('/') ? path : `/${path}`}`
}

private checkBaseUrlParameter(baseUrl?: string): string {
private assertBaseUrl(baseUrl?: string) {
if (!baseUrl && !this.baseUrl) {
throw new Error('No base url has been provided')
}
return baseUrl ? baseUrl : (this.baseUrl as string)
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface IOID4VCIRestClient extends IPluginMethodMap {
export interface IVCIClientCreateOfferUriRequestArgs {
grants: Grant
credentials: (CredentialOfferFormat | string)[]
baseUri?: string
baseUrl?: string
}

export interface IVCIClientCreateOfferUriResponse {
Expand Down
File renamed without changes.
26 changes: 0 additions & 26 deletions packages/oid4vci-rest-client/__tests__/integration.test.ts

This file was deleted.

40 changes: 0 additions & 40 deletions packages/oid4vci-rest-client/__tests__/mockedEndpoints.test.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
{ "path": "oid4vci-issuer-store" },
{ "path": "oid4vci-issuer" },
{ "path": "oid4vci-issuer-rest-api" },
{ "path": "oid4vci-issuer-rest-client" },
{ "path": "siopv2-oid4vp-common" },
{ "path": "siopv2-oid4vp-op-auth" },
{ "path": "siopv2-oid4vp-rp-auth" },
Expand Down
Loading

0 comments on commit 6520fbe

Please sign in to comment.