aboutsummaryrefslogtreecommitdiffstats
path: root/test/unit
diff options
context:
space:
mode:
authorkumavis <aaron@kumavis.me>2018-07-03 04:54:05 +0800
committerkumavis <aaron@kumavis.me>2018-07-03 04:54:05 +0800
commita89902c1701f2dcc41effc06d2315e515946b2ca (patch)
tree922408cf99bcca97c956d5860ac80c2c0c472ecc /test/unit
parent055346843bc90a5168151ba2adc9deacedf8afd4 (diff)
parent4c86f25f5bc4fa18847ca1b77e005afc3f37eddc (diff)
downloadtangerine-wallet-browser-a89902c1701f2dcc41effc06d2315e515946b2ca.tar.gz
tangerine-wallet-browser-a89902c1701f2dcc41effc06d2315e515946b2ca.tar.zst
tangerine-wallet-browser-a89902c1701f2dcc41effc06d2315e515946b2ca.zip
Merge branch 'develop' of github.com:MetaMask/metamask-extension into network-remove-provider-engine
Diffstat (limited to 'test/unit')
-rw-r--r--test/unit/app/account-import-strategies.spec.js62
-rw-r--r--test/unit/app/controllers/notice-controller-test.js50
-rw-r--r--test/unit/test-utils.js17
3 files changed, 66 insertions, 63 deletions
diff --git a/test/unit/app/account-import-strategies.spec.js b/test/unit/app/account-import-strategies.spec.js
index 83cfaeb3e..216c2f698 100644
--- a/test/unit/app/account-import-strategies.spec.js
+++ b/test/unit/app/account-import-strategies.spec.js
@@ -1,31 +1,59 @@
const assert = require('assert')
const path = require('path')
-const accountImporter = require('../../../app/scripts/account-import-strategies/index')
const ethUtil = require('ethereumjs-util')
+const accountImporter = require('../../../app/scripts/account-import-strategies/index')
+const { assertRejects } = require('../test-utils')
describe('Account Import Strategies', function () {
const privkey = '0x4cfd3e90fc78b0f86bf7524722150bb8da9c60cd532564d7ff43f5716514f553'
const json = '{"version":3,"id":"dbb54385-0a99-437f-83c0-647de9f244c3","address":"a7f92ce3fba24196cf6f4bd2e1eb3db282ba998c","Crypto":{"ciphertext":"bde13d9ade5c82df80281ca363320ce254a8a3a06535bbf6ffdeaf0726b1312c","cipherparams":{"iv":"fbf93718a57f26051b292f072f2e5b41"},"cipher":"aes-128-ctr","kdf":"scrypt","kdfparams":{"dklen":32,"salt":"7ffe00488319dec48e4c49a120ca49c6afbde9272854c64d9541c83fc6acdffe","n":8192,"r":8,"p":1},"mac":"2adfd9c4bc1cdac4c85bddfb31d9e21a684e0e050247a70c5698facf6b7d4681"}}'
- it('imports a private key and strips 0x prefix', async function () {
- const importPrivKey = await accountImporter.importAccount('Private Key', [ privkey ])
- assert.equal(importPrivKey, ethUtil.stripHexPrefix(privkey))
- })
+ describe('private key import', function () {
+ it('imports a private key and strips 0x prefix', async function () {
+ const importPrivKey = await accountImporter.importAccount('Private Key', [ privkey ])
+ assert.equal(importPrivKey, ethUtil.stripHexPrefix(privkey))
+ })
- it('fails when password is incorrect for keystore', async function () {
- const wrongPassword = 'password2'
+ it('throws an error for empty string private key', async () => {
+ assertRejects(async function() {
+ await accountImporter.importAccount('Private Key', [ '' ])
+ }, Error, 'no empty strings')
+ })
- try {
- await accountImporter.importAccount('JSON File', [ json, wrongPassword])
- } catch (error) {
- assert.equal(error.message, 'Key derivation failed - possibly wrong passphrase')
- }
- })
+ it('throws an error for undefined string private key', async () => {
+ assertRejects(async function () {
+ await accountImporter.importAccount('Private Key', [ undefined ])
+ })
+ })
- it('imports json string and password to return a private key', async function () {
- const fileContentsPassword = 'password1'
- const importJson = await accountImporter.importAccount('JSON File', [ json, fileContentsPassword])
- assert.equal(importJson, '0x5733876abe94146069ce8bcbabbde2677f2e35fa33e875e92041ed2ac87e5bc7')
+ it('throws an error for undefined string private key', async () => {
+ assertRejects(async function () {
+ await accountImporter.importAccount('Private Key', [])
+ })
+ })
+
+ it('throws an error for invalid private key', async () => {
+ assertRejects(async function () {
+ await accountImporter.importAccount('Private Key', [ 'popcorn' ])
+ })
+ })
})
+ describe('JSON keystore import', function () {
+ it('fails when password is incorrect for keystore', async function () {
+ const wrongPassword = 'password2'
+
+ try {
+ await accountImporter.importAccount('JSON File', [ json, wrongPassword])
+ } catch (error) {
+ assert.equal(error.message, 'Key derivation failed - possibly wrong passphrase')
+ }
+ })
+
+ it('imports json string and password to return a private key', async function () {
+ const fileContentsPassword = 'password1'
+ const importJson = await accountImporter.importAccount('JSON File', [ json, fileContentsPassword])
+ assert.equal(importJson, '0x5733876abe94146069ce8bcbabbde2677f2e35fa33e875e92041ed2ac87e5bc7')
+ })
+ })
})
diff --git a/test/unit/app/controllers/notice-controller-test.js b/test/unit/app/controllers/notice-controller-test.js
index e78b69623..b3ae75080 100644
--- a/test/unit/app/controllers/notice-controller-test.js
+++ b/test/unit/app/controllers/notice-controller-test.js
@@ -14,18 +14,6 @@ describe('notice-controller', function () {
})
describe('notices', function () {
- describe('#getNoticesList', function () {
- it('should return an empty array when new', function (done) {
- // const testList = [{
- // id: 0,
- // read: false,
- // title: 'Futuristic Notice',
- // }]
- var result = noticeController.getNoticesList()
- assert.equal(result.length, 0)
- done()
- })
- })
describe('#setNoticesList', function () {
it('should set data appropriately', function (done) {
@@ -41,36 +29,6 @@ describe('notice-controller', function () {
})
})
- describe('#updateNoticeslist', function () {
- it('should integrate the latest changes from the source', function (done) {
- var testList = [{
- id: 55,
- read: false,
- title: 'Futuristic Notice',
- }]
- noticeController.setNoticesList(testList)
- noticeController.updateNoticesList().then(() => {
- var newList = noticeController.getNoticesList()
- assert.ok(newList[0].id === 55)
- assert.ok(newList[1])
- done()
- })
- })
- it('should not overwrite any existing fields', function (done) {
- var testList = [{
- id: 0,
- read: false,
- title: 'Futuristic Notice',
- }]
- noticeController.setNoticesList(testList)
- var newList = noticeController.getNoticesList()
- assert.equal(newList[0].id, 0)
- assert.equal(newList[0].title, 'Futuristic Notice')
- assert.equal(newList.length, 1)
- done()
- })
- })
-
describe('#markNoticeRead', function () {
it('should mark a notice as read', function (done) {
var testList = [{
@@ -86,7 +44,7 @@ describe('notice-controller', function () {
})
})
- describe('#getLatestUnreadNotice', function () {
+ describe('#getNextUnreadNotice', function () {
it('should retrieve the latest unread notice', function (done) {
var testList = [
{id: 0, read: true, title: 'Past Notice'},
@@ -94,8 +52,8 @@ describe('notice-controller', function () {
{id: 2, read: false, title: 'Future Notice'},
]
noticeController.setNoticesList(testList)
- var latestUnread = noticeController.getLatestUnreadNotice()
- assert.equal(latestUnread.id, 2)
+ var latestUnread = noticeController.getNextUnreadNotice()
+ assert.equal(latestUnread.id, 1)
done()
})
it('should return undefined if no unread notices exist.', function (done) {
@@ -105,7 +63,7 @@ describe('notice-controller', function () {
{id: 2, read: true, title: 'Future Notice'},
]
noticeController.setNoticesList(testList)
- var latestUnread = noticeController.getLatestUnreadNotice()
+ var latestUnread = noticeController.getNextUnreadNotice()
assert.ok(!latestUnread)
done()
})
diff --git a/test/unit/test-utils.js b/test/unit/test-utils.js
new file mode 100644
index 000000000..7d0ae4d91
--- /dev/null
+++ b/test/unit/test-utils.js
@@ -0,0 +1,17 @@
+const assert = require('assert')
+
+module.exports = {
+ assertRejects,
+}
+
+// assert.rejects added in node v10
+async function assertRejects (asyncFn, regExp) {
+ let f = () => {}
+ try {
+ await asyncFn()
+ } catch (error) {
+ f = () => { throw error }
+ } finally {
+ assert.throws(f, regExp)
+ }
+}