Exemple #1
0
    def test_scenario_1_simple_vote(self):
        with ConsoleLogDuration("### administrator_creates_election"):
            self.administrator_creates_election()

        with ConsoleLogDuration("### administrator_regenerates_passwords_for_some_voters"):
            self.administrator_regenerates_passwords_for_some_voters()

        with ConsoleLogDuration("### verify_election_consistency using `belenios_tool verify` (#0)"):
            verify_election_consistency(self.election_id)

        with ConsoleLogDuration("### all_voters_vote_in_sequences"):
            self.all_voters_vote_in_sequences()

        with ConsoleLogDuration("### verify_election_consistency using `belenios_tool verify` (#1)"):
            verify_election_consistency(self.election_id)

        with ConsoleLogDuration("### create_election_data_snapshot (#0)"):
            snapshot_folder = create_election_data_snapshot(self.election_id)
            console_log("snapshot_folder: ", snapshot_folder)

        try:
            with ConsoleLogDuration("### some_voters_revote"):
                self.some_voters_revote()

            with ConsoleLogDuration("### verify_election_consistency using `belenios_tool verify-diff` (#2)"):
                verify_election_consistency(self.election_id, snapshot_folder)
        finally:
            with ConsoleLogDuration("### delete_election_data_snapshot"):
                delete_election_data_snapshot(snapshot_folder)

        with ConsoleLogDuration("### verify_election_consistency using `belenios_tool verify` (#3)"):
            verify_election_consistency(self.election_id)

        with ConsoleLogDuration("### administrator_does_tallying_of_election"):
            self.administrator_does_tallying_of_election()

        with ConsoleLogDuration("### verify_election_consistency using `belenios_tool verify` (#4)"):
            verify_election_consistency(self.election_id)

        with ConsoleLogDuration("### one_voter_revotes_after_the_election_is_closed"):
            self.one_voter_revotes_after_the_election_is_closed()
Exemple #2
0
    def test_scenario_2_manual_vote(self):
        console_log(
            "### Running test method BeleniosTestElectionScenario2::test_scenario_2_manual_vote()"
        )
        with ConsoleLogDuration(
                "### administrator_starts_creation_of_manual_election"):
            self.administrator_starts_creation_of_manual_election()

        with ConsoleLogDuration(
                "### credential_authority_sends_credentials_to_voters"):
            self.credential_authority_sends_credentials_to_voters()

        with ConsoleLogDuration("### administrator_invites_trustees"):
            self.administrator_invites_trustees()

        with ConsoleLogDuration("### trustees_generate_election_private_keys"):
            self.trustees_generate_election_private_keys()

        with ConsoleLogDuration(
                "### administrator_completes_creation_of_election"):
            self.administrator_completes_creation_of_election()

        with ConsoleLogDuration(
                "### verify_election_consistency using `belenios_tool verify` (#0)"
        ):
            verify_election_consistency(self.election_id)

        with ConsoleLogDuration("### all_voters_vote_in_sequences"):
            self.all_voters_vote_in_sequences()

        with ConsoleLogDuration(
                "### verify_election_consistency using `belenios_tool verify` (#1)"
        ):
            verify_election_consistency(self.election_id)

        with ConsoleLogDuration(
                "### Starting step: create_election_data_snapshot (#0)"):
            snapshot_folder = create_election_data_snapshot(self.election_id)
            console_log("snapshot_folder: ", snapshot_folder)

        try:
            with ConsoleLogDuration("### some_voters_revote"):
                self.some_voters_revote()

            with ConsoleLogDuration(
                    "### verify_election_consistency using `belenios_tool verify-diff` (#2)"
            ):
                verify_election_consistency(self.election_id, snapshot_folder)
        finally:
            with ConsoleLogDuration("### delete_election_data_snapshot"):
                delete_election_data_snapshot(snapshot_folder)

        with ConsoleLogDuration(
                "### verify_election_consistency using `belenios_tool verify` (#3)"
        ):
            verify_election_consistency(self.election_id)

        with ConsoleLogDuration(
                "### administrator_starts_tallying_of_election"):
            self.administrator_starts_tallying_of_election()

        with ConsoleLogDuration("### trustees_do_partial_decryption"):
            self.trustees_do_partial_decryption()

        with ConsoleLogDuration(
                "### administrator_finishes_tallying_of_election"):
            self.administrator_finishes_tallying_of_election()

        with ConsoleLogDuration(
                "### verify_election_consistency using `belenios_tool verify` (#4)"
        ):
            verify_election_consistency(self.election_id)
