Skip to content

Commit

Permalink
Merge pull request #120 from c4dt/playwright
Browse files Browse the repository at this point in the history
Add remaining ballots tests
  • Loading branch information
PascalinDe authored Feb 16, 2024
2 parents 1a32df8 + a0abffd commit 9e6df75
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 2 deletions.
78 changes: 76 additions & 2 deletions web/frontend/tests/ballot.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@ import { expect, test } from '@playwright/test';
import { default as i18n } from 'i18next';
import { assertHasFooter, assertHasNavBar, initI18n, logIn, setUp } from './shared';
import { FORMID } from './mocks/shared';
import { SCIPER_ADMIN, SCIPER_OTHER_USER, SCIPER_USER, mockPersonalInfo } from './mocks/api';
import {
SCIPER_ADMIN,
SCIPER_OTHER_USER,
SCIPER_USER,
mockFormsVote,
mockPersonalInfo,
} from './mocks/api';
import { mockFormsFormID } from './mocks/evoting';
import Form from './json/evoting/forms/open.json';

initI18n();

Expand Down Expand Up @@ -43,7 +50,74 @@ test('Assert ballot form is correctly handled for anonymous users, non-voter use
await page.goto(`/ballot/show/${FORMID}`, { waitUntil: 'networkidle' });
await expect(page).toHaveURL(`/ballot/show/${FORMID}`);
await expect(castVoteButton).toBeVisible();
await expect(page.getByText(i18n.t('vote'))).toBeVisible();
await expect(page.getByText(i18n.t('vote'), { exact: true })).toBeVisible();
await expect(page.getByText(i18n.t('voteExplanation'))).toBeVisible();
});
});

test('Assert ballot is displayed properly', async ({ page }) => {
const content = await page.getByTestId('content');
// TODO integrate localisation
i18n.changeLanguage('en'); // force 'en' for this test
await expect(content.locator('xpath=./div/div[3]/h3')).toContainText(Form.Configuration.Title.En);
const scaffold = Form.Configuration.Scaffold.at(0);
await expect(content.locator('xpath=./div/div[3]/div/div/h3')).toContainText(scaffold.Title.En);
const select = scaffold.Selects.at(0);
await expect(
content.locator('xpath=./div/div[3]/div/div/div/div/div/div[1]/div/h3')
).toContainText(select.Title.En);
await expect(
page.getByText(i18n.t('selectBetween', { minSelect: select.MinN, maxSelect: select.MaxN }))
).toBeVisible();
for (const choice of select.Choices.map((x) => JSON.parse(x))) {
await expect(page.getByRole('checkbox', { name: choice.en })).toBeVisible();
}
i18n.changeLanguage(); // unset language for the other tests
});

test('Assert minimum/maximum number of choices are handled correctly', async ({ page }) => {
const castVoteButton = await page.getByRole('button', { name: i18n.t('castVote') });
const select = Form.Configuration.Scaffold.at(0).Selects.at(0);
await test.step(
`Assert minimum number of choices (${select.MinN}) are handled correctly`,
async () => {
await castVoteButton.click();
await expect(
page.getByText(
i18n.t('minSelectError', { min: select.MinN, singularPlural: i18n.t('singularAnswer') })
)
).toBeVisible();
}
);
await test.step(
`Assert maximum number of choices (${select.MaxN}) are handled correctly`,
async () => {
for (const choice of select.Choices.map((x) => JSON.parse(x))) {
await page.getByRole('checkbox', { name: choice.en }).setChecked(true);
}
await castVoteButton.click();
await expect(page.getByText(i18n.t('maxSelectError', { max: select.MaxN }))).toBeVisible();
}
);
});

test('Assert that correct number of choices are accepted', async ({ page, baseURL }) => {
await mockFormsVote(page);
page.waitForRequest(async (request) => {
const body = await request.postDataJSON();
return (
request.url() === `${baseURL}/api/evoting/forms/${FORMID}/vote` &&
request.method() === 'POST' &&
body.UserID === null &&
body.Ballot.length === 1 &&
body.Ballot.at(0).K.length === 32 &&
body.Ballot.at(0).C.length === 32
);
});
await page
.getByRole('checkbox', {
name: JSON.parse(Form.Configuration.Scaffold.at(0).Selects.at(0).Choices.at(0)).en,
})
.setChecked(true);
await page.getByRole('button', { name: i18n.t('castVote') }).click();
});
16 changes: 16 additions & 0 deletions web/frontend/tests/mocks/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ export async function mockForms(page: page) {
});
}

export async function mockFormsVote(page: page) {
await page.route(`/api/evoting/forms/${FORMID}/vote`, async (route) => {
if (route.request().method() === 'POST') {
await route.fulfill({
status: 200,
contentType: 'application/json',
body: {
Status: 0,
Token:
'eyJTdGF0dXMiOjAsIlRyYW5zYWN0aW9uSUQiOiJQQWluaEVjNVNzM2JiVWkxbldNWU55dWdPVkFpdVZ3YklZcGpKTFJ1SUdnPSIsIkxhc3RCbG9ja0lkeCI6NSwiVGltZSI6MTcwODAxNDgyMSwiSGFzaCI6ImtVT3g3Ykw0eC9IYXdwanppTityTVFIL3Fmb1pnRHBFUFc3S2tzRWl1TTA9IiwiU2lnbmF0dXJlIjoiZXlKT1lXMWxJam9pUWt4VExVTlZVbFpGTFVKT01qVTJJaXdpUkdGMFlTSTZJbU00UjFwUVYyWjZTRlpqT1dGV1dWaGhTbEUwWVhKT1UyRTRVbXB2VEVOSGIyZFBlbWRpVDFKSlYxVnVSbkE0ZFdaemQyMWlVVXA2ZWpScWJHNVFRa3QzUzJwWmVEaDJkVmgzZUhCWE5FeE1hVFZWUkRsUlBUMGlmUT09In0=',
},
});
}
});
}

// /api/config

export async function mockProxy(page: page) {
Expand Down

0 comments on commit 9e6df75

Please sign in to comment.