@@ -266,52 +266,55 @@ blockchainExplorerMock.reset = function() {
266266} ;
267267
268268
269+ helpers . newDb = ( extra , cb ) => {
270+ extra = extra || '' ;
271+ mongodb . MongoClient . connect ( config . mongoDb . uri + extra , function ( err , in_db ) {
272+ if ( err ) return done ( err ) ;
273+ in_db . dropDatabase ( function ( err ) {
274+ return cb ( err , in_db ) ;
275+ } ) ;
276+ } ) ;
277+ }
269278
270279var db ;
271280describe ( 'client API' , function ( ) {
272281 var clients , app , sandbox ;
273282 var i = 0 ;
274283
275284 before ( ( done ) => {
276- mongodb . MongoClient . connect ( config . mongoDb . uri , function ( err , in_db ) {
277- if ( err ) return done ( err ) ;
278- db = in_db ;
279- console . log ( "db object points to the database : " + db . databaseName ) ;
280-
281- return done ( ) ;
285+ helpers . newDb ( '' , ( err , in_db ) => {
286+ db = in_db ;
287+ return done ( err ) ;
282288 } ) ;
283289 } ) ;
284290
285291 beforeEach ( function ( done ) {
286- db . dropDatabase ( function ( err ) {
287- if ( err ) return done ( err ) ;
288- var storage = new Storage ( {
289- db : db ,
290- } ) ;
291- var expressApp = new ExpressApp ( ) ;
292- expressApp . start ( {
293- ignoreRateLimiter : true ,
294- storage : storage ,
295- blockchainExplorer : blockchainExplorerMock ,
296- disableLogs : true ,
297- } ,
298- function ( ) {
299- app = expressApp . app ;
300-
301- // Generates 5 clients
302- clients = _ . map ( _ . range ( 5 ) , function ( i ) {
303- return helpers . newClient ( app ) ;
304- } ) ;
305- blockchainExplorerMock . reset ( ) ;
306- sandbox = sinon . createSandbox ( ) ;
292+ var storage = new Storage ( {
293+ db : db ,
294+ } ) ;
295+ var expressApp = new ExpressApp ( ) ;
296+ expressApp . start ( {
297+ ignoreRateLimiter : true ,
298+ storage : storage ,
299+ blockchainExplorer : blockchainExplorerMock ,
300+ disableLogs : true ,
301+ } ,
302+ function ( ) {
303+ app = expressApp . app ;
307304
308- if ( ! process . env . BWC_SHOW_LOGS ) {
309- sandbox . stub ( log , 'warn' ) ;
310- sandbox . stub ( log , 'info' ) ;
311- sandbox . stub ( log , 'error' ) ;
312- }
313- done ( ) ;
305+ // Generates 5 clients
306+ clients = _ . map ( _ . range ( 5 ) , function ( i ) {
307+ return helpers . newClient ( app ) ;
314308 } ) ;
309+ blockchainExplorerMock . reset ( ) ;
310+ sandbox = sinon . createSandbox ( ) ;
311+
312+ if ( ! process . env . BWC_SHOW_LOGS ) {
313+ sandbox . stub ( log , 'warn' ) ;
314+ sandbox . stub ( log , 'info' ) ;
315+ sandbox . stub ( log , 'error' ) ;
316+ }
317+ done ( ) ;
315318 } ) ;
316319 } ) ;
317320 afterEach ( function ( done ) {
@@ -1096,6 +1099,13 @@ describe('client API', function() {
10961099 } ) ;
10971100
10981101 describe ( 'Wallet Creation' , function ( ) {
1102+
1103+ beforeEach ( ( done ) => {
1104+ db . dropDatabase ( function ( err ) {
1105+ return done ( err ) ;
1106+ } ) ;
1107+ } ) ;
1108+
10991109 it ( 'should fail to create wallet in bogus device' , function ( done ) {
11001110 clients [ 0 ] . seedFromRandomWithMnemonic ( ) ;
11011111 clients [ 0 ] . keyDerivationOk = false ;
@@ -1208,7 +1218,6 @@ describe('client API', function() {
12081218 should . not . exist ( err ) ;
12091219
12101220 clients [ 0 ] . createAddress ( function ( err , x ) {
1211- console . log ( '[client.js.1210:err:]' , err ) ; //TODO
12121221 should . not . exist ( err ) ;
12131222 should . not . exist ( err ) ;
12141223 x . coin . should . equal ( 'bch' ) ;
@@ -1558,6 +1567,7 @@ console.log('[client.js.1210:err:]',err); //TODO
15581567 } ) ;
15591568
15601569 it ( 'should create a 2-3 wallet with given mnemonic' , function ( done ) {
1570+
15611571 var words = 'forget announce travel fury farm alpha chaos choice talent sting eagle supreme' ;
15621572 clients [ 0 ] . seedFromMnemonic ( words ) ;
15631573 clients [ 0 ] . createWallet ( 'mywallet' , 'creator' , 2 , 3 , {
@@ -2072,14 +2082,17 @@ console.log('[client.js.1210:err:]',err); //TODO
20722082 describe ( 'Transaction Proposals Creation and Locked funds' , function ( ) {
20732083 var myAddress ;
20742084 beforeEach ( function ( done ) {
2075- helpers . createAndJoinWallet ( clients , 2 , 3 , { } , function ( w ) {
2076- clients [ 0 ] . createAddress ( function ( err , address ) {
2077- should . not . exist ( err ) ;
2078- myAddress = address ;
2079- blockchainExplorerMock . setUtxo ( address , 2 , 2 ) ;
2080- blockchainExplorerMock . setUtxo ( address , 2 , 2 ) ;
2081- blockchainExplorerMock . setUtxo ( address , 1 , 2 , 0 ) ;
2082- done ( ) ;
2085+ db . dropDatabase ( function ( err ) {
2086+ helpers . createAndJoinWallet ( clients , 2 , 3 , { } , function ( w ) {
2087+
2088+ clients [ 0 ] . createAddress ( function ( err , address ) {
2089+ should . not . exist ( err ) ;
2090+ myAddress = address ;
2091+ blockchainExplorerMock . setUtxo ( address , 2 , 2 ) ;
2092+ blockchainExplorerMock . setUtxo ( address , 2 , 2 ) ;
2093+ blockchainExplorerMock . setUtxo ( address , 1 , 2 , 0 ) ;
2094+ done ( err ) ;
2095+ } ) ;
20832096 } ) ;
20842097 } ) ;
20852098 } ) ;
@@ -2223,7 +2236,7 @@ console.log('[client.js.1210:err:]',err); //TODO
22232236 toAddress : 'n2TBMPzPECGUfcT2EByiTJ12TPZkhN2mN5' ,
22242237 } ] ,
22252238 feePerKb : 123e2 ,
2226- changeAddress : myAddress ,
2239+ changeAddress : myAddress . address ,
22272240 message : 'hello' ,
22282241 } ;
22292242
@@ -2270,7 +2283,7 @@ console.log('[client.js.1210:err:]',err); //TODO
22702283 async . each ( tamperings , function ( tamperFn , next ) {
22712284 helpers . tamperResponse ( clients [ 0 ] , 'post' , '/v2/txproposals/' , args , tamperFn , function ( ) {
22722285 clients [ 0 ] . createTxProposal ( opts , function ( err , txp ) {
2273- should . exist ( err , tamperFn ) ;
2286+ should . exist ( err , 'For tamper function ' + tamperFn ) ;
22742287 err . should . be . an . instanceOf ( Errors . SERVER_COMPROMISED ) ;
22752288 next ( ) ;
22762289 } ) ;
@@ -2763,6 +2776,11 @@ console.log('[client.js.1210:err:]',err); //TODO
27632776
27642777 describe ( 'Payment Protocol' , function ( ) {
27652778 var http ;
2779+ beforeEach ( ( done ) => {
2780+ db . dropDatabase ( function ( err ) {
2781+ done ( ) ;
2782+ } ) ;
2783+ } ) ;
27662784
27672785 describe ( 'Shared wallet' , function ( ) {
27682786 beforeEach ( function ( done ) {
@@ -3184,7 +3202,8 @@ console.log('[client.js.1210:err:]',err); //TODO
31843202 var s = refund_to . get ( 'script' ) ;
31853203 s = new Bitcore_ [ 'bch' ] . Script ( s . buffer . slice ( s . offset , s . limit ) ) ;
31863204 var addr = new Bitcore_ [ 'bch' ] . Address . fromScript ( s ) ;
3187- addr . toString ( ) . should . equal ( changeAddress ) ;
3205+ var addrStr = addr . toLegacyAddress ( ) ;
3206+ addrStr . should . equal ( changeAddress ) ;
31883207 done ( ) ;
31893208 } ) ;
31903209 } ) ;
@@ -4274,6 +4293,16 @@ console.log('[client.js.1210:err:]',err); //TODO
42744293 } ) ;
42754294
42764295 describe ( 'Recovery' , function ( ) {
4296+ var db2 ;
4297+ before ( ( done ) => {
4298+ helpers . newDb ( 2 , ( err , in_db ) => {
4299+ db2 = in_db ;
4300+ return done ( err ) ;
4301+ } ) ;
4302+ } ) ;
4303+
4304+
4305+
42774306 it ( 'should be able to gain access to a 1-1 wallet with just the xPriv' , function ( done ) {
42784307 helpers . createAndJoinWallet ( clients , 1 , 1 , function ( ) {
42794308 var xpriv = clients [ 0 ] . credentials . xPrivKey ;
@@ -4339,7 +4368,7 @@ console.log('[client.js.1210:err:]',err); //TODO
43394368 should . exist ( addr ) ;
43404369
43414370 var storage = new Storage ( {
4342- db : helpers . newDb ( ) ,
4371+ db : db2 ,
43434372 } ) ;
43444373
43454374 var newApp ;
@@ -4407,7 +4436,7 @@ console.log('[client.js.1210:err:]',err); //TODO
44074436 blockchainExplorerMock . setUtxo ( addr , 1 , 2 ) ;
44084437
44094438 var storage = new Storage ( {
4410- db : helpers . newDb ( ) ,
4439+ db : db2 ,
44114440 } ) ;
44124441 var newApp ;
44134442 var expressApp = new ExpressApp ( ) ;
@@ -4462,7 +4491,7 @@ console.log('[client.js.1210:err:]',err); //TODO
44624491 should . exist ( addr ) ;
44634492
44644493 var storage = new Storage ( {
4465- db : helpers . newDb ( ) ,
4494+ db : db2 ,
44664495 } ) ;
44674496 var newApp ;
44684497 var expressApp = new ExpressApp ( ) ;
@@ -4525,7 +4554,7 @@ console.log('[client.js.1210:err:]',err); //TODO
45254554 should . exist ( addr ) ;
45264555
45274556 var storage = new Storage ( {
4528- db : helpers . newDb ( ) ,
4557+ db : db2 ,
45294558 } ) ;
45304559
45314560 var newApp ;
@@ -4857,6 +4886,14 @@ console.log('[client.js.1210:err:]',err); //TODO
48574886 } ) ;
48584887
48594888 describe ( 'Legacy Copay Import' , function ( ) {
4889+ var db2 ;
4890+ before ( ( done ) => {
4891+ helpers . newDb ( 2 , ( err , in_db ) => {
4892+ db2 = in_db ;
4893+ return done ( err ) ;
4894+ } ) ;
4895+ } ) ;
4896+
48604897 it ( 'Should get wallets from profile' , function ( done ) {
48614898 var t = ImportData . copayers [ 0 ] ;
48624899 var c = helpers . newClient ( app ) ;
@@ -5024,7 +5061,7 @@ console.log('[client.js.1210:err:]',err); //TODO
50245061
50255062 // New BWS server...
50265063 var storage = new Storage ( {
5027- db : helpers . newDb ( ) ,
5064+ db : db2 ,
50285065 } ) ;
50295066 var newApp ;
50305067 var expressApp = new ExpressApp ( ) ;
@@ -5472,7 +5509,7 @@ console.log('[client.js.1210:err:]',err); //TODO
54725509 it ( 'should handle tx serialization error when building tx' , function ( done ) {
54735510 var sandbox = sinon . sandbox . create ( ) ;
54745511
5475- var se = sandbox . stub ( B . Transaction . prototype , 'serialize' , function ( ) {
5512+ var se = sandbox . stub ( B . Transaction . prototype , 'serialize' ) . callsFake ( function ( ) {
54765513 throw new Error ( 'this is an error' ) ;
54775514 } ) ;
54785515
@@ -5555,7 +5592,7 @@ console.log('[client.js.1210:err:]',err); //TODO
55555592
55565593 var client = new Client ( ) ;
55575594
5558- var _f = sandbox . stub ( client , '_fetchLatestNotifications' , function ( interval , cb ) {
5595+ var _f = sandbox . stub ( client , '_fetchLatestNotifications' ) . callsFake ( function ( interval , cb ) {
55595596 cb ( new Errors . NOT_FOUND ) ;
55605597 } ) ;
55615598
@@ -5575,7 +5612,7 @@ console.log('[client.js.1210:err:]',err); //TODO
55755612
55765613 var client = new Client ( ) ;
55775614
5578- var _f = sandbox . stub ( client , '_fetchLatestNotifications' , function ( interval , cb ) {
5615+ var _f = sandbox . stub ( client , '_fetchLatestNotifications' ) . callsFake ( function ( interval , cb ) {
55795616 cb ( new Errors . NOT_AUTHORIZED ) ;
55805617 } ) ;
55815618
@@ -5609,15 +5646,15 @@ console.log('[client.js.1210:err:]',err); //TODO
56095646 var client = new Client ( ) ;
56105647 client . credentials = { } ;
56115648
5612- var ow = sandbox . stub ( client , 'openWallet' , function ( callback ) {
5649+ var ow = sandbox . stub ( client , 'openWallet' ) . callsFake ( function ( callback ) {
56135650 callback ( new Error ( ) ) ;
56145651 } ) ;
56155652
5616- var ip = sandbox . stub ( client , 'isPrivKeyExternal' , function ( ) {
5653+ var ip = sandbox . stub ( client , 'isPrivKeyExternal' ) . callsFake ( function ( ) {
56175654 return false ;
56185655 } ) ;
56195656
5620- var aa = sandbox . stub ( client , 'addAccess' , function ( options , callback ) {
5657+ var aa = sandbox . stub ( client , 'addAccess' ) . callsFake ( function ( options , callback ) {
56215658 callback ( new Error ( ) ) ;
56225659 } ) ;
56235660
0 commit comments