mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-16 00:20:06 +00:00
test(get-token): covered getTokenRequestErrorPrefix
This commit is contained in:
@@ -55,7 +55,7 @@ describe('getAccessTokenForSasjs', () => {
|
|||||||
authConfig.refresh_token
|
authConfig.refresh_token
|
||||||
).catch((e: any) => e)
|
).catch((e: any) => e)
|
||||||
|
|
||||||
expect(error).toContain('Error while getting access token')
|
expect(error).toContain('Error while fetching access token')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ describe('getAccessTokenForViya', () => {
|
|||||||
authConfig.refresh_token
|
authConfig.refresh_token
|
||||||
).catch((e: any) => e)
|
).catch((e: any) => e)
|
||||||
|
|
||||||
expect(error).toContain('Error while getting access token')
|
expect(error).toContain('Error while fetching access token')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
81
src/auth/spec/getTokenRequestErrorPrefix.spec.ts
Normal file
81
src/auth/spec/getTokenRequestErrorPrefix.spec.ts
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
import { ServerType } from '@sasjs/utils/types'
|
||||||
|
import { getTokenRequestErrorPrefix } from '../getTokenRequestErrorPrefix'
|
||||||
|
|
||||||
|
describe('getTokenRequestErrorPrefix', () => {
|
||||||
|
it('should return error prefix', () => {
|
||||||
|
// INFO: Viya with only required attributes
|
||||||
|
let operation: 'fetching access token' = 'fetching access token'
|
||||||
|
const funcName = 'testFunc'
|
||||||
|
const url = '/SASjsApi/auth/token'
|
||||||
|
|
||||||
|
let expectedPrefix = `Error while ${operation} from ${url}
|
||||||
|
Thrown by the @sasjs/adapter ${funcName} function.
|
||||||
|
|
||||||
|
Response from Viya is below.
|
||||||
|
`
|
||||||
|
|
||||||
|
expect(
|
||||||
|
getTokenRequestErrorPrefix(operation, funcName, ServerType.SasViya, url)
|
||||||
|
).toEqual(expectedPrefix)
|
||||||
|
|
||||||
|
// INFO: Sasjs with data and headers
|
||||||
|
const data = {
|
||||||
|
grant_type: 'authorization_code',
|
||||||
|
code: 'testCode'
|
||||||
|
}
|
||||||
|
const headers = {
|
||||||
|
Authorization: 'Basic test=',
|
||||||
|
Accept: 'application/json'
|
||||||
|
}
|
||||||
|
|
||||||
|
expectedPrefix = `Error while ${operation} from ${url}
|
||||||
|
Thrown by the @sasjs/adapter ${funcName} function.
|
||||||
|
Payload:
|
||||||
|
${JSON.stringify(data, null, 2)}
|
||||||
|
Headers:
|
||||||
|
${JSON.stringify(headers, null, 2)}
|
||||||
|
|
||||||
|
Response from Sasjs is below.
|
||||||
|
`
|
||||||
|
|
||||||
|
expect(
|
||||||
|
getTokenRequestErrorPrefix(
|
||||||
|
operation,
|
||||||
|
funcName,
|
||||||
|
ServerType.Sasjs,
|
||||||
|
url,
|
||||||
|
data,
|
||||||
|
headers
|
||||||
|
)
|
||||||
|
).toEqual(expectedPrefix)
|
||||||
|
|
||||||
|
// INFO: Viya with all attributes
|
||||||
|
const clientId = 'testId'
|
||||||
|
const clientSecret = 'testSecret'
|
||||||
|
|
||||||
|
expectedPrefix = `Error while ${operation} from ${url}
|
||||||
|
Thrown by the @sasjs/adapter ${funcName} function.
|
||||||
|
Payload:
|
||||||
|
${JSON.stringify(data, null, 2)}
|
||||||
|
Headers:
|
||||||
|
${JSON.stringify(headers, null, 2)}
|
||||||
|
ClientId: ${clientId}
|
||||||
|
ClientSecret: ${clientSecret}
|
||||||
|
|
||||||
|
Response from Viya is below.
|
||||||
|
`
|
||||||
|
|
||||||
|
expect(
|
||||||
|
getTokenRequestErrorPrefix(
|
||||||
|
operation,
|
||||||
|
funcName,
|
||||||
|
ServerType.SasViya,
|
||||||
|
url,
|
||||||
|
data,
|
||||||
|
headers,
|
||||||
|
clientId,
|
||||||
|
clientSecret
|
||||||
|
)
|
||||||
|
).toEqual(expectedPrefix)
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
|
import { ServerType } from '@sasjs/utils'
|
||||||
import { generateToken, mockAuthResponse } from './mockResponses'
|
import { generateToken, mockAuthResponse } from './mockResponses'
|
||||||
import { RequestClient } from '../../request/RequestClient'
|
import { RequestClient } from '../../request/RequestClient'
|
||||||
import { refreshTokensForSasjs } from '../refreshTokensForSasjs'
|
import { refreshTokensForSasjs } from '../refreshTokensForSasjs'
|
||||||
|
import { getTokenRequestErrorPrefixResponse } from '../getTokenRequestErrorPrefix'
|
||||||
|
|
||||||
const requestClient = new (<jest.Mock<RequestClient>>RequestClient)()
|
const requestClient = new (<jest.Mock<RequestClient>>RequestClient)()
|
||||||
|
|
||||||
@@ -38,9 +40,9 @@ describe('refreshTokensForSasjs', () => {
|
|||||||
const error = await refreshTokensForSasjs(
|
const error = await refreshTokensForSasjs(
|
||||||
requestClient,
|
requestClient,
|
||||||
refresh_token
|
refresh_token
|
||||||
).catch((e: any) => e)
|
).catch((e: any) => getTokenRequestErrorPrefixResponse(e, ServerType.Sasjs))
|
||||||
|
|
||||||
expect(error).toEqual(`Error while refreshing tokens: ${tokenError}`)
|
expect(error).toEqual(tokenError)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
import { AuthConfig } from '@sasjs/utils'
|
import { AuthConfig, ServerType } from '@sasjs/utils'
|
||||||
import * as NodeFormData from 'form-data'
|
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'
|
import * as IsNodeModule from '../../utils/isNode'
|
||||||
|
import { getTokenRequestErrorPrefixResponse } from '../getTokenRequestErrorPrefix'
|
||||||
|
|
||||||
const requestClient = new (<jest.Mock<RequestClient>>RequestClient)()
|
const requestClient = new (<jest.Mock<RequestClient>>RequestClient)()
|
||||||
|
|
||||||
@@ -67,9 +68,11 @@ describe('refreshTokensForViya', () => {
|
|||||||
authConfig.client,
|
authConfig.client,
|
||||||
authConfig.secret,
|
authConfig.secret,
|
||||||
authConfig.refresh_token
|
authConfig.refresh_token
|
||||||
).catch((e: any) => e)
|
).catch((e: any) =>
|
||||||
|
getTokenRequestErrorPrefixResponse(e, ServerType.SasViya)
|
||||||
|
)
|
||||||
|
|
||||||
expect(error).toEqual(`Error while refreshing tokens: ${tokenError}`)
|
expect(error).toEqual(tokenError)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should throw an error if environment is not Node', async () => {
|
it('should throw an error if environment is not Node', async () => {
|
||||||
|
|||||||
@@ -11,8 +11,8 @@ import {
|
|||||||
NotFoundError,
|
NotFoundError,
|
||||||
InternalServerError
|
InternalServerError
|
||||||
} from '../types/errors'
|
} from '../types/errors'
|
||||||
import { prefixMessage } from '@sasjs/utils/error'
|
|
||||||
import { RequestClient } from '../request/RequestClient'
|
import { RequestClient } from '../request/RequestClient'
|
||||||
|
import { getTokenRequestErrorPrefixResponse } from '../auth/getTokenRequestErrorPrefix'
|
||||||
|
|
||||||
const axiosActual = jest.requireActual('axios')
|
const axiosActual = jest.requireActual('axios')
|
||||||
|
|
||||||
@@ -66,14 +66,18 @@ describe('RequestClient', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should response the POST method with Unauthorized', async () => {
|
it('should response the POST method with Unauthorized', async () => {
|
||||||
await expect(
|
const expectedError = new LoginRequiredError({
|
||||||
adapter.getAccessToken('clientId', 'clientSecret', 'incorrect')
|
error: 'unauthorized',
|
||||||
).rejects.toEqual(
|
error_description: 'Bad credentials'
|
||||||
prefixMessage(
|
})
|
||||||
new LoginRequiredError(incorrectAuthCodeErr),
|
|
||||||
'Error while getting access token. '
|
const rejectionErrorMessage = await adapter
|
||||||
|
.getAccessToken('clientId', 'clientSecret', 'incorrect')
|
||||||
|
.catch((err) =>
|
||||||
|
getTokenRequestErrorPrefixResponse(err.message, ServerType.SasViya)
|
||||||
)
|
)
|
||||||
)
|
|
||||||
|
expect(rejectionErrorMessage).toEqual(expectedError.message)
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('handleError', () => {
|
describe('handleError', () => {
|
||||||
@@ -209,15 +213,15 @@ describe('RequestClient - Self Signed Server', () => {
|
|||||||
serverType: ServerType.SasViya
|
serverType: ServerType.SasViya
|
||||||
})
|
})
|
||||||
|
|
||||||
await expect(
|
const expectedError = 'self signed certificate'
|
||||||
adapterWithoutCertificate.getAccessToken(
|
|
||||||
'clientId',
|
const rejectionErrorMessage = await adapterWithoutCertificate
|
||||||
'clientSecret',
|
.getAccessToken('clientId', 'clientSecret', 'authCode')
|
||||||
'authCode'
|
.catch((err) =>
|
||||||
|
getTokenRequestErrorPrefixResponse(err.message, ServerType.SasViya)
|
||||||
)
|
)
|
||||||
).rejects.toThrow(
|
|
||||||
`Error while getting access token. ${ERROR_MESSAGES.selfSigned}`
|
expect(rejectionErrorMessage).toEqual(expectedError)
|
||||||
)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should response the POST method using insecure flag', async () => {
|
it('should response the POST method using insecure flag', async () => {
|
||||||
@@ -247,14 +251,18 @@ describe('RequestClient - Self Signed Server', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should response the POST method with Unauthorized', async () => {
|
it('should response the POST method with Unauthorized', async () => {
|
||||||
await expect(
|
const expectedError = new LoginRequiredError({
|
||||||
adapter.getAccessToken('clientId', 'clientSecret', 'incorrect')
|
error: 'unauthorized',
|
||||||
).rejects.toEqual(
|
error_description: 'Bad credentials'
|
||||||
prefixMessage(
|
})
|
||||||
new LoginRequiredError(incorrectAuthCodeErr),
|
|
||||||
'Error while getting access token. '
|
const rejectionErrorMessage = await adapter
|
||||||
|
.getAccessToken('clientId', 'clientSecret', 'incorrect')
|
||||||
|
.catch((err) =>
|
||||||
|
getTokenRequestErrorPrefixResponse(err.message, ServerType.SasViya)
|
||||||
)
|
)
|
||||||
)
|
|
||||||
|
expect(rejectionErrorMessage).toEqual(expectedError.message)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user