Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('should throw an error when refresh token flow fails', async () => {
const onTokenInfoRefreshedMock = jest.fn()
const tokenInfo = await authClient.anonymousFlow()
tokenInfo.expires_at = 0 // emulate expired access_token
tokenInfo.refresh_token = 'invalid' // emulate broken refresh_token
const tokenProvider = new TokenProvider(
{
sdkAuth: authClient,
onTokenInfoRefreshed: onTokenInfoRefreshedMock,
},
tokenInfo
)
try {
await tokenProvider.getAccessToken()
throw new Error('Should throw an error')
} catch (err) {
expect(err.toString()).toEqual(
expect.stringContaining(
'BadRequest: The refresh token was not found. It may have expired.'
)
)
it('should refresh expired access_token', async () => {
const onTokenInfoRefreshedMock = jest.fn()
const tokenInfo = await authClient.anonymousFlow()
tokenInfo.expires_at = 0 // emulate expired access_token
const tokenProvider = new TokenProvider(
{
sdkAuth: authClient,
onTokenInfoRefreshed: onTokenInfoRefreshedMock,
},
tokenInfo
)
const accessToken = await tokenProvider.getAccessToken()
expect(accessToken).not.toEqual(tokenInfo.access_token)
expect(onTokenInfoRefreshedMock).toHaveBeenCalledTimes(1)
expect(accessToken).not.toEqual(
onTokenInfoRefreshedMock.mock.calls[0][0].refresh_token
)
const newTokenInfo = await tokenProvider.getTokenInfo()
expect(newTokenInfo.expires_at).not.toEqual(0)
it('should automatically retrieve tokenInfo', async () => {
const tokenProvider = new TokenProvider({
sdkAuth: authClient,
fetchTokenInfo: sdkAuth => sdkAuth.clientCredentialsFlow(),
})
const tokenInfo = await tokenProvider.getTokenInfo()
// check returned properties
expect(tokenInfo).toHaveProperty('access_token')
expect(tokenInfo).toHaveProperty('scope', `manage_project:${projectKey}`)
expect(tokenInfo).toHaveProperty('expires_in')
expect(tokenInfo).toHaveProperty('token_type', 'Bearer')
// use client to do a test request
const client = getApiClient(
`${tokenInfo.token_type} ${tokenInfo.access_token}`
)
const response = await client.execute({
it('should throw invalid client credentials error', async () => {
const _apiConfig = {
...apiConfig,
credentials: {
clientId: 'invalidClientId',
clientSecret: 'invalidClientSecret',
},
}
const _authClient = new SdkAuth(_apiConfig)
try {
await _authClient.clientCredentialsFlow()
throw new Error('Should throw an error')
} catch (err) {
expect(err).toEqual({
code: 401,
statusCode: 401,
status: 401,
message:
'Please provide valid client credentials using HTTP Basic Authentication.',
errors: [
{
code: 'invalid_client',
message:
'Please provide valid client credentials using HTTP Basic Authentication.',
beforeEach(() => {
authClient = new SdkAuth(apiConfig)
})