From 836051940825bbadc8f1d4cf3cf362da06a0322d Mon Sep 17 00:00:00 2001 From: Yury Shkoda Date: Fri, 21 Apr 2023 11:03:41 +0300 Subject: [PATCH] fix(refreshTokensForViya): added throw error if not Node --- src/auth/refreshTokensForViya.ts | 6 +++++- src/auth/spec/refreshTokensForViya.spec.ts | 13 +++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/auth/refreshTokensForViya.ts b/src/auth/refreshTokensForViya.ts index ecb2c4d..fe5f2b7 100644 --- a/src/auth/refreshTokensForViya.ts +++ b/src/auth/refreshTokensForViya.ts @@ -17,6 +17,10 @@ export async function refreshTokensForViya( clientSecret: string, refreshToken: string ) { + if (!isNode()) { + throw new Error(`Method 'refreshTokensForViya' can only be used by Node.`) + } + const url = '/SASLogon/oauth/token' const token = typeof Buffer === 'undefined' @@ -27,7 +31,7 @@ export async function refreshTokensForViya( Authorization: 'Basic ' + token } - const formData = isNode() ? new NodeFormData() : new FormData() + const formData = new NodeFormData() formData.append('grant_type', 'refresh_token') formData.append('refresh_token', refreshToken) diff --git a/src/auth/spec/refreshTokensForViya.spec.ts b/src/auth/spec/refreshTokensForViya.spec.ts index 49b8f19..e18b983 100644 --- a/src/auth/spec/refreshTokensForViya.spec.ts +++ b/src/auth/spec/refreshTokensForViya.spec.ts @@ -3,6 +3,7 @@ import * as NodeFormData from 'form-data' import { generateToken, mockAuthResponse } from './mockResponses' import { RequestClient } from '../../request/RequestClient' import { refreshTokensForViya } from '../refreshTokensForViya' +import * as IsNodeModule from '../../utils/isNode' const requestClient = new (>RequestClient)() @@ -70,6 +71,18 @@ describe('refreshTokensForViya', () => { expect(error).toEqual(`Error while refreshing tokens: ${tokenError}`) }) + + it('should throw an error if environment is not Node', async () => { + jest.spyOn(IsNodeModule, 'isNode').mockImplementation(() => false) + + const expectedError = new Error( + `Method 'refreshTokensForViya' can only be used by Node.` + ) + + expect( + refreshTokensForViya(requestClient, 'client', 'secret', 'token') + ).rejects.toEqual(expectedError) + }) }) const setupMocks = () => {