Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion src/lib/proxy/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { ERROR_CODES, METHODS, STATUS } from '../../types'
import { WebCommandOpenUrl, WebCommandSearchElement } from '../../types/proxy'
import { ADMIN_SERVICE_AUTH_ERROR_CODES, WebCommandOpenUrl, WebCommandSearchElement } from '../../types/proxy'

describe('proxy', () => {
beforeEach(() => {
Expand Down Expand Up @@ -202,6 +202,54 @@ describe('proxy', () => {
await expect(runWebCommandsPipeline([])).rejects.toBe(ERROR_CODES.NO_BRIDGE)
})
})

describe('requestAdminServiceAuth', () => {
it('success response', async () => {
const response = { ref: 'proxy-admin-service-auth', payload: { status: STATUS.SUCCESS } }
const sendClientEventMock = vi.fn().mockResolvedValue(response)

vi.doMock('@expressms/smartapp-bridge', () => ({
default: { sendClientEvent: sendClientEventMock },
}))

const { requestAdminServiceAuth } = await import('./index')

const result = await requestAdminServiceAuth()

expect(sendClientEventMock).toHaveBeenCalledTimes(1)
expect(sendClientEventMock).toHaveBeenCalledWith({
method: METHODS.REQUEST_ADMIN_SERVICE_AUTH,
params: {},
timeout: 10_000,
hide_recv_event_data: true,
})
expect(result).toBe(response)
})

it('error response', async () => {
const response = {
ref: 'proxy-admin-service-auth-error',
payload: { status: STATUS.ERROR, errorCode: ADMIN_SERVICE_AUTH_ERROR_CODES.UNAUTHORIZED },
}
const sendClientEventMock = vi.fn().mockResolvedValue(response)

vi.doMock('@expressms/smartapp-bridge', () => ({
default: { sendClientEvent: sendClientEventMock },
}))

const { requestAdminServiceAuth } = await import('./index')

const result = await requestAdminServiceAuth()

expect(result.payload.errorCode).toBe(ADMIN_SERVICE_AUTH_ERROR_CODES.UNAUTHORIZED)
})

it('no bridge', async () => {
vi.doMock('@expressms/smartapp-bridge', () => ({ default: undefined }))
const { requestAdminServiceAuth } = await import('./index')
await expect(requestAdminServiceAuth()).rejects.toBe(ERROR_CODES.NO_BRIDGE)
})
})
})


24 changes: 23 additions & 1 deletion src/lib/proxy/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import bridge from '@expressms/smartapp-bridge'
import { CookieItem, ERROR_CODES, METHODS, StatusResponse } from '../../types'
import { CredentialsType, GetCredentialsResponse, WebCommandsPipeline } from '../../types/proxy'
import {
CredentialsType,
GetCredentialsResponse,
RequestAdminServiceAuthResponse,
WebCommandsPipeline,
} from '../../types/proxy'

/**
* Set cookies for web resouce. It's needed for SSO auth cases.
Expand Down Expand Up @@ -104,6 +109,23 @@ export const deleteCredentials = (): Promise<StatusResponse> => {
.then(event => event as StatusResponse)
}

/**
* Request admin service auth for web resource
* @returns Promise that'll be fullfilled with `payload.status` on success, otherwise rejected with reason
*/
export const requestAdminServiceAuth = (): Promise<RequestAdminServiceAuthResponse> => {
if (!bridge) return Promise.reject(ERROR_CODES.NO_BRIDGE)

return bridge
.sendClientEvent({
method: METHODS.REQUEST_ADMIN_SERVICE_AUTH,
params: {},
timeout: 10_000,
hide_recv_event_data: true,
})
.then(event => event as RequestAdminServiceAuthResponse)
}

/**
* Run pipeline for manipulating web resource
* @param pipeline Pipeline with jobs & command
Expand Down
1 change: 1 addition & 0 deletions src/types/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export enum METHODS {
GET_NFC_STATUS = 'get_nfc_status',
OPEN_CLIENT_CONTACTS = 'open_client_contacts',
ALLOW_IOS_PINCH_TO_ZOOM = 'allow_ios_pinch_to_zoom',
REQUEST_ADMIN_SERVICE_AUTH = 'request_admin_service_auth',
}

export enum STATUS {
Expand Down
14 changes: 14 additions & 0 deletions src/types/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,17 @@ export interface WebCommandsJob {
}

export type WebCommandsPipeline = Array<WebCommandsJob>

export enum ADMIN_SERVICE_AUTH_ERROR_CODES {
UNAUTHORIZED = 'admin_service_unauthorized',
UNAVAILABLE = 'admin_service_unavailable',
NETWORK_ERROR = 'network_error',
TIMEOUT = 'timeout',
}

export interface RequestAdminServiceAuthResponse extends Omit<EmitterEventPayload, 'payload'> {
payload: {
status: STATUS
errorCode?: ADMIN_SERVICE_AUTH_ERROR_CODES | null
}
}
Loading