Пример #1
0
    def test_get_elections(self):
        device = Device.objects.create(device_token="some-device-token")
        headers = {"HTTP_X_DEVICE_ID": device.device_token}
        factories.create_election(device=device, external_id="abc123")
        factories.create_election(device=device, external_id="def456")

        response = self.client.get(self.url, **headers)

        # Verify response.
        self.assertEqual(response.status_code, 200)
        self.assertDictEqual(response.json(), fixtures.EXPECTED_RESPONSE_GET_ELECTIONS)
Пример #2
0
    def test_post_create_vote(self):
        responses.add(
            responses.GET,
            "https://api.themoviedb.org/3/movie/603",
            json=movie_fixtures.MOVIE_INFO_RESPONSE,
            status=200,
        )

        # Set up device.
        device = Device.objects.create(device_token="abc123")
        headers = {"HTTP_X_DEVICE_ID": device.device_token}

        # Set up election.
        election = factories.create_election(device)
        candidate = factories.create_candidate(election, election.participants.first())

        url = reverse("votes", kwargs={"candidate_id": candidate.id})
        response = self.client.post(url, data={}, format="json", **headers)

        # Verify response.
        self.assertEqual(response.status_code, 201)
        self.assertDictEqual(response.json(), fixtures.EXPECTED_RESPONSE_CREATE_VOTE)

        # Verify vote in database.
        self.assertEqual(candidate.votes.count(), 1)
        self.assertEqual(
            candidate.votes.first().participant, election.participants.first()
        )
Пример #3
0
    def test_post_reactivates_deleted_vote(self):
        responses.add(
            responses.GET,
            "https://api.themoviedb.org/3/movie/603",
            json=movie_fixtures.MOVIE_INFO_RESPONSE,
            status=200,
        )

        # Set up election.
        election = factories.create_election()
        participant = election.participants.first()
        candidate = factories.create_candidate(election, participant)
        vote = Vote.objects.create(
            participant=participant,
            candidate=candidate,
            deleted_at=datetime.datetime.now(tz=timezone.utc),
        )
        headers = {"HTTP_X_DEVICE_ID": participant.device.device_token}

        url = reverse("votes", kwargs={"candidate_id": candidate.id})
        response = self.client.post(url, data={}, format="json", **headers)

        # Verify response.
        self.assertEqual(response.status_code, 201)

        # Verify vote in database.
        vote.refresh_from_db()
        self.assertIsNone(vote.deleted_at)
Пример #4
0
    def test_delete_removes_vote(self):
        responses.add(
            responses.GET,
            "https://api.themoviedb.org/3/movie/603",
            json=movie_fixtures.MOVIE_INFO_RESPONSE,
            status=200,
        )

        # Set up election.
        election = factories.create_election()
        participant = election.participants.first()
        candidate = factories.create_candidate(election, participant)
        vote = Vote.objects.create(participant=participant, candidate=candidate)
        headers = {"HTTP_X_DEVICE_ID": participant.device.device_token}

        url = reverse("votes", kwargs={"candidate_id": candidate.id})
        response = self.client.delete(url, **headers)

        # Verify response.
        self.assertEqual(response.status_code, 200)
        self.assertDictEqual(response.json(), fixtures.EXPECTED_RESPONSE_DELETE_VOTE)

        # Verify vote in database.
        vote.refresh_from_db()
        self.assertIsNotNone(vote.deleted_at)
Пример #5
0
    def test_get_election(self):
        responses.add(
            responses.GET,
            "https://api.themoviedb.org/3/movie/603",
            json=movie_fixtures.MOVIE_INFO_RESPONSE,
            status=200,
        )

        # Set up device.
        device = Device.objects.create(device_token="some-device-token")
        headers = {"HTTP_X_DEVICE_ID": device.device_token}

        # Set up election.
        election = factories.create_election(device=device)
        candidate = factories.create_candidate(election, election.participants.first())
        Vote.objects.create(
            participant=election.participants.first(), candidate=candidate
        )

        url = reverse("election_detail", kwargs={"election_id": election.external_id})
        response = self.client.get(url, **headers)

        # Verify response.
        self.assertEqual(response.status_code, 200)
        self.assertDictEqual(
            response.json(), fixtures.EXPECTED_RESPONSE_GET_ELECTION_DETAIL
        )
