Skip to content

Commit

Permalink
Changed test behavior between anonymous and not anonymous for better …
Browse files Browse the repository at this point in the history
…testing performance
  • Loading branch information
marcvelmer committed Aug 10, 2023
1 parent 4fd6057 commit 8fc6bd5
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 483 deletions.
16 changes: 15 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,21 @@ jobs:
docker-compose -f test/integration/util/docker-compose.yml port blind-csp 5000 >> $GITHUB_ENV
- name: Integration tests
run: yarn test:integration --ci --coverage
run: yarn test:integration --ci --coverage --maxWorkers=2
env:
API_URL: http://${{ env.voconed_hostport }}/v2
BLINDCSP_URL: http://${{ env.blindcsp_hostport }}/v1
BLINDCSP_PRIVKEY: f6e530882cde11e2050989d04c3fc523412b2fc2723c0a6155932a62ffafc2b6
# that privkey is hardcoded in docker-compose.yml and corresponds to:
# address 0xc3E925C7D971601c85eB042032F415852E56A038
# CSP root key 039d3ea0d911f4497903e3701b19b9c2efa2b7c3c646f3fc63d46d0a684b781b82
BLINDCSP_PUBKEY: 039d3ea0d911f4497903e3701b19b9c2efa2b7c3c646f3fc63d46d0a684b781b82
FAUCET_URL: ${{ secrets.FAUCET_URL }}
FAUCET_AUTH_TOKEN: ${{ secrets.FAUCET_AUTH_TOKEN }}
FAUCET_TOKEN_LIMIT: 100

- name: Anonymous integration tests
run: yarn test:integration:zk --ci --coverage
env:
API_URL: http://${{ env.voconed_hostport }}/v2
BLINDCSP_URL: http://${{ env.blindcsp_hostport }}/v1
Expand Down
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,13 @@
"build": "rollup -c",
"ts-types": "tsc --emitDeclarationOnly --outDir dist",
"test": "yarn test:unit && yarn test:api && yarn test:integration",
"test:unit": "jest test/unit",
"test:integration": "jest test/integration --forceExit --runInBand",
"test:unit": "jest test/unit --verbose",
"test:integration": "yarn test:integration:account && yarn test:integration:csp && yarn test:integration:election && yarn test:integration:other",
"test:integration:account": "jest test/integration/account.test.ts",
"test:integration:csp": "jest test/integration/csp.test.ts",
"test:integration:election": "jest test/integration/election.test.ts",
"test:integration:other": "jest test/integration/other.test.ts",
"test:integration:zk": "jest test/integration/zk.test.ts --forceExit --runInBand",
"test:api": "jest test/api",
"lint": "eslint src test --ext .js,.jsx,.ts,.tsx",
"lintfix": "yarn lint --fix",
Expand Down
2 changes: 1 addition & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1124,7 +1124,7 @@ export class VocdoniSDKClient {

const isInCensus = await this.isInCensus(election.id);
if (!isInCensus) {
throw Error('Not in census');
return Promise.resolve(0);
}

return this.wallet
Expand Down
108 changes: 0 additions & 108 deletions test/integration/election.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { clientParams } from './util/client.params';
// @ts-ignore
import { waitForElectionReady } from './util/client.utils';
import { SDK_VERSION } from '../../src/version';
import { AnonymousVote } from '../../src/types/vote/anonymous';

let client: VocdoniSDKClient;
let wallet: Wallet;
Expand Down Expand Up @@ -884,111 +883,4 @@ describe('Election integration tests', () => {
expect(election.census.censusURI).toEqual(census2.censusURI);
});
}, 185000);
it('should create an anonymous election and vote successfully', async () => {
const census = new PlainCensus();
const voter1 = Wallet.createRandom();
const voter2 = Wallet.createRandom();
// User that votes with account with SIK
census.add((client.wallet as Wallet).address);
// User that votes and has no account
census.add(voter1.address);
// User that votes with account without SIK
census.add(voter2.address);

const election = createElection(census, {
anonymous: true,
});

await client.createAccount({
account: null,
faucetPackage: null,
sik: true,
password: 'password123',
});

await client
.createElection(election)
.then((electionId) => {
expect(electionId).toMatch(/^[0-9a-fA-F]{64}$/);
client.setElectionId(electionId);
return client.fetchElection();
})
.then((publishedElection) => {
expect(publishedElection.electionType.anonymous).toBeTruthy();
return waitForElectionReady(client, publishedElection.id);
})
.then(async () => {
await expect(async () => {
await client.submitVote(new Vote([0]));
}).rejects.toThrow();
const vote = new AnonymousVote([0], 'password123');
return client.submitVote(vote);
})
.then(() => {
client.wallet = voter1;
const vote = new AnonymousVote([0], 'password456');
return client.submitVote(vote);
})
.then(() => {
client.wallet = voter2;
return client.createAccount({ sik: false });
})
.then(() => {
const vote = new Vote([1]);
return client.submitVote(vote);
});
}, 285000);
it('should create an anonymous election with 12 participants and each of them should vote correctly', async () => {
const numVotes = 12; // should be even number
const census = new PlainCensus();

const participants: Wallet[] = [...new Array(numVotes)].map(() => Wallet.createRandom());
census.add(participants.map((participant) => participant.address));

const election = createElection(census, {
anonymous: true,
});

await client.createAccount();

await client
.createElection(election)
.then((electionId) => {
expect(electionId).toMatch(/^[0-9a-fA-F]{64}$/);
client.setElectionId(electionId);
return waitForElectionReady(client, electionId);
})
.then(async () => {
for (let i = 0; i < participants.length; i++) {
client.wallet = participants[i];
let vote: Vote | AnonymousVote = new Vote([i % 2]);

if (i % 3 == 0) {
await client.createAccount({
account: null,
faucetPackage: null,
sik: true,
password: participants[i].address,
});
vote = new AnonymousVote([i % 2], participants[i].address);
} else if (i % 3 == 1) {
await client.createAccount({ sik: false });
}
const isInCensus = await client.isInCensus();
expect(isInCensus).toBeTruthy();
await expect(async () => {
await client.hasAlreadyVoted();
}).rejects.toThrow();
await expect(async () => {
await client.isAbleToVote();
}).rejects.toThrow();
await client.submitVote(vote);
}
})
.then(() => client.fetchElection())
.then((election) => {
expect(election.electionType.anonymous).toBeTruthy();
expect(election.voteCount).toEqual(numVotes);
});
}, 720000);
});
Loading

0 comments on commit 8fc6bd5

Please sign in to comment.