Пример #1
0
    def test_count_ballot(self):
        """
        Ensures that ballots can be counted and tallied appropriately.
        """
        voter1, voter2, voter3 = all_voters[0:3]
        ballot_num1 = balloting.issue_ballot(voter1.national_id)
        ballot_num2 = balloting.issue_ballot(voter2.national_id)
        ballot_num3 = balloting.issue_ballot(voter3.national_id)

        all_candidates = registry.get_all_candidates()
        assert len(all_candidates) == 3

        ballot1 = Ballot(ballot_num1, all_candidates[0].candidate_id, "")
        ballot2 = Ballot(ballot_num2, all_candidates[1].candidate_id, "")
        ballot3 = Ballot(ballot_num3, all_candidates[1].candidate_id, "")

        assert balloting.count_ballot(ballot1, voter1.national_id) == BallotStatus.BALLOT_COUNTED
        assert balloting.count_ballot(ballot2, voter2.national_id) == BallotStatus.BALLOT_COUNTED
        assert balloting.count_ballot(ballot3, voter3.national_id) == BallotStatus.BALLOT_COUNTED

        assert registry.get_voter_status(voter1.national_id) == VoterStatus.BALLOT_COUNTED
        assert registry.get_voter_status(voter2.national_id) == VoterStatus.BALLOT_COUNTED
        assert registry.get_voter_status(voter3.national_id) == VoterStatus.BALLOT_COUNTED

        winning_candidate = balloting.compute_election_winner()
        assert winning_candidate.candidate_id == all_candidates[1].candidate_id
        assert winning_candidate.name == all_candidates[1].name
Пример #2
0
    def test_de_register_fraudster(self):
        """
        Checks that fraudsters cannot be completely de-registered
        """
        voter = all_voters[0]
        ballot_number1 = balloting.issue_ballot(voter.national_id)
        ballot_number2 = balloting.issue_ballot(voter.national_id)

        all_candidates = registry.get_all_candidates()
        ballot1 = Ballot(ballot_number1, all_candidates[0].candidate_id, "first ballot")
        ballot2 = Ballot(ballot_number2, all_candidates[0].candidate_id, "second ballot")

        # Commit Fraud
        assert balloting.count_ballot(ballot1, voter.national_id) == BallotStatus.BALLOT_COUNTED
        assert balloting.count_ballot(ballot2, voter.national_id) == BallotStatus.FRAUD_COMMITTED

        # De-register the voter
        assert registry.de_register_voter(voter.national_id) is False

        # Make sure the fraudster's name is still recorded
        fraudsters = balloting.get_all_fraudulent_voters()
        assert len(fraudsters) == 1
        assert (voter.first_name + " " + voter.last_name) in balloting.get_all_fraudulent_voters()

        # Make sure the voter's status is still that fraud was committed
        assert registry.get_voter_status(voter.national_id) == VoterStatus.FRAUD_COMMITTED
Пример #3
0
    def test_count_election_winner_plurality(self):
        """
        If nobody gets the majority, the winner still is the one with the plurality.
        """
        all_candidates = registry.get_all_candidates()

        voter1, voter2, voter3, voter4 = all_voters[0:4]
        ballot_number1 = balloting.issue_ballot(voter1.national_id)
        ballot_number2 = balloting.issue_ballot(voter2.national_id)
        ballot_number3 = balloting.issue_ballot(voter3.national_id)
        ballot_number4 = balloting.issue_ballot(voter4.national_id)

        # No candidate has a majority, but one has a plurality.
        ballot1 = Ballot(ballot_number1, all_candidates[0].candidate_id, "")
        ballot2 = Ballot(ballot_number2, all_candidates[0].candidate_id, "")
        ballot3 = Ballot(ballot_number3, all_candidates[1].candidate_id, "")
        ballot4 = Ballot(ballot_number4, all_candidates[2].candidate_id, "")

        balloting.count_ballot(ballot1, voter1.national_id)
        balloting.count_ballot(ballot2, voter2.national_id)
        balloting.count_ballot(ballot3, voter3.national_id)
        balloting.count_ballot(ballot4, voter4.national_id)

        winning_candidate = balloting.compute_election_winner()
        assert winning_candidate.candidate_id == all_candidates[0].candidate_id
        assert winning_candidate.name == all_candidates[0].name