Пример #6
0
    def test_put_elections_returns_error_when_participant_is_not_initiator_of_election(
        self,
    ):
        # Set up device.
        first_device = factories.create_device(device_token="abc123")
        second_device = factories.create_device(device_token="def456")
        headers = {"HTTP_X_DEVICE_ID": second_device.device_token}

        # Set up election.
        election = factories.create_election(first_device)
        second_participant = factories.create_participant(election, second_device)
        headers = {"HTTP_X_DEVICE_ID": second_participant.device.device_token}

        url = reverse("election_detail", kwargs={"election_id": election.external_id})
        title = "This is an updated test title."
        data = {"title": title}

        response = self.client.put(url, data=data, format="json", **headers)

        # Verify response.
        self.assertEqual(response.status_code, 400)
        response_json = response.json()
        self.assertEqual(
            response_json["error"],
            "The participant is not the initiator of the election.",
        )
Пример #7
0
    def test_delete_returns_error_when_candidate_does_not_exist(self):
        # Set up election.
        election = factories.create_election()
        participant = election.participants.first()
        headers = {"HTTP_X_DEVICE_ID": participant.device.device_token}

        url = reverse("votes", kwargs={"candidate_id": "123"})
        response = self.client.delete(url, **headers)

        # Verify response.
        self.assertEqual(response.status_code, 404)
Пример #8
0
    def test_post_returns_error_when_device_header_is_missing(self):
        # Set up election
        election = factories.create_election()

        url = reverse("participants", kwargs={"election_id": election.external_id})
        response = self.client.post(url, data={"name": "Jane"}, format="json")

        # Verify response.
        self.assertEqual(response.status_code, 400)
        response_json = response.json()
        self.assertEqual(response_json["error"], "Missing header: `X-Device-ID`.")
Пример #9
0
    def test_post_returns_error_when_participant_not_part_of_election(self):
        # Set up device.
        first_device = factories.create_device(device_token="abc123")
        second_device = factories.create_device(device_token="def456")
        headers = {"HTTP_X_DEVICE_ID": second_device.device_token}

        # Set up elections.
        first_election = factories.create_election(first_device, external_id="abc123")
        factories.create_election(second_device, external_id="def456")

        url = reverse("candidates", kwargs={"election_id": first_election.external_id})
        response = self.client.post(
            url, data={"movie_id": "603"}, format="json", **headers
        )

        # Verify response.
        self.assertEqual(response.status_code, 400)
        response_json = response.json()
        self.assertEqual(
            response_json["error"],
            "Participant with the provided device ID does not exist in the election.",
        )
Пример #10
0
    def test_get_elections_when_participant_is_deleted(self):
        device = Device.objects.create(device_token="some-device-token")
        headers = {"HTTP_X_DEVICE_ID": device.device_token}
        election = factories.create_election(device=device, external_id="abc123")
        participant = election.participants.first()
        participant.deleted_at = datetime.datetime.now(tz=timezone.utc)
        participant.save()

        response = self.client.get(self.url, **headers)

        # Verify response.
        self.assertEqual(response.status_code, 200)
        self.assertDictEqual(response.json(), {"results": []})
Пример #11
0
    def test_put_elections_returns_error_when_device_header_is_missing(self):
        # Set up election.
        election = factories.create_election()

        url = reverse("election_detail", kwargs={"election_id": election.external_id})
        title = "This is an updated test title."
        data = {"title": title}

        response = self.client.put(url, data=data, format="json")

        # Verify response.
        self.assertEqual(response.status_code, 400)
        response_json = response.json()
        self.assertEqual(response_json["error"], "Missing header: `X-Device-ID`.")
Пример #12
0
    def test_post_returns_error_when_name_is_missing(self):
        # Set up device.
        device = Device.objects.create(device_token="some-device-token")
        headers = {"HTTP_X_DEVICE_ID": device.device_token}

        # Set up election.
        election = factories.create_election()

        url = reverse("participants", kwargs={"election_id": election.external_id})
        response = self.client.post(url, data={}, format="json", **headers)

        # Verify response.
        self.assertEqual(response.status_code, 400)
        response_json = response.json()
        self.assertEqual(response_json["error"], "Missing parameter: `name`.")
