mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-16 08:30:07 +00:00
test(sessionManager): improve test coverage of 'waitForSession'
This commit is contained in:
@@ -4,6 +4,7 @@ import { NoSessionStateError } from '../types/errors'
|
|||||||
import * as dotenv from 'dotenv'
|
import * as dotenv from 'dotenv'
|
||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
import { Logger, LogLevel } from '@sasjs/utils'
|
import { Logger, LogLevel } from '@sasjs/utils'
|
||||||
|
import { Session } from '../types'
|
||||||
|
|
||||||
jest.mock('axios')
|
jest.mock('axios')
|
||||||
const mockedAxios = axios as jest.Mocked<typeof axios>
|
const mockedAxios = axios as jest.Mocked<typeof axios>
|
||||||
@@ -48,6 +49,16 @@ describe('SessionManager', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe('waitForSession', () => {
|
describe('waitForSession', () => {
|
||||||
|
const session: Session = {
|
||||||
|
id: 'id',
|
||||||
|
state: '',
|
||||||
|
links: [{ rel: 'state', href: '', uri: '', type: '', method: 'GET' }],
|
||||||
|
attributes: {
|
||||||
|
sessionInactiveTimeout: 0
|
||||||
|
},
|
||||||
|
creationTimeStamp: ''
|
||||||
|
}
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
;(process as any).logger = new Logger(LogLevel.Off)
|
;(process as any).logger = new Logger(LogLevel.Off)
|
||||||
})
|
})
|
||||||
@@ -55,12 +66,13 @@ describe('SessionManager', () => {
|
|||||||
it('should reject with NoSessionStateError if SAS server did not provide session state', async () => {
|
it('should reject with NoSessionStateError if SAS server did not provide session state', async () => {
|
||||||
let requestAttempt = 0
|
let requestAttempt = 0
|
||||||
const requestAttemptLimit = 10
|
const requestAttemptLimit = 10
|
||||||
|
const sessionState = 'idle'
|
||||||
|
|
||||||
mockedAxios.get.mockImplementation(() => {
|
mockedAxios.get.mockImplementation(() => {
|
||||||
requestAttempt += 1
|
requestAttempt += 1
|
||||||
|
|
||||||
if (requestAttempt >= requestAttemptLimit) {
|
if (requestAttempt >= requestAttemptLimit) {
|
||||||
return Promise.resolve({ data: 'idle', status: 200 })
|
return Promise.resolve({ data: sessionState, status: 200 })
|
||||||
}
|
}
|
||||||
|
|
||||||
return Promise.resolve({ data: '', status: 304 })
|
return Promise.resolve({ data: '', status: 304 })
|
||||||
@@ -68,29 +80,60 @@ describe('SessionManager', () => {
|
|||||||
|
|
||||||
jest.spyOn((process as any).logger, 'info')
|
jest.spyOn((process as any).logger, 'info')
|
||||||
|
|
||||||
await expect(
|
sessionManager.debug = true
|
||||||
sessionManager['waitForSession'](
|
|
||||||
{
|
|
||||||
id: 'id',
|
|
||||||
state: '',
|
|
||||||
links: [
|
|
||||||
{ rel: 'state', href: '', uri: '', type: '', method: 'GET' }
|
|
||||||
],
|
|
||||||
attributes: {
|
|
||||||
sessionInactiveTimeout: 0
|
|
||||||
},
|
|
||||||
creationTimeStamp: ''
|
|
||||||
},
|
|
||||||
null,
|
|
||||||
'access_token'
|
|
||||||
)
|
|
||||||
).resolves.toEqual('idle')
|
|
||||||
|
|
||||||
expect((process as any).logger.info).toHaveBeenCalledTimes(1)
|
await expect(
|
||||||
expect((process as any).logger.info).toHaveBeenLastCalledWith(
|
sessionManager['waitForSession'](session, null, 'access_token')
|
||||||
|
).resolves.toEqual(sessionState)
|
||||||
|
|
||||||
|
expect(mockedAxios.get).toHaveBeenCalledTimes(requestAttemptLimit)
|
||||||
|
expect((process as any).logger.info).toHaveBeenCalledTimes(3)
|
||||||
|
expect((process as any).logger.info).toHaveBeenNthCalledWith(
|
||||||
|
1,
|
||||||
|
'Polling session status...'
|
||||||
|
)
|
||||||
|
expect((process as any).logger.info).toHaveBeenNthCalledWith(
|
||||||
|
2,
|
||||||
`Could not get session state. Server responded with 304 whilst checking state: ${process.env.SERVER_URL}`
|
`Could not get session state. Server responded with 304 whilst checking state: ${process.env.SERVER_URL}`
|
||||||
)
|
)
|
||||||
expect(mockedAxios.get).toHaveBeenCalledTimes(requestAttemptLimit)
|
expect((process as any).logger.info).toHaveBeenNthCalledWith(
|
||||||
|
3,
|
||||||
|
`Current session state is '${sessionState}'`
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should throw an error if there is no session link', async () => {
|
||||||
|
const customSession = JSON.parse(JSON.stringify(session))
|
||||||
|
customSession.links = []
|
||||||
|
|
||||||
|
mockedAxios.get.mockImplementation(() =>
|
||||||
|
Promise.resolve({ data: customSession.state, status: 200 })
|
||||||
|
)
|
||||||
|
|
||||||
|
await expect(
|
||||||
|
sessionManager['waitForSession'](customSession, null, 'access_token')
|
||||||
|
).rejects.toContain('Error while getting session state link.')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should throw an error if could not get session state', async () => {
|
||||||
|
mockedAxios.get.mockImplementation(() => Promise.reject('Mocked error'))
|
||||||
|
|
||||||
|
await expect(
|
||||||
|
sessionManager['waitForSession'](session, null, 'access_token')
|
||||||
|
).rejects.toContain('Error while getting session state.')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should return session state', async () => {
|
||||||
|
const customSession = JSON.parse(JSON.stringify(session))
|
||||||
|
customSession.state = 'completed'
|
||||||
|
|
||||||
|
mockedAxios.get.mockImplementation(() =>
|
||||||
|
Promise.resolve({ data: customSession.state, status: 200 })
|
||||||
|
)
|
||||||
|
|
||||||
|
await expect(
|
||||||
|
sessionManager['waitForSession'](customSession, null, 'access_token')
|
||||||
|
).resolves.toEqual(customSession.state)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user