Ejemplo n.º 1
0
    def test_list(self):
        user = UserFactory.create()
        survey = SurveyFactory.create(user_id=user.pk)

        SurveyResponseFactory.create(survey_id=survey.pk, user_id=user.pk)

        response = self.client.get("/users/{}/survey-responses".format(
            user.id))
        resp_content = json.loads(response.content)

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.get('Content-Type', ''), 'application/json')
        self.assertEqual(len(resp_content), 1)
Ejemplo n.º 2
0
    def test_get_does_not_show_other_user_surveys(self):
        user1 = UserFactory.create()
        user2 = UserFactory.create(username="******")
        survey = SurveyFactory.create(user_id=user1.pk)

        SurveyResponseFactory.create(survey_id=survey.pk, user_id=user1.pk)

        survey_response2 = SurveyResponseFactory.create(survey_id=survey.pk,
                                                        user_id=user2.pk)

        response = self.client.get("/users/{}/survey-responses/{}".format(
            user1.id, survey_response2))

        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
Ejemplo n.º 3
0
    def test_get_does_not_show_other_survey_responses(self):
        user = UserFactory.create()
        survey1 = SurveyFactory.create(user_id=user.pk)
        survey2 = SurveyFactory.create(user_id=user.pk)

        SurveyResponseFactory.create(survey_id=survey1.pk, user_id=user.pk)

        survey_response2 = SurveyResponseFactory.create(survey_id=survey2.pk,
                                                        user_id=user.pk)

        response = self.client.get("/surveys/{}/survey-responses/{}".format(
            survey1.pk, survey_response2))

        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
Ejemplo n.º 4
0
    def test_put(self):
        user = UserFactory.create()
        survey = SurveyFactory.create(user_id=user.pk)

        SurveyResponseFactory.create(survey_id=survey.pk, user_id=user.pk)

        data = {"survey_id": 100, "user_id": 200}

        response = self.client.put("/users/{}/survey-responses/{}".format(
            user.id, survey.id),
                                   data=data)

        self.assertEqual(response.status_code,
                         status.HTTP_405_METHOD_NOT_ALLOWED)
        self.assertEqual(response.get('Content-Type', ''), 'application/json')
Ejemplo n.º 5
0
    def test_list_does_not_show_other_survey_responses(self):
        user = UserFactory.create()
        survey1 = SurveyFactory.create(user_id=user.pk)
        survey2 = SurveyFactory.create(user_id=user.pk)

        SurveyResponseFactory.create(survey_id=survey1.pk, user_id=user.pk)

        SurveyResponseFactory.create(survey_id=survey2.pk, user_id=user.pk)

        response = self.client.get("/surveys/{}/survey-responses".format(
            survey1.pk))
        resp_content = json.loads(response.content)

        survey_ids = list(map(lambda x: x['survey_id'], resp_content))

        self.assertIn(survey1.id, survey_ids)
        self.assertNotIn(survey2.id, survey_ids)
Ejemplo n.º 6
0
    def test_delete(self):
        user = UserFactory.create()
        survey = SurveyFactory.create(user_id=user.pk)

        survey_response = SurveyResponseFactory.create(survey_id=survey.pk,
                                                       user_id=user.pk)

        response = self.client.delete("/users/{}/survey-responses/{}".format(
            user.id, survey_response.id))

        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
Ejemplo n.º 7
0
    def test_list_does_not_show_other_user_surveys(self):
        user1 = UserFactory.create()
        user2 = UserFactory.create(username="******")
        survey = SurveyFactory.create(user_id=user1.pk)

        SurveyResponseFactory.create(survey_id=survey.pk, user_id=user1.pk)

        SurveyResponseFactory.create(survey_id=survey.pk, user_id=user2.pk)

        response = self.client.get("/users/{}/survey-responses".format(
            user1.id))
        resp_content = json.loads(response.content)

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.get('Content-Type', ''), 'application/json')

        user_ids = list(map(lambda x: x['user_id'], resp_content))

        self.assertIn(user1.pk, user_ids)
        self.assertNotIn(user2.pk, user_ids)
        self.assertEqual(len(resp_content), 1)
Ejemplo n.º 8
0
    def test_get(self):
        user = UserFactory.create()
        survey = SurveyFactory.create(user_id=user.pk)

        survey_response = SurveyResponseFactory.create(survey_id=survey.pk,
                                                       user_id=user.pk)

        response = self.client.get("/users/{}/survey-responses/{}".format(
            user.id, survey_response.id))

        resp_content = json.loads(response.content)

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.get('Content-Type', ''), 'application/json')
        self.assertEqual(resp_content['id'], survey_response.pk)
        self.assertEqual(resp_content['survey_id'], survey.id)
        self.assertEqual(resp_content['user_id'], user.id)
        self.assertIsNotNone(resp_content['created_at'])
Ejemplo n.º 9
0
    def test_create_if_unavailable(self):
        factory = APIRequestFactory()
        user = UserFactory.create()
        survey = SurveyFactory.create(available_places=0, user_id=user.pk)

        survey_response_factory = SurveyResponseFactory(user_id=user.pk,
                                                        survey_id=survey.pk)

        original_count = SurveyResponse.objects.count()

        request = factory.post("/survey-responses/")

        serialiser_context = {
            'request': Request(request),
        }

        SurveyResponseSerialiser(instance=survey_response_factory,
                                 context=serialiser_context)

        self.assertEqual(SurveyResponse.objects.count(), original_count)
Ejemplo n.º 10
0
    def test_create(self):
        factory = APIRequestFactory()
        user = UserFactory.create()
        survey = SurveyFactory.create(user_id=user.pk)

        self.assertEqual(SurveyResponse.objects.count(), 0)

        request = factory.post("/survey-responses/")

        survey_response_factory = SurveyResponseFactory(user_id=user.pk,
                                                        survey_id=survey.pk)

        serialiser_context = {
            'request': Request(request),
        }

        SurveyResponseSerialiser(instance=survey_response_factory,
                                 context=serialiser_context)

        survey_response = SurveyResponse.objects.all().last()

        self.assertEqual(SurveyResponse.objects.count(), 1)
        self.assertEqual(survey_response.survey, survey)
        self.assertEqual(survey_response.user, user)
Ejemplo n.º 11
0
    def test_model_fields(self):
        factory = APIRequestFactory()
        user = UserFactory.create()
        survey = SurveyFactory.create(user_id=user.pk)
        request = factory.get("/survey-responses/{}".format(1))
        survey_response_factory = SurveyResponseFactory(survey_id=survey.pk,
                                                        user_id=user.pk)

        serialiser_context = {
            'request': Request(request),
        }

        survey_response_serialiser = SurveyResponseSerialiser(
            instance=survey_response_factory, context=serialiser_context).data

        self.assertEqual(survey_response_serialiser['survey_id'],
                         survey_response_factory.survey_id)

        self.assertEqual(survey_response_serialiser['user_id'],
                         survey_response_factory.user_id)
        self.assertIn(
            survey_response_factory.created_at.strftime("%Y-%m-%dT%H:%M:%S"),
            survey_response_serialiser['created_at'],
        )