mirror of
https://github.com/sasjs/adapter.git
synced 2025-12-11 09:24:35 +00:00
Compare commits
45 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f1cceeb5e6 | ||
|
|
6fee2548fd | ||
|
|
91005066cf | ||
|
|
e1f17ef47d | ||
|
|
8a40071c35 | ||
|
|
430957eb3d | ||
|
|
25874be679 | ||
|
|
ed8440434f | ||
|
|
0f9884c1b6 | ||
|
|
d126a05347 | ||
|
|
3e26bbbbba | ||
|
|
982cc8f7a0 | ||
|
|
d1770698e0 | ||
|
|
b78e8617c4 | ||
|
|
3ce9ca0986 | ||
|
|
04d17c3680 | ||
|
|
d26e15f91c | ||
|
|
83c46091b3 | ||
|
|
d640d7c040 | ||
|
|
c934eb2b08 | ||
|
|
24dd5e32ad | ||
|
|
a23103b2c3 | ||
|
|
35aa4235e4 | ||
|
|
e9be1cf99a | ||
|
|
c7b0821081 | ||
|
|
4a4618dd32 | ||
|
|
d223e83c60 | ||
|
|
d1f1a20126 | ||
|
|
4b89e3762f | ||
|
|
bc110288de | ||
|
|
e94e16b52c | ||
|
|
76aacee016 | ||
|
|
1a3bd5d1f5 | ||
|
|
3f6e89d716 | ||
|
|
361ec84638 | ||
|
|
35cc1e4f62 | ||
|
|
64a976e888 | ||
|
|
7e2cb8491f | ||
|
|
2cdab7522d | ||
|
|
a07eabc408 | ||
|
|
7279c23fe2 | ||
|
|
80707d77d9 | ||
|
|
d5920c5885 | ||
|
|
6a3a6b4485 | ||
|
|
3f796b300d |
@@ -14,4 +14,5 @@ What code changes have been made to achieve the intent.
|
||||
|
||||
- [ ] Code is formatted correctly (`npm run lint:fix`).
|
||||
- [ ] All unit tests are passing (`npm test`).
|
||||
- [ ] All `sasjs-tests` unit tests are passing (`npm test`).
|
||||
- [ ] All `sasjs-tests` are passing (instructions available [here](https://github.com/sasjs/adapter/blob/master/sasjs-tests/README.md)).
|
||||
|
||||
File diff suppressed because one or more lines are too long
231
docs/classes/reflection-790.reflection-214.fileuploader.html
Normal file
231
docs/classes/reflection-790.reflection-214.fileuploader.html
Normal file
File diff suppressed because one or more lines are too long
312
docs/classes/reflection-790.reflection-214.sas9apiclient.html
Normal file
312
docs/classes/reflection-790.reflection-214.sas9apiclient.html
Normal file
File diff suppressed because one or more lines are too long
1641
docs/classes/reflection-790.reflection-214.sasjs.html
Normal file
1641
docs/classes/reflection-790.reflection-214.sasjs.html
Normal file
File diff suppressed because one or more lines are too long
1444
docs/classes/reflection-790.reflection-214.sasviyaapiclient.html
Normal file
1444
docs/classes/reflection-790.reflection-214.sasviyaapiclient.html
Normal file
File diff suppressed because one or more lines are too long
323
docs/classes/reflection-790.reflection-214.sessionmanager.html
Normal file
323
docs/classes/reflection-790.reflection-214.sessionmanager.html
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
106
docs/modules/reflection-790.html
Normal file
106
docs/modules/reflection-790.html
Normal file
File diff suppressed because one or more lines are too long
128
docs/modules/reflection-790.reflection-214.html
Normal file
128
docs/modules/reflection-790.reflection-214.html
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
2579
package-lock.json
generated
2579
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
10
package.json
10
package.json
@@ -37,15 +37,15 @@
|
||||
"license": "ISC",
|
||||
"devDependencies": {
|
||||
"@types/isomorphic-fetch": "0.0.35",
|
||||
"@types/jest": "^26.0.14",
|
||||
"@types/jest": "^26.0.15",
|
||||
"cp": "^0.2.0",
|
||||
"jest": "^25.5.4",
|
||||
"path": "^0.12.7",
|
||||
"rimraf": "^3.0.2",
|
||||
"semantic-release": "^17.1.2",
|
||||
"terser-webpack-plugin": "^4.2.2",
|
||||
"semantic-release": "^17.2.3",
|
||||
"terser-webpack-plugin": "^4.2.3",
|
||||
"ts-jest": "^25.5.1",
|
||||
"ts-loader": "^8.0.4",
|
||||
"ts-loader": "^8.0.11",
|
||||
"tslint": "^6.1.3",
|
||||
"tslint-config-prettier": "^1.18.0",
|
||||
"typedoc": "^0.17.8",
|
||||
@@ -53,7 +53,7 @@
|
||||
"typedoc-plugin-external-module-name": "^4.0.3",
|
||||
"typescript": "^3.9.7",
|
||||
"webpack": "^4.44.2",
|
||||
"webpack-cli": "^3.3.12"
|
||||
"webpack-cli": "^4.2.0"
|
||||
},
|
||||
"main": "index.js",
|
||||
"dependencies": {
|
||||
|
||||
@@ -37,6 +37,17 @@ export const basicTests = (
|
||||
assertion: (response: any) =>
|
||||
response && response.isLoggedIn && response.userName === userName
|
||||
},
|
||||
{
|
||||
title: "Multiple Log in attempts",
|
||||
description: "Should fail on first attempt and should log the user in on second attempt",
|
||||
test: async () => {
|
||||
await adapter.logOut()
|
||||
await adapter.logIn('invalid', 'invalid')
|
||||
return adapter.logIn(userName, password)
|
||||
},
|
||||
assertion: (response: any) =>
|
||||
response && response.isLoggedIn && response.userName === userName
|
||||
},
|
||||
{
|
||||
title: "Default config",
|
||||
description:
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { isLogInRequired, needsRetry, isUrl } from './utils'
|
||||
import { CsrfToken } from './types/CsrfToken'
|
||||
import { UploadFile } from './types/UploadFile'
|
||||
import { ErrorResponse } from './types'
|
||||
|
||||
const requestRetryLimit = 5
|
||||
|
||||
@@ -18,29 +19,31 @@ export class FileUploader {
|
||||
private retryCount = 0
|
||||
|
||||
public uploadFile(sasJob: string, files: UploadFile[], params: any) {
|
||||
if (files?.length < 1)
|
||||
throw new Error('At least one file must be provided.')
|
||||
|
||||
let paramsString = ''
|
||||
|
||||
for (let param in params) {
|
||||
if (params.hasOwnProperty(param)) {
|
||||
paramsString += `&${param}=${params[param]}`
|
||||
}
|
||||
}
|
||||
|
||||
const program = this.appLoc
|
||||
? this.appLoc.replace(/\/?$/, '/') + sasJob.replace(/^\//, '')
|
||||
: sasJob
|
||||
const uploadUrl = `${this.serverUrl}${this.jobsPath}/?${
|
||||
'_program=' + program
|
||||
}${paramsString}`
|
||||
|
||||
const headers = {
|
||||
'cache-control': 'no-cache'
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
if (files?.length < 1)
|
||||
reject(new ErrorResponse('At least one file must be provided.'))
|
||||
if (!sasJob || sasJob === '')
|
||||
reject(new ErrorResponse('sasJob must be provided.'))
|
||||
|
||||
let paramsString = ''
|
||||
|
||||
for (let param in params) {
|
||||
if (params.hasOwnProperty(param)) {
|
||||
paramsString += `&${param}=${params[param]}`
|
||||
}
|
||||
}
|
||||
|
||||
const program = this.appLoc
|
||||
? this.appLoc.replace(/\/?$/, '/') + sasJob.replace(/^\//, '')
|
||||
: sasJob
|
||||
const uploadUrl = `${this.serverUrl}${this.jobsPath}/?${
|
||||
'_program=' + program
|
||||
}${paramsString}`
|
||||
|
||||
const headers = {
|
||||
'cache-control': 'no-cache'
|
||||
}
|
||||
|
||||
const formData = new FormData()
|
||||
|
||||
for (let file of files) {
|
||||
@@ -76,7 +79,7 @@ export class FileUploader {
|
||||
})
|
||||
.then((responseText) => {
|
||||
if (isLogInRequired(responseText))
|
||||
reject('You must be logged in to upload a file')
|
||||
reject(new ErrorResponse('You must be logged in to upload a file.'))
|
||||
|
||||
if (needsRetry(responseText)) {
|
||||
if (this.retryCount < requestRetryLimit) {
|
||||
@@ -95,10 +98,18 @@ export class FileUploader {
|
||||
try {
|
||||
resolve(JSON.parse(responseText))
|
||||
} catch (e) {
|
||||
reject(e)
|
||||
reject(
|
||||
new ErrorResponse(
|
||||
'Error while parsing json from upload response.',
|
||||
e
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
})
|
||||
.catch((err: any) => {
|
||||
reject(new ErrorResponse('Upload request failed.', err))
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -164,9 +164,9 @@ export class SASViyaApiClient {
|
||||
for (const promise of promises) results.push(await promise())
|
||||
|
||||
results.forEach((result: any, index: number) => {
|
||||
if (result && result.body && result.body.details) {
|
||||
if (result && result.error && result.error.details) {
|
||||
try {
|
||||
const resultParsed = JSON.parse(result.body.details)
|
||||
const resultParsed = result.error.details
|
||||
|
||||
if (resultParsed && resultParsed.body) {
|
||||
let sysUserId = ''
|
||||
@@ -574,7 +574,7 @@ export class SASViyaApiClient {
|
||||
}
|
||||
|
||||
if (jobStatus === 'failed' || jobStatus === 'error') {
|
||||
return Promise.reject({ error: currentJob.error, log })
|
||||
return Promise.reject({ job: currentJob, log })
|
||||
}
|
||||
|
||||
let resultLink
|
||||
@@ -607,7 +607,7 @@ export class SASViyaApiClient {
|
||||
})
|
||||
|
||||
return Promise.reject(
|
||||
new ErrorResponse('Job execution failed', {
|
||||
new ErrorResponse('Job execution failed.', {
|
||||
status: 500,
|
||||
body: log
|
||||
})
|
||||
@@ -1032,6 +1032,8 @@ export class SASViyaApiClient {
|
||||
jobToExecute.code = code
|
||||
}
|
||||
|
||||
if (!code) code = ''
|
||||
|
||||
const linesToExecute = code.replace(/\r\n/g, '\n').split('\n')
|
||||
return await this.executeScript(
|
||||
sasJob,
|
||||
@@ -1112,7 +1114,7 @@ export class SASViyaApiClient {
|
||||
}
|
||||
|
||||
if (!jobToExecute) {
|
||||
throw new Error(`The job ${sasJob} was not found.`)
|
||||
throw new Error(`Job was not found.`)
|
||||
}
|
||||
const jobDefinitionLink = jobToExecute?.links.find(
|
||||
(l) => l.rel === 'getResource'
|
||||
|
||||
144
src/SASjs.ts
144
src/SASjs.ts
@@ -411,6 +411,29 @@ export default class SASjs {
|
||||
}
|
||||
}
|
||||
|
||||
private async getLoginForm(response: any) {
|
||||
const pattern: RegExp = /<form.+action="(.*Logon[^"]*).*>/
|
||||
const matches = pattern.exec(response)
|
||||
const formInputs: any = {}
|
||||
|
||||
if (matches && matches.length) {
|
||||
this.setLoginUrl(matches)
|
||||
const inputs = response.match(/<input.*"hidden"[^>]*>/g)
|
||||
|
||||
if (inputs) {
|
||||
inputs.forEach((inputStr: string) => {
|
||||
const valueMatch = inputStr.match(/name="([^"]*)"\svalue="([^"]*)/)
|
||||
|
||||
if (valueMatch && valueMatch.length) {
|
||||
formInputs[valueMatch[1]] = valueMatch[2]
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return Object.keys(formInputs).length ? formInputs : null
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether a session is active, or login is required.
|
||||
* @returns - a promise which resolves with an object containing two values - a boolean `isLoggedIn`, and a string `userName`.
|
||||
@@ -419,10 +442,16 @@ export default class SASjs {
|
||||
const loginResponse = await fetch(this.loginUrl.replace('.do', ''))
|
||||
const responseText = await loginResponse.text()
|
||||
const isLoggedIn = /<button.+onClick.+logout/gm.test(responseText)
|
||||
let loginForm: any = null
|
||||
|
||||
if (!isLoggedIn) {
|
||||
loginForm = await this.getLoginForm(responseText)
|
||||
}
|
||||
|
||||
return Promise.resolve({
|
||||
isLoggedIn,
|
||||
userName: this.userName
|
||||
userName: this.userName,
|
||||
loginForm
|
||||
})
|
||||
}
|
||||
|
||||
@@ -440,7 +469,7 @@ export default class SASjs {
|
||||
|
||||
this.userName = loginParams.username
|
||||
|
||||
const { isLoggedIn } = await this.checkSession()
|
||||
const { isLoggedIn, loginForm } = await this.checkSession()
|
||||
if (isLoggedIn) {
|
||||
this.resendWaitingRequests()
|
||||
|
||||
@@ -450,15 +479,13 @@ export default class SASjs {
|
||||
})
|
||||
}
|
||||
|
||||
const loginForm = await this.getLoginForm()
|
||||
|
||||
for (const key in loginForm) {
|
||||
loginParams[key] = loginForm[key]
|
||||
}
|
||||
const loginParamsStr = serialize(loginParams)
|
||||
|
||||
return fetch(this.loginUrl, {
|
||||
method: 'post',
|
||||
method: 'POST',
|
||||
credentials: 'include',
|
||||
referrerPolicy: 'same-origin',
|
||||
body: loginParamsStr,
|
||||
@@ -780,11 +807,23 @@ export default class SASjs {
|
||||
} else {
|
||||
this.retryCountComputeApi = 0
|
||||
reject(
|
||||
new ErrorResponse('Compute API retry requests limit reached')
|
||||
new ErrorResponse('Compute API retry requests limit reached.')
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if (response?.log) {
|
||||
this.appendSasjsRequest(response.log, sasJob, null)
|
||||
}
|
||||
|
||||
if (error.toString().includes('Job was not found')) {
|
||||
reject(
|
||||
new ErrorResponse('Service not found on the server.', {
|
||||
sasJob: sasJob
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
if (error && error.status === 401) {
|
||||
if (loginRequiredCallback) loginRequiredCallback(true)
|
||||
sasjsWaitingRequest.requestPromise.resolve = resolve
|
||||
@@ -792,10 +831,8 @@ export default class SASjs {
|
||||
sasjsWaitingRequest.config = config
|
||||
this.sasjsWaitingRequests.push(sasjsWaitingRequest)
|
||||
} else {
|
||||
reject(new ErrorResponse('Job execution failed', error))
|
||||
reject(new ErrorResponse('Job execution failed.', error))
|
||||
}
|
||||
|
||||
this.appendSasjsRequest(response.log, sasJob, null)
|
||||
})
|
||||
}
|
||||
)
|
||||
@@ -875,12 +912,24 @@ export default class SASjs {
|
||||
} else {
|
||||
this.retryCountJeseApi = 0
|
||||
reject(
|
||||
new ErrorResponse('Jes API retry requests limit reached')
|
||||
new ErrorResponse('Jes API retry requests limit reached.')
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
reject(new ErrorResponse('Job execution failed', e))
|
||||
if (e?.log) {
|
||||
this.appendSasjsRequest(e.log, sasJob, null)
|
||||
}
|
||||
|
||||
if (e.toString().includes('Job was not found')) {
|
||||
reject(
|
||||
new ErrorResponse('Service not found on the server.', {
|
||||
sasJob: sasJob
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
reject(new ErrorResponse('Job execution failed.', e))
|
||||
})
|
||||
)
|
||||
}
|
||||
@@ -1064,7 +1113,7 @@ export default class SASjs {
|
||||
} else {
|
||||
reject(
|
||||
new ErrorResponse(
|
||||
'Job WEB execution failed',
|
||||
'Job WEB execution failed.',
|
||||
this.parseSAS9ErrorResponse(responseText)
|
||||
)
|
||||
)
|
||||
@@ -1082,7 +1131,7 @@ export default class SASjs {
|
||||
} catch (e) {
|
||||
reject(
|
||||
new ErrorResponse(
|
||||
'Job WEB debug response parsing failed',
|
||||
'Job WEB debug response parsing failed.',
|
||||
{ response: resText, exception: e }
|
||||
)
|
||||
)
|
||||
@@ -1091,7 +1140,7 @@ export default class SASjs {
|
||||
(err: any) => {
|
||||
reject(
|
||||
new ErrorResponse(
|
||||
'Job WEB debug response parsing failed',
|
||||
'Job WEB debug response parsing failed.',
|
||||
err
|
||||
)
|
||||
)
|
||||
@@ -1100,19 +1149,34 @@ export default class SASjs {
|
||||
} catch (e) {
|
||||
reject(
|
||||
new ErrorResponse(
|
||||
'Job WEB debug response parsing failed',
|
||||
'Job WEB debug response parsing failed.',
|
||||
{ response: responseText, exception: e }
|
||||
)
|
||||
)
|
||||
}
|
||||
} else {
|
||||
this.updateUsername(responseText)
|
||||
if (
|
||||
responseText.includes(
|
||||
'The requested URL /SASStoredProcess/do/ was not found on this server.'
|
||||
) ||
|
||||
responseText.includes('Stored process not found')
|
||||
) {
|
||||
reject(
|
||||
new ErrorResponse(
|
||||
'Service not found on the server.',
|
||||
{ service: sasJob },
|
||||
responseText
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
try {
|
||||
const parsedJson = JSON.parse(responseText)
|
||||
resolve(parsedJson)
|
||||
} catch (e) {
|
||||
reject(
|
||||
new ErrorResponse('Job WEB response parsing failed', {
|
||||
new ErrorResponse('Job WEB response parsing failed.', {
|
||||
response: responseText,
|
||||
exception: e
|
||||
})
|
||||
@@ -1123,7 +1187,7 @@ export default class SASjs {
|
||||
}
|
||||
})
|
||||
.catch((e: Error) => {
|
||||
reject(new ErrorResponse('Job WEB request failed', e))
|
||||
reject(new ErrorResponse('Job WEB request failed.', e))
|
||||
})
|
||||
}
|
||||
)
|
||||
@@ -1264,10 +1328,20 @@ export default class SASjs {
|
||||
}
|
||||
}
|
||||
|
||||
private fetchLogFileContent(logLink: string) {
|
||||
/**
|
||||
* Fetches content of the log file
|
||||
* @param logLink - url of the log file.
|
||||
* @param accessToken - an access token for an authorized user.
|
||||
*/
|
||||
public fetchLogFileContent(logLink: string, accessToken?: string) {
|
||||
const headers: any = { 'Content-Type': 'application/json' }
|
||||
|
||||
if (accessToken) headers.Authorization = 'Bearer ' + accessToken
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
fetch(logLink, {
|
||||
method: 'GET'
|
||||
method: 'GET',
|
||||
headers
|
||||
})
|
||||
.then((response: any) => response.text())
|
||||
.then((response: any) => resolve(response))
|
||||
@@ -1365,11 +1439,15 @@ export default class SASjs {
|
||||
this.sasjsConfig.serverUrl === undefined ||
|
||||
this.sasjsConfig.serverUrl === ''
|
||||
) {
|
||||
let url = `${location.protocol}//${location.hostname}`
|
||||
if (location.port) {
|
||||
url = `${url}:${location.port}`
|
||||
if (typeof location !== 'undefined') {
|
||||
let url = `${location.protocol}//${location.hostname}`
|
||||
|
||||
if (location.port) url = `${url}:${location.port}`
|
||||
|
||||
this.sasjsConfig.serverUrl = url
|
||||
} else {
|
||||
this.sasjsConfig.serverUrl = ''
|
||||
}
|
||||
this.sasjsConfig.serverUrl = url
|
||||
}
|
||||
|
||||
if (this.sasjsConfig.serverUrl.slice(-1) === '/') {
|
||||
@@ -1434,26 +1512,6 @@ export default class SASjs {
|
||||
}
|
||||
}
|
||||
|
||||
private async getLoginForm() {
|
||||
const pattern: RegExp = /<form.+action="(.*Logon[^"]*).*>/
|
||||
const response = await fetch(this.loginUrl).then((r) => r.text())
|
||||
const matches = pattern.exec(response)
|
||||
const formInputs: any = {}
|
||||
if (matches && matches.length) {
|
||||
this.setLoginUrl(matches)
|
||||
const inputs = response.match(/<input.*"hidden"[^>]*>/g)
|
||||
if (inputs) {
|
||||
inputs.forEach((inputStr: string) => {
|
||||
const valueMatch = inputStr.match(/name="([^"]*)"\svalue="([^"]*)/)
|
||||
if (valueMatch && valueMatch.length) {
|
||||
formInputs[valueMatch[1]] = valueMatch[2]
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
return Object.keys(formInputs).length ? formInputs : null
|
||||
}
|
||||
|
||||
private async createFoldersAndServices(
|
||||
parentFolder: string,
|
||||
membersJson: any[],
|
||||
|
||||
137
src/test/FileUploader.spec.ts
Normal file
137
src/test/FileUploader.spec.ts
Normal file
@@ -0,0 +1,137 @@
|
||||
import { FileUploader } from '../FileUploader'
|
||||
import { UploadFile } from '../types'
|
||||
|
||||
const sampleResponse = `{
|
||||
"SYSUSERID": "cas",
|
||||
"_DEBUG":" ",
|
||||
"SYS_JES_JOB_URI": "/jobExecution/jobs/000-000-000-000",
|
||||
"_PROGRAM" : "/Public/app/editors/loadfile",
|
||||
"SYSCC" : "0",
|
||||
"SYSJOBID" : "117382",
|
||||
"SYSWARNINGTEXT" : ""
|
||||
}`
|
||||
|
||||
const prepareFilesAndParams = () => {
|
||||
const files: UploadFile[] = [
|
||||
{
|
||||
file: new File([''], 'testfile'),
|
||||
fileName: 'testfile'
|
||||
}
|
||||
]
|
||||
const params = { table: 'libtable' }
|
||||
|
||||
return { files, params }
|
||||
}
|
||||
|
||||
describe('FileUploader', () => {
|
||||
let originalFetch: any
|
||||
const fileUploader = new FileUploader(
|
||||
'/sample/apploc',
|
||||
'https://sample.server.com',
|
||||
'/jobs/path',
|
||||
null,
|
||||
null
|
||||
)
|
||||
|
||||
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()
|
||||
|
||||
fileUploader.uploadFile(sasJob, files, params).then((res: any) => {
|
||||
expect(JSON.stringify(res)).toEqual(
|
||||
JSON.stringify(JSON.parse(sampleResponse))
|
||||
)
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should an error when no files are provided', async (done) => {
|
||||
const sasJob = 'test/upload'
|
||||
const files: UploadFile[] = []
|
||||
const params = { table: 'libtable' }
|
||||
|
||||
fileUploader.uploadFile(sasJob, files, params).catch((err: any) => {
|
||||
expect(err.error.message).toEqual('At least one file must be provided.')
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should throw an error when no sasJob is provided', async (done) => {
|
||||
const sasJob = ''
|
||||
const { files, params } = prepareFilesAndParams()
|
||||
|
||||
fileUploader.uploadFile(sasJob, files, params).catch((err: any) => {
|
||||
expect(err.error.message).toEqual('sasJob must be provided.')
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
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">')
|
||||
})
|
||||
)
|
||||
|
||||
const sasJob = 'test'
|
||||
const { files, params } = prepareFilesAndParams()
|
||||
|
||||
fileUploader.uploadFile(sasJob, files, params).catch((err: any) => {
|
||||
expect(err.error.message).toEqual(
|
||||
'You must be logged in to upload a file.'
|
||||
)
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
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"')
|
||||
})
|
||||
)
|
||||
|
||||
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.'
|
||||
)
|
||||
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"}')
|
||||
})
|
||||
)
|
||||
|
||||
const sasJob = 'test'
|
||||
const { files, params } = prepareFilesAndParams()
|
||||
|
||||
fileUploader.uploadFile(sasJob, files, params).catch((err: any) => {
|
||||
expect(err.error.message).toEqual('Upload request failed.')
|
||||
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,4 +1,4 @@
|
||||
import { parseGeneratedCode } from './index'
|
||||
import { parseGeneratedCode } from '../../utils/index'
|
||||
|
||||
it('should parse generated code', async (done) => {
|
||||
expect(sampleResponse).toBeTruthy()
|
||||
@@ -1,4 +1,4 @@
|
||||
import { parseSourceCode } from './index'
|
||||
import { parseSourceCode } from '../../utils/index'
|
||||
|
||||
it('should parse SAS9 source code', async (done) => {
|
||||
expect(sampleResponse).toBeTruthy()
|
||||
@@ -1,17 +1,19 @@
|
||||
export class ErrorResponse {
|
||||
body: ErrorBody
|
||||
error: ErrorBody
|
||||
|
||||
constructor(message: string, details?: any) {
|
||||
let detailsString = ''
|
||||
let raw
|
||||
constructor(message: string, details?: any, raw?: any) {
|
||||
let detailsString = details
|
||||
|
||||
try {
|
||||
detailsString = JSON.stringify(details)
|
||||
} catch {
|
||||
raw = details
|
||||
if (typeof details !== 'object') {
|
||||
try {
|
||||
detailsString = JSON.parse(details)
|
||||
} catch {
|
||||
raw = details
|
||||
detailsString = ''
|
||||
}
|
||||
}
|
||||
|
||||
this.body = {
|
||||
this.error = {
|
||||
message,
|
||||
details: detailsString,
|
||||
raw
|
||||
|
||||
Reference in New Issue
Block a user