|
5 | 5 | from faker import Faker |
6 | 6 | from frappe.tests import IntegrationTestCase |
7 | 7 |
|
| 8 | +from ballot.id.doctypes import ( |
| 9 | + CANDIDATE, |
| 10 | + CANDIDATE_APPLICATION, |
| 11 | + ELECTION, |
| 12 | + NOMINATION_FORM, |
| 13 | + TEAM, |
| 14 | + VOTE, |
| 15 | +) |
| 16 | +from ballot.tests.utils import ( |
| 17 | + create_candidate_application, |
| 18 | + create_election, |
| 19 | + create_election_team, |
| 20 | + create_nomination_form, |
| 21 | +) |
| 22 | + |
8 | 23 | fake = Faker() |
9 | 24 |
|
10 | | -ELECTION = "Election" |
11 | | -TEAM = "Election Team" |
12 | | -CANDIDATE_DOC = "Election Candidate" |
13 | | -FORM = "Election Nomination Form" |
14 | | -APPLICATION_DOC = "Election Candidate Application" |
15 | | -VOTE_DOC = "Candidate Vote" |
| 25 | + |
16 | 26 | TEAM_OWNER = "test1@example.com" |
17 | | -CANDIDATE_1 = "test2@example.com" |
18 | | -CANDIDATE_2 = "test3@example.com" |
| 27 | +CANDIDATE_USER = "test2@example.com" |
| 28 | +VOTER = "test3@example.com" |
19 | 29 |
|
20 | 30 |
|
21 | 31 | class TestCandidateVote(IntegrationTestCase): |
22 | 32 | def setUp(self): |
23 | 33 | frappe.set_user(TEAM_OWNER) |
24 | | - |
25 | | - # Create a Team |
26 | | - team = frappe.get_doc( |
27 | | - { |
28 | | - "doctype": TEAM, |
29 | | - "team_name": fake.name(), |
30 | | - } |
| 34 | + self.election_team = create_election_team() |
| 35 | + self.election = create_election( |
| 36 | + "Test Election", self.election_team.name, voting_status="Live" |
31 | 37 | ) |
32 | | - team.insert() |
33 | | - self.team = team |
34 | | - |
35 | | - # Create an Election, linked to the above team |
36 | | - election = frappe.get_doc( |
37 | | - { |
38 | | - "doctype": ELECTION, |
39 | | - "title": fake.name(), |
40 | | - "organizing_team": self.team.name, |
41 | | - } |
| 38 | + self.nomination_form = create_nomination_form(self.election, status="Live") |
| 39 | + frappe.set_user(CANDIDATE_USER) |
| 40 | + self.candidate_application = create_candidate_application( |
| 41 | + self.election.name, self.nomination_form.name |
42 | 42 | ) |
43 | | - election.insert() |
44 | | - self.election = election |
45 | | - |
46 | | - # Create a Nomination Form for the election |
47 | | - form = frappe.get_doc( |
| 43 | + self.candidate = frappe.get_doc( |
| 44 | + CANDIDATE, |
48 | 45 | { |
49 | | - "doctype": FORM, |
50 | | - "status": "Live", |
| 46 | + "linked_application": self.candidate_application.name, |
51 | 47 | "election": self.election.name, |
52 | | - } |
| 48 | + }, |
53 | 49 | ) |
54 | | - form.insert() |
55 | | - self.form = form |
56 | 50 |
|
57 | | - # Create a 1st Candidate Application for the election |
58 | | - frappe.set_user(CANDIDATE_1) |
59 | | - application_1 = frappe.get_doc( |
| 51 | + def tearDown(self): |
| 52 | + frappe.set_user("Administrator") |
| 53 | + frappe.delete_doc(TEAM, self.election_team.name, force=True) |
| 54 | + frappe.delete_doc(ELECTION, self.election.name, force=1) |
| 55 | + frappe.delete_doc(NOMINATION_FORM, self.nomination_form.name, force=1) |
| 56 | + frappe.delete_doc(CANDIDATE_APPLICATION, self.candidate_application.name, force=1) |
| 57 | + frappe.delete_doc(CANDIDATE, self.candidate.name, force=1) |
| 58 | + |
| 59 | + def test_single_candidate_vote(self): |
| 60 | + frappe.set_user(VOTER) |
| 61 | + |
| 62 | + candidate_vote = frappe.get_doc( |
60 | 63 | { |
61 | | - "doctype": APPLICATION_DOC, |
62 | | - "user": CANDIDATE_1, |
| 64 | + "doctype": VOTE, |
| 65 | + "vote_by": frappe.session.user, |
63 | 66 | "election": self.election.name, |
64 | | - "nomination_form": form.name, |
65 | | - "full_name": fake.name(), |
66 | | - "email": fake.email(), |
| 67 | + "candidate_tiers": [ |
| 68 | + { |
| 69 | + "rank": 1, |
| 70 | + "candidate": self.candidate.name, |
| 71 | + } |
| 72 | + ], |
67 | 73 | } |
68 | 74 | ) |
69 | | - application_1.insert() |
| 75 | + candidate_vote.submit() |
70 | 76 |
|
71 | | - # Create a 2nd Candidate Application for the election |
72 | | - frappe.set_user(CANDIDATE_2) |
73 | | - application_2 = frappe.get_doc( |
| 77 | + # Given a Voter has already voted for a Candidate, they should not be able to vote again |
| 78 | + second_vote = frappe.get_doc( |
74 | 79 | { |
75 | | - "doctype": APPLICATION_DOC, |
76 | | - "user": CANDIDATE_2, |
| 80 | + "doctype": VOTE, |
| 81 | + "vote_by": frappe.session.user, |
77 | 82 | "election": self.election.name, |
78 | | - "nomination_form": form.name, |
79 | | - "full_name": fake.name(), |
80 | | - "email": fake.email(), |
| 83 | + "candidate_tiers": [ |
| 84 | + { |
| 85 | + "rank": 1, |
| 86 | + "candidate": self.candidate.name, |
| 87 | + } |
| 88 | + ], |
81 | 89 | } |
82 | 90 | ) |
83 | | - application_2.insert() |
84 | | - |
85 | | - # Accept the applications |
86 | | - frappe.set_user(TEAM_OWNER) |
87 | | - application_1.status = "Accepted" |
88 | | - application_1.save() |
89 | | - |
90 | | - application_2.status = "Accepted" |
91 | | - application_2.save() |
92 | | - |
93 | | - self.application_1 = application_1 |
94 | | - self.application_2 = application_2 |
| 91 | + with self.assertRaises(frappe.ValidationError): |
| 92 | + second_vote.submit() |
95 | 93 |
|
96 | | - frappe.set_user("Administarator") |
| 94 | + def test_valid_user_vote(self): |
| 95 | + frappe.set_user(VOTER) |
97 | 96 |
|
98 | | - # Get Candidate Docs |
99 | | - candidate_1 = frappe.get_doc( |
100 | | - CANDIDATE_DOC, |
101 | | - { |
102 | | - "election": self.election.name, |
103 | | - "linked_application": self.application_1.name, |
104 | | - }, |
105 | | - ) |
106 | | - candidate_2 = frappe.get_doc( |
107 | | - CANDIDATE_DOC, |
| 97 | + candidate_vote = frappe.get_doc( |
108 | 98 | { |
| 99 | + "doctype": VOTE, |
| 100 | + "vote_by": "not_a_valid_user@example.com", |
109 | 101 | "election": self.election.name, |
110 | | - "linked_application": self.application_2.name, |
111 | | - }, |
| 102 | + "candidate_tiers": [ |
| 103 | + { |
| 104 | + "rank": 1, |
| 105 | + "candidate": self.candidate.name, |
| 106 | + } |
| 107 | + ], |
| 108 | + } |
112 | 109 | ) |
113 | 110 |
|
114 | | - self.candidate_1 = candidate_1 |
115 | | - self.candidate_2 = candidate_2 |
| 111 | + with self.assertRaises(frappe.ValidationError): |
| 112 | + candidate_vote.submit() |
116 | 113 |
|
117 | | - def tearDown(self): |
118 | | - frappe.set_user("Administrator") |
119 | | - frappe.delete_doc(TEAM, self.team.name, force=1) |
120 | | - frappe.delete_doc(ELECTION, self.election.name, force=1) |
121 | | - frappe.delete_doc(FORM, self.form.name, force=1) |
122 | | - frappe.delete_doc(APPLICATION_DOC, self.application_1.name, force=1) |
123 | | - frappe.delete_doc(APPLICATION_DOC, self.application_2.name, force=1) |
124 | | - frappe.delete_doc(CANDIDATE_DOC, self.candidate_1.name, force=1) |
125 | | - frappe.delete_doc(CANDIDATE_DOC, self.candidate_2.name, force=1) |
126 | | - |
127 | | - def test_single_vote(self): |
128 | | - # Given a candidate vote is created for an election |
129 | | - # When the user tries to vote for another candidate in the same election |
130 | | - # Then the user should not be able to vote for another candidate |
131 | | - |
132 | | - frappe.set_user("test4@example.com") |
133 | | - vote1 = frappe.get_doc( |
134 | | - { |
135 | | - "doctype": VOTE_DOC, |
136 | | - "vote_by": frappe.session.user, |
137 | | - "election": self.election.name, |
138 | | - "candidate": self.candidate_1.name, |
139 | | - } |
140 | | - ) |
141 | | - vote1.insert() |
| 114 | + def test_voting_when_voting_is_closed(self): |
| 115 | + frappe.set_user(TEAM_OWNER) |
| 116 | + self.election.voting_status = "Closed" |
| 117 | + self.election.save() |
142 | 118 |
|
143 | | - vote2 = frappe.get_doc( |
| 119 | + frappe.set_user(VOTER) |
| 120 | + |
| 121 | + candidate_vote = frappe.get_doc( |
144 | 122 | { |
145 | | - "doctype": VOTE_DOC, |
| 123 | + "doctype": VOTE, |
146 | 124 | "vote_by": frappe.session.user, |
147 | 125 | "election": self.election.name, |
148 | | - "candidate": self.candidate_2.name, |
| 126 | + "candidate_tiers": [ |
| 127 | + { |
| 128 | + "rank": 1, |
| 129 | + "candidate": self.candidate.name, |
| 130 | + } |
| 131 | + ], |
149 | 132 | } |
150 | 133 | ) |
151 | 134 |
|
152 | 135 | with self.assertRaises(frappe.ValidationError): |
153 | | - vote2.insert() |
154 | | - |
155 | | - frappe.set_user('Administrator') |
156 | | - vote1.delete(force=1) |
| 136 | + candidate_vote.submit() |
0 commit comments