From 5317c14d54a6333071cbf278d47e55ff074c46d2 Mon Sep 17 00:00:00 2001 From: Yury Shkoda Date: Thu, 29 Jul 2021 10:24:03 +0300 Subject: [PATCH] test(sessionManager): improve test coverage of 'waitForSession' --- src/test/SessionManager.spec.ts | 85 +++++++++++++++++++++++++-------- 1 file changed, 64 insertions(+), 21 deletions(-) diff --git a/src/test/SessionManager.spec.ts b/src/test/SessionManager.spec.ts index 976f919..ace8526 100644 --- a/src/test/SessionManager.spec.ts +++ b/src/test/SessionManager.spec.ts @@ -4,6 +4,7 @@ import { NoSessionStateError } from '../types/errors' import * as dotenv from 'dotenv' import axios from 'axios' import { Logger, LogLevel } from '@sasjs/utils' +import { Session } from '../types' jest.mock('axios') const mockedAxios = axios as jest.Mocked @@ -48,6 +49,16 @@ describe('SessionManager', () => { }) describe('waitForSession', () => { + const session: Session = { + id: 'id', + state: '', + links: [{ rel: 'state', href: '', uri: '', type: '', method: 'GET' }], + attributes: { + sessionInactiveTimeout: 0 + }, + creationTimeStamp: '' + } + beforeEach(() => { ;(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 () => { let requestAttempt = 0 const requestAttemptLimit = 10 + const sessionState = 'idle' mockedAxios.get.mockImplementation(() => { requestAttempt += 1 if (requestAttempt >= requestAttemptLimit) { - return Promise.resolve({ data: 'idle', status: 200 }) + return Promise.resolve({ data: sessionState, status: 200 }) } return Promise.resolve({ data: '', status: 304 }) @@ -68,29 +80,60 @@ describe('SessionManager', () => { jest.spyOn((process as any).logger, 'info') - await expect( - sessionManager['waitForSession']( - { - id: 'id', - state: '', - links: [ - { rel: 'state', href: '', uri: '', type: '', method: 'GET' } - ], - attributes: { - sessionInactiveTimeout: 0 - }, - creationTimeStamp: '' - }, - null, - 'access_token' - ) - ).resolves.toEqual('idle') + sessionManager.debug = true - expect((process as any).logger.info).toHaveBeenCalledTimes(1) - expect((process as any).logger.info).toHaveBeenLastCalledWith( + await expect( + 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}` ) - 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) }) }) })