1- import * as should from 'should' ;
1+ import should from 'should' ;
22import 'should-http' ;
33import 'should-sinon' ;
44import '../lib/asserts' ;
55
6- import * as nock from 'nock' ;
76import * as sinon from 'sinon' ;
7+ import * as nock from 'nock' ;
8+ import proxyquire from 'proxyquire' ;
89
9- import * as fs from 'fs' ;
10- import * as http from 'http' ;
11- import * as https from 'https' ;
12- import * as debugLib from 'debug' ;
13- import * as path from 'path' ;
10+ import debugLib from 'debug' ;
1411import { Environments } from 'bitgo' ;
1512
1613import { SSL_OP_NO_TLSv1 } from 'constants' ;
1714import { TlsConfigurationError , NodeEnvironmentError } from '../../src/errors' ;
1815
19- nock . disableNetConnect ( ) ;
20-
2116import { app as expressApp , startup , createServer , createBaseUri , prepareIpc } from '../../src/expressApp' ;
22- import * as clientRoutes from '../../src/clientRoutes' ;
17+
18+ // commonJS imports to allow stubbing
19+ const path = require ( 'path' ) ;
20+ const http = require ( 'http' ) ;
21+ const https = require ( 'https' ) ;
22+ const fs = require ( 'fs' ) ;
23+
24+ nock . disableNetConnect ( ) ;
25+ proxyquire . noPreserveCache ( ) ;
2326
2427describe ( 'Bitgo Express' , function ( ) {
2528 describe ( 'server initialization' , function ( ) {
@@ -371,7 +374,7 @@ describe('Bitgo Express', function () {
371374 } ) ;
372375
373376 it ( 'should remove the socket before binding if IPC socket exists and is a socket' , async ( ) => {
374- const statStub = sinon . stub ( fs , 'statSync' ) . returns ( { isSocket : ( ) => true } as unknown as fs . Stats ) ;
377+ const statStub = sinon . stub ( fs , 'statSync' ) . returns ( { isSocket : ( ) => true } ) ;
375378 const unlinkStub = sinon . stub ( fs , 'unlinkSync' ) ;
376379 await prepareIpc ( 'testipc' ) . should . be . resolved ( ) ;
377380 unlinkStub . calledWithExactly ( 'testipc' ) . should . be . true ( ) ;
@@ -381,7 +384,7 @@ describe('Bitgo Express', function () {
381384 } ) ;
382385
383386 it ( 'should fail if IPC socket is not actually a socket' , async ( ) => {
384- const statStub = sinon . stub ( fs , 'statSync' ) . returns ( { isSocket : ( ) => false } as unknown as fs . Stats ) ;
387+ const statStub = sinon . stub ( fs , 'statSync' ) . returns ( { isSocket : ( ) => false } ) ;
385388 const unlinkStub = sinon . stub ( fs , 'unlinkSync' ) ;
386389 await prepareIpc ( 'testipc' ) . should . be . rejectedWith ( / I P C s o c k e t i s n o t a c t u a l l y a s o c k e t / ) ;
387390 unlinkStub . notCalled . should . be . true ( ) ;
@@ -404,58 +407,81 @@ describe('Bitgo Express', function () {
404407 } ) ;
405408
406409 it ( 'should only call setupAPIRoutes when running in regular mode' , ( ) => {
410+ const setupAPIRoutesStub = sinon . stub ( ) ;
411+ const setupSigningRoutesStub = sinon . stub ( ) ;
412+
413+ const { app } = proxyquire ( '../../src/expressApp' , {
414+ './clientRoutes' : {
415+ setupAPIRoutes : setupAPIRoutesStub ,
416+ setupSigningRoutes : setupSigningRoutesStub ,
417+ setupLightningSignerNodeRoutes : sinon . stub ( ) ,
418+ } ,
419+ } ) ;
420+
407421 const args : any = {
408422 env : 'test' ,
409423 signerMode : undefined ,
410424 } ;
411425
412- const apiStub = sinon . stub ( clientRoutes , 'setupAPIRoutes' ) ;
413- const signerStub = sinon . stub ( clientRoutes , 'setupSigningRoutes' ) ;
414-
415- expressApp ( args ) ;
416- apiStub . should . have . been . calledOnce ( ) ;
417- signerStub . called . should . be . false ( ) ;
418- apiStub . restore ( ) ;
419- signerStub . restore ( ) ;
426+ app ( args ) ;
427+ setupAPIRoutesStub . should . have . been . calledOnce ( ) ;
428+ setupSigningRoutesStub . called . should . be . false ( ) ;
420429 } ) ;
421430
422431 it ( 'should only call setupLightningSignerNodeRoutes when running with lightningSignerFileSystemPath' , ( ) => {
432+ const setupAPIRoutesStub = sinon . stub ( ) ;
433+ const setupSigningRoutesStub = sinon . stub ( ) ;
434+ const setupLightningSignerNodeRoutesStub = sinon . stub ( ) ;
435+ const { app } = proxyquire ( '../../src/expressApp' , {
436+ './clientRoutes' : {
437+ setupAPIRoutes : setupAPIRoutesStub ,
438+ setupSigningRoutes : setupSigningRoutesStub ,
439+ setupLightningSignerNodeRoutes : setupLightningSignerNodeRoutesStub ,
440+ } ,
441+ fs : {
442+ readFileSync : ( ) => {
443+ return validLightningSignerConfigJSON ;
444+ } ,
445+ } ,
446+ } ) ;
447+
423448 const args : any = {
424449 env : 'test' ,
425450 lightningSignerFileSystemPath : 'lightningSignerFileSystemPath' ,
426451 } ;
427452
428- const readValidStub = sinon . stub ( fs , 'readFileSync' ) . returns ( validLightningSignerConfigJSON ) ;
429- const lightningSignerStub = sinon . stub ( clientRoutes , 'setupLightningSignerNodeRoutes' ) ;
430- const apiStub = sinon . stub ( clientRoutes , 'setupAPIRoutes' ) ;
431- const signerStub = sinon . stub ( clientRoutes , 'setupSigningRoutes' ) ;
432-
433- expressApp ( args ) ;
434- lightningSignerStub . should . have . been . calledOnce ( ) ;
435- apiStub . should . have . been . calledOnce ( ) ;
436- signerStub . called . should . be . false ( ) ;
437- apiStub . restore ( ) ;
438- signerStub . restore ( ) ;
439- readValidStub . restore ( ) ;
453+ app ( args ) ;
454+ setupLightningSignerNodeRoutesStub . should . have . been . calledOnce ( ) ;
455+ setupAPIRoutesStub . should . have . been . calledOnce ( ) ;
456+ setupSigningRoutesStub . called . should . be . false ( ) ;
440457 } ) ;
441458
442459 it ( 'should only call setupSigningRoutes when running in signer mode' , ( ) => {
460+ const setupAPIRoutesStub = sinon . stub ( ) ;
461+ const setupSigningRoutesStub = sinon . stub ( ) ;
462+
463+ const { app } = proxyquire ( '../../src/expressApp' , {
464+ './clientRoutes' : {
465+ setupAPIRoutes : setupAPIRoutesStub ,
466+ setupSigningRoutes : setupSigningRoutesStub ,
467+ setupLightningSignerNodeRoutes : sinon . stub ( ) ,
468+ } ,
469+ fs : {
470+ readFileSync : ( ) => {
471+ return validPrvJSON ;
472+ } ,
473+ } ,
474+ } ) ;
475+
443476 const args : any = {
444477 env : 'test' ,
445478 signerMode : 'signerMode' ,
446479 signerFileSystemPath : 'signerFileSystemPath' ,
447480 } ;
448481
449- const apiStub = sinon . stub ( clientRoutes , 'setupAPIRoutes' ) ;
450- const signerStub = sinon . stub ( clientRoutes , 'setupSigningRoutes' ) ;
451- const readFileStub = sinon . stub ( fs , 'readFileSync' ) . returns ( validPrvJSON ) ;
452-
453- expressApp ( args ) ;
454- signerStub . should . have . been . calledOnce ( ) ;
455- apiStub . called . should . be . false ( ) ;
456- apiStub . restore ( ) ;
457- signerStub . restore ( ) ;
458- readFileStub . restore ( ) ;
482+ app ( args ) ;
483+ setupSigningRoutesStub . should . have . been . calledOnce ( ) ;
484+ setupAPIRoutesStub . called . should . be . false ( ) ;
459485 } ) ;
460486
461487 it ( 'should require a signerFileSystemPath and signerMode are both set when running in signer mode' , function ( ) {
@@ -555,7 +581,7 @@ describe('Bitgo Express', function () {
555581
556582 it ( 'should set keepAliveTimeout and headersTimeout if specified in config for HTTP server' , async function ( ) {
557583 const createServerStub = sinon . stub ( http , 'createServer' ) . callsFake ( ( ) => {
558- return { listen : sinon . stub ( ) , setTimeout : sinon . stub ( ) } as unknown as http . Server ;
584+ return { listen : sinon . stub ( ) , setTimeout : sinon . stub ( ) } ;
559585 } ) ;
560586
561587 const args : any = {
@@ -575,7 +601,7 @@ describe('Bitgo Express', function () {
575601
576602 it ( 'should set keepAliveTimeout and headersTimeout if specified in config for HTTPS server' , async function ( ) {
577603 const createServerStub = sinon . stub ( https , 'createServer' ) . callsFake ( ( ) => {
578- return { listen : sinon . stub ( ) , setTimeout : sinon . stub ( ) } as unknown as https . Server ;
604+ return { listen : sinon . stub ( ) , setTimeout : sinon . stub ( ) } ;
579605 } ) ;
580606
581607 const args : any = {
@@ -597,7 +623,7 @@ describe('Bitgo Express', function () {
597623
598624 it ( 'should not set keepAliveTimeout and headersTimeout if not specified in config for HTTP server' , async function ( ) {
599625 const createServerStub = sinon . stub ( http , 'createServer' ) . callsFake ( ( ) => {
600- return { listen : sinon . stub ( ) , setTimeout : sinon . stub ( ) } as unknown as http . Server ;
626+ return { listen : sinon . stub ( ) , setTimeout : sinon . stub ( ) } ;
601627 } ) ;
602628
603629 const args : any = {
@@ -616,7 +642,7 @@ describe('Bitgo Express', function () {
616642
617643 it ( 'should not set keepAliveTimeout and headersTimeout if not specified in config for HTTPS server' , async function ( ) {
618644 const createServerStub = sinon . stub ( https , 'createServer' ) . callsFake ( ( ) => {
619- return { listen : sinon . stub ( ) , setTimeout : sinon . stub ( ) } as unknown as https . Server ;
645+ return { listen : sinon . stub ( ) , setTimeout : sinon . stub ( ) } ;
620646 } ) ;
621647
622648 const args : any = {
0 commit comments