Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add remaining ballots tests #120

Merged
merged 2 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this on the agenda? Or a "Far in the future" TODO?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a reminder that this test is not taking into account localisation (there is a ticket for testing the localisation in general)

so it is a "if/when you implement the tests for the localisation, do not forget to adapt this one" as I took care to make all the other tests are localisation-independent

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
Loading