Skip to content

Commit 9e074f9

Browse files
Update tests
1 parent 6384992 commit 9e074f9

1 file changed

Lines changed: 144 additions & 42 deletions

File tree

packages/wallet/dapp-client/test/ethauth-proof.test.ts

Lines changed: 144 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,18 @@
11
import { afterEach, describe, expect, it, vi } from 'vitest'
2-
import { Secp256k1 } from 'ox'
32

4-
import { ChainSessionManager } from '../src/ChainSessionManager.js'
53
import { DappClient } from '../src/DappClient.js'
6-
import { TransportMode } from '../src/types/index.js'
4+
import { DappTransport } from '../src/DappTransport.js'
5+
import { RequestActionType, TransportMode } from '../src/types/index.js'
76
import { WebStorage } from '../src/utils/storage.js'
87

98
describe('ETHAuth proof persistence', () => {
109
afterEach(() => {
10+
vi.restoreAllMocks()
1111
vi.unstubAllGlobals()
1212
})
1313

14-
it('persists ETHAuth proof only when requested during createNewSession', async () => {
15-
const fetchMock = vi.fn()
16-
vi.stubGlobal('fetch', fetchMock)
17-
vi.stubGlobal('window', { fetch: fetchMock } as any)
18-
19-
const ethAuthProof = {
20-
typedData: {
21-
domain: {},
22-
types: {},
23-
message: {},
24-
},
25-
ewtString: 'proof-string',
26-
} as any
27-
28-
const sequenceStorage = {
14+
const createSequenceStorageMock = () =>
15+
({
2916
setPendingRedirectRequest: vi.fn().mockResolvedValue(undefined),
3017
isRedirectRequestPending: vi.fn().mockResolvedValue(false),
3118
saveTempSessionPk: vi.fn().mockResolvedValue(undefined),
@@ -43,50 +30,148 @@ describe('ETHAuth proof persistence', () => {
4330
getSessionlessConnection: vi.fn().mockResolvedValue(null),
4431
clearSessionlessConnection: vi.fn().mockResolvedValue(undefined),
4532
saveEthAuthProof: vi.fn().mockResolvedValue(undefined),
46-
getEthAuthProof: vi.fn().mockResolvedValue(ethAuthProof),
33+
getEthAuthProof: vi.fn().mockResolvedValue(null),
4734
clearEthAuthProof: vi.fn().mockResolvedValue(undefined),
4835
clearAllData: vi.fn().mockResolvedValue(undefined),
36+
}) as any
37+
38+
it('persists ETHAuth proof when connect requests ethAuth in redirect mode', async () => {
39+
const fetchMock = vi.fn()
40+
vi.stubGlobal('fetch', fetchMock)
41+
vi.stubGlobal('window', { fetch: fetchMock } as any)
42+
43+
const ethAuthProof = {
44+
typedData: {
45+
domain: {},
46+
types: {},
47+
message: {},
48+
},
49+
ewtString: 'proof-string',
4950
} as any
5051

51-
const transport = {
52-
mode: TransportMode.POPUP,
53-
sendRequest: vi.fn().mockResolvedValue({
54-
walletAddress: '0x1111111111111111111111111111111111111111',
55-
ethAuthProof,
52+
const sequenceStorage = createSequenceStorageMock()
53+
const sendRequestMock = vi.spyOn(DappTransport.prototype, 'sendRequest').mockResolvedValue({
54+
walletAddress: '0x1111111111111111111111111111111111111111',
55+
ethAuthProof,
56+
} as any)
57+
58+
const client = new DappClient('https://wallet.example', 'https://dapp.example', 'test-project-access-key', {
59+
sequenceStorage,
60+
transportMode: TransportMode.REDIRECT,
61+
canUseIndexedDb: false,
62+
redirectActionHandler: vi.fn(),
63+
})
64+
65+
await client.connect(1, undefined, {
66+
ethAuth: {
67+
app: 'app-name',
68+
},
69+
})
70+
71+
expect(sendRequestMock).toHaveBeenCalledWith(
72+
RequestActionType.CREATE_NEW_SESSION,
73+
'https://dapp.example',
74+
expect.objectContaining({
75+
ethAuth: {
76+
app: 'app-name',
77+
},
5678
}),
57-
closeWallet: vi.fn(),
79+
expect.any(Object),
80+
)
81+
expect(sequenceStorage.saveEthAuthProof).toHaveBeenCalledWith(ethAuthProof)
82+
})
83+
84+
it('persists ETHAuth proof when connect requests ethAuth in popup mode', async () => {
85+
const fetchMock = vi.fn()
86+
vi.stubGlobal('fetch', fetchMock)
87+
vi.stubGlobal('window', { fetch: fetchMock } as any)
88+
vi.stubGlobal('document', {} as any)
89+
90+
const ethAuthProof = {
91+
typedData: {
92+
domain: {},
93+
types: {},
94+
message: {},
95+
},
96+
ewtString: 'proof-string',
5897
} as any
5998

60-
const manager = new ChainSessionManager(
61-
1,
62-
transport,
63-
'test-project-access-key',
64-
'https://keymachine.sequence.app',
65-
'https://nodes.sequence.app/{network}',
66-
'https://{network}-relayer.sequence.app',
99+
const sequenceStorage = createSequenceStorageMock()
100+
const sendRequestMock = vi.spyOn(DappTransport.prototype, 'sendRequest').mockResolvedValue({
101+
walletAddress: '0x1111111111111111111111111111111111111111',
102+
ethAuthProof,
103+
} as any)
104+
105+
const client = new DappClient('https://wallet.example', 'https://dapp.example', 'test-project-access-key', {
67106
sequenceStorage,
68-
'https://example.com/redirect',
69-
undefined,
70-
vi.fn(() => Secp256k1.randomPrivateKey()),
71-
false,
72-
)
107+
transportMode: TransportMode.POPUP,
108+
canUseIndexedDb: false,
109+
})
73110

74-
await manager.createNewSession('https://example.com', undefined, {
111+
await client.connect(1, undefined, {
75112
ethAuth: {
76113
app: 'app-name',
77114
},
78115
})
79116

117+
expect(sendRequestMock).toHaveBeenCalledWith(
118+
RequestActionType.CREATE_NEW_SESSION,
119+
'https://dapp.example',
120+
expect.objectContaining({
121+
ethAuth: {
122+
app: 'app-name',
123+
},
124+
}),
125+
expect.any(Object),
126+
)
80127
expect(sequenceStorage.saveEthAuthProof).toHaveBeenCalledWith(ethAuthProof)
81-
expect(sequenceStorage.clearEthAuthProof).not.toHaveBeenCalled()
82128
})
83129

84-
it('clears ETHAuth proof on disconnect', async () => {
85-
const sequenceStorage = new WebStorage()
130+
it('does not persist ETHAuth proof when connect does not request ethAuth', async () => {
131+
const fetchMock = vi.fn()
132+
vi.stubGlobal('fetch', fetchMock)
133+
vi.stubGlobal('window', { fetch: fetchMock } as any)
134+
135+
const ethAuthProof = {
136+
typedData: {
137+
domain: {},
138+
types: {},
139+
message: {},
140+
},
141+
ewtString: 'proof-string',
142+
} as any
143+
144+
const sequenceStorage = createSequenceStorageMock()
145+
const sendRequestMock = vi.spyOn(DappTransport.prototype, 'sendRequest').mockResolvedValue({
146+
walletAddress: '0x1111111111111111111111111111111111111111',
147+
ethAuthProof,
148+
} as any)
149+
86150
const client = new DappClient('https://wallet.example', 'https://dapp.example', 'test-project-access-key', {
87151
sequenceStorage,
152+
transportMode: TransportMode.REDIRECT,
153+
canUseIndexedDb: false,
154+
redirectActionHandler: vi.fn(),
88155
})
89156

157+
await client.connect(1)
158+
159+
expect(sendRequestMock).toHaveBeenCalledWith(
160+
RequestActionType.CREATE_NEW_SESSION,
161+
'https://dapp.example',
162+
expect.not.objectContaining({
163+
ethAuth: expect.anything(),
164+
}),
165+
expect.any(Object),
166+
)
167+
expect(sequenceStorage.saveEthAuthProof).not.toHaveBeenCalled()
168+
})
169+
170+
it('clears ETHAuth proof on disconnect', async () => {
171+
const fetchMock = vi.fn()
172+
vi.stubGlobal('fetch', fetchMock)
173+
vi.stubGlobal('window', { fetch: fetchMock } as any)
174+
90175
const ethAuthProof = {
91176
typedData: {
92177
domain: {},
@@ -96,7 +181,24 @@ describe('ETHAuth proof persistence', () => {
96181
ewtString: 'proof-string',
97182
} as any
98183

99-
await sequenceStorage.saveEthAuthProof(ethAuthProof)
184+
vi.spyOn(DappTransport.prototype, 'sendRequest').mockResolvedValue({
185+
walletAddress: '0x1111111111111111111111111111111111111111',
186+
ethAuthProof,
187+
} as any)
188+
189+
const client = new DappClient('https://wallet.example', 'https://dapp.example', 'test-project-access-key', {
190+
sequenceStorage: new WebStorage(),
191+
transportMode: TransportMode.REDIRECT,
192+
canUseIndexedDb: false,
193+
redirectActionHandler: vi.fn(),
194+
})
195+
196+
await client.connect(1, undefined, {
197+
ethAuth: {
198+
app: 'app-name',
199+
},
200+
})
201+
100202
expect(await client.getEthAuthProof()).toEqual(ethAuthProof)
101203

102204
await client.disconnect()

0 commit comments

Comments
 (0)