Skip to content

Commit f6f4279

Browse files
committed
add validation unit test for consumeProtectedData
1 parent 57bb49c commit f6f4279

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import { describe, expect, it, jest } from '@jest/globals';
2+
import { ValidationError } from 'yup';
3+
import { consumeProtectedData } from '../../../src/lib/dataProtectorSharing/consumeProtectedData.js';
4+
import { getRandomAddress, getRequiredFieldMessage } from '../../test-utils.js';
5+
import { DEFAULT_SHARING_CONTRACT_ADDRESS } from '../../../src/config/config.js';
6+
7+
describe('consumeProtectedData', () => {
8+
describe('Check validation for input parameters', () => {
9+
describe('When protected data is NOT given', () => {
10+
it('should throw a yup ValidationError with the correct message', async () => {
11+
const missingProtectedData = undefined;
12+
13+
await expect(
14+
consumeProtectedData({
15+
// @ts-expect-error No need for iexec here
16+
iexec: {},
17+
sharingContractAddress: DEFAULT_SHARING_CONTRACT_ADDRESS,
18+
protectedData: missingProtectedData,
19+
app: getRandomAddress(),
20+
})
21+
).rejects.toThrow(
22+
new ValidationError(getRequiredFieldMessage('protectedData'))
23+
);
24+
});
25+
});
26+
27+
describe('When given protected data is NOT valid', () => {
28+
it('should throw a yup ValidationError with the correct message', async () => {
29+
const invalidProtectedData = '0x123456...';
30+
31+
await expect(
32+
consumeProtectedData({
33+
// @ts-expect-error No need for iexec here
34+
iexec: {},
35+
sharingContractAddress: DEFAULT_SHARING_CONTRACT_ADDRESS,
36+
protectedData: invalidProtectedData,
37+
app: getRandomAddress(),
38+
})
39+
).rejects.toThrow(
40+
new ValidationError(
41+
'protectedData should be an ethereum address or a ENS name'
42+
)
43+
);
44+
});
45+
});
46+
47+
describe('When app address is NOT given', () => {
48+
it('should throw a yup ValidationError with the correct message', async () => {
49+
const missingAppAddress = undefined;
50+
51+
await expect(
52+
consumeProtectedData({
53+
// @ts-expect-error No need for iexec here
54+
iexec: {},
55+
sharingContractAddress: DEFAULT_SHARING_CONTRACT_ADDRESS,
56+
protectedData: getRandomAddress(),
57+
app: missingAppAddress,
58+
})
59+
).rejects.toThrow(new ValidationError(getRequiredFieldMessage('app')));
60+
});
61+
});
62+
63+
describe('When given app address is NOT valid', () => {
64+
it('should throw a yup ValidationError with the correct message', async () => {
65+
const invalidAppAddress = '0x123456...';
66+
67+
await expect(
68+
consumeProtectedData({
69+
// @ts-expect-error No need for iexec here
70+
iexec: {},
71+
sharingContractAddress: DEFAULT_SHARING_CONTRACT_ADDRESS,
72+
protectedData: getRandomAddress(),
73+
app: invalidAppAddress,
74+
})
75+
).rejects.toThrow(
76+
new ValidationError('app should be an ethereum address or a ENS name')
77+
);
78+
});
79+
});
80+
81+
describe('When maxPrice is not a positive number', () => {
82+
it('should throw a yup ValidationError with the correct message', async () => {
83+
const invalidMaxPrice = -1;
84+
85+
await expect(
86+
consumeProtectedData({
87+
// @ts-expect-error No need for iexec here
88+
iexec: {},
89+
sharingContractAddress: DEFAULT_SHARING_CONTRACT_ADDRESS,
90+
protectedData: getRandomAddress(),
91+
app: getRandomAddress(),
92+
maxPrice: invalidMaxPrice,
93+
})
94+
).rejects.toThrow(
95+
new ValidationError('maxPrice must be greater than or equal to 0')
96+
);
97+
});
98+
});
99+
100+
describe('When path is not a valid string', () => {
101+
it('should throw a yup ValidationError with the correct message', async () => {
102+
const invalidPath = 42;
103+
104+
await expect(
105+
consumeProtectedData({
106+
// @ts-expect-error No need for iexec here
107+
iexec: {},
108+
sharingContractAddress: DEFAULT_SHARING_CONTRACT_ADDRESS,
109+
protectedData: getRandomAddress(),
110+
app: getRandomAddress(),
111+
// @ts-expect-error Type 'number' is not assignable to type 'string'
112+
path: invalidPath,
113+
})
114+
).rejects.toThrow(new ValidationError('path should be a string'));
115+
});
116+
});
117+
});
118+
});

0 commit comments

Comments
 (0)