mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-09 05:20:05 +00:00
Merge branch 'master' into issue-186
This commit is contained in:
@@ -1,34 +1,16 @@
|
||||
import { ContextManager } from '../ContextManager'
|
||||
import { RequestClient } from '../request/RequestClient'
|
||||
import * as dotenv from 'dotenv'
|
||||
import axios from 'axios'
|
||||
jest.mock('axios')
|
||||
const mockedAxios = axios as jest.Mocked<typeof axios>
|
||||
|
||||
describe('ContextManager', () => {
|
||||
let originalFetch: any
|
||||
let fetchCallNumber = 0
|
||||
|
||||
const fakeGlobalFetch = (fakeResponses: object[]) => {
|
||||
;(global as any).fetch = jest.fn().mockImplementation(() => {
|
||||
const fakeResponse = fakeResponses[fetchCallNumber]
|
||||
|
||||
if (
|
||||
fetchCallNumber !== fakeResponses.length &&
|
||||
fakeResponses.length > 1
|
||||
) {
|
||||
if (fetchCallNumber + 1 === fakeResponses.length) fetchCallNumber = 0
|
||||
else fetchCallNumber += 1
|
||||
} else {
|
||||
fetchCallNumber = 0
|
||||
}
|
||||
|
||||
return Promise.resolve({
|
||||
ok: true,
|
||||
headers: { get: () => '' },
|
||||
json: () => Promise.resolve(fakeResponse)
|
||||
})
|
||||
})
|
||||
}
|
||||
dotenv.config()
|
||||
|
||||
const contextManager = new ContextManager(
|
||||
process.env.SERVER_URL as string,
|
||||
() => {}
|
||||
new RequestClient(process.env.SERVER_URL as string)
|
||||
)
|
||||
|
||||
const defaultComputeContexts = contextManager.getDefaultComputeContexts
|
||||
@@ -43,14 +25,6 @@ describe('ContextManager', () => {
|
||||
Math.floor(Math.random() * defaultLauncherContexts.length)
|
||||
]
|
||||
|
||||
beforeAll(() => {
|
||||
originalFetch = (global as any).fetch
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
;(global as any).fetch = originalFetch
|
||||
})
|
||||
|
||||
describe('getComputeContexts', () => {
|
||||
it('should fetch compute contexts', async () => {
|
||||
const sampleComputeContext = {
|
||||
@@ -65,7 +39,9 @@ describe('ContextManager', () => {
|
||||
items: [sampleComputeContext]
|
||||
}
|
||||
|
||||
fakeGlobalFetch([sampleResponse])
|
||||
mockedAxios.get.mockImplementation(() =>
|
||||
Promise.resolve({ data: sampleResponse })
|
||||
)
|
||||
|
||||
await expect(contextManager.getComputeContexts()).resolves.toEqual([
|
||||
sampleComputeContext
|
||||
@@ -87,7 +63,9 @@ describe('ContextManager', () => {
|
||||
items: [sampleComputeContext]
|
||||
}
|
||||
|
||||
fakeGlobalFetch([sampleResponse])
|
||||
mockedAxios.get.mockImplementation(() =>
|
||||
Promise.resolve({ data: sampleResponse })
|
||||
)
|
||||
|
||||
await expect(contextManager.getLauncherContexts()).resolves.toEqual([
|
||||
sampleComputeContext
|
||||
@@ -137,7 +115,9 @@ describe('ContextManager', () => {
|
||||
items: [sampleComputeContext]
|
||||
}
|
||||
|
||||
fakeGlobalFetch([sampleResponse])
|
||||
mockedAxios.get.mockImplementation(() =>
|
||||
Promise.resolve({ data: sampleResponse })
|
||||
)
|
||||
|
||||
await expect(
|
||||
contextManager.createComputeContext(
|
||||
@@ -176,10 +156,13 @@ describe('ContextManager', () => {
|
||||
items: [sampleNewComputeContext]
|
||||
}
|
||||
|
||||
fakeGlobalFetch([
|
||||
sampleResponseExistingComputeContexts,
|
||||
sampleResponseCreatedComputeContext
|
||||
])
|
||||
mockedAxios.get.mockImplementation(() =>
|
||||
Promise.resolve({ data: sampleResponseExistingComputeContexts })
|
||||
)
|
||||
|
||||
mockedAxios.post.mockImplementation(() =>
|
||||
Promise.resolve({ data: sampleResponseCreatedComputeContext })
|
||||
)
|
||||
|
||||
await expect(
|
||||
contextManager.createComputeContext(
|
||||
@@ -226,10 +209,13 @@ describe('ContextManager', () => {
|
||||
items: [sampleNewComputeContext]
|
||||
}
|
||||
|
||||
fakeGlobalFetch([
|
||||
sampleResponseExistingComputeContexts,
|
||||
sampleResponseCreatedComputeContext
|
||||
])
|
||||
mockedAxios.get.mockImplementation(() =>
|
||||
Promise.resolve({ data: sampleResponseExistingComputeContexts })
|
||||
)
|
||||
|
||||
mockedAxios.post.mockImplementation(() =>
|
||||
Promise.resolve({ data: sampleResponseCreatedComputeContext })
|
||||
)
|
||||
|
||||
await expect(
|
||||
contextManager.createComputeContext(
|
||||
@@ -287,11 +273,16 @@ describe('ContextManager', () => {
|
||||
items: [sampleNewComputeContext]
|
||||
}
|
||||
|
||||
fakeGlobalFetch([
|
||||
sampleResponseExistingComputeContexts,
|
||||
sampleResponseCreatedLauncherContext,
|
||||
sampleResponseCreatedComputeContext
|
||||
])
|
||||
mockedAxios.get
|
||||
.mockImplementationOnce(() =>
|
||||
Promise.resolve({ data: sampleResponseExistingComputeContexts })
|
||||
)
|
||||
.mockImplementationOnce(() =>
|
||||
Promise.resolve({ data: sampleResponseCreatedLauncherContext })
|
||||
)
|
||||
mockedAxios.post.mockImplementation(() =>
|
||||
Promise.resolve({ data: sampleResponseCreatedComputeContext })
|
||||
)
|
||||
|
||||
await expect(
|
||||
contextManager.createComputeContext(
|
||||
@@ -346,7 +337,9 @@ describe('ContextManager', () => {
|
||||
items: [sampleLauncherContext]
|
||||
}
|
||||
|
||||
fakeGlobalFetch([sampleResponse])
|
||||
mockedAxios.get.mockImplementation(() =>
|
||||
Promise.resolve({ data: sampleResponse })
|
||||
)
|
||||
|
||||
await expect(
|
||||
contextManager.createLauncherContext(contextName, 'Test Description')
|
||||
@@ -380,10 +373,13 @@ describe('ContextManager', () => {
|
||||
items: [sampleNewLauncherContext]
|
||||
}
|
||||
|
||||
fakeGlobalFetch([
|
||||
sampleResponseExistingLauncherContext,
|
||||
sampleResponseCreatedLauncherContext
|
||||
])
|
||||
mockedAxios.get.mockImplementation(() =>
|
||||
Promise.resolve({ data: sampleResponseExistingLauncherContext })
|
||||
)
|
||||
|
||||
mockedAxios.post.mockImplementation(() =>
|
||||
Promise.resolve({ data: sampleResponseCreatedLauncherContext })
|
||||
)
|
||||
|
||||
await expect(
|
||||
contextManager.createLauncherContext(contextName, 'Test Description')
|
||||
@@ -448,7 +444,9 @@ describe('ContextManager', () => {
|
||||
items: [sampleComputeContext]
|
||||
}
|
||||
|
||||
fakeGlobalFetch([sampleResponseGetComputeContextByName])
|
||||
mockedAxios.put.mockImplementation(() =>
|
||||
Promise.resolve({ data: sampleResponseGetComputeContextByName })
|
||||
)
|
||||
|
||||
const expectedResponse = {
|
||||
etag: '',
|
||||
@@ -475,7 +473,9 @@ describe('ContextManager', () => {
|
||||
items: [sampleComputeContext]
|
||||
}
|
||||
|
||||
fakeGlobalFetch([sampleResponse])
|
||||
mockedAxios.get.mockImplementation(() =>
|
||||
Promise.resolve({ data: sampleResponse })
|
||||
)
|
||||
|
||||
const user = 'testUser'
|
||||
|
||||
@@ -508,7 +508,9 @@ describe('ContextManager', () => {
|
||||
items: [sampleComputeContext]
|
||||
}
|
||||
|
||||
fakeGlobalFetch([sampleResponse])
|
||||
mockedAxios.get.mockImplementation(() =>
|
||||
Promise.resolve({ data: sampleResponse })
|
||||
)
|
||||
|
||||
const fakedExecuteScript = async () => {
|
||||
return Promise.resolve({ log: '' })
|
||||
@@ -567,10 +569,13 @@ describe('ContextManager', () => {
|
||||
items: [sampleComputeContext]
|
||||
}
|
||||
|
||||
fakeGlobalFetch([
|
||||
sampleResponseGetComputeContextByName,
|
||||
sampleResponseDeletedContext
|
||||
])
|
||||
mockedAxios.get.mockImplementation(() =>
|
||||
Promise.resolve({ data: sampleResponseGetComputeContextByName })
|
||||
)
|
||||
|
||||
mockedAxios.delete.mockImplementation(() =>
|
||||
Promise.resolve({ data: sampleResponseDeletedContext })
|
||||
)
|
||||
|
||||
const expectedResponse = {
|
||||
etag: '',
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
import { FileUploader } from '../FileUploader'
|
||||
import { UploadFile } from '../types'
|
||||
import { RequestClient } from '../request/RequestClient'
|
||||
import axios from 'axios'
|
||||
jest.mock('axios')
|
||||
const mockedAxios = axios as jest.Mocked<typeof axios>
|
||||
|
||||
const sampleResponse = `{
|
||||
"SYSUSERID": "cas",
|
||||
@@ -24,39 +28,22 @@ const prepareFilesAndParams = () => {
|
||||
}
|
||||
|
||||
describe('FileUploader', () => {
|
||||
let originalFetch: any
|
||||
const fileUploader = new FileUploader(
|
||||
'/sample/apploc',
|
||||
'https://sample.server.com',
|
||||
'/jobs/path',
|
||||
null,
|
||||
null
|
||||
new RequestClient('https://sample.server.com')
|
||||
)
|
||||
|
||||
beforeAll(() => {
|
||||
originalFetch = (global as any).fetch
|
||||
})
|
||||
|
||||
beforeEach(() => {
|
||||
;(global as any).fetch = jest.fn().mockImplementation(() =>
|
||||
Promise.resolve({
|
||||
text: () => Promise.resolve(sampleResponse)
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
afterAll(() => {
|
||||
;(global as any).fetch = originalFetch
|
||||
})
|
||||
|
||||
it('should upload successfully', async (done) => {
|
||||
const sasJob = 'test/upload'
|
||||
const { files, params } = prepareFilesAndParams()
|
||||
mockedAxios.post.mockImplementation(() =>
|
||||
Promise.resolve({ data: sampleResponse })
|
||||
)
|
||||
|
||||
fileUploader.uploadFile(sasJob, files, params).then((res: any) => {
|
||||
expect(JSON.stringify(res)).toEqual(
|
||||
JSON.stringify(JSON.parse(sampleResponse))
|
||||
)
|
||||
expect(res).toEqual(JSON.parse(sampleResponse))
|
||||
done()
|
||||
})
|
||||
})
|
||||
@@ -83,10 +70,8 @@ describe('FileUploader', () => {
|
||||
})
|
||||
|
||||
it('should throw an error when login is required', async (done) => {
|
||||
;(global as any).fetch = jest.fn().mockImplementation(() =>
|
||||
Promise.resolve({
|
||||
text: () => Promise.resolve('<form action="Logon">')
|
||||
})
|
||||
mockedAxios.post.mockImplementation(() =>
|
||||
Promise.resolve({ data: '<form action="Logon">' })
|
||||
)
|
||||
|
||||
const sasJob = 'test'
|
||||
@@ -101,35 +86,29 @@ describe('FileUploader', () => {
|
||||
})
|
||||
|
||||
it('should throw an error when invalid JSON is returned by the server', async (done) => {
|
||||
;(global as any).fetch = jest.fn().mockImplementation(() =>
|
||||
Promise.resolve({
|
||||
text: () => Promise.resolve('{invalid: "json"')
|
||||
})
|
||||
mockedAxios.post.mockImplementation(() =>
|
||||
Promise.resolve({ data: '{invalid: "json"' })
|
||||
)
|
||||
|
||||
const sasJob = 'test'
|
||||
const { files, params } = prepareFilesAndParams()
|
||||
|
||||
fileUploader.uploadFile(sasJob, files, params).catch((err: any) => {
|
||||
expect(err.error.message).toEqual(
|
||||
'Error while parsing json from upload response.'
|
||||
)
|
||||
expect(err.error.message).toEqual('File upload request failed.')
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should throw an error when the server request fails', async (done) => {
|
||||
;(global as any).fetch = jest.fn().mockImplementation(() =>
|
||||
Promise.resolve({
|
||||
text: () => Promise.reject('{message: "Server error"}')
|
||||
})
|
||||
mockedAxios.post.mockImplementation(() =>
|
||||
Promise.reject({ data: '{message: "Server error"}' })
|
||||
)
|
||||
|
||||
const sasJob = 'test'
|
||||
const { files, params } = prepareFilesAndParams()
|
||||
|
||||
fileUploader.uploadFile(sasJob, files, params).catch((err: any) => {
|
||||
expect(err.error.message).toEqual('Upload request failed.')
|
||||
expect(err.error.message).toEqual('File upload request failed.')
|
||||
|
||||
done()
|
||||
})
|
||||
|
||||
@@ -1,25 +1,19 @@
|
||||
import { SessionManager } from '../SessionManager'
|
||||
import * as dotenv from 'dotenv'
|
||||
import { RequestClient } from '../request/RequestClient'
|
||||
import axios from 'axios'
|
||||
jest.mock('axios')
|
||||
const mockedAxios = axios as jest.Mocked<typeof axios>
|
||||
|
||||
describe('SessionManager', () => {
|
||||
dotenv.config()
|
||||
|
||||
let originalFetch: any
|
||||
|
||||
const sessionManager = new SessionManager(
|
||||
process.env.SERVER_URL as string,
|
||||
process.env.DEFAULT_COMPUTE_CONTEXT as string,
|
||||
() => {}
|
||||
new RequestClient('https://sample.server.com')
|
||||
)
|
||||
|
||||
beforeAll(() => {
|
||||
originalFetch = (global as any).fetch
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
;(global as any).fetch = originalFetch
|
||||
})
|
||||
|
||||
describe('getVariable', () => {
|
||||
it('should fetch session variable', async () => {
|
||||
const sampleResponse = {
|
||||
@@ -31,12 +25,8 @@ describe('SessionManager', () => {
|
||||
version: 1
|
||||
}
|
||||
|
||||
;(global as any).fetch = jest.fn().mockImplementation(() =>
|
||||
Promise.resolve({
|
||||
ok: true,
|
||||
headers: { get: () => '' },
|
||||
json: () => Promise.resolve(sampleResponse)
|
||||
})
|
||||
mockedAxios.get.mockImplementation(() =>
|
||||
Promise.resolve({ data: sampleResponse })
|
||||
)
|
||||
|
||||
const expectedResponse = { etag: '', result: sampleResponse }
|
||||
|
||||
Reference in New Issue
Block a user