Skip to content

Commit

Permalink
Merge pull request #446 from webkom/e2e-normal-voting
Browse files Browse the repository at this point in the history
Add E2E tests supporting normal and stv elections
  • Loading branch information
SmithPeder committed Oct 9, 2021
2 parents d3d411d + 705d528 commit d764458
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 53 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
41 changes: 33 additions & 8 deletions features/admin.feature
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Feature: Admin
And I am on page "/admin/create_election"
When I create an election
Then The election should exist
And The election should have type "normal"
And The election should have seats "1"

Scenario: Create invalid election
Given There is an inactive election
Expand All @@ -23,6 +25,12 @@ Feature: Admin
And I fill in "alternative0" with "A"
Then Button "Submit" should be disabled

Scenario: Correct fields for election types
Given I am on page "/admin/create_election"
Then I should not find "input[name='seats']"
When I select option "Preferansevalg"
Then I should find "input[name='seats']"

Scenario: Create election with more seats
Given There is an inactive election
And I am on page "/admin/create_election"
Expand All @@ -35,22 +43,39 @@ Feature: Admin
Then Button "Submit" should not be disabled
When I click "Submit"
Then I see alert "Avstemning lagret"
And The election "test election" should have type "stv"
And The election "test election" should have seats "2"

Scenario: Count votes for STV election
Given There is an active "stv" election
And The election has "seats" 2
And There are 2 users
And The election has 2 votes
And The election is deactivated
And I am on the edit election page
When I click "Kalkuler resultat"
Then I should see the stv log
And I should see 2 in "election.voteCount"
And I should see 0 in "election.blankVoteCount"
And I should see 2 in "election.seats"
And There should be 2 winners
And I should see "test alternative" as winner 1
And I should see "another test alternative" as winner 2

Scenario: Count votes
Given There is an active election
Scenario: Count votes for NORMAL election
Given There is an active "normal" election
And The election has votes
And The election is deactivated
And I am on the edit election page
When I click "Kalkuler resultat"
Then I should see votes
And I should see 1 in "election.voteCount"
Then I should see 1 in "election.voteCount"
And I should see 0 in "election.blankVoteCount"
And I should see 1 in "election.seats"
And There should be 1 winner
And I should see "test alternative" as winner 1

Scenario: Count votes for active elections
Given There is an active election
Given There is an active "stv" election
When I am on the edit election page
Then I should not find "button.toggle-show"

Expand All @@ -62,7 +87,7 @@ Feature: Admin
Then I should see the alternative "Alternative"

Scenario: Adding alternatives to active elections
Given There is an active election
Given There is an active "stv" election
When I am on the edit election page
Then I should not find "input#new-alternative"
And I should not find "button.add-alternative"
Expand All @@ -76,15 +101,15 @@ Feature: Admin
And I should not find "button.add-alternative"

Scenario: Deactivating elections
Given There is an active election
Given There is an active "stv" election
And I am on the edit election page
When I click "Deaktiver"
Then I see "Aktiver"
And I should find "input#new-alternative"
And I should find "button.add-alternative"

Scenario: Count number of votes and active users
Given There is an active election
Given There is an active "stv" election
And The election has votes
And I am on the edit election page
Then I should see 1 in "votedUsers"
Expand Down
43 changes: 28 additions & 15 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 election
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 @@ -44,16 +57,16 @@ Feature: Election
Then I see "Blank stemme" in ".ballot h3"

Scenario: Retrieve vote from localStorage
Given There is an active election
And I have voted on the election
And I am on page "/retrieve"
Given There is an active "stv" election
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å: activeElection1" in ".vote-result-feedback h3"
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."
52 changes: 46 additions & 6 deletions features/step_definitions/adminSteps.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ const chai = require('chai');
const Bluebird = require('bluebird');
const chaiAsPromised = require('chai-as-promised');
const Election = require('../../app/models/election');
const User = require('../../app/models/user');

const expect = chai.expect;

const hash = '$2a$10$qxTI.cWwa2kwcjx4SI9KAuV4KxuhtlGOk33L999UQf1rux.4PBz7y'; // 'password'

chai.use(chaiAsPromised);

module.exports = function () {
Expand All @@ -27,9 +30,9 @@ module.exports = function () {

Bluebird.all([
expect(election.element(by.css('span')).getText()).to.eventually.equal(
this.election.title
this.stvElection.title
),
expect(alternatives.count()).to.eventually.equal(1),
expect(alternatives.count()).to.eventually.equal(2),
]);
});

Expand Down Expand Up @@ -67,15 +70,52 @@ module.exports = function () {
})
);

this.Given(/^The election has votes$/, async function () {
await this.election.addVote(this.user, [this.alternatives[0]]);
this.Given(/^There are (\d+) users$/, async function (userCount) {
this.users = await Promise.all(
[...Array(Number(userCount)).keys()].map((i) =>
User.create({
username: `testuser${i}`,
cardKey: `${i}${i}TESTCARDKEY`,
hash,
})
)
);
});

this.Then(
/The election(?: "([^"]*)")? should have ([^"]*) "([^"]*)"/,
(electionTitle, field, value) => {
Election.find({ title: electionTitle || newElection.title })
.exec()
.spread((election) => {
expect(election[field].toString()).to.equal(value);
});
}
);

this.Given(
/^The election has "([^"]*)" (\d+)|(?:"([^"]*)")$"/,
async function (field, num, str) {
this.election[field] = num || str;
await this.election.save();
}
);

this.Given(/^The election has(?: (\d+))? votes$/, async function (count) {
if (count) {
for (let i = 0; i < count; i++) {
await this.election.addVote(this.users[i], [this.alternatives[i]]);
}
} else {
await this.election.addVote(this.user, [this.alternatives[0]]);
}
});

this.Given(/^I am on the edit election page$/, function () {
browser.get(`/admin/election/${this.election.id}/edit`);
});

this.Then(/^I should see votes$/, function () {
this.Then(/^I should see the stv log$/, function () {
const alternatives = element.all(
by.repeater('(key, value) in elem.counts')
);
Expand All @@ -86,7 +126,7 @@ module.exports = function () {
`${this.alternatives[0].description} with 1 votes`
),
expect(alternatives.get(1).getText()).to.eventually.equal(
`${this.alternatives[1].description} with 0 votes`
`${this.alternatives[1].description} with 1 votes`
),
expect(alternatives.get(2).getText()).to.eventually.equal(
`${this.alternatives[2].description} with 0 votes`
Expand Down
Loading

0 comments on commit d764458

Please sign in to comment.