Пример #4
0
    def test_catch_fraud(self):
        """
        Checks to make sure that if someone is caught voting twice:

        1. Only their first ballot is counted
        2. The fraudster's name is recorded
        """
        voter = all_voters[0]
        ballot_number1 = balloting.issue_ballot(voter.national_id)
        ballot_number2 = balloting.issue_ballot(voter.national_id)
        ballot_number3 = balloting.issue_ballot(voter.national_id)

        all_candidates = registry.get_all_candidates()
        ballot1 = Ballot(ballot_number1, all_candidates[0].candidate_id, "first ballot")
        ballot2 = Ballot(ballot_number2, all_candidates[0].candidate_id, "second ballot")
        ballot3 = Ballot(ballot_number3, all_candidates[0].candidate_id, "third ballot")

        # Have the voter commit fraud
        assert balloting.count_ballot(ballot1, voter.national_id) == BallotStatus.BALLOT_COUNTED
        assert balloting.count_ballot(ballot2, voter.national_id) == BallotStatus.FRAUD_COMMITTED
        assert registry.get_voter_status(voter.national_id) == VoterStatus.FRAUD_COMMITTED
        assert balloting.count_ballot(ballot3, voter.national_id) == BallotStatus.FRAUD_COMMITTED
        assert registry.get_voter_status(voter.national_id) == VoterStatus.FRAUD_COMMITTED

        # Make sure the fraudster's name is recorded
        fraudsters = balloting.get_all_fraudulent_voters()
        assert len(fraudsters) == 1
        assert (voter.first_name + " " + voter.last_name) in balloting.get_all_fraudulent_voters()

        # Make sure ballot 2 and 3 weren't counted
        ballot_comments = balloting.get_all_ballot_comments()
        assert len(ballot_comments) == 1
        assert "first ballot" in ballot_comments
Пример #5
0
    def test_ballot_comment_redaction_national_id(self):
        """
        Checks that comment redaction works
        """
        voter = all_voters[0]
        ballot_number = balloting.issue_ballot(voter.national_id)

        original_comment = """
        345-23-2334
        345232334
        345 43 3452
        """.strip()

        all_candidates = registry.get_all_candidates()
        ballot = Ballot(ballot_number, all_candidates[0].candidate_id, original_comment)
        assert balloting.count_ballot(ballot, voter.national_id) == BallotStatus.BALLOT_COUNTED

        all_ballot_comments = balloting.get_all_ballot_comments()
        assert len(all_ballot_comments) == 1

        expected_redacted_comment = """
        [REDACTED NATIONAL ID]
        [REDACTED NATIONAL ID]
        [REDACTED NATIONAL ID]
        """.strip()
        assert list(all_ballot_comments)[0] == expected_redacted_comment
def redact_free_text(free_text: str) -> str:
    """
    :param: free_text The free text to remove sensitive data from
    :returns: The redacted free text
    """
    # TODO: Implement this! Feel free to change the function parameters if you need to

    # redact names
    actual_candidates = registry.get_all_candidates()
    actual_candidate_names = set(
        [candidate.name for candidate in actual_candidates])

    for name in actual_candidates:
        free_text = re.sub(name, REDACTED_NAME, free_text, flags=re.IGNORECASE)

    # redact phone number
    free_text = re.sub(phone_redex, REDACTED_PHONE_NUMBER, free_text)

    # redact email
    free_text = re.sub(email_redex, REDACTED_EMAIL, free_text)

    # redact national id
    free_text = re.sub(national_id_redex, REDACTED_NATIONAL_ID, free_text)

    return free_text
Пример #7
0
    def test_unregistered_voter_count_ballot(self):
        """
        Checks that an unregistered voter cannot use an existing ballot for another voter to vote.
        """
        unregistered_voter = Voter("Daniel", "Salt", "999-99-9999")
        registered_voter = all_voters[0]
        valid_ballot_number = balloting.issue_ballot(registered_voter.national_id)

        all_candidates = registry.get_all_candidates()

        valid_ballot = Ballot(valid_ballot_number, all_candidates[0].candidate_id, "Valid Ballot")
        assert balloting.count_ballot(valid_ballot, unregistered_voter.national_id) == \
               BallotStatus.VOTER_NOT_REGISTERED
    def test_candidate_registration(self):
        """
        Checks to see if candidates are actually registered successfully
        """
        expected_candidate_names = {"Kathryn Collins", "Aditya Guha", "Rina Harvey"}
        for candidate_name in expected_candidate_names:
            registry.register_candidate(candidate_name)

        actual_candidates = registry.get_all_candidates()
        actual_candidate_names = set([candidate.name for candidate in actual_candidates])
        assert actual_candidate_names == expected_candidate_names

        for candidate in actual_candidates:
            assert registry.candidate_is_registered(candidate)
Пример #9
0
    def test_invalidate_ballot_after_use(self):
        """
        Ensures that a ballot that is cast cannot be invalidated
        """
        voter = all_voters[0]
        ballot_number1 = balloting.issue_ballot(voter.national_id)

        all_candidates = registry.get_all_candidates()
        ballot1 = Ballot(ballot_number1, all_candidates[0].candidate_id, "valid now, but invalid later")

        assert balloting.count_ballot(ballot1, voter.national_id) == BallotStatus.BALLOT_COUNTED
        assert balloting.invalidate_ballot(ballot_number1) is False

        # The ballot cannot be invalidated after being cast - it should still be counted
        assert len(balloting.get_all_ballot_comments()) == 1
Пример #10
0
    def test_invalidate_ballot_before_use(self):
        """
        Ensures that an invalidated ballot cannot be used
        """
        voter = all_voters[0]
        ballot_number1 = balloting.issue_ballot(voter.national_id)

        all_candidates = registry.get_all_candidates()
        ballot1 = Ballot(ballot_number1, all_candidates[0].candidate_id, "")

        assert balloting.invalidate_ballot(ballot_number1)
        assert balloting.count_ballot(ballot1, voter.national_id) == BallotStatus.INVALID_BALLOT

        # However, ensure the voter can still vote with another ballot
        ballot_number2 = balloting.issue_ballot(voter.national_id)
        ballot2 = Ballot(ballot_number2, all_candidates[0].candidate_id, "")

        assert balloting.count_ballot(ballot2, voter.national_id) == BallotStatus.BALLOT_COUNTED
Пример #11
0
    def test_count_ballot_mismatch(self):
        """
        If the wrong voter issues a ballot, the count_ballot endpoint should say so. The ballot should still remain
        for the true voter to used
        """
        voter1, voter2 = all_voters[0:2]
        ballot_number2 = balloting.issue_ballot(voter2.national_id)

        all_candidates = registry.get_all_candidates()

        # Have the wrong voter cast the ballot
        ballot2_attempt1 = Ballot(ballot_number2, all_candidates[0].candidate_id, "Attempt 1")
        assert balloting.count_ballot(ballot2_attempt1, voter1.national_id) == BallotStatus.VOTER_BALLOT_MISMATCH

        # Now, have the right voter cast the ballot
        ballot2_attempt2 = Ballot(ballot_number2, all_candidates[1].candidate_id, "Attempt 2")
        assert balloting.count_ballot(ballot2_attempt2, voter2.national_id) == BallotStatus.BALLOT_COUNTED

        ballot_comments = balloting.get_all_ballot_comments()
        assert len(ballot_comments) == 1
        assert "Attempt 2" in ballot_comments
Пример #12
0
    def test_ballot_comment_redaction_composite(self):
        """
        Checks that comment redaction works
        """
        voter = all_voters[0]
        ballot_number = balloting.issue_ballot(voter.national_id)

        original_comment = """
        Hi there,
        My name is {0}, and prioritizing public transportation is _very_ important to me. It takes me at least 90
        minutes to get to work each day, largely because the infrastructure here hasn't been built yet. It's critical
        for us to invest in this. If you'd like to contact me please reach out to me at 329 112-4535, or at
        [email protected].

        Cheers,

        {0} {1}
        Id: {2}
        """.format(voter.first_name, voter.last_name, voter.national_id).strip()

        all_candidates = registry.get_all_candidates()
        ballot = Ballot(ballot_number, all_candidates[0].candidate_id, original_comment)
        assert balloting.count_ballot(ballot, voter.national_id) == BallotStatus.BALLOT_COUNTED

        all_ballot_comments = balloting.get_all_ballot_comments()
        assert len(all_ballot_comments) == 1

        expected_redacted_comment = """
        Hi there,
        My name is {0}, and prioritizing public transportation is _very_ important to me. It takes me at least 90
        minutes to get to work each day, largely because the infrastructure here hasn't been built yet. It's critical
        for us to invest in this. If you'd like to contact me please reach out to me at [REDACTED PHONE NUMBER], or at
        [REDACTED EMAIL].

        Cheers,

        {0} {1}
        Id: {2}
        """.format("[REDACTED NAME]", "[REDACTED NAME]", "[REDACTED NATIONAL ID]").strip()
        assert list(all_ballot_comments)[0] == expected_redacted_comment
Пример #13
0
    def test_ballot_comment_redaction_phone_number(self):
        """
        Checks that comment redaction works
        """
        voter = all_voters[0]
        ballot_number = balloting.issue_ballot(voter.national_id)

        original_comment = """
        (839) 838-1627,839 838-1627,839-838-1627,8398381627
        """.strip()

        all_candidates = registry.get_all_candidates()
        ballot = Ballot(ballot_number, all_candidates[0].candidate_id, original_comment)
        assert balloting.count_ballot(ballot, voter.national_id) == BallotStatus.BALLOT_COUNTED

        all_ballot_comments = balloting.get_all_ballot_comments()
        assert len(all_ballot_comments) == 1

        expected_redacted_comment = """
        [REDACTED PHONE NUMBER],[REDACTED PHONE NUMBER],[REDACTED PHONE NUMBER],[REDACTED PHONE NUMBER]
        """.strip()
        assert list(all_ballot_comments)[0] == expected_redacted_comment
Пример #14
0
def get_all_candidates():
    return jsons.dumps(registry.get_all_candidates())