Exemple #3
0
    def test_scenario_2_manual_vote_with_monkeys(self):
        console_log(
            "### Running test method BeleniosTestElectionScenario2WithMonkeys::test_scenario_2_manual_vote_with_monkeys()"
        )
        with ConsoleLogDuration(
                "### administrator_starts_creation_of_manual_election"):
            self.administrator_starts_creation_of_manual_election()

        with ConsoleLogDuration(
                "### credential_authority_sends_credentials_to_voters"):
            self.credential_authority_sends_credentials_to_voters()

        with ConsoleLogDuration("### administrator_invites_trustees"):
            self.administrator_invites_trustees()

        with ConsoleLogDuration("### trustees_generate_election_private_keys"):
            self.trustees_generate_election_private_keys()

        with ConsoleLogDuration(
                "### administrator_completes_creation_of_election"):
            self.administrator_completes_creation_of_election()

        with ConsoleLogDuration(
                "### verify_election_consistency using `belenios_tool verify` (#0)"
        ):
            verify_election_consistency(self.election_id)

        self.voters_data = {
        }  # We reset this (set by `BeleniosTestElectionWithCreationBase`) because we generate voters data in several parts
        voters_who_will_vote = random.sample(self.voters_email_addresses,
                                             settings.NUMBER_OF_VOTING_VOTERS)
        console_log("voters who will vote:", voters_who_will_vote)
        start_index_of_voters_who_vote_in_first_part = 0
        end_index_of_voters_who_vote_in_first_part = settings.NUMBER_OF_VOTING_VOTERS_IN_FIRST_PART
        console_log(
            f"number of (normal) voters who will vote in first part: {end_index_of_voters_who_vote_in_first_part} (indexes {start_index_of_voters_who_vote_in_first_part} included to {end_index_of_voters_who_vote_in_first_part} excluded)"
        )
        start_index_of_voters_who_vote_in_second_part = end_index_of_voters_who_vote_in_first_part
        end_index_of_voters_who_vote_in_second_part = end_index_of_voters_who_vote_in_first_part + settings.NUMBER_OF_MONKEY_VOTING_VOTERS
        console_log(
            f"number of (smart monkey) voters who will vote in second part: {end_index_of_voters_who_vote_in_second_part - start_index_of_voters_who_vote_in_second_part} (indexes {start_index_of_voters_who_vote_in_second_part} included to {end_index_of_voters_who_vote_in_second_part} excluded)"
        )
        start_index_of_voters_who_vote_in_third_part = end_index_of_voters_who_vote_in_second_part
        end_index_of_voters_who_vote_in_third_part = settings.NUMBER_OF_VOTING_VOTERS
        console_log(
            f"number of (normal) voters who will vote in third part: {end_index_of_voters_who_vote_in_third_part - start_index_of_voters_who_vote_in_third_part} (indexes {start_index_of_voters_who_vote_in_third_part} included to {end_index_of_voters_who_vote_in_third_part} excluded)"
        )
        verify_every_x_votes = 5

        with ConsoleLogDuration(
                "### some_voters_vote_in_sequences (first part)"):
            self.some_voters_vote_in_sequences(
                voters_who_will_vote,
                start_index=start_index_of_voters_who_vote_in_first_part,
                end_index=end_index_of_voters_who_vote_in_first_part,
                verify_every_x_votes=verify_every_x_votes)

        with ConsoleLogDuration("### smart monkeys vote (second part)"):
            smart_monkey_voters_who_will_vote_now = voters_who_will_vote[
                start_index_of_voters_who_vote_in_second_part:
                end_index_of_voters_who_vote_in_second_part]
            timeout = settings.EXPLICIT_WAIT_TIMEOUT
            voters_who_will_vote_now_data = populate_credential_and_password_for_voters_from_sent_emails(
                self.fake_sent_emails_manager,
                smart_monkey_voters_who_will_vote_now, settings.ELECTION_TITLE)
            voters_who_will_vote_now_data = populate_random_votes_for_voters(
                voters_who_will_vote_now_data)
            self.update_voters_data(voters_who_will_vote_now_data)

            for idx, voter in enumerate(voters_who_will_vote_now_data):
                console_log(
                    f"#### Voting as smart monkey {idx+1} of {settings.NUMBER_OF_MONKEY_VOTING_VOTERS}"
                )
                voter_email_address = voter["email_address"]
                voter_username = voter["username"]
                voter_password = voter["password"]
                voter_credential = voter["credential"]
                voter_decided_vote = voter["votes"]
                election_url = voter[
                    "election_page_url"]  # this is the same as `election_id_to_election_home_page_url(self.election_id)`
                smart_ballot_tracker = smart_monkey_votes(
                    self.browser, timeout, election_url, voter_username,
                    voter_password, voter_credential, voter_decided_vote)
                if smart_ballot_tracker:
                    voter["smart_ballot_tracker"] = smart_ballot_tracker
                else:
                    raise Exception(
                        "Monkey voter did not complete its vote properly")
                self.voters_email_addresses_who_have_voted[
                    voter_email_address] = True
                self.browser.quit()
                self.browser = initialize_browser_for_scenario_2()

        with ConsoleLogDuration(
                "### some_voters_vote_in_sequences (third part)"):
            self.some_voters_vote_in_sequences(
                voters_who_will_vote,
                start_index=start_index_of_voters_who_vote_in_third_part,
                end_index=end_index_of_voters_who_vote_in_third_part,
                verify_every_x_votes=verify_every_x_votes)

        with ConsoleLogDuration(
                "### verify_election_consistency using `belenios_tool verify` (#1)"
        ):
            verify_election_consistency(self.election_id)

        with ConsoleLogDuration(
                "### Starting step: create_election_data_snapshot (#0)"):
            snapshot_folder = create_election_data_snapshot(self.election_id)
            console_log("snapshot_folder: ", snapshot_folder)

        try:
            with ConsoleLogDuration("### some_voters_revote"):
                self.some_voters_revote()

            with ConsoleLogDuration(
                    "### verify_election_consistency using `belenios_tool verify-diff` (#2)"
            ):
                verify_election_consistency(self.election_id, snapshot_folder)
        finally:
            with ConsoleLogDuration("### delete_election_data_snapshot"):
                delete_election_data_snapshot(snapshot_folder)

        with ConsoleLogDuration(
                "### verify_election_consistency using `belenios_tool verify` (#3)"
        ):
            verify_election_consistency(self.election_id)

        with ConsoleLogDuration(
                "### administrator_starts_tallying_of_election"):
            self.administrator_starts_tallying_of_election()

        with ConsoleLogDuration("### trustees_do_partial_decryption"):
            self.trustees_do_partial_decryption()

        with ConsoleLogDuration(
                "### administrator_finishes_tallying_of_election"):
            self.administrator_finishes_tallying_of_election()

        with ConsoleLogDuration(
                "### verify_election_consistency using `belenios_tool verify` (#4)"
        ):
            verify_election_consistency(self.election_id)
