diff --git a/src/auth/spec/getAccessTokenForSasjs.spec.ts b/src/auth/spec/getAccessTokenForSasjs.spec.ts index e70ab39..2d85154 100644 --- a/src/auth/spec/getAccessTokenForSasjs.spec.ts +++ b/src/auth/spec/getAccessTokenForSasjs.spec.ts @@ -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') }) }) diff --git a/src/auth/spec/getAccessTokenForViya.spec.ts b/src/auth/spec/getAccessTokenForViya.spec.ts index 01ea181..a74db7b 100644 --- a/src/auth/spec/getAccessTokenForViya.spec.ts +++ b/src/auth/spec/getAccessTokenForViya.spec.ts @@ -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') }) }) diff --git a/src/auth/spec/getTokenRequestErrorPrefix.spec.ts b/src/auth/spec/getTokenRequestErrorPrefix.spec.ts new file mode 100644 index 0000000..6c0ccd9 --- /dev/null +++ b/src/auth/spec/getTokenRequestErrorPrefix.spec.ts @@ -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) + }) +}) diff --git a/src/auth/spec/refreshTokensForSasjs.spec.ts b/src/auth/spec/refreshTokensForSasjs.spec.ts index 0a8a49f..ade5913 100644 --- a/src/auth/spec/refreshTokensForSasjs.spec.ts +++ b/src/auth/spec/refreshTokensForSasjs.spec.ts @@ -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 (>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) }) }) diff --git a/src/auth/spec/refreshTokensForViya.spec.ts b/src/auth/spec/refreshTokensForViya.spec.ts index e18b983..d35a940 100644 --- a/src/auth/spec/refreshTokensForViya.spec.ts +++ b/src/auth/spec/refreshTokensForViya.spec.ts @@ -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 (>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 () => { diff --git a/src/test/RequestClient.spec.ts b/src/test/RequestClient.spec.ts index b428380..fc9cc5d 100644 --- a/src/test/RequestClient.spec.ts +++ b/src/test/RequestClient.spec.ts @@ -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) }) })