-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathTokenPocketUser.test.ts
137 lines (113 loc) · 3.81 KB
/
TokenPocketUser.test.ts
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import './__mocks__/tp-eosjs.mock'
import { wallet, chain, authSignResponse, pushActionResponse } from './__mocks__/data.mock'
import { UALErrorType } from 'universal-authenticator-library'
import * as tp from 'tp-eosjs'
import { Name } from './interfaces'
import { TokenPocketUser } from './TokenPocketUser'
import { UALTokenPocketError } from './UALTokenPocketError'
describe('TokenPocketUser', () => {
let user: TokenPocketUser
beforeEach(() => {
user = new TokenPocketUser(chain, wallet)
})
it('gets the account name', async () => {
const accountName = await user.getAccountName()
expect(accountName).toEqual(wallet.name)
})
it('get the keys', async () => {
const keys = await user.getKeys()
expect(keys).toEqual([wallet.address])
})
it('gets the chain ID', async () => {
const chainId = await user.getChainId()
expect(chainId).toEqual(chain.chainId)
})
describe('signArbitrary', () => {
const publicKey = 'testPublicKey'
const data = 'testSignData'
const helpText = 'test help text'
beforeEach(() => {
tp.eosAuthSign.mockReturnValue(authSignResponse)
})
it('signs arbitrary data', async () => {
const signature = await user.signArbitrary(publicKey, data, helpText)
expect(signature).toEqual(authSignResponse.data.signature)
expect(tp.eosAuthSign).toHaveBeenCalledWith(
{ from: wallet.name, publicKey, signdata: data }
)
})
it('throws UALError on api error', async () => {
const errorMsg = 'Unable to sign'
tp.eosAuthSign.mockImplementation(() => {
throw new Error(errorMsg)
})
let didThrow = true
try {
await user.signArbitrary(publicKey, data, helpText)
didThrow = false
} catch (e) {
const ex = e as UALTokenPocketError
expect(ex.message).toEqual('Unable to sign arbitrary string')
expect(ex.source).toEqual(Name)
expect(ex.type).toEqual(UALErrorType.Signing)
expect(ex.cause).toEqual(new Error(errorMsg))
}
expect(didThrow).toBe(true)
})
it('throws UALError if result is not set', async () => {
tp.eosAuthSign.mockReturnValue({
result: false,
data: {},
msg: 'test'
})
let didThrow = true
try {
await user.signArbitrary(publicKey, data, helpText)
didThrow = false
} catch (e) {
const ex = e as UALTokenPocketError
expect(ex.message).toEqual('Unable to sign arbitrary string')
expect(ex.source).toEqual(Name)
expect(ex.type).toEqual(UALErrorType.Signing)
expect(ex.cause).toEqual(new Error('No result returned'))
}
expect(didThrow).toBe(true)
})
})
describe('signTransaction', () => {
const transaction = { test: 'test' }
beforeEach(() => {
tp.pushEosAction.mockReturnValue(pushActionResponse)
})
it('signs the transaction', async () => {
const result = await user.signTransaction(transaction, {})
expect(result).toEqual({
transaction,
wasBroadcast: true,
transactionId: pushActionResponse.data.transactionId
})
expect(tp.pushEosAction).toHaveBeenCalledWith(
{ test: 'test', account: wallet.name, address: wallet.address}
)
})
it('throws UALError on failed signTransaction', async () => {
tp.pushEosAction.mockImplementation(() => {
throw new Error('Unable to transact')
})
let didThrow = true
try {
await user.signTransaction(transaction, {})
didThrow = false
} catch (e) {
const ex = e as UALTokenPocketError
expect(ex.source).toEqual(Name)
expect(ex.type).toEqual(UALErrorType.Signing)
expect(ex.cause).not.toBeNull()
}
expect(didThrow).toBe(true)
})
})
afterEach(() => {
jest.clearAllMocks()
})
})