Skip to content

Commit

Permalink
Test: Implement 0-3A Download CSV of voting history
Browse files Browse the repository at this point in the history
All the results of voting, information about the voter, and how they voted should be downloadable in csv format at any time
  • Loading branch information
Sital999 committed Nov 20, 2024
1 parent f91245b commit 1b9c38e
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 13 deletions.
14 changes: 14 additions & 0 deletions integration_test/lib/helpers/file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import fs = require('fs');
const Papa = require('papaparse');

export function getCSVResults(filePath: string) {
return new Promise((resolve, reject) => {
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
return reject(err); // Reject the promise on error
}
const parsed = Papa.parse(data, { header: true });
resolve(parsed.data); // Resolve the promise with parsed data
});
});
}
20 changes: 20 additions & 0 deletions integration_test/lib/pages/representativesPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ export default class RepresentativesPage {

async getRepresentativeId(isDelegate: boolean = false): Promise<string> {
await expect(this.page.getByRole('row').first()).toBeVisible();
await expect(
this.page
.locator('[data-id="1"]')
.filter({ has: this.page.getByTestId('edit-active-voter-1') })
.locator(
`[data-testid^="${isDelegate ? 'delegate' : 'alternate'}-name-"]`
)
).toBeVisible();
const representativeTestId = await this.page
.locator('[data-id="1"]')
.filter({ has: this.page.getByTestId('edit-active-voter-1') })
Expand All @@ -99,4 +107,16 @@ export default class RepresentativesPage {
.getAttribute('data-testid');
return representativeTestId.split('-').pop();
}

async getActiveVoterId(workshopId = 1): Promise<string> {
await expect(
this.page
.locator(`[data-id="${workshopId}"]`)
.locator('[data-field="active_voter_cell"]')
).toBeVisible();
return await this.page
.locator(`[data-id="${workshopId}"]`)
.locator('[data-field="active_voter_cell"]')
.getAttribute('data-testid');
}
}
8 changes: 7 additions & 1 deletion integration_test/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion integration_test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"@cardanoapi/cardano-test-wallet": "^2.1.1",
"@faker-js/faker": "^9.2.0",
"csv-parse": "^5.5.6",
"libcardano": "^1.4.6"
"libcardano": "^1.4.6",
"papaparse": "^5.4.1"
}
}
62 changes: 60 additions & 2 deletions integration_test/tests/0-common/comon.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { setAllureEpic } from '@helpers/allure';
import { expect, Locator, Page } from '@playwright/test';
import { test } from '@fixtures/poll';
import { forEachUser, getUserPages } from '@helpers/userRoles';
import path = require('path');
import fs = require('fs');
import { getCSVResults } from '@helpers/file';

test.beforeEach(async () => {
await setAllureEpic('0. All Users');
Expand Down Expand Up @@ -183,7 +186,7 @@ test.describe('User profile', () => {
* Acceptance Criteria 2: Given that I am on the results page of a closed poll, when I press on the tile of a given voter, then I am taken to their profile page and can see their voting record
*/

test('0-2A-1. Can navigate to user profile from delegate/alternate listing page', async ({
test('0-2A-2. Can navigate to user profile from delegate/alternate listing page', async ({
page,
}) => {
await page.goto('/');
Expand All @@ -206,7 +209,7 @@ test.describe('User profile', () => {
// should navigate to /representatives/xx
});

test('0-2A-1. Can navigate to user profile from voter view in poll results page', async ({
test('0-2A-3. Can navigate to user profile from voter view in poll results page', async ({
page,
pollId,
}) => {
Expand All @@ -227,3 +230,58 @@ test.describe('User profile', () => {
await page.waitForURL(/\/representatives\/\d+$/);
});
});

/**
* Description: All the results of voting, information about the voter, and how they voted should be downloadable in csv format at any time.
*
* User Story: As an Obsever I want to download the results in CSV format so that I can audit them
*
* Acceptance Criteria: Given that I am on the page of a delegate or alternate, when I click "Download Votes" button then a CSV of my voting history is downloaded to my device.
*
*/

test.describe('CSV File', () => {
test.use({ pollType: 'VotedPoll' });

test('0-3A. Can download CSV of voting history', async ({
page,
pollId,
browser,
}) => {
test.slow();
const pages = await getUserPages(browser);
await Promise.all(
pages.map(async (page, index) => {
await page.goto('/representatives/13');
// Trigger the download
const [download] = await Promise.all([
page.waitForEvent('download'), // Wait for the download event
await expect(page.getByTestId('download-user-votes-btn')).toBeVisible(
{ timeout: 10_000 }
),
await page.getByTestId('download-user-votes-btn').click(),
]);

// Save the downloaded file
const filePath = path.join(
__dirname,
`convention_voting_app_${index}.csv`
);
await download.saveAs(filePath);

// Verify the file exists
expect(fs.existsSync(filePath)).toBeTruthy();

// fetch csv results
const pollResults = await getCSVResults(filePath);

// assert vote result
expect(pollResults[0].poll).toBeTruthy();
expect(pollResults[0].vote).toBe('yes');

// Clean up (optional)
fs.unlinkSync(filePath);
})
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ test.describe('Open Close Poll', () => {
await pollPage.beginVoteBtn.click();

await expect(page.getByText('Poll voting is open!')).toBeVisible();
await expect(pollPage.closeVoteBtn).toBeVisible();
await expect(pollPage.closeVoteBtn).toBeVisible({ timeout: 10_000 });
});

/**
Expand Down Expand Up @@ -493,10 +493,7 @@ test.describe('Voting Power', () => {
await representativePage.goto();

// fetch active voter id before switching active voting power
const previousActiveVoterId = await page
.locator('[data-id="1"]')
.locator('[data-field="active_voter_cell"]')
.getAttribute('data-testid');
const previousActiveVoterId = await representativePage.getActiveVoterId();

// fetch delegate and alternate id of same row
const representativesIds = await Promise.all([
Expand All @@ -510,10 +507,7 @@ test.describe('Voting Power', () => {
await expect(page.getByText('Active voter updated!')).toBeVisible({
timeout: 10_000,
});
const currentActiveVoterId = await page
.locator('[data-id="1"]')
.locator('[data-field="active_voter_cell"]')
.getAttribute('data-testid');
const currentActiveVoterId = await representativePage.getActiveVoterId();

expect(representativesIds).toEqual(
expect.arrayContaining([currentActiveVoterId, previousActiveVoterId])
Expand Down

0 comments on commit 1b9c38e

Please sign in to comment.