Skip to content

Commit

Permalink
updating e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
krpeacock committed Oct 20, 2023
1 parent fa18735 commit 06f5845
Show file tree
Hide file tree
Showing 11 changed files with 224 additions and 70 deletions.
8 changes: 6 additions & 2 deletions e2e/browser/cypress/e2e/ecdsa.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ const canisterId = ids.whoami.local;

const setup = async () => {
const identity1 = await ECDSAKeyIdentity.generate();
const whoami1 = createActor(ids.whoami.local, { agentOptions: { identity: identity1 } });
const whoami1 = createActor(ids.whoami.local, {
agentOptions: { verifyQuerySignatures: false, identity: identity1 },
});

const principal1 = await whoami1.whoami();

Expand Down Expand Up @@ -34,7 +36,9 @@ describe('ECDSAKeyIdentity tests with SubtleCrypto', () => {

const identity2 = await ECDSAKeyIdentity.fromKeyPair(storedKeyPair);

const whoami2 = createActor(canisterId, { agentOptions: { identity: identity2 } });
const whoami2 = createActor(canisterId, {
agentOptions: { verifyQuerySignatures: false, identity: identity2 },
});

const principal2 = await whoami2.whoami();

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 6 additions & 2 deletions e2e/node/basic/canisterStatus.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CanisterStatus, HttpAgent } from '@dfinity/agent';
import { Principal } from '@dfinity/principal';
import counter from '../canisters/counter';
import { makeAgent } from '../utils/agent';

jest.setTimeout(30_000);
afterEach(async () => {
Expand All @@ -9,7 +10,7 @@ afterEach(async () => {
describe('canister status', () => {
it('should fetch successfully', async () => {
const counterObj = await (await counter)();
const agent = new HttpAgent({ host: `http://localhost:${process.env.REPLICA_PORT}` });
const agent = await makeAgent();
await agent.fetchRootKey();
const request = await CanisterStatus.request({
canisterId: Principal.from(counterObj.canisterId),
Expand All @@ -21,7 +22,10 @@ describe('canister status', () => {
});
it('should throw an error if fetchRootKey has not been called', async () => {
const counterObj = await (await counter)();
const agent = new HttpAgent({ host: `http://localhost:${process.env.REPLICA_PORT}` });
const agent = new HttpAgent({
host: `http://localhost:${process.env.REPLICA_PORT ?? 4943}`,
verifyQuerySignatures: false,
});
const shouldThrow = async () => {
// eslint-disable-next-line no-useless-catch
try {
Expand Down
66 changes: 47 additions & 19 deletions e2e/node/basic/identity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
ECDSAKeyIdentity,
} from '@dfinity/identity';
import { Secp256k1KeyIdentity } from '@dfinity/identity-secp256k1';
import agent from '../utils/agent';
import agent, { makeAgent } from '../utils/agent';
import identityCanister from '../canisters/identity';

function createIdentity(seed: number): SignIdentity {
Expand All @@ -33,7 +33,7 @@ async function createIdentityActor(
idl: IDL.InterfaceFactory,
): Promise<any> {
const identity = createIdentity(seed);
const agent1 = new HttpAgent({ source: await agent, identity });
const agent1 = await makeAgent({ identity });
return Actor.createActor(idl, {
canisterId,
agent: agent1,
Expand All @@ -52,7 +52,7 @@ async function createSecp256k1IdentityActor(
}

const identity = Secp256k1KeyIdentity.generate(seed1);
const agent1 = new HttpAgent({ source: await agent, identity });
const agent1 = await makeAgent({ identity });
return Actor.createActor(idl, {
canisterId,
agent: agent1,
Expand All @@ -70,7 +70,9 @@ async function createEcdsaIdentityActor(
} else {
effectiveIdentity = await ECDSAKeyIdentity.generate();
}
const agent1 = new HttpAgent({ source: await agent, identity: effectiveIdentity });
const agent1 = await makeAgent({
identity: effectiveIdentity,
});
return Actor.createActor(idl, {
canisterId,
agent: agent1,
Expand Down Expand Up @@ -147,15 +149,19 @@ test('delegation: principal is the same between delegated keys with secp256k1',

const identityActor1 = Actor.createActor(idl, {
canisterId,
agent: new HttpAgent({ source: await agent, identity: masterKey }),
agent: await makeAgent({
identity: masterKey,
}),
}) as any;
const identityActor2 = Actor.createActor(idl, {
canisterId,
agent: new HttpAgent({ source: await agent, identity: sessionKey }),
agent: await makeAgent({
identity: sessionKey,
}),
}) as any;
const identityActor3 = Actor.createActor(idl, {
canisterId,
agent: new HttpAgent({ source: await agent, identity: id3 }),
agent: await makeAgent({ identity: id3 }),
}) as any;

const principal1 = await identityActor1.whoami_query();
Expand All @@ -178,15 +184,19 @@ test('delegation: principal is the same between delegated keys', async () => {

const identityActor1 = Actor.createActor(idl, {
canisterId,
agent: new HttpAgent({ source: await agent, identity: masterKey }),
agent: await makeAgent({
identity: masterKey,
}),
}) as any;
const identityActor2 = Actor.createActor(idl, {
canisterId,
agent: new HttpAgent({ source: await agent, identity: sessionKey }),
agent: await makeAgent({
identity: sessionKey,
}),
}) as any;
const identityActor3 = Actor.createActor(idl, {
canisterId,
agent: new HttpAgent({ source: await agent, identity: id3 }),
agent: await makeAgent({ identity: id3 }),
}) as any;

const principal1 = await identityActor1.whoami_query();
Expand Down Expand Up @@ -215,19 +225,27 @@ test('delegation: works with 3 keys', async () => {

const identityActorBottom = Actor.createActor(idl, {
canisterId,
agent: new HttpAgent({ source: await agent, identity: bottomKey }),
agent: await makeAgent({
identity: bottomKey,
}),
}) as any;
const identityActorMiddle = Actor.createActor(idl, {
canisterId,
agent: new HttpAgent({ source: await agent, identity: middleKey }),
agent: await makeAgent({
identity: middleKey,
}),
}) as any;
const identityActorRoot = Actor.createActor(idl, {
canisterId,
agent: new HttpAgent({ source: await agent, identity: rootKey }),
agent: await makeAgent({
identity: rootKey,
}),
}) as any;
const identityActorDelegated = Actor.createActor(idl, {
canisterId,
agent: new HttpAgent({ source: await agent, identity: idDelegated }),
agent: await makeAgent({
identity: idDelegated,
}),
}) as any;

const principalBottom = await identityActorBottom.whoami_query();
Expand Down Expand Up @@ -267,23 +285,33 @@ test('delegation: works with 4 keys', async () => {

const identityActorBottom = Actor.createActor(idl, {
canisterId,
agent: new HttpAgent({ source: await agent, identity: bottomKey }),
agent: await makeAgent({
identity: bottomKey,
}),
}) as any;
const identityActorMiddle = Actor.createActor(idl, {
canisterId,
agent: new HttpAgent({ source: await agent, identity: middleKey }),
agent: await makeAgent({
identity: middleKey,
}),
}) as any;
const identityActorMiddle2 = Actor.createActor(idl, {
canisterId,
agent: new HttpAgent({ source: await agent, identity: middle2Key }),
agent: await makeAgent({
identity: middle2Key,
}),
}) as any;
const identityActorRoot = Actor.createActor(idl, {
canisterId,
agent: new HttpAgent({ source: await agent, identity: rootKey }),
agent: await makeAgent({
identity: rootKey,
}),
}) as any;
const identityActorDelegated = Actor.createActor(idl, {
canisterId,
agent: new HttpAgent({ source: await agent, identity: idDelegated }),
agent: await makeAgent({
identity: idDelegated,
}),
}) as any;

const principalBottom = await identityActorBottom.whoami_query();
Expand Down
114 changes: 114 additions & 0 deletions e2e/node/basic/mainnet.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { Actor, AnonymousIdentity, HttpAgent, Identity } from '@dfinity/agent';
import { IDL } from '@dfinity/candid';
import { Ed25519KeyIdentity } from '@dfinity/identity';
import { Principal } from '@dfinity/principal';

const createWhoamiActor = (identity: Identity) => {
const canisterId = 'ivcos-eqaaa-aaaab-qablq-cai';
const idlFactory = () => {
return IDL.Service({
whoami: IDL.Func([], [IDL.Principal], ['query']),
});
};
jest.useFakeTimers();
new Date(Date.now());

const agent = new HttpAgent({ host: 'https://icp-api.io', fetch: fetch, identity });

return Actor.createActor(idlFactory, {
agent,
canisterId,
});
};

describe('certified query', () => {
it('should verify a query certificate', async () => {
const actor = createWhoamiActor(new AnonymousIdentity());

const result = await actor.whoami();

expect(result).toBeInstanceOf(Principal);
});
jest.setTimeout(100_000);
it('should verify lots of query certificates', async () => {
let count = 0;
const identities = Array.from({ length: 20 }).map(() => {
const newIdentity = Ed25519KeyIdentity.generate(new Uint8Array(32).fill(count));
count++;
return newIdentity;
});
const actors = identities.map(createWhoamiActor);
const promises = actors.map(actor => actor.whoami());

const results = await Promise.all(promises);
results.forEach(result => {
expect(result).toBeInstanceOf(Principal);
});
expect(results.length).toBe(20);

expect(results).toMatchInlineSnapshot(`
Array [
Object {
"__principal__": "535yc-uxytb-gfk7h-tny7p-vjkoe-i4krp-3qmcl-uqfgr-cpgej-yqtjq-rqe",
},
Object {
"__principal__": "wf3fv-4c4nr-7ks2b-xa4u7-kf3no-32glf-lf7e4-4ng4a-wwtlu-a2vnq-nae",
},
Object {
"__principal__": "52mr2-fw2ng-2ofst-7jekz-xbymo-3ysz7-itwdk-bgstz-r7g4g-oz5vi-pqe",
},
Object {
"__principal__": "skpwg-42fe4-eyep5-nfyz7-66wvg-hthea-q3eek-vonbv-5wpxs-nxhmh-fqe",
},
Object {
"__principal__": "ghaya-cncjm-ntxgt-af5pp-6hzsz-tvwlv-hrlfc-ocq3t-ai7vk-vyixr-cqe",
},
Object {
"__principal__": "ebtho-zkeqd-ebs74-f5if3-lk6js-3a5q5-37xt4-3ylns-h7r4n-tkhly-aqe",
},
Object {
"__principal__": "5zqn5-627q2-spx2w-mt6bm-llsnz-gipvv-5femn-3box4-vzuh6-bn723-sae",
},
Object {
"__principal__": "tek7g-2zmny-nzjwg-ansf7-rkxv6-z32x6-3flbb-ous5d-pygjx-wkhlc-jae",
},
Object {
"__principal__": "jjn6g-sh75l-r3cxb-wxrkl-frqld-6p6qq-d4ato-wske5-op7s5-n566f-bqe",
},
Object {
"__principal__": "447dk-byguq-fqkfn-7h4r6-lpk74-itbnh-mkutj-m6tmy-igaff-hmfel-cqe",
},
Object {
"__principal__": "muo3f-ines5-bxbwm-6wi5e-z663m-3zte2-r7d4x-pleey-xqvxt-scwc5-jae",
},
Object {
"__principal__": "snzff-yj2qd-fjns7-lqhvw-rsgq7-tohk2-fjnw4-uq3d6-wtk56-pxgry-mqe",
},
Object {
"__principal__": "pb54o-aqais-24v7j-msopl-bqeuv-paefp-vuoqc-gkezk-grujb-oetl6-sae",
},
Object {
"__principal__": "bf37n-7ybos-wmqt6-yiov5-24q4m-ajpjk-madkr-snuj2-ngruk-tspnh-aqe",
},
Object {
"__principal__": "tbjmy-vay5e-hqvv6-sval4-zfxmm-aii6f-b7p55-hwtz5-dejtl-qcuh2-aqe",
},
Object {
"__principal__": "7d3pe-dh4ov-fp5xz-nctjc-5rduh-gzv3t-5ioyh-4dvx3-x4lgj-hz63t-dqe",
},
Object {
"__principal__": "37axv-sazcg-75pi3-owhxr-kollq-xnzjz-zfxsv-nzdbp-yaelp-shcul-jae",
},
Object {
"__principal__": "r772c-4dz5f-rpg4e-qzxgg-7bxlb-67zpu-bitgb-vsx7k-mmagd-6zk3d-4qe",
},
Object {
"__principal__": "knkon-d3kx7-du4wt-r2fo6-uwc5a-hwhkk-m7snf-nxfhu-6bhgb-k6dn5-wae",
},
Object {
"__principal__": "entn3-mas6a-37smu-vadtg-wgsno-zokif-vyphu-umase-lfwqe-dmcrk-kae",
},
]
`);
});
});
18 changes: 7 additions & 11 deletions e2e/node/canisters/counter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { IDL } from '@dfinity/candid';
import { Principal } from '@dfinity/principal';
import { readFileSync } from 'fs';
import path from 'path';
import agent, { port, identity } from '../utils/agent';
import agent, { port, identity, makeAgent } from '../utils/agent';

let cache: {
canisterId: Principal;
Expand Down Expand Up @@ -52,15 +52,9 @@ export async function noncelessCanister(): Promise<{
actor: any;
}> {
const module = readFileSync(path.join(__dirname, 'counter.wasm'));
const disableNonceAgent = await Promise.resolve(
new HttpAgent({
host: 'http://127.0.0.1:' + port,
identity,
disableNonce: true,
}),
).then(async agent => {
await agent.fetchRootKey();
return agent;
const disableNonceAgent = await makeAgent({
identity,
disableNonce: true,
});

const canisterId = await Actor.createCanister({ agent: disableNonceAgent });
Expand All @@ -84,7 +78,9 @@ export async function noncelessCanister(): Promise<{

export const createActor = async (options?: HttpAgentOptions) => {
const module = readFileSync(path.join(__dirname, 'counter.wasm'));
const agent = new HttpAgent({ host: `http://localhost:${process.env.REPLICA_PORT}`, ...options });
const agent = await makeAgent({
...options,
});
await agent.fetchRootKey();

const canisterId = await Actor.createCanister({ agent });
Expand Down
21 changes: 13 additions & 8 deletions e2e/node/utils/agent.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import { HttpAgent } from '@dfinity/agent';
import { HttpAgent, HttpAgentOptions } from '@dfinity/agent';
import { Ed25519KeyIdentity } from '@dfinity/identity';

export const identity = Ed25519KeyIdentity.generate();
export const principal = identity.getPrincipal();

export const port = parseInt(process.env['REPLICA_PORT'] || '', 10);
export const port = parseInt(process.env['REPLICA_PORT'] || '4943', 10);
if (Number.isNaN(port)) {
throw new Error('The environment variable REPLICA_PORT is not a number.');
}

const agent = Promise.resolve(new HttpAgent({ host: 'http://127.0.0.1:' + port, identity })).then(
async agent => {
await agent.fetchRootKey();
return agent;
},
);
export const makeAgent = async (options?: HttpAgentOptions) => {
const agent = new HttpAgent({
host: `http://localhost:${process.env.REPLICA_PORT ?? 4943}`,
verifyQuerySignatures: false,
...options,
});
await agent.fetchRootKey();
return agent;
};

const agent = makeAgent();

export default agent;
Loading

0 comments on commit 06f5845

Please sign in to comment.