Пример #13
0
    def test_post_returns_error_when_participant_does_not_exist(self):
        headers = {"HTTP_X_DEVICE_ID": "invalid_device_id"}

        # Set up election.
        election = factories.create_election()

        url = reverse("candidates", kwargs={"election_id": election.external_id})
        response = self.client.post(
            url, data={"movie_id": "123"}, format="json", **headers
        )

        # Verify response.
        self.assertEqual(response.status_code, 400)
        response_json = response.json()
        self.assertEqual(
            response_json["error"],
            "Participant with the provided device ID does not exist in the election.",
        )
Пример #14
0
    def test_delete_returns_error_when_participant_does_not_exist(self):
        # Set up election.
        election = factories.create_election()
        participant = election.participants.first()
        candidate = factories.create_candidate(election, participant)
        Vote.objects.create(participant=participant, candidate=candidate)
        headers = {"HTTP_X_DEVICE_ID": "invalid_device_id"}

        url = reverse("votes", kwargs={"candidate_id": candidate.id})
        response = self.client.delete(url, **headers)

        # Verify response.
        self.assertEqual(response.status_code, 400)
        response_json = response.json()
        self.assertEqual(
            response_json["error"],
            "Participant with the provided device ID does not exist in the election.",
        )
Пример #15
0
    def test_delete_returns_error_is_participant_already_deleted(self):
        # Set up election.
        election = factories.create_election()

        # Set up participant.
        device = Device.objects.create(device_token="some-device-token")
        factories.create_participant(election, device, is_deleted=True)
        headers = {"HTTP_X_DEVICE_ID": device.device_token}

        url = reverse("participants", kwargs={"election_id": election.external_id})
        response = self.client.delete(url, **headers)

        # Verify response.
        self.assertEqual(response.status_code, 400)
        response_json = response.json()
        self.assertEqual(
            response_json["error"],
            "Participant with the provided device ID does not exist in the election.",
        )
Пример #16
0
    def test_post_returns_error_when_candidate_already_exists(self):
        # Set up device.
        device = Device.objects.create(device_token="abc123")
        headers = {"HTTP_X_DEVICE_ID": device.device_token}

        # Set up election.
        election = factories.create_election(device)
        candidate = factories.create_candidate(election, election.participants.first())

        url = reverse("candidates", kwargs={"election_id": election.external_id})
        response = self.client.post(
            url, data={"movie_id": candidate.movie_id}, format="json", **headers
        )

        # Verify response.
        self.assertEqual(response.status_code, 400)
        response_json = response.json()
        self.assertEqual(
            response_json["error"], "A candidate for the movie provided already exists."
        )
Пример #17
0
    def test_delete_removes_participant(self):
        # Set up election.
        election = factories.create_election()

        # Set up participant.
        device = Device.objects.create(device_token="some-device-token")
        participant = factories.create_participant(election, device)
        headers = {"HTTP_X_DEVICE_ID": device.device_token}

        url = reverse("participants", kwargs={"election_id": election.external_id})
        response = self.client.delete(url, **headers)

        # Verify response.
        self.assertEqual(response.status_code, 200)
        self.assertDictEqual(
            response.json(), fixtures.EXPECTED_RESPONSE_CREATE_ELECTION
        )

        # Verify participant in database.
        participant = election.participants.filter(device=device).first()
        self.assertIsNotNone(participant.deleted_at)
Пример #18
0
    def test_post_reactivates_deleted_participant(self):
        # Set up election.
        election = factories.create_election()
        name = "Jill"

        # Set up participant.
        device = Device.objects.create(device_token="some-device-token")
        participant = factories.create_participant(election, device, is_deleted=True)
        headers = {"HTTP_X_DEVICE_ID": device.device_token}

        url = reverse("participants", kwargs={"election_id": election.external_id})
        response = self.client.post(url, data={"name": name}, format="json", **headers)

        # Verify response.
        self.assertEqual(response.status_code, 201)

        # Verify participant in database.
        participant = election.participants.filter(device=device).first()
        self.assertIsNotNone(participant)
        self.assertIsNone(participant.deleted_at)
        self.assertEqual(participant.name, name)
Пример #19
0
    def test_put_elections_returns_error_when_when_participant_does_not_exist(self):
        # Set up device.
        device = Device.objects.create(device_token="some-device-token")
        headers = {"HTTP_X_DEVICE_ID": device.device_token}

        # Set up election.
        election = factories.create_election()

        url = reverse("election_detail", kwargs={"election_id": election.external_id})
        title = "This is an updated test title."
        data = {"title": title}

        response = self.client.put(url, data=data, format="json", **headers)

        # Verify response.
        self.assertEqual(response.status_code, 400)
        response_json = response.json()
        self.assertEqual(
            response_json["error"],
            "Participant with the provided device ID does not exist in the election.",
        )
