aboutsummaryrefslogtreecommitdiffstats
path: root/test/unit/metamask-controller-test.js
blob: 414610404d4b30321b157dfde418bfac91e00511 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
var assert = require('assert')
var MetaMaskController = require('../../app/scripts/metamask-controller')
var sinon = require('sinon')
var extend = require('xtend')
const STORAGE_KEY = 'metamask-config'

describe('MetaMaskController', function() {
  const noop = () => {}
  let controller = new MetaMaskController({
    showUnconfirmedMessage: noop,
    unlockAccountMessage: noop,
    showUnapprovedTx: noop,
    setData,
    loadData,
  })

  beforeEach(function() {
    // sinon allows stubbing methods that are easily verified
    this.sinon = sinon.sandbox.create()
    window.localStorage = {} // Hacking localStorage support into JSDom
  })

  afterEach(function() {
    // sinon requires cleanup otherwise it will overwrite context
    this.sinon.restore()
  })

  describe('#enforceTxValidations', function () {
    it('returns null for positive values', function() {
      var sample = {
        value: '0x01'
      }
      var res = controller.enforceTxValidations(sample)
      assert.equal(res, null, 'no error')
    })


    it('returns error for negative values', function() {
      var sample = {
        value: '-0x01'
      }
      var res = controller.enforceTxValidations(sample)
      assert.ok(res, 'error')
    })
  })
})


function loadData () {
  var oldData = getOldStyleData()
  var newData
  try {
    newData = JSON.parse(window.localStorage[STORAGE_KEY])
  } catch (e) {}

  var data = extend({
    meta: {
      version: 0,
    },
    data: {
      config: {
        provider: {
          type: 'testnet',
        },
      },
    },
  }, oldData || null, newData || null)
  return data
}

function getOldStyleData () {
  var config, wallet, seedWords

  var result = {
    meta: { version: 0 },
    data: {},
  }

  try {
    config = JSON.parse(window.localStorage['config'])
    result.data.config = config
  } catch (e) {}
  try {
    wallet = JSON.parse(window.localStorage['lightwallet'])
    result.data.wallet = wallet
  } catch (e) {}
  try {
    seedWords = window.localStorage['seedWords']
    result.data.seedWords = seedWords
  } catch (e) {}

  return result
}

function setData (data) {
  window.localStorage[STORAGE_KEY] = JSON.stringify(data)
}