Ejemplo n.º 1
0
def test_cannot_be_created_without_all_required_params(api_client: APIClient,
                                                       param: str,
                                                       vote_data: VotePostData,
                                                       user: User) -> None:
    del vote_data[param]
    response = _create_vote(api_client, vote_data, user)
    assert response.status_code == status.HTTP_400_BAD_REQUEST
    assert param in str(response.data)
Ejemplo n.º 2
0
def test_create_vote(api_client: APIClient, vote_data: VotePostData,
                     eligible_voter: User) -> None:
    response = _create_vote(api_client, vote_data, eligible_voter)
    assert response.status_code == status.HTTP_201_CREATED
    vote = el_models.Vote.objects.get(id=response.data["id"])
    assert all((
        vote.answer_ciphertext == vote_data["answer_ciphertext"],
        vote.author.id == eligible_voter.id,
        vote.question.id == vote_data["question"],
    ))
Ejemplo n.º 3
0
def test_voter_authorized_in_one_election_cannot_vote_in_another(
    api_client: APIClient,
    eligible_voter: models.User,
    vote_data: VotePostData,
    question_factory,
):
    other_election_question = question_factory(
        state=el_models.Election.State.OPENED)
    vote_data["question"] = other_election_question.id
    response = _create_vote(api_client, vote_data, eligible_voter)
    assert response.status_code == status.HTTP_403_FORBIDDEN
Ejemplo n.º 4
0
def test_cannot_vote_in_not_opened_election(
    api_client: APIClient,
    vote_data: VotePostData,
    user: User,
    eligible_voter_factory,
    _question: el_models.Question,
):
    voter = eligible_voter_factory(user=user, election=_question.election)
    vote_data["question"] = _question.id
    response = _create_vote(api_client, vote_data, voter)
    assert response.status_code == status.HTTP_400_BAD_REQUEST
Ejemplo n.º 5
0
def test_create_vote_twice_on_the_same_question(api_client: APIClient,
                                                vote: el_models.Vote) -> None:
    response = _create_vote(
        api_client,
        {
            "answer_ciphertext": "baby yoda",
            "question": vote.answer.question.id
        },
        vote.author,
    )
    assert response.status_code == status.HTTP_400_BAD_REQUEST
Ejemplo n.º 6
0
def test_voter_is_created_on_first_vote(
    api_client: APIClient,
    vote_data: VotePostData,
    opened_election: el_models.Election,
    eligible_voter: User,
):
    response = _create_vote(api_client, vote_data, eligible_voter)
    assert response.status_code == status.HTTP_201_CREATED

    voter = apollo.elections.models.vote.Voter.objects.last()
    assert all(
        (voter.user == eligible_voter, voter.election == opened_election))
Ejemplo n.º 7
0
def test_unauthorized_user_cannot_vote(api_client: APIClient,
                                       vote_data: VotePostData,
                                       user: User) -> None:
    response = _create_vote(api_client, vote_data, user)
    assert response.status_code == status.HTTP_403_FORBIDDEN
Ejemplo n.º 8
0
def test_cannot_create_for_non_existent_question(api_client: APIClient,
                                                 vote_data: VotePostData,
                                                 user: User) -> None:
    vote_data["question"] = 4040
    response = _create_vote(api_client, vote_data, user)
    assert response.status_code == status.HTTP_400_BAD_REQUEST