Skip to content

Commit ed43264

Browse files
test: update tests
1 parent 1b337aa commit ed43264

File tree

1 file changed

+149
-114
lines changed

1 file changed

+149
-114
lines changed

tests/e2e/fetchUserContacts.test.ts

Lines changed: 149 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -42,120 +42,155 @@ describe('web3mail.fetchMyContacts()', () => {
4242
await waitSubgraphIndexing();
4343
}, 4 * MAX_EXPECTED_BLOCKTIME + MAX_EXPECTED_WEB2_SERVICES_TIME);
4444

45-
it(
46-
'Tow different user should have different contacts',
47-
async () => {
48-
const user1 = Wallet.createRandom().address;
49-
const user2 = Wallet.createRandom().address;
50-
const defaultConfig = getChainDefaultConfig(DEFAULT_CHAIN_ID);
51-
expect(defaultConfig).not.toBeNull();
52-
const authorizedApp = defaultConfig!.dappAddress;
53-
54-
await dataProtector.grantAccess({
55-
authorizedApp: authorizedApp,
56-
protectedData: protectedData1.address,
57-
authorizedUser: user1,
58-
});
59-
60-
await dataProtector.grantAccess({
61-
authorizedApp: authorizedApp,
62-
protectedData: protectedData2.address,
63-
authorizedUser: user2,
64-
});
65-
66-
const contactUser1 = await web3mail.fetchUserContacts({
67-
userAddress: user1,
68-
});
69-
const contactUser2 = await web3mail.fetchUserContacts({
70-
userAddress: user2,
71-
});
72-
expect(contactUser1).not.toEqual(contactUser2);
73-
},
74-
MAX_EXPECTED_WEB2_SERVICES_TIME
75-
);
76-
77-
it(
78-
'Test that the protected data can be accessed by authorized user',
79-
async () => {
80-
const userWithAccess = Wallet.createRandom().address;
81-
const defaultConfig = getChainDefaultConfig(DEFAULT_CHAIN_ID);
82-
expect(defaultConfig).not.toBeNull();
83-
const authorizedApp = defaultConfig!.dappAddress;
84-
85-
await dataProtector.grantAccess({
86-
authorizedApp: authorizedApp,
87-
protectedData: protectedData1.address,
88-
authorizedUser: userWithAccess,
89-
});
90-
const contacts = await web3mail.fetchUserContacts({
91-
userAddress: userWithAccess,
92-
});
93-
expect(contacts.length).toBeGreaterThan(0);
94-
},
95-
MAX_EXPECTED_WEB2_SERVICES_TIME
96-
);
97-
98-
it(
99-
'should throw a protocol error',
100-
async () => {
101-
// Call getTestConfig to get the default configuration
102-
const [ethProvider, defaultOptions] = getTestConfig(wallet.privateKey);
103-
const user1 = Wallet.createRandom().address;
104-
105-
const options = {
106-
...defaultOptions,
107-
iexecOptions: {
108-
...defaultOptions.iexecOptions,
109-
iexecGatewayURL: 'https://test',
110-
},
111-
};
112-
113-
// Pass the modified options to IExecWeb3mail
114-
const invalidWeb3mail = new IExecWeb3mail(ethProvider, options);
115-
let error: WorkflowError | undefined;
116-
117-
try {
118-
await invalidWeb3mail.fetchUserContacts({
45+
describe('when no access is granted', () => {
46+
it(
47+
'should return an empty contact array',
48+
async () => {
49+
// Call getTestConfig to get the default configuration
50+
const [ethProvider, defaultOptions] = getTestConfig(wallet.privateKey);
51+
const user1 = Wallet.createRandom().address;
52+
53+
const invalidWeb3mail = new IExecWeb3mail(ethProvider, defaultOptions);
54+
55+
const contacts = await invalidWeb3mail.fetchUserContacts({
56+
userAddress: user1,
57+
});
58+
expect(contacts.length).toBe(0);
59+
},
60+
2 * MAX_EXPECTED_BLOCKTIME + MAX_EXPECTED_WEB2_SERVICES_TIME
61+
);
62+
});
63+
64+
describe('when access is granted', () => {
65+
it(
66+
'Two different user should have different contacts',
67+
async () => {
68+
const user1 = Wallet.createRandom().address;
69+
const user2 = Wallet.createRandom().address;
70+
const defaultConfig = getChainDefaultConfig(DEFAULT_CHAIN_ID);
71+
expect(defaultConfig).not.toBeNull();
72+
const authorizedApp = defaultConfig!.dappAddress;
73+
74+
await dataProtector.grantAccess({
75+
authorizedApp: authorizedApp,
76+
protectedData: protectedData1.address,
77+
authorizedUser: user1,
78+
});
79+
80+
await dataProtector.grantAccess({
81+
authorizedApp: authorizedApp,
82+
protectedData: protectedData2.address,
83+
authorizedUser: user2,
84+
});
85+
86+
const contactUser1 = await web3mail.fetchUserContacts({
11987
userAddress: user1,
12088
});
121-
} catch (err) {
122-
error = err as WorkflowError;
123-
}
124-
125-
expect(error).toBeInstanceOf(WorkflowError);
126-
expect(error?.message).toBe(
127-
"A service in the iExec protocol appears to be unavailable. You can retry later or contact iExec's technical support for help."
128-
);
129-
expect(error?.isProtocolError).toBe(true);
130-
},
131-
2 * MAX_EXPECTED_BLOCKTIME + MAX_EXPECTED_WEB2_SERVICES_TIME
132-
);
133-
134-
it(
135-
'should throw a fetchUserContacts error',
136-
async () => {
137-
// Call getTestConfig to get the default configuration
138-
const [ethProvider, defaultOptions] = getTestConfig(wallet.privateKey);
139-
140-
const options = {
141-
...defaultOptions,
142-
dataProtectorSubgraph: 'https://test',
143-
};
144-
145-
// Pass the modified options to IExecWeb3mail
146-
const invalidWeb3mail = new IExecWeb3mail(ethProvider, options);
147-
let error: WorkflowError | undefined;
148-
149-
try {
150-
await invalidWeb3mail.fetchMyContacts();
151-
} catch (err) {
152-
error = err as WorkflowError;
153-
}
154-
155-
expect(error).toBeInstanceOf(WorkflowError);
156-
expect(error?.message).toBe('Failed to fetch user contacts');
157-
expect(error?.isProtocolError).toBe(false);
158-
},
159-
2 * MAX_EXPECTED_BLOCKTIME + MAX_EXPECTED_WEB2_SERVICES_TIME
160-
);
89+
const contactUser2 = await web3mail.fetchUserContacts({
90+
userAddress: user2,
91+
});
92+
expect(contactUser1).not.toEqual(contactUser2);
93+
},
94+
MAX_EXPECTED_WEB2_SERVICES_TIME
95+
);
96+
97+
it(
98+
'Test that the protected data can be accessed by authorized user',
99+
async () => {
100+
const userWithAccess = Wallet.createRandom().address;
101+
const defaultConfig = getChainDefaultConfig(DEFAULT_CHAIN_ID);
102+
expect(defaultConfig).not.toBeNull();
103+
const authorizedApp = defaultConfig!.dappAddress;
104+
105+
await dataProtector.grantAccess({
106+
authorizedApp: authorizedApp,
107+
protectedData: protectedData1.address,
108+
authorizedUser: userWithAccess,
109+
});
110+
const contacts = await web3mail.fetchUserContacts({
111+
userAddress: userWithAccess,
112+
});
113+
expect(contacts.length).toBeGreaterThan(0);
114+
},
115+
MAX_EXPECTED_WEB2_SERVICES_TIME
116+
);
117+
});
118+
119+
describe('when iexec market API is not reachable', () => {
120+
it(
121+
'should throw a protocol error',
122+
async () => {
123+
// Call getTestConfig to get the default configuration
124+
const [ethProvider, defaultOptions] = getTestConfig(wallet.privateKey);
125+
const user1 = Wallet.createRandom().address;
126+
127+
const options = {
128+
...defaultOptions,
129+
iexecOptions: {
130+
...defaultOptions.iexecOptions,
131+
iexecGatewayURL: 'https://test',
132+
},
133+
};
134+
135+
// Pass the modified options to IExecWeb3mail
136+
const invalidWeb3mail = new IExecWeb3mail(ethProvider, options);
137+
let error: WorkflowError | undefined;
138+
139+
try {
140+
await invalidWeb3mail.fetchUserContacts({
141+
userAddress: user1,
142+
});
143+
} catch (err) {
144+
error = err as WorkflowError;
145+
}
146+
147+
expect(error).toBeInstanceOf(WorkflowError);
148+
expect(error?.message).toBe(
149+
"A service in the iExec protocol appears to be unavailable. You can retry later or contact iExec's technical support for help."
150+
);
151+
expect(error?.isProtocolError).toBe(true);
152+
},
153+
2 * MAX_EXPECTED_BLOCKTIME + MAX_EXPECTED_WEB2_SERVICES_TIME
154+
);
155+
});
156+
157+
describe('when subgraph is not reachable', () => {
158+
it(
159+
'should throw a fetchUserContacts error',
160+
async () => {
161+
// Call getTestConfig to get the default configuration
162+
const [ethProvider, defaultOptions] = getTestConfig(wallet.privateKey);
163+
164+
const defaultConfig = getChainDefaultConfig(DEFAULT_CHAIN_ID);
165+
expect(defaultConfig).not.toBeNull();
166+
const authorizedApp = defaultConfig!.dappAddress;
167+
168+
await dataProtector.grantAccess({
169+
authorizedApp: authorizedApp,
170+
protectedData: protectedData1.address,
171+
authorizedUser: ethProvider.address,
172+
});
173+
174+
const options = {
175+
...defaultOptions,
176+
dataProtectorSubgraph: 'https://test',
177+
};
178+
179+
// Pass the modified options to IExecWeb3mail
180+
const invalidWeb3mail = new IExecWeb3mail(ethProvider, options);
181+
let error: WorkflowError | undefined;
182+
183+
try {
184+
await invalidWeb3mail.fetchMyContacts();
185+
} catch (err) {
186+
error = err as WorkflowError;
187+
}
188+
189+
expect(error).toBeInstanceOf(WorkflowError);
190+
expect(error?.message).toBe('Failed to fetch user contacts');
191+
expect(error?.isProtocolError).toBe(false);
192+
},
193+
2 * MAX_EXPECTED_BLOCKTIME + MAX_EXPECTED_WEB2_SERVICES_TIME
194+
);
195+
});
161196
});

0 commit comments

Comments
 (0)