mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-05 11:40:06 +00:00
fix(refreshTokensForViya): added throw error if not Node
This commit is contained in:
@@ -17,6 +17,10 @@ export async function refreshTokensForViya(
|
|||||||
clientSecret: string,
|
clientSecret: string,
|
||||||
refreshToken: string
|
refreshToken: string
|
||||||
) {
|
) {
|
||||||
|
if (!isNode()) {
|
||||||
|
throw new Error(`Method 'refreshTokensForViya' can only be used by Node.`)
|
||||||
|
}
|
||||||
|
|
||||||
const url = '/SASLogon/oauth/token'
|
const url = '/SASLogon/oauth/token'
|
||||||
const token =
|
const token =
|
||||||
typeof Buffer === 'undefined'
|
typeof Buffer === 'undefined'
|
||||||
@@ -27,7 +31,7 @@ export async function refreshTokensForViya(
|
|||||||
Authorization: 'Basic ' + token
|
Authorization: 'Basic ' + token
|
||||||
}
|
}
|
||||||
|
|
||||||
const formData = isNode() ? new NodeFormData() : new FormData()
|
const formData = new NodeFormData()
|
||||||
formData.append('grant_type', 'refresh_token')
|
formData.append('grant_type', 'refresh_token')
|
||||||
formData.append('refresh_token', refreshToken)
|
formData.append('refresh_token', refreshToken)
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import * as NodeFormData from 'form-data'
|
|||||||
import { generateToken, mockAuthResponse } from './mockResponses'
|
import { generateToken, mockAuthResponse } from './mockResponses'
|
||||||
import { RequestClient } from '../../request/RequestClient'
|
import { RequestClient } from '../../request/RequestClient'
|
||||||
import { refreshTokensForViya } from '../refreshTokensForViya'
|
import { refreshTokensForViya } from '../refreshTokensForViya'
|
||||||
|
import * as IsNodeModule from '../../utils/isNode'
|
||||||
|
|
||||||
const requestClient = new (<jest.Mock<RequestClient>>RequestClient)()
|
const requestClient = new (<jest.Mock<RequestClient>>RequestClient)()
|
||||||
|
|
||||||
@@ -70,6 +71,18 @@ describe('refreshTokensForViya', () => {
|
|||||||
|
|
||||||
expect(error).toEqual(`Error while refreshing tokens: ${tokenError}`)
|
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 = () => {
|
const setupMocks = () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user