def test_get_does_not_show_other_user_surveys(self): user1 = UserFactory.create() user2 = UserFactory.create(pk=2, username="******") SurveyFactory.create(user_id=user1.pk) survey2 = SurveyFactory.create(pk=2, user_id=user2.pk) response = self.client.get("/users/{}/surveys{}".format( user1.id, survey2.id)) self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
def test_list_does_not_show_other_user_surveys(self): user1 = UserFactory.create() user2 = UserFactory.create(pk=2, username="******") SurveyFactory.create(user_id=user1.pk) SurveyFactory.create(pk=2, user_id=user2.pk) response = self.client.get("/users/{}/surveys".format(user1.id)) resp_content = json.loads(response.content) user_ids = list(map(lambda x: x['user_id'], resp_content)) self.assertIn(user1.id, user_ids) self.assertNotIn(user2.id, user_ids) self.assertEqual(len(resp_content), 1)
def test_post(self): user = UserFactory.create() survey = SurveyFactory.create(user_id=user.pk) available_places = survey.available_places data = {"survey_id": survey.id, "user_id": user.id} self.assertEqual(SurveyResponse.objects.count(), 0) response = self.client.post("/users/{}/survey-responses".format( user.id), data=data) resp_content = json.loads(response.content) self.assertEqual(SurveyResponse.objects.count(), 1) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(response.get('Content-Type', ''), 'application/json') self.assertEqual(resp_content['survey_id'], survey.id) self.assertEqual(resp_content['user_id'], user.id) self.assertIsNotNone(resp_content['created_at']) self.assertEqual( Survey.objects.get(pk=survey.pk).available_places, available_places - 1)
def test_delete(self): user = UserFactory.create() survey = SurveyFactory.create(user_id=user.pk) response = self.client.delete("/users/{}/surveys/{}".format( user.id, survey.id)) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
def test_survey_name_is_under_255_characters(self): name = "a" * 256 available_places = 4 user = UserFactory.create() survey = Survey.create(name, available_places, user.id) with self.assertRaises(forms.ValidationError): survey.full_clean()
def test_list(self): user = UserFactory.create() SurveyFactory.create(user_id=user.pk) response = self.client.get("/users/{}/surveys".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)
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("/surveys/{}/survey-responses/{}".format( survey.pk, survey_response.pk)) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
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)
def test_put(self): client = APIClient() user = UserFactory.create() survey = SurveyFactory.create(user_id=user.pk) data = {"name": "Opinions about oranges", "available_places": 30} response = client.put("/users/{}/surveys/{}/".format( user.id, survey.id), data=data) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.get('Content-Type', ''), 'application/json')
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)
def test_get(self): user = UserFactory.create() survey = SurveyFactory.create(user_id=user.pk) response = self.client.get("/users/{}/surveys/{}".format( user.id, survey.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.id) self.assertEqual(resp_content['available_places'], survey.available_places) self.assertEqual(resp_content['user_id'], user.id)
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')
def test_model_fields(self): factory = APIRequestFactory() user = UserFactory.create() request = factory.get("/surveys/{}".format(1)) survey_factory = SurveyFactory(user_id=user.pk) serialiser_context = { 'request': Request(request), } for field_name in ['name', 'available_places', 'user_id']: self.assertEqual( SurveySerialiser(instance=survey_factory, context=serialiser_context).data[field_name], getattr(survey_factory, field_name))
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("/survey-responses") 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)
def test_post_if_unavailable(self): user = UserFactory.create() survey = SurveyFactory.create(available_places=0, user_id=user.pk) data = {"survey_id": survey.id, "user_id": user.id} original_count = SurveyResponse.objects.count() response = self.client.post("/users/{}/survey-responses".format( user.id), data=data) self.assertEqual(SurveyResponse.objects.count(), original_count) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertIn('No more available places for this survey', response.content.decode("utf-8"))
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)
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'])
def test_create(self): factory = APIRequestFactory() user = UserFactory.create() request = factory.post("/surveys/") survey_factory = SurveyFactory(user_id=user.pk) serialiser_context = { 'request': Request(request), } SurveySerialiser(instance=survey_factory, context=serialiser_context) survey = Survey.objects.all().last() self.assertEqual(survey.name, survey_factory.name) self.assertEqual(survey.available_places, survey_factory.available_places) self.assertEqual(survey.user, user)
def test_post(self): user = UserFactory.create() data = { "name": "Opinions about apples", "available_places": 30, "user_id": user.id } self.assertEqual(Survey.objects.count(), 0) response = self.client.post("/users/{}/surveys".format(user.id), data=data) resp_content = json.loads(response.content) self.assertEqual(Survey.objects.count(), 1) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(response.get('Content-Type', ''), 'application/json') self.assertEqual(resp_content['name'], data['name']) self.assertEqual(resp_content['available_places'], data['available_places'])
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)
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)
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'], )