mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-04 11:10:05 +00:00
test: updated unit tests related to tokens operations
This commit is contained in:
@@ -55,7 +55,7 @@ describe('getAccessTokenForSasjs', () => {
|
||||
authConfig.refresh_token
|
||||
).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
|
||||
).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 { RequestClient } from '../../request/RequestClient'
|
||||
import { refreshTokensForSasjs } from '../refreshTokensForSasjs'
|
||||
import { getTokenRequestErrorPrefixResponse } from '../getTokenRequestErrorPrefix'
|
||||
|
||||
const requestClient = new (<jest.Mock<RequestClient>>RequestClient)()
|
||||
|
||||
@@ -38,9 +40,9 @@ describe('refreshTokensForSasjs', () => {
|
||||
const error = await refreshTokensForSasjs(
|
||||
requestClient,
|
||||
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 { generateToken, mockAuthResponse } from './mockResponses'
|
||||
import { RequestClient } from '../../request/RequestClient'
|
||||
import { refreshTokensForViya } from '../refreshTokensForViya'
|
||||
import * as IsNodeModule from '../../utils/isNode'
|
||||
import { getTokenRequestErrorPrefixResponse } from '../getTokenRequestErrorPrefix'
|
||||
|
||||
const requestClient = new (<jest.Mock<RequestClient>>RequestClient)()
|
||||
|
||||
@@ -67,9 +68,11 @@ describe('refreshTokensForViya', () => {
|
||||
authConfig.client,
|
||||
authConfig.secret,
|
||||
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 () => {
|
||||
|
||||
@@ -11,8 +11,8 @@ import {
|
||||
NotFoundError,
|
||||
InternalServerError
|
||||
} from '../types/errors'
|
||||
import { prefixMessage } from '@sasjs/utils/error'
|
||||
import { RequestClient } from '../request/RequestClient'
|
||||
import { getTokenRequestErrorPrefixResponse } from '../auth/getTokenRequestErrorPrefix'
|
||||
|
||||
const axiosActual = jest.requireActual('axios')
|
||||
|
||||
@@ -66,14 +66,18 @@ describe('RequestClient', () => {
|
||||
})
|
||||
|
||||
it('should response the POST method with Unauthorized', async () => {
|
||||
await expect(
|
||||
adapter.getAccessToken('clientId', 'clientSecret', 'incorrect')
|
||||
).rejects.toEqual(
|
||||
prefixMessage(
|
||||
new LoginRequiredError(incorrectAuthCodeErr),
|
||||
'Error while getting access token. '
|
||||
const expectedError = new LoginRequiredError({
|
||||
error: 'unauthorized',
|
||||
error_description: 'Bad credentials'
|
||||
})
|
||||
|
||||
const rejectionErrorMessage = await adapter
|
||||
.getAccessToken('clientId', 'clientSecret', 'incorrect')
|
||||
.catch((err) =>
|
||||
getTokenRequestErrorPrefixResponse(err.message, ServerType.SasViya)
|
||||
)
|
||||
)
|
||||
|
||||
expect(rejectionErrorMessage).toEqual(expectedError.message)
|
||||
})
|
||||
|
||||
describe('handleError', () => {
|
||||
@@ -209,15 +213,15 @@ describe('RequestClient - Self Signed Server', () => {
|
||||
serverType: ServerType.SasViya
|
||||
})
|
||||
|
||||
await expect(
|
||||
adapterWithoutCertificate.getAccessToken(
|
||||
'clientId',
|
||||
'clientSecret',
|
||||
'authCode'
|
||||
const expectedError = 'self signed certificate'
|
||||
|
||||
const rejectionErrorMessage = await adapterWithoutCertificate
|
||||
.getAccessToken('clientId', 'clientSecret', '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 () => {
|
||||
@@ -247,14 +251,18 @@ describe('RequestClient - Self Signed Server', () => {
|
||||
})
|
||||
|
||||
it('should response the POST method with Unauthorized', async () => {
|
||||
await expect(
|
||||
adapter.getAccessToken('clientId', 'clientSecret', 'incorrect')
|
||||
).rejects.toEqual(
|
||||
prefixMessage(
|
||||
new LoginRequiredError(incorrectAuthCodeErr),
|
||||
'Error while getting access token. '
|
||||
const expectedError = new LoginRequiredError({
|
||||
error: 'unauthorized',
|
||||
error_description: 'Bad credentials'
|
||||
})
|
||||
|
||||
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