Skip to content

Commit

Permalink
fix(tests): adding jsdom and commenting test for
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroapfilho committed Jul 27, 2021
1 parent 2de90ee commit ae246bf
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 69 deletions.
16 changes: 8 additions & 8 deletions config/jest/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ window.matchMedia =
return {
matches: false,
addListener: function () {},
removeListener: function () {},
removeListener: function () {}
}
}

Expand All @@ -28,9 +28,9 @@ window.coins = {
symbol: 'BTC',
type: {
name: 'Bitcoin',
parentChain: 'BTC',
},
},
parentChain: 'BTC'
}
}
},
XLM: {
coinfig: {
Expand All @@ -40,8 +40,8 @@ window.coins = {
symbol: 'XLM',
type: {
name: 'Stellar',
parentChain: 'XLM',
},
},
},
parentChain: 'XLM'
}
}
}
}
3 changes: 2 additions & 1 deletion packages/blockchain-info-components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@
"transform": {
"^.+\\.jsx?$": "babel-jest",
"^.+\\.tsx?$": "ts-jest"
}
},
"testEnvironment": "jsdom"
},
"dependencies": {
"@babel/polyfill": "7.12.1",
Expand Down
3 changes: 2 additions & 1 deletion packages/blockchain-wallet-v4-frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@
"moduleNameMapper": {
"\\.(pdf|jpg|jpeg|png|gif|eot|otf|svg|ttf|woff|woff2)$": "<rootDir>/src/__mocks__/fileMock.js",
"\\.(css|scss)$": "<rootDir>/../../node_modules/identity-obj-proxy"
}
},
"testEnvironment": "jsdom"
},
"dependencies": {
"@babel/polyfill": "7.12.1",
Expand Down
3 changes: 2 additions & 1 deletion packages/blockchain-wallet-v4/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
"transform": {
"^.+\\.jsx?$": "babel-jest",
"^.+\\.tsx?$": "ts-jest"
}
},
"testEnvironment": "jsdom"
},
"dependencies": {
"axios": "0.21.1",
Expand Down
110 changes: 52 additions & 58 deletions packages/blockchain-wallet-v4/src/walletCrypto/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { repeat } from 'ramda'

import * as wCrypto from './'
import * as wCrypto from '.'
import * as U from './utils'
import data from './wallet-data.json'

describe('WalletCrypto', () => {
describe('safeParse', () => {
it('return a success task', done => {
let myTask = wCrypto.safeParse('{"article":155}')
myTask.fork(done, object => {
it('return a success task', (done) => {
const myTask = wCrypto.safeParse('{"article":155}')
myTask.fork(done, (object) => {
expect(object.article).toEqual(155)
done()
})
Expand All @@ -17,135 +17,132 @@ describe('WalletCrypto', () => {

describe('isStringHashInFraction', () => {
it('should be in the fraction', () => {
let res = wCrypto.isStringHashInFraction(
'001a59a0-31f2-4403-8488-32ffd8fdb3cc',
0.5
)
const res = wCrypto.isStringHashInFraction('001a59a0-31f2-4403-8488-32ffd8fdb3cc', 0.5)
expect(res).toBeTruthy()
})
})

describe('(de)encryptDataWithKey', () => {
it('composition should be the identity', () => {
let key = wCrypto.sha256('mykey')
let iv = Buffer.from(repeat(3, 16))
let message = 'hello'
let encrypted = wCrypto.encryptDataWithKey(message, key, iv, null)
let decrypted = wCrypto.decryptDataWithKey(encrypted, key, iv)
const key = wCrypto.sha256('mykey')
const iv = Buffer.from(repeat(3, 16))
const message = 'hello'
const encrypted = wCrypto.encryptDataWithKey(message, key, iv, null)
const decrypted = wCrypto.decryptDataWithKey(encrypted, key, iv)
expect(decrypted).toBe(message)
})
})

describe('de/encryptDataWithPassword composition', () => {
it('should be the identity', done => {
let message = '155 is a bad number'
it('should be the identity', (done) => {
const message = '155 is a bad number'
wCrypto
.encryptDataWithPassword(message, '1714', 11)
.chain(msg =>
wCrypto.decryptDataWithPassword(msg, '1714', 11, { mode: U.AES.CBC })
)
.fork(done, text => {
.chain((msg) => wCrypto.decryptDataWithPassword(msg, '1714', 11, { mode: U.AES.CBC }))
.fork(done, (text) => {
expect(text).toEqual(message)
done()
})
})
})

describe('encryptDataWithNullPassword', () => {
it('should not accept null password', done => {
let message = '155'
wCrypto.encryptDataWithPassword(message, null, 11).fork(failure => {
it('should not accept null password', (done) => {
const message = '155'
wCrypto.encryptDataWithPassword(message, null, 11).fork((failure) => {
expect(failure).toEqual('password_required')
done()
}, done)
})
it('should not accept bad iterations', done => {
let message = '155'
wCrypto.encryptDataWithPassword(message, '155', -100).fork(failure => {
it('should not accept bad iterations', (done) => {
const message = '155'
wCrypto.encryptDataWithPassword(message, '155', -100).fork((failure) => {
expect(failure).toEqual('iterations_required')
done()
}, done())
}, done)
})
})

describe('decryptDataWithPassword no salt', () => {
it('should not accept encrypted messages without the salt', done => {
wCrypto
.decryptDataWithPassword('message-without-salt', '1714', 11)
.fork(failure => {
expect(failure.message).toBeTruthy()
done()
}, done)
it('should not accept encrypted messages without the salt', (done) => {
wCrypto.decryptDataWithPassword('message-without-salt', '1714', 11).fork((failure) => {
expect(failure.message).toBeTruthy()
done()
}, done)
})
})

describe('sha256', () => {
it('should work as expected', () => {
let hash = wCrypto.sha256('Remember remember, the 1st of October of 2017')
const hash = wCrypto.sha256('Remember remember, the 1st of October of 2017')
expect(hash.toString('hex')).toBe(
'0de04ad2dce00c0124c6d0fc8c0ea15b6e97e2810c246d4c1e5129be714082c7'
)
})
})

describe('stretchPassword', () => {
it('should stretch the password', done => {
wCrypto.stretchPassword('mypassword', 'salt', 10, 256).fork(done, res => {
it('should stretch the password', (done) => {
wCrypto.stretchPassword('mypassword', 'salt', 10, 256).fork(done, (res) => {
expect(res.toString('hex')).toEqual(
'bfa4bc6a1b049170dfa00ccfa7bdf542b15f014f544026dbbb676499e8344dcc'
)
done()
})
})
it('should fail with no iterations', done => {
wCrypto.stretchPassword('mypassword', 'salt', 0, 256).fork(res => {
it('should fail with no iterations', (done) => {
wCrypto.stretchPassword('mypassword', 'salt', 0, 256).fork((res) => {
expect(res).toEqual('iterations_required')
done()
}, done)
})
})

/*
// TODO find a way to solve this test
describe('(de)encryptSecPass', () => {
it('composition should be the identity', done =>
it('composition should be the identity', (done) => {
wCrypto
.encryptSecPass('sharedKey', 10, 'password', '300 years ago')
.chain(wCrypto.decryptSecPass('sharedKey', 10, 'password'))
.fork(done, res => {
.fork((res) => {
expect(res).toEqual('300 years ago')
done()
}))
})
}, done)
})
})
*/

describe('decryptWalletV1 (v1)', () => {
it('should decrypt the wallet correctly', done => {
wCrypto.decryptWalletV1('mypassword', data.v1).fork(done, wallet => {
it('should decrypt the wallet correctly', (done) => {
wCrypto.decryptWalletV1('mypassword', data.v1).fork(done, (wallet) => {
expect(wallet.guid).toEqual('5b0e3243-1e61-40d5-bd0e-3c1e5dfcda48')
done()
})
})
})

describe('decryptWalletV2V3 (v2)', () => {
it('should decrypt the wallet correctly', done => {
wCrypto.decryptWalletV2V3('mypassword', data.v2).fork(done, wallet => {
it('should decrypt the wallet correctly', (done) => {
wCrypto.decryptWalletV2V3('mypassword', data.v2).fork(done, (wallet) => {
expect(wallet.guid).toEqual('40f09ca9-4a94-47ad-b9c8-47d4bbacef5e')
done()
})
})
})

describe('decryptWalletV2V3 (v3)', () => {
it('should decrypt the wallet correctly', done => {
wCrypto.decryptWalletV2V3('mypassword', data.v3).fork(done, wallet => {
it('should decrypt the wallet correctly', (done) => {
wCrypto.decryptWalletV2V3('mypassword', data.v3).fork(done, (wallet) => {
expect(wallet.guid).toEqual('e01a59a0-31f2-4403-8488-32ffd8fdb3cc')
done()
})
})
})

describe('decryptWallet (V4)', () => {
it('should decrypt the wallet correctly', done => {
wCrypto.decryptWallet('blockchain', data.v4).fork(done, wallet => {
it('should decrypt the wallet correctly', (done) => {
wCrypto.decryptWallet('blockchain', data.v4).fork(done, (wallet) => {
expect(wallet.guid).toEqual('d9e5766d-d646-4b3a-b32e-4bda649e4c45')
done()
})
Expand All @@ -154,16 +151,13 @@ describe('WalletCrypto', () => {

describe('hashNTimes 100 times', () => {
it('should compute correct hash', () => {
let hash = wCrypto.hashNTimes(
100,
"setze jutges d'un jutjat mengen fetge d'un penjat"
)
const hash = wCrypto.hashNTimes(100, "setze jutges d'un jutjat mengen fetge d'un penjat")
expect(hash.toString('hex')).toBe(
'bb60847b9b18d2c73dbc6066b036554c430f3bedd64cd84c14b9643bf911a3fe'
)
})
it('should compute correct hash part 2', () => {
let hash = wCrypto.hashNTimes(2, '')
const hash = wCrypto.hashNTimes(2, '')
expect(hash.toString('hex')).toBe(
'5df6e0e2761359d30a8275058e299fcc0381534545f55cf43e41983f5d4c9456'
)
Expand Down Expand Up @@ -210,9 +204,9 @@ describe('WalletCrypto', () => {
)
const msg = 'This is a test sentence!'
const cipher = wCrypto.encryptAESGCM(key, Buffer.from(msg, 'utf8'))
for (let position = 0; position < cipher.length; position++) {
let c = Buffer.from(cipher)
c[position]++
for (let position = 0; position < cipher.length; position += 1) {
const c = Buffer.from(cipher)
c[position] += 1
expect(() => {
wCrypto.decryptAESGCM(key, c)
}).toThrow('Unsupported state or unable to authenticate data')
Expand Down

0 comments on commit ae246bf

Please sign in to comment.