1+ import { join } from 'path' ;
2+ import { promises as fs } from 'fs' ;
3+ import moment from 'moment' ;
4+ import * as chai from 'chai' ;
5+ import { expect } from 'chai' ;
6+ import { KEYUTIL , KJUR } from 'jsrsasign' ;
7+ import { stub } from 'sinon' ;
8+ import sinonChai from 'sinon-chai' ;
9+ chai . use ( sinonChai ) ;
10+
11+ // Set sandbox dir as config before initializing utils
12+ const sandboxDir = join ( __dirname , 'resources' , 'sandbox' ) ;
13+ process . env . DATA_DIRECTORY = sandboxDir ;
14+
15+ import * as app from '../src/app' ;
16+
17+ describe ( 'app' , ( ) => {
18+
19+ beforeEach ( async ( ) => {
20+ if ( ( await fs . stat ( sandboxDir ) ) . isDirectory ( ) ) {
21+ await fs . rm ( sandboxDir , { recursive : true , force : true } )
22+ }
23+ await fs . mkdir ( sandboxDir )
24+ await fs . mkdir ( join ( sandboxDir , 'peer-certs' ) )
25+
26+ const start = moment ( ) . utc ( ) . subtract ( 1 , 'minute' ) ;
27+ const end = moment ( start ) . add ( 12 , 'months' ) ;
28+ const startUTC = start . format ( 'YYYYMMDDHHmmss\\Z' ) ;
29+ const endUTC = end . format ( 'YYYYMMDDHHmmss\\Z' ) ;
30+ const keypair = KEYUTIL . generateKeypair ( 'EC' , 'secp256r1' ) ;
31+ const keyPEM = KEYUTIL . getPEM ( keypair . prvKeyObj , "PKCS8PRV" )
32+ const certPEM = KJUR . asn1 . x509 . X509Util . newCertPEM ( {
33+ serial : { int : 4 } ,
34+ sigalg : { name : "SHA256withECDSA" } ,
35+ issuer : { str : "/C=US/O=a" } ,
36+ notbefore : { str : startUTC } ,
37+ notafter : { str : endUTC } ,
38+ subject : { str : "/C=US/O=b" } ,
39+ sbjpubkey : keypair . pubKeyObj ,
40+ sbjprvkey : keypair . prvKeyObj ,
41+ cakey : keypair . prvKeyObj ,
42+ } as any ) ;
43+ await fs . writeFile ( join ( sandboxDir , 'key.pem' ) , keyPEM ) ;
44+ await fs . writeFile ( join ( sandboxDir , 'cert.pem' ) , certPEM ) ;
45+
46+ await fs . writeFile ( join ( sandboxDir , 'config.json' ) , JSON . stringify ( {
47+ api : {
48+ hostname : "localhost" ,
49+ port : 10101 ,
50+ } ,
51+ p2p : {
52+ hostname : "localhost" ,
53+ port : 10102 ,
54+ }
55+ } , null , 2 ) ) ;
56+
57+ stub ( process , 'exit' ) ;
58+ } ) ;
59+
60+ afterEach ( ( ) => {
61+ ( process . exit as any ) . restore ( ) ;
62+ } )
63+
64+ it ( 'starts and stops' , async ( ) => {
65+
66+ await app . start ( ) ;
67+ await app . stop ( ) ;
68+
69+ expect ( process . exit ) . to . be . called ;
70+
71+ } )
72+
73+ } )
0 commit comments