mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-07 12:30:06 +00:00
test(RequestClient): updated
This commit is contained in:
@@ -46,7 +46,7 @@ export async function getAccessToken(
|
|||||||
)
|
)
|
||||||
.then((res) => res.result as SasAuthResponse)
|
.then((res) => res.result as SasAuthResponse)
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
throw prefixMessage(err, 'Error while getting access token')
|
throw prefixMessage(err, 'Error while getting access token. ')
|
||||||
})
|
})
|
||||||
|
|
||||||
return authResponse
|
return authResponse
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'
|
import { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'
|
||||||
import { CsrfToken } from '..'
|
import { CsrfToken } from '..'
|
||||||
import { isAuthorizeFormRequired, isLogInRequired } from '../auth'
|
import { isAuthorizeFormRequired, isLogInRequired } from '../auth'
|
||||||
import {
|
import {
|
||||||
|
|||||||
@@ -5,6 +5,12 @@ import { app, mockedAuthResponse } from './SAS_server_app'
|
|||||||
import { ServerType } from '@sasjs/utils'
|
import { ServerType } from '@sasjs/utils'
|
||||||
import SASjs from '../SASjs'
|
import SASjs from '../SASjs'
|
||||||
import * as axiosModules from '../utils/createAxiosInstance'
|
import * as axiosModules from '../utils/createAxiosInstance'
|
||||||
|
import {
|
||||||
|
clientCert,
|
||||||
|
createCertificates,
|
||||||
|
rootCaCert,
|
||||||
|
serverCert
|
||||||
|
} from './serverUtils'
|
||||||
|
|
||||||
const axiosActual = jest.requireActual('axios')
|
const axiosActual = jest.requireActual('axios')
|
||||||
|
|
||||||
@@ -17,6 +23,11 @@ jest
|
|||||||
const PORT = 8000
|
const PORT = 8000
|
||||||
const SERVER_URL = `https://localhost:${PORT}/`
|
const SERVER_URL = `https://localhost:${PORT}/`
|
||||||
|
|
||||||
|
const ERROR_MESSAGES = {
|
||||||
|
selfSigned: 'self signed certificate',
|
||||||
|
CCA: 'unable to verify the first certificate'
|
||||||
|
}
|
||||||
|
|
||||||
describe('RequestClient', () => {
|
describe('RequestClient', () => {
|
||||||
let server: http.Server
|
let server: http.Server
|
||||||
|
|
||||||
@@ -50,10 +61,8 @@ describe('RequestClient', () => {
|
|||||||
it('should response the POST method with Unauthorized', async () => {
|
it('should response the POST method with Unauthorized', async () => {
|
||||||
await expect(
|
await expect(
|
||||||
adapter.getAccessToken('clientId', 'clientSecret', 'incorrect')
|
adapter.getAccessToken('clientId', 'clientSecret', 'incorrect')
|
||||||
).rejects.toEqual(
|
).rejects.toThrow(
|
||||||
new Error(
|
'Error while getting access token. Request failed with status code 401'
|
||||||
'Error while getting access tokenRequest failed with status code 401'
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -97,10 +106,8 @@ describe('RequestClient - Self Signed Server', () => {
|
|||||||
'clientSecret',
|
'clientSecret',
|
||||||
'authCode'
|
'authCode'
|
||||||
)
|
)
|
||||||
).rejects.toEqual(
|
).rejects.toThrow(
|
||||||
expect.objectContaining({
|
`Error while getting access token. ${ERROR_MESSAGES.selfSigned}`
|
||||||
message: 'Error while getting access tokenself signed certificate'
|
|
||||||
})
|
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -135,10 +142,8 @@ describe('RequestClient - Self Signed Server', () => {
|
|||||||
it('should response the POST method with Unauthorized', async () => {
|
it('should response the POST method with Unauthorized', async () => {
|
||||||
await expect(
|
await expect(
|
||||||
adapter.getAccessToken('clientId', 'clientSecret', 'incorrect')
|
adapter.getAccessToken('clientId', 'clientSecret', 'incorrect')
|
||||||
).rejects.toEqual(
|
).rejects.toThrow(
|
||||||
new Error(
|
'Error while getting access token. Request failed with status code 401'
|
||||||
'Error while getting access tokenRequest failed with status code 401'
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
77
src/test/serverUtils.ts
Normal file
77
src/test/serverUtils.ts
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
var https = require('https')
|
||||||
|
var { Cert } = require('selfsigned-ca')
|
||||||
|
|
||||||
|
// Root CA certificate used to sign other certificates.
|
||||||
|
// argument(s) point to .crt and .key file paths - ./selfsigned.root-ca.crt & ./selfsigned.root-ca.key
|
||||||
|
export const rootCaCert = new Cert('selfsigned.root-ca')
|
||||||
|
// The certificate generated for use in the HTTP server. It is signed by the CA certificate.
|
||||||
|
// That way you can create any amount of certificates and they will be all trusted as long
|
||||||
|
// as the Root CA certificate is trusted (installed to device's keychain).
|
||||||
|
// argument(s) point to .crt and .key file paths - ./selfsigned.localhost.crt & ./selfsigned.localhost.key
|
||||||
|
export const serverCert = new Cert(`selfsigned.localhost`)
|
||||||
|
export const clientCert = new Cert(`selfsigned.client`)
|
||||||
|
|
||||||
|
// .then(startHttpsServer)
|
||||||
|
// .then(() => console.log('certificates ready, server listening'))
|
||||||
|
// .catch(console.error)
|
||||||
|
|
||||||
|
export async function createCertificates() {
|
||||||
|
// await createRootCertificate()
|
||||||
|
console.log('creating server certificate')
|
||||||
|
createServerCertificate()
|
||||||
|
console.log('server certificate created & stored')
|
||||||
|
}
|
||||||
|
|
||||||
|
function startHttpsServer() {
|
||||||
|
var server = https.createServer(serverCert, (req: any, res: any) => {
|
||||||
|
res.writeHead(200)
|
||||||
|
res.end('hello world\n')
|
||||||
|
})
|
||||||
|
server.listen(443)
|
||||||
|
}
|
||||||
|
|
||||||
|
async function loadRootCertificate() {
|
||||||
|
await rootCaCert.load()
|
||||||
|
if (!(await rootCaCert.isInstalled())) {
|
||||||
|
// Make sure the CA is installed to device's keychain so that all server certificates
|
||||||
|
// signed by the CA are automatically trusted and green.
|
||||||
|
await rootCaCert.install()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function createRootCertificate() {
|
||||||
|
console.log('createRootCertificate')
|
||||||
|
// Couldn't load existing root CA certificate. Generate new one.
|
||||||
|
rootCaCert.createRootCa({
|
||||||
|
subject: {
|
||||||
|
commonName: 'My Trusted Certificate Authority'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
console.log('rootCaCert', rootCaCert)
|
||||||
|
// console.log('createRootCertificate saving')
|
||||||
|
// await rootCaCert.save()
|
||||||
|
// console.log('createRootCertificate saved')
|
||||||
|
// Install the newly created CA to device's keychain so that all server certificates
|
||||||
|
// signed by the CA are automatically trusted and green.
|
||||||
|
// await rootCaCert.install()
|
||||||
|
// console.log('createRootCertificate installed')
|
||||||
|
}
|
||||||
|
|
||||||
|
async function createServerCertificate() {
|
||||||
|
var serverCertOptions = {
|
||||||
|
subject: {
|
||||||
|
commonName: 'localhost'
|
||||||
|
},
|
||||||
|
extensions: [
|
||||||
|
{
|
||||||
|
name: 'subjectAltName',
|
||||||
|
altNames: [
|
||||||
|
{ type: 2, value: 'localhost' }, // DNS
|
||||||
|
{ type: 7, ip: '127.0.0.1' } // IP
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
serverCert.create(serverCertOptions, rootCaCert)
|
||||||
|
await serverCert.save()
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user