Пример #20
0
    def test_delete_returns_error_if_vote_already_deleted(self):
        # Set up election.
        election = factories.create_election()
        participant = election.participants.first()
        candidate = factories.create_candidate(election, participant)
        Vote.objects.create(
            participant=participant,
            candidate=candidate,
            deleted_at=datetime.datetime.now(tz=timezone.utc),
        )
        headers = {"HTTP_X_DEVICE_ID": participant.device.device_token}

        url = reverse("votes", kwargs={"candidate_id": candidate.id})
        response = self.client.delete(url, **headers)

        # Verify response.
        self.assertEqual(response.status_code, 400)
        response_json = response.json()
        self.assertEqual(
            response_json["error"],
            "Vote with the provided device ID does not exist for the candidate.",
        )
Пример #21
0
    def test_post_returns_error_when_participant_already_voted_for_candidate(self):
        # Set up device.
        device = Device.objects.create(device_token="abc123")
        headers = {"HTTP_X_DEVICE_ID": device.device_token}

        # Set up election.
        election = factories.create_election(device)
        candidate = factories.create_candidate(election, election.participants.first())
        Vote.objects.create(
            participant=election.participants.first(), candidate=candidate
        )

        url = reverse("votes", kwargs={"candidate_id": candidate.id})
        response = self.client.post(url, data={}, format="json", **headers)

        # Verify response.
        self.assertEqual(response.status_code, 400)
        response_json = response.json()
        self.assertEqual(
            response_json["error"],
            "The participant has already voted for the candidate.",
        )
Пример #22
0
    def test_put(self):
        # Set up device.
        device = Device.objects.create(device_token="some-device-token")
        headers = {"HTTP_X_DEVICE_ID": device.device_token}

        # Set up election.
        election = factories.create_election(device=device)

        url = reverse("election_detail", kwargs={"election_id": election.external_id})
        title = "This is an updated test title."
        data = {"title": title}

        response = self.client.put(url, data=data, format="json", **headers)

        # Verify response.
        self.assertEqual(response.status_code, 200)
        self.assertDictEqual(
            response.json(), fixtures.EXPECTED_RESPONSE_UPDATE_ELECTION
        )

        # Verify election in database.
        election = Election.objects.filter(external_id=election.external_id).first()
        self.assertEqual(election.title, title)
Пример #23
0
    def test_post_returns_error_when_movie_does_not_exist(self):
        responses.add(
            responses.GET,
            "https://api.themoviedb.org/3/movie/603",
            json=movie_fixtures.MOVIE_NOT_FOUND_RESPONSE,
            status=404,
        )

        # Set up device.
        device = Device.objects.create(device_token="abc123")
        headers = {"HTTP_X_DEVICE_ID": device.device_token}

        # Set up election.
        election = factories.create_election(device)

        url = reverse("candidates", kwargs={"election_id": election.external_id})
        response = self.client.post(
            url, data={"movie_id": "603"}, format="json", **headers
        )

        # Verify response.
        self.assertEqual(response.status_code, 400)
        response_json = response.json()
        self.assertEqual(response_json["error"], "Movie does not exist.")
Пример #24
0
    def test_post_creates_participant(self):
        # Set up device.
        device = Device.objects.create(device_token="some-device-token")
        headers = {"HTTP_X_DEVICE_ID": device.device_token}

        # Set up election.
        election = factories.create_election()
        name = "Jane"

        url = reverse("participants", kwargs={"election_id": election.external_id})
        response = self.client.post(url, data={"name": name}, format="json", **headers)

        # Verify response.
        self.assertEqual(response.status_code, 201)
        self.assertDictEqual(
            response.json(), fixtures.EXPECTED_RESPONSE_CREATE_PARTICIPANT
        )

        # Verify participant in database.
        participant = election.participants.filter(device=device).first()
        self.assertIsNotNone(participant)
        self.assertEqual(participant.name, name)
        self.assertEqual(participant.device_id, device.id)
        self.assertFalse(participant.is_initiator)