1
0
mirror of https://github.com/sasjs/adapter.git synced 2026-01-06 20:10:05 +00:00

chore(*): refactor common functionality into JobExecutor, handle all auth-related scenarios

This commit is contained in:
Krishna Acondy
2021-02-02 08:18:48 +00:00
parent 2ea49a425f
commit 03b5e1d824
11 changed files with 307 additions and 375 deletions

View File

@@ -1,6 +1,5 @@
import { AuthManager } from '../AuthManager'
import * as dotenv from 'dotenv'
import * as authModule from '..'
import { ServerType } from '@sasjs/utils/types'
import axios from 'axios'
import {
@@ -8,8 +7,8 @@ import {
mockLoginSuccessResponse
} from './mockResponses'
import { serialize } from '../../utils'
import { RequestClient } from '../../request/RequestClient'
jest.mock('axios')
jest.mock('../parseAndSubmitAuthorizeForm')
const mockedAxios = axios as jest.Mocked<typeof axios>
describe('AuthManager', () => {
@@ -18,6 +17,7 @@ describe('AuthManager', () => {
const serverType = ServerType.SasViya
const userName = 'test-username'
const password = 'test-password'
const requestClient = new RequestClient(serverUrl)
beforeAll(() => {
dotenv.config()
@@ -25,12 +25,16 @@ describe('AuthManager', () => {
})
it('should instantiate and set the correct URLs for a Viya server', () => {
const authManager = new AuthManager(serverUrl, serverType, authCallback)
const authManager = new AuthManager(
serverUrl,
serverType,
requestClient,
authCallback
)
expect(authManager).toBeTruthy()
expect((authManager as any).serverUrl).toEqual(serverUrl)
expect((authManager as any).serverType).toEqual(serverType)
expect((authManager as any).httpClient).toBeTruthy()
expect((authManager as any).loginUrl).toEqual(`/SASLogon/login`)
expect((authManager as any).logoutUrl).toEqual('/SASLogon/logout.do?')
})
@@ -39,18 +43,27 @@ describe('AuthManager', () => {
const authCallback = () => Promise.resolve()
const serverType = ServerType.Sas9
const authManager = new AuthManager(serverUrl, serverType, authCallback)
const authManager = new AuthManager(
serverUrl,
serverType,
requestClient,
authCallback
)
expect(authManager).toBeTruthy()
expect((authManager as any).serverUrl).toEqual(serverUrl)
expect((authManager as any).serverType).toEqual(serverType)
expect((authManager as any).httpClient).toBeTruthy()
expect((authManager as any).loginUrl).toEqual(`/SASLogon/login`)
expect((authManager as any).logoutUrl).toEqual('/SASLogon/logout?')
})
it('should call the auth callback and return when already logged in', async (done) => {
const authManager = new AuthManager(serverUrl, serverType, authCallback)
const authManager = new AuthManager(
serverUrl,
serverType,
requestClient,
authCallback
)
jest.spyOn(authManager, 'checkSession').mockImplementation(() =>
Promise.resolve({
isLoggedIn: true,
@@ -68,7 +81,12 @@ describe('AuthManager', () => {
})
it('should post a login request to the server if not logged in', async (done) => {
const authManager = new AuthManager(serverUrl, serverType, authCallback)
const authManager = new AuthManager(
serverUrl,
serverType,
requestClient,
authCallback
)
jest.spyOn(authManager, 'checkSession').mockImplementation(() =>
Promise.resolve({
isLoggedIn: false,
@@ -96,7 +114,6 @@ describe('AuthManager', () => {
loginParams,
{
withCredentials: true,
responseType: 'text',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Accept: '*/*'
@@ -108,9 +125,14 @@ describe('AuthManager', () => {
})
it('should parse and submit the authorisation form when necessary', async (done) => {
const authManager = new AuthManager(serverUrl, serverType, authCallback)
const authManager = new AuthManager(
serverUrl,
serverType,
requestClient,
authCallback
)
jest
.spyOn(authModule, 'parseAndSubmitAuthorizeForm')
.spyOn(requestClient, 'authorize')
.mockImplementation(() => Promise.resolve())
jest.spyOn(authManager, 'checkSession').mockImplementation(() =>
Promise.resolve({
@@ -125,15 +147,19 @@ describe('AuthManager', () => {
await authManager.logIn(userName, password)
expect(authModule.parseAndSubmitAuthorizeForm).toHaveBeenCalledWith(
mockLoginAuthoriseRequiredResponse,
serverUrl
expect(requestClient.authorize).toHaveBeenCalledWith(
mockLoginAuthoriseRequiredResponse
)
done()
})
it('should check and return session information if logged in', async (done) => {
const authManager = new AuthManager(serverUrl, serverType, authCallback)
const authManager = new AuthManager(
serverUrl,
serverType,
requestClient,
authCallback
)
mockedAxios.get.mockImplementation(() =>
Promise.resolve({ data: '<button onClick="logout">' })
)
@@ -141,9 +167,12 @@ describe('AuthManager', () => {
const response = await authManager.checkSession()
expect(response.isLoggedIn).toBeTruthy()
expect(mockedAxios.get).toHaveBeenNthCalledWith(1, `/SASLogon/login`, {
withCredentials: true,
responseType: 'text',
transformResponse: undefined,
headers: {
Accept: '*/*'
Accept: '*/*',
'Content-Type': 'text/plain'
}
})
@@ -151,7 +180,12 @@ describe('AuthManager', () => {
})
it('should check and return session information if logged in', async (done) => {
const authManager = new AuthManager(serverUrl, serverType, authCallback)
const authManager = new AuthManager(
serverUrl,
serverType,
requestClient,
authCallback
)
mockedAxios.get.mockImplementation(() =>
Promise.resolve({ data: '<button onClick="logout">' })
)
@@ -159,9 +193,12 @@ describe('AuthManager', () => {
const response = await authManager.checkSession()
expect(response.isLoggedIn).toBeTruthy()
expect(mockedAxios.get).toHaveBeenNthCalledWith(1, `/SASLogon/login`, {
withCredentials: true,
responseType: 'text',
transformResponse: undefined,
headers: {
Accept: '*/*'
Accept: '*/*',
'Content-Type': 'text/plain'
}
})