From 2849e6ed078f3454ae4e105c41bd484fe070bd88 Mon Sep 17 00:00:00 2001 From: Saad Jutt Date: Wed, 6 Oct 2021 14:06:00 +0500 Subject: [PATCH] test(RequestClient): updated --- src/auth/getAccessToken.ts | 2 +- src/request/RequestClient.ts | 2 +- src/test/RequestClient.spec.ts | 29 +++++++------ src/test/serverUtils.ts | 77 ++++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 14 deletions(-) create mode 100644 src/test/serverUtils.ts diff --git a/src/auth/getAccessToken.ts b/src/auth/getAccessToken.ts index d51833e..f42e442 100644 --- a/src/auth/getAccessToken.ts +++ b/src/auth/getAccessToken.ts @@ -46,7 +46,7 @@ export async function getAccessToken( ) .then((res) => res.result as SasAuthResponse) .catch((err) => { - throw prefixMessage(err, 'Error while getting access token') + throw prefixMessage(err, 'Error while getting access token. ') }) return authResponse diff --git a/src/request/RequestClient.ts b/src/request/RequestClient.ts index 409b6ba..c65cf0e 100644 --- a/src/request/RequestClient.ts +++ b/src/request/RequestClient.ts @@ -1,4 +1,4 @@ -import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios' +import { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios' import { CsrfToken } from '..' import { isAuthorizeFormRequired, isLogInRequired } from '../auth' import { diff --git a/src/test/RequestClient.spec.ts b/src/test/RequestClient.spec.ts index f947f1b..d74397a 100644 --- a/src/test/RequestClient.spec.ts +++ b/src/test/RequestClient.spec.ts @@ -5,6 +5,12 @@ import { app, mockedAuthResponse } from './SAS_server_app' import { ServerType } from '@sasjs/utils' import SASjs from '../SASjs' import * as axiosModules from '../utils/createAxiosInstance' +import { + clientCert, + createCertificates, + rootCaCert, + serverCert +} from './serverUtils' const axiosActual = jest.requireActual('axios') @@ -17,6 +23,11 @@ jest const PORT = 8000 const SERVER_URL = `https://localhost:${PORT}/` +const ERROR_MESSAGES = { + selfSigned: 'self signed certificate', + CCA: 'unable to verify the first certificate' +} + describe('RequestClient', () => { let server: http.Server @@ -50,10 +61,8 @@ describe('RequestClient', () => { it('should response the POST method with Unauthorized', async () => { await expect( adapter.getAccessToken('clientId', 'clientSecret', 'incorrect') - ).rejects.toEqual( - new Error( - 'Error while getting access tokenRequest failed with status code 401' - ) + ).rejects.toThrow( + 'Error while getting access token. Request failed with status code 401' ) }) }) @@ -97,10 +106,8 @@ describe('RequestClient - Self Signed Server', () => { 'clientSecret', 'authCode' ) - ).rejects.toEqual( - expect.objectContaining({ - message: 'Error while getting access tokenself signed certificate' - }) + ).rejects.toThrow( + `Error while getting access token. ${ERROR_MESSAGES.selfSigned}` ) }) @@ -135,10 +142,8 @@ describe('RequestClient - Self Signed Server', () => { it('should response the POST method with Unauthorized', async () => { await expect( adapter.getAccessToken('clientId', 'clientSecret', 'incorrect') - ).rejects.toEqual( - new Error( - 'Error while getting access tokenRequest failed with status code 401' - ) + ).rejects.toThrow( + 'Error while getting access token. Request failed with status code 401' ) }) }) diff --git a/src/test/serverUtils.ts b/src/test/serverUtils.ts new file mode 100644 index 0000000..91fe35e --- /dev/null +++ b/src/test/serverUtils.ts @@ -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() +}