Exemple #4
0
    def cast_all_votes_from_csv(self):
        browser = self.browser
        generated_files_destination_folder = settings.GENERATED_FILES_DESTINATION_FOLDER
        csv_file_path = os.path.join(generated_files_destination_folder,
                                     'all_votes.csv')
        with open(csv_file_path, 'r', newline='') as csvfile:
            csvreader = csv.DictReader(csvfile, delimiter=',', quotechar='|')
            current_row = 0
            for row in csvreader:
                current_row += 1
                if current_row <= settings.SKIP_ROWS_IN_CSV_FILE:
                    continue

                voter_email_address = row['voter_email_address']
                voter_password = row['voter_password']
                voter_encrypted_ballot_file_name = row[
                    'voter_encrypted_ballot_file_name']
                election_page_url = row['election_page_url']

                with ConsoleLogDuration(
                        f"Row {current_row} (voter {voter_email_address})"):
                    # Go to election home
                    browser.get(election_page_url)

                    wait_a_bit()

                    # She clicks on "en" language
                    select = Select(
                        wait_for_element_exists(
                            browser, ".lang_box select",
                            settings.EXPLICIT_WAIT_TIMEOUT))
                    select.select_by_visible_text("en")
                    submit = wait_for_element_exists(
                        browser, ".lang_box input[type=submit]",
                        settings.EXPLICIT_WAIT_TIMEOUT)
                    submit.click()

                    wait_a_bit()

                    # Click on advanced mode
                    advanced_mode_link_expected_label = "Advanced mode"
                    advanced_mode_link_element = wait_for_an_element_with_link_text_exists(
                        browser, advanced_mode_link_expected_label,
                        settings.EXPLICIT_WAIT_TIMEOUT)
                    advanced_mode_link_element.click()

                    wait_a_bit()

                    # Browse file and submit it
                    browse_button_css_selector = "form input[name=encrypted_vote][type=file]"
                    browse_button_element = wait_for_element_exists(
                        browser, browse_button_css_selector)
                    path_of_file_to_upload = os.path.join(
                        generated_files_destination_folder,
                        voter_encrypted_ballot_file_name)
                    browse_button_element.clear()
                    browse_button_element.send_keys(path_of_file_to_upload)
                    browse_button_element.submit()

                    wait_a_bit()

                    # Submit login form
                    username_field_css_selector = "form input[name=username]"
                    username_field_element = wait_for_element_exists(
                        browser, username_field_css_selector)
                    username_field_element.clear()
                    username_field_element.send_keys(voter_email_address)
                    password_field_css_selector = "form input[name=password]"
                    password_field_element = wait_for_element_exists(
                        browser, password_field_css_selector)
                    password_field_element.clear()
                    password_field_element.send_keys(voter_password)
                    password_field_element.submit()

                    wait_a_bit()

                    @try_several_times(max_attempts=3)
                    def verify_step_5_and_6(browser, timeout):
                        # Verify that page contains a ballot tracker
                        smart_ballot_tracker_css_selector = "#ballot_tracker"
                        smart_ballot_tracker_element = wait_for_element_exists_and_has_non_empty_content(
                            browser, smart_ballot_tracker_css_selector,
                            timeout)
                        my_smart_ballot_tracker_value = smart_ballot_tracker_element.get_attribute(
                            'innerText')
                        assert len(my_smart_ballot_tracker_value) > 5

                        # Click "I cast my vote" button
                        submit_button_css_selector = "form input[type=submit]"
                        submit_button_expected_content = "I cast my vote"
                        verify_all_elements_have_attribute_value(
                            browser, submit_button_css_selector, "value",
                            submit_button_expected_content, timeout)
                        submit_button_element = wait_for_element_exists(
                            browser, submit_button_css_selector, timeout)
                        submit_button_element.click()

                        wait_a_bit()

                        # Verify that vote has been accepted by the server

                        @try_several_times(max_attempts=3)
                        def verify_that_we_are_on_step_6(browser, timeout):
                            all_ballots_link_expected_label = "ballot box"
                            all_ballots_element = wait_for_an_element_with_link_text_exists(
                                browser, all_ballots_link_expected_label,
                                timeout)

                            current_step_css_selector = ".current_step"
                            current_step_expected_content = "Step 6/6: Thank you for voting!"
                            wait_for_element_exists_and_contains_expected_text(
                                browser, current_step_css_selector,
                                current_step_expected_content, timeout)

                            return all_ballots_element

                        all_ballots_element = verify_that_we_are_on_step_6(
                            browser, timeout)

                        # Go to all ballots page
                        all_ballots_element.click()

                        wait_a_bit()

                        # Verify presence of my ballot
                        my_smart_ballot_tracker_link_element = wait_for_an_element_with_link_text_exists(
                            browser, my_smart_ballot_tracker_value,
                            settings.EXPLICIT_WAIT_TIMEOUT)
                        my_smart_ballot_tracker_link_element.click()

                    timeout = settings.EXPLICIT_WAIT_TIMEOUT
                    verify_step_5_and_6(browser, timeout)
Exemple #5
0
    def test_scenario_4_manual_vote_with_threshold(self):
        console_log(
            "### Running test method BeleniosTestElectionScenario4::test_scenario_4_manual_vote_with_threshold()"
        )
        with ConsoleLogDuration(
                "### administrator_starts_creation_of_manual_election"):
            self.administrator_starts_creation_of_manual_election()

        with ConsoleLogDuration(
                "### credential_authority_sends_credentials_to_voters"):
            self.credential_authority_sends_credentials_to_voters()

        with ConsoleLogDuration(
                "### administrator_invites_trustees_and_sets_threshold"):
            self.administrator_invites_trustees_and_sets_threshold()

        with ConsoleLogDuration("### trustees_do_initialization_step_1_of_3"):
            self.trustees_do_initialization_step_1_of_3()

        with ConsoleLogDuration("### trustees_do_initialization_step_2_of_3"):
            self.trustees_do_initialization_step_2_of_3()

        with ConsoleLogDuration("### trustees_do_initialization_step_3_of_3"):
            self.trustees_do_initialization_step_3_of_3()

        with ConsoleLogDuration(
                "### administrator_completes_creation_of_election"):
            self.administrator_completes_creation_of_election()

        with ConsoleLogDuration(
                "### verify_election_consistency using `belenios_tool verify` (#0)"
        ):
            verify_election_consistency(self.election_id)

        with ConsoleLogDuration("### all_voters_vote_in_sequences"):
            self.all_voters_vote_in_sequences()

        with ConsoleLogDuration(
                "### verify_election_consistency using `belenios_tool verify` (#1)"
        ):
            verify_election_consistency(self.election_id)

        with ConsoleLogDuration("### create_election_data_snapshot (#0)"):
            snapshot_folder = create_election_data_snapshot(self.election_id)

        try:
            with ConsoleLogDuration("### some_voters_revote"):
                self.some_voters_revote()

            with ConsoleLogDuration(
                    "### verify_election_consistency using `belenios_tool verify-diff` (#2)"
            ):
                verify_election_consistency(self.election_id, snapshot_folder)
        finally:
            with ConsoleLogDuration("### delete_election_data_snapshot"):
                delete_election_data_snapshot(snapshot_folder)

        with ConsoleLogDuration(
                "### verify_election_consistency using `belenios_tool verify` (#3)"
        ):
            verify_election_consistency(self.election_id)

        with ConsoleLogDuration(
                "### administrator_starts_tallying_of_election"):
            self.administrator_starts_tallying_of_election(
                settings.TRUSTEES_THRESHOLD_VALUE)

        with ConsoleLogDuration("### trustees_do_partial_decryption"):
            self.trustees_do_partial_decryption(
                settings.TRUSTEES_THRESHOLD_VALUE)

        with ConsoleLogDuration(
                "### administrator_finishes_tallying_of_election"):
            self.administrator_finishes_tallying_of_election(
                settings.TRUSTEES_THRESHOLD_VALUE)

        with ConsoleLogDuration(
                "### verify_election_consistency using `belenios_tool verify` (#4)"
        ):
            verify_election_consistency(self.election_id)