Skip to content

Commit 33aed22

Browse files
committed
address cr feedback
1 parent e1be876 commit 33aed22

File tree

1 file changed

+116
-58
lines changed

1 file changed

+116
-58
lines changed

app/tests/utils/proving/provingMachine.generatePayload.test.ts

Lines changed: 116 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -70,67 +70,67 @@ const {
7070
generateTEEInputsDisclose,
7171
} = require('../../../src/utils/proving/provingInputs');
7272

73+
function setupDefaultStores() {
74+
useProvingStore.setState({
75+
circuitType: 'register',
76+
passportData: { documentCategory: 'passport', mock: false },
77+
secret: 'sec',
78+
uuid: '123',
79+
sharedKey: Buffer.alloc(32, 1),
80+
env: 'prod',
81+
});
82+
83+
useSelfAppStore.setState({
84+
selfApp: createMockSelfApp(),
85+
});
86+
87+
useProtocolStore.setState({
88+
passport: createMockProtocolState(),
89+
id_card: createMockProtocolState(),
90+
});
91+
}
92+
93+
function createMockSelfApp() {
94+
return {
95+
chainID: 42220 as const,
96+
userId: 'u',
97+
userDefinedData: '0x0',
98+
endpointType: 'https' as const,
99+
endpoint: 'https://e',
100+
scope: 's',
101+
sessionId: '',
102+
appName: '',
103+
logoBase64: '',
104+
header: '',
105+
userIdType: 'uuid' as const,
106+
devMode: false,
107+
disclosures: {},
108+
version: 1,
109+
};
110+
}
111+
112+
function createMockProtocolState() {
113+
return {
114+
dsc_tree: 'tree',
115+
csca_tree: [['a']],
116+
commitment_tree: null,
117+
deployed_circuits: null,
118+
circuits_dns_mapping: null,
119+
alternative_csca: {},
120+
fetch_deployed_circuits: jest.fn(() => Promise.resolve()),
121+
fetch_circuits_dns_mapping: jest.fn(() => Promise.resolve()),
122+
fetch_csca_tree: jest.fn(() => Promise.resolve()),
123+
fetch_dsc_tree: jest.fn(() => Promise.resolve()),
124+
fetch_identity_tree: jest.fn(() => Promise.resolve()),
125+
fetch_alternative_csca: jest.fn(() => Promise.resolve()),
126+
fetch_all: jest.fn(() => Promise.resolve()),
127+
};
128+
}
129+
73130
describe('_generatePayload', () => {
74131
beforeEach(() => {
75132
jest.clearAllMocks();
76-
useProvingStore.setState({
77-
circuitType: 'register',
78-
passportData: { documentCategory: 'passport', mock: false },
79-
secret: 'sec',
80-
uuid: '123',
81-
sharedKey: Buffer.alloc(32, 1),
82-
env: 'prod',
83-
});
84-
useSelfAppStore.setState({
85-
selfApp: {
86-
chainID: 42220,
87-
userId: 'u',
88-
userDefinedData: '0x0',
89-
endpointType: 'https',
90-
endpoint: 'https://e',
91-
scope: 's',
92-
sessionId: '',
93-
appName: '',
94-
logoBase64: '',
95-
header: '',
96-
userIdType: 'uuid',
97-
devMode: false,
98-
disclosures: {},
99-
version: 1,
100-
},
101-
});
102-
useProtocolStore.setState({
103-
passport: {
104-
dsc_tree: 'tree',
105-
csca_tree: [['a']],
106-
commitment_tree: null,
107-
deployed_circuits: null,
108-
circuits_dns_mapping: null,
109-
alternative_csca: {},
110-
fetch_deployed_circuits: jest.fn(),
111-
fetch_circuits_dns_mapping: jest.fn(),
112-
fetch_csca_tree: jest.fn(),
113-
fetch_dsc_tree: jest.fn(),
114-
fetch_identity_tree: jest.fn(),
115-
fetch_alternative_csca: jest.fn(),
116-
fetch_all: jest.fn(),
117-
},
118-
id_card: {
119-
commitment_tree: null,
120-
dsc_tree: null,
121-
csca_tree: null,
122-
deployed_circuits: null,
123-
circuits_dns_mapping: null,
124-
alternative_csca: {},
125-
fetch_deployed_circuits: jest.fn(),
126-
fetch_circuits_dns_mapping: jest.fn(),
127-
fetch_csca_tree: jest.fn(),
128-
fetch_dsc_tree: jest.fn(),
129-
fetch_identity_tree: jest.fn(),
130-
fetch_alternative_csca: jest.fn(),
131-
fetch_all: jest.fn(),
132-
},
133-
} as any);
133+
setupDefaultStores();
134134
});
135135

136136
it('register circuit', async () => {
@@ -152,8 +152,22 @@ describe('_generatePayload', () => {
152152
useProvingStore.setState({ circuitType: 'dsc' });
153153
const payload = await useProvingStore.getState()._generatePayload();
154154
expect(generateTEEInputsDSC).toHaveBeenCalled();
155+
expect(generateTEEInputsDSC).toHaveBeenCalledWith(
156+
expect.objectContaining({
157+
documentCategory: 'passport',
158+
mock: false,
159+
}),
160+
[['a']], // csca_tree
161+
'prod', // env
162+
);
155163
expect(useProvingStore.getState().endpointType).toBe('celo');
156164
expect(payload.params.uuid).toBe('123');
165+
expect(payload.params).toEqual({
166+
uuid: '123',
167+
nonce: [0],
168+
cipher_text: [1],
169+
auth_tag: [2],
170+
});
157171
});
158172

159173
it('disclose circuit', async () => {
@@ -163,4 +177,48 @@ describe('_generatePayload', () => {
163177
expect(useProvingStore.getState().endpointType).toBe('https');
164178
expect(payload.params.uuid).toBe('123');
165179
});
180+
181+
it('handles missing passport data', async () => {
182+
useProvingStore.setState({ passportData: null });
183+
await expect(
184+
useProvingStore.getState()._generatePayload(),
185+
).rejects.toThrow();
186+
});
187+
188+
it('handles input generation failure', async () => {
189+
// Reset all mocks first
190+
jest.clearAllMocks();
191+
setupDefaultStores();
192+
193+
generateTEEInputsRegister.mockImplementation(() => {
194+
throw new Error('Input generation failed');
195+
});
196+
await expect(useProvingStore.getState()._generatePayload()).rejects.toThrow(
197+
'Input generation failed',
198+
);
199+
200+
// Restore the mock to its original implementation
201+
generateTEEInputsRegister.mockRestore();
202+
});
203+
204+
it('handles encryption failure', async () => {
205+
// Reset all mocks first
206+
jest.clearAllMocks();
207+
setupDefaultStores();
208+
209+
// Restore all original implementations first
210+
generateTEEInputsRegister.mockImplementation(() => ({
211+
inputs: { r: 1 },
212+
circuitName: 'reg',
213+
endpointType: 'celo',
214+
endpoint: 'https://reg',
215+
}));
216+
217+
encryptAES256GCM.mockImplementation(() => {
218+
throw new Error('Encryption failed');
219+
});
220+
await expect(useProvingStore.getState()._generatePayload()).rejects.toThrow(
221+
'Encryption failed',
222+
);
223+
});
166224
});

0 commit comments

Comments
 (0)