Skip to content

Commit

Permalink
Merge pull request #1 from wultra/issues/activation-helper-improve
Browse files Browse the repository at this point in the history
Issues/activation helper improve
  • Loading branch information
hvge authored Sep 21, 2022
2 parents 438294e + f0ced48 commit ae504e8
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 19 deletions.
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,23 @@ type RNActivationHelper = ActivationHelper<PowerAuth, PowerAuthCreateActivationR

const PA_SERVER_URL = "http://localhost:8080/powerauth-java-server"
const PA_ENROLLMENT = "http://localhost:8080/enrollment-server"
const PA_INSTANCE = 'your-app-instance-id'

export interface CustomActivationHelperPrepareData extends ActivationHelperPrepareData {
instanceId?: string
}

/**
* Function create instnace of activation helper typed with RN wrapper objects.
*/
async function getActivationHelper(): Promise<RNActivationHelper> {
const cfg = { connection: { baseUrl: PA_SERVER_URL}}
const helper = await ActivationHelper.createWithConfig<PowerAuth>(cfg)
const helper: RNActivationHelper = await ActivationHelper.createWithConfig(cfg)
helper.createSdk = async (appSetup, prepareData) => {
// Prepare instanceId. We're using custom data in prepare interface to keep instance id.
const instanceId = prepareData?.customData?.get('instanceId') ?? 'your-app-instance-id'
const instanceId = (prepareData as CustomActivationHelperPrepareData).instanceId ?? PA_INSTANCE
const sdk = new PowerAuth(instanceId)
if (sdk.isConfigured()) {
if (await sdk.isConfigured()) {
await sdk.deconfigure() // depending on whether you expect config changes
}
const unsecure = PA_ENROLLMENT.startsWith('http://')
Expand All @@ -67,7 +72,7 @@ async function getActivationHelper(): Promise<RNActivationHelper> {
helper.prepareStep = async (helper, activation, prepareData) => {
if (!prepareData) throw new Error('Missing prepare data object')
if (!prepareData.password) throw new Error('Missing password in prepare data object')
const sdk = helper.getPowerAuthSdk()
const sdk = await helper.getPowerAuthSdk()
const deviceName = 'Test device'
const activationData = PowerAuthActivation.createWithActivationCode(activation.activationCode!, deviceName)
// Create activation
Expand All @@ -89,7 +94,7 @@ async function getActivationHelper(): Promise<RNActivationHelper> {
/**
* Function prepare activation to active state.
*/
async function prepareActivationWithHelper(prepareData: ActivationHelperPrepareData): Promise<RNActivationHelper> {
async function prepareActivationWithHelper(prepareData: CustomActivationHelperPrepareData): Promise<RNActivationHelper> {
const config = { connection: { baseUrl: PA_SERVER_URL }}
const helper = await createActivationHelper(config, prepareData)
await helper.createActivation(helper.userId, prepareData)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "powerauth-js-test-client",
"version": "0.1.1",
"version": "0.2.0",
"description": "Client library for PowerAuth Server RESTful API to support JavaScript integration tests with PowerAuth Server",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
14 changes: 10 additions & 4 deletions src/helpers/ActivationHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ export interface ActivationHelperPrepareData {
* OTP validation mode.
*/
otpValidation?: ActivationOtpValidation
/**
* Custom data.
*/
customData?: Map<string, any>
}

/**
Expand Down Expand Up @@ -158,6 +154,16 @@ export class ActivationHelper<SDK, PrepareResult> {
return await this.withSDK(a => a)
}

/**
* Return SDK instance. If there's no such instance available, then throws an error.
*/
get powerAuthSdk(): SDK {
if (!this.sdkInstance) {
throw new Error('PowerAuth SDK instance is not available')
}
return this.sdkInstance
}

/**
* Result from prepare activation step. If result is not available then throws an error.
*/
Expand Down
8 changes: 4 additions & 4 deletions tests/Activations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { testServerConfiguration } from "./config/config";
import { ActivationHelper, ActivationHelperPrepareData, ActivationStatus, Config, Logger, PowerAuthTestServer } from "../src/index";
import { CreateActivationData, MiniMobileClient } from "./crypto/MiniMobileClient";
import { createActivationHelper } from "./config/helpers";
import { createActivationHelper, CustomActivationHelperPrepareData } from "./config/helpers";


const activationPayload: CreateActivationData = {
Expand All @@ -26,10 +26,9 @@ const activationPayload: CreateActivationData = {
deviceInfo: 'nodejs-tests',
extras: 'some-extras'
}
const preapreData: ActivationHelperPrepareData = {
customData: new Map<string, any>()
const preapreData: CustomActivationHelperPrepareData = {
activationPayload: activationPayload
}
preapreData.customData!.set('activationPayload', activationPayload)

describe('Manage PowerAuth applications', () => {

Expand Down Expand Up @@ -68,6 +67,7 @@ describe('Manage PowerAuth applications', () => {

test('Test automatic activation create ', async () => {
const activation = await activationHelper.createActivation()
expect(activationHelper.powerAuthSdk).toBeDefined()
let status = await activationHelper.getActivationStatus()
expect(status).toBe(ActivationStatus.ACTIVE)
})
Expand Down
13 changes: 8 additions & 5 deletions tests/config/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,25 @@ import { testServerConfiguration } from "./config";

export type TypedActivationHelper = ActivationHelper<MiniMobileClient, boolean>

export interface CustomActivationHelperPrepareData extends ActivationHelperPrepareData {
activationPayload?: CreateActivationData
}

/**
* Create activation helper with configured prepare step. This helper can automatically create activation
* and move it to 'ACTIVE' state.
*/
export async function createActivationHelper(config: Config | undefined = undefined, prepareData: ActivationHelperPrepareData | undefined = undefined): Promise<TypedActivationHelper> {
export async function createActivationHelper(config: Config | undefined = undefined, prepareData: CustomActivationHelperPrepareData | undefined = undefined): Promise<TypedActivationHelper> {
// Acquire config
const cfg = config ?? await testServerConfiguration()
const activationData = prepareData?.customData?.get('activationPayload') as CreateActivationData
const activationPayload = activationData ?? {
const activationPayload = prepareData?.activationPayload ?? {
activationName: 'activation-test',
platform: 'nodejs',
deviceInfo: 'nodejs-tests',
extras: 'some-extras'
}
// Create activation helper with using MiniMobileClient as SDK implementation
const helper = await ActivationHelper.createWithConfig<MiniMobileClient,boolean>(cfg)
const helper: TypedActivationHelper = await ActivationHelper.createWithConfig(cfg)
helper.createSdk = async (appSetup, _) => {
return new MiniMobileClient(appSetup)
}
Expand Down Expand Up @@ -63,7 +66,7 @@ export async function createActivationHelper(config: Config | undefined = undefi
/**
* Create fully prepared activation in ACTIVE state.
*/
export async function createActivationWithActivationHelper(config: Config | undefined = undefined, prepareData: ActivationHelperPrepareData | undefined = undefined): Promise<TypedActivationHelper> {
export async function createActivationWithActivationHelper(config: Config | undefined = undefined, prepareData: CustomActivationHelperPrepareData | undefined = undefined): Promise<TypedActivationHelper> {
const helper = await createActivationHelper(config, prepareData)
await helper.createActivation(helper.userId, prepareData)
return helper
Expand Down

0 comments on commit ae504e8

Please sign in to comment.