Skip to content

Commit

Permalink
Add E2E tests for normal voting
Browse files Browse the repository at this point in the history
  • Loading branch information
LudvigHz committed Oct 9, 2021
1 parent 36dcae9 commit 705d528
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 38 deletions.
2 changes: 1 addition & 1 deletion app/views/partials/election.pug
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@

// -------------------------------------------------------------------
button.btn.btn-lg.btn-default(type='button', ng-click='confirm()') {{ priorities.length === 0 ? "Stem Blank" : "Avgi stemme" }}
button.btn.btn-lg.btn-default(type='button', ng-click='confirm()') {{ priorities.length === 0 ? "Stem blank" : "Avgi stemme" }}

div

Expand Down
37 changes: 25 additions & 12 deletions features/election.feature
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,38 @@ Feature: Election
Given I am logged in

Scenario: Find election
Given There is an active election
Given There is an active "stv" election
When I go to page "/"
Then I see an active election

Scenario: Hide election after vote
Given There is an active election
Scenario: Hide election after vote (STV)
Given There is an active "stv" election
And I have voted on the election
When I go to page "/"
Then I see "Ingen aktive avstemninger."

Scenario: Voting on one alternative
Given There is an active election
Scenario: Voting on one alternative (STV)
Given There is an active "stv" election
When I vote on an election
Then I see alert "Takk for din stemme!"

Scenario: Voting on a NORMAL election
Given There is an active "normal" election
When I select "test alternative"
Then Button "Avgi stemme" should not be disabled
When I deselect "test alternative"
Then Button "Stem blank" should not be disabled
When I select "another test alternative"
When I submit the vote
Then I have 1 alternative on the confirmation ballot
And I see "another test alternative" as priority 1 on the confirmation ballot
When I confirm the vote
Then I see "Takk for din stemme!"

Scenario: Prioritizing alternatives
Given There is an active "stv" election
When I select "another test alternative"
When I select "test alternative"
And I select "test alternative"
Then I see "another test alternative" as priority 1
And I see "test alternative" as priority 2
When I submit the vote
Expand All @@ -34,8 +47,8 @@ Feature: Election
Then I see alert "Takk for din stemme!"

Scenario: Voting blank
Given There is an active election
When I submit the vote
Given There is an active "stv" election
When I click "Stem blank"
Then I see "Blank stemme" in ".ballot h3"
When I confirm the vote
Then I see alert "Takk for din stemme!"
Expand All @@ -45,15 +58,15 @@ Feature: Election

Scenario: Retrieve vote from localStorage
Given There is an active "stv" election
And I have voted on the election
And I am on page "/retrieve"
When I vote on an election
Given I am on page "/retrieve"
Then I see my hash in "voteHash"
When I submit the form
Then I see "Din prioritering på: activeElectionSTV" in ".vote-result-feedback h3"
And I see "test alternative" as priority 1 on the receipt

Scenario: Retrieve vote with invalid hash
Given There is an active election
Given There is an active "normal" election
And I am on page "/retrieve"
And I fill in "voteHash" with "invalidhash"
When I submit the form
Expand All @@ -65,7 +78,7 @@ Feature: Election
Then I see "Ingen aktive avstemninger."

Scenario: Show alert to inactive users
Given There is an active election
Given There is an active "normal" election
And There is an inactive user with card key "1234"
When I vote on an election
Then I see alert "Brukeren din er deaktivert, vennligst henvend deg til skranken."
64 changes: 42 additions & 22 deletions features/step_definitions/electionSteps.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,31 @@ by.addLocator(
);

module.exports = function () {
this.Given(/^There is an (in)?active "([^"]*)" election$/, async function (
arg,
this.Given(/^There is an active "([^"]*)" election$/, async function (
electionType
) {
this.normalElection.active = false;
this.stvElection.active = false;
await this.normalElection.save();
this.stvElection.active = false;
await this.stvElection.save();

const active = arg !== 'in';
if (electionType === ElectionTypes.STV) {
this.election = this.stvElection;
} else if (electionType === ElectionTypes.NORMAL) {
this.election = this.normalElection;
}
this.election.active = active;
return this.election.save();
this.election.active = true;
await this.election.save();
const driver = browser.driver;
driver.get('http://localhost:3000/');
});

this.Given(/^There is an (in)?active election$/, async function (arg) {
const active = arg !== 'in';
this.Given(/^There is an inactive election$/, async function () {
this.normalElection.active = false;
await this.normalElection.save();

this.stvElection.active = false;
await this.stvElection.save();
this.election = this.normalElection;
this.election.active = active;
return this.election.save();
});

this.Given(/^The election is (de)?activated/, function (arg) {
Expand Down Expand Up @@ -77,9 +76,15 @@ module.exports = function () {
]);
});

this.When(/^I select "([^"]*)"$/, function (alternative) {
this.When(/^I (de)?select "([^"]*)"$/, function (de, alternative) {
const alternatives = element.all(
by.repeater('alternative in getPossibleAlternatives()')
by.repeater(
this.election.type == ElectionTypes.STV
? de == 'de'
? 'alternative in priorities track by alternative._id'
: 'alternative in getPossibleAlternatives()'
: 'alternative in activeElection.alternatives'
)
);
const wantedAlternative = alternatives
.filter((a) =>
Expand All @@ -102,41 +107,48 @@ module.exports = function () {
});

this.When(/^I submit the vote$/, () => {
element(by.css('button')).click();
element(by.buttonText('Avgi stemme')).click();
});

function vote(election) {
function vote(type) {
let alternatives;
if (election.type === ElectionTypes.STV)
if (type == ElectionTypes.STV)
alternatives = element.all(
by.repeater('alternative in getPossibleAlternatives()')
);
else if (election.type === ElectionTypes.NORMAL)
else if (type == ElectionTypes.NORMAL) {
alternatives = element.all(
by.repeater('alternative in activeElection.alternatives')
);
}
const alternative = alternatives.first();
const button = element(by.buttonText('Avgi stemme'));

alternative.click();
button.click();
}

this.Given(/^I have voted on the election$/, function () {
vote(this.election);
confirmVote(true);
this.Given(/^I have voted on the election$/, async function () {
if (this.election.type === ElectionTypes.NORMAL)
await this.election.addVote(this.user, [this.alternatives[0]]);
else if (this.election.type === ElectionTypes.STV)
await this.election.addVote(this.user, [this.stvAlternatives[0]]);
});

this.When(/^I vote on an election$/, function () {
vote();
vote(this.election.type);
confirmVote(true);
});

this.Then(/^I see my hash in "([^"]*)"$/, function (name) {
const input = element(by.name(name));
const alternative =
this.election.type === ElectionTypes.NORMAL
? this.alternatives[0]
: this.stvAlternatives[0];
return Vote.findOne({
priorities: {
$all: [this.alternatives[0]],
$all: [alternative],
},
}).then((foundVote) =>
expect(input.getAttribute('value')).to.eventually.equal(foundVote.hash)
Expand All @@ -154,6 +166,14 @@ module.exports = function () {
).to.eventually.contain(alternative.toUpperCase());
});

this.Then(/^I have (\d+) alternative on the confirmation ballot$/, function (
count
) {
const priorities = element.all(by.repeater('alternative in priorities'));

return expect(priorities.count()).to.eventually.equal(Number(count));
});

this.Then(
/^I see "([^"]*)" as priority (\d+) on the confirmation ballot$/,
function (alternative, position) {
Expand Down
10 changes: 7 additions & 3 deletions features/support/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = function () {
title: 'activeElectionSTV',
type: ElectionTypes.STV,
description: 'active election STV',
active: false,
active: true,
};

const activeNormalElectionData = {
Expand All @@ -39,17 +39,21 @@ module.exports = function () {
await clearCollections();
const stvElection = await new Election(activeSTVElectionData);
this.stvElection = stvElection;
this.election = stvElection;

const normalElection = await new Election(activeNormalElectionData);
this.normalElection = normalElection;
this.election = normalElection;

this.alternatives = await Promise.all(
alternatives.map((alternative) => new Alternative(alternative))
);

this.stvAlternatives = await Promise.all(
alternatives.map((alternative) => new Alternative(alternative))
);

for (let i = 0; i < alternatives.length; i++) {
await stvElection.addAlternative(this.alternatives[i]);
await stvElection.addAlternative(this.stvAlternatives[i]);
await normalElection.addAlternative(this.alternatives[i]);
}

Expand Down

0 comments on commit 705d528

Please sign in to comment.