class PrivateParticipantApiTests(TestCase): """Test the authorized user participant API""" def setUp(self): self.client = APIClient() self.organizer = UserFactory(email=fake.safe_email(), first_name=fake.first_name()) self.new_organizer = UserFactory(email=fake.safe_email(), first_name=fake.first_name()) self.follower = UserFactory(email=fake.safe_email(), first_name=fake.first_name()) self.event = EventFactory(organizer=self.organizer) self.private_event = EventFactory(organizer=self.organizer) self.private_event.status = Event.Status.PRIVATE.value, self.private_event.save() self.cancel_event = EventFactory(organizer=self.organizer) self.cancel_event.status = Event.Status.CANCEL.value self.cancel_event.save() self.participant = ParticipantFactory(event=self.event, user=self.follower) def test_create_participant_successful(self): """Test creating a new participant""" self.client.force_authenticate(self.new_organizer) url = listCreate_url(self.event.id) res = self.client.post(url) self.assertEqual(res.status_code, status.HTTP_201_CREATED) def test_not_create_the_same_participant(self): """Test not creating the same participant""" self.client.force_authenticate(self.new_organizer) url = listCreate_url(self.event.id) res = self.client.post(url) self.assertEqual(res.status_code, status.HTTP_201_CREATED) res = self.client.post(url) self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST) def test_not_create_participant_in_private_event(self): """Test not creating a new participant in private event""" self.client.force_authenticate(self.new_organizer) url = listCreate_url(self.private_event.id) res = self.client.post(url) self.assertEqual(res.status_code, status.HTTP_403_FORBIDDEN) def test_not_create_participant_in_canceled_event(self): """Test not creating a new participant in canceled event""" self.client.force_authenticate(self.new_organizer) url = listCreate_url(self.cancel_event.id) res = self.client.post(url) self.assertEqual(res.status_code, status.HTTP_403_FORBIDDEN) def test_not_create_participant_in_deleted_event(self): """Test not creating a new participant in deleted event""" self.client.force_authenticate(self.new_organizer) self.event.delete() url = listCreate_url(self.event.id) res = self.client.post(url) self.assertEqual(res.status_code, status.HTTP_403_FORBIDDEN) def test_cancel_participant(self): """Test canceling a participant""" self.client.force_authenticate(self.follower) url = cancel_url(self.event.id) res = self.client.patch(url) self.assertEqual(res.status_code, status.HTTP_200_OK) self.participant.refresh_from_db() self.assertEqual(self.participant.status, Participant.Status.CANCEL.value) def test_join_participant(self): """Test joinning a participant""" self.client.force_authenticate(self.follower) self.participant.status = Participant.Status.CANCEL.value self.participant.save() self.participant.refresh_from_db() url = join_url(self.event.id) res = self.client.patch(url) self.assertEqual(res.status_code, status.HTTP_200_OK) self.participant.refresh_from_db() self.assertEqual(self.participant.status, Participant.Status.JOIN.value)
class PrivateEventCommentApiTests(TestCase): """Test the authorized user Event comment API""" def setUp(self): self.client = APIClient() self.organizer = UserFactory(email=fake.safe_email(), first_name=fake.first_name()) self.comment_user = UserFactory(email=fake.safe_email(), first_name=fake.first_name()) self.event = EventFactory(organizer=self.organizer) self.private_event = EventFactory(organizer=self.organizer) self.private_event.status = Event.Status.PRIVATE.value self.private_event.save() self.organizer_comment = EventCommentFactory(event=self.event, user=self.organizer) self.event_comment = EventCommentFactory(event=self.event, user=self.comment_user) self.client.force_authenticate(self.organizer) def test_create_event_comment_successful(self): """Test creating a new event comment""" str_comment = fake.text(max_nb_chars=500) payload = { 'comment': str_comment, } url = detail_url(self.event.id) res = self.client.post(url, payload) self.assertEqual(res.status_code, status.HTTP_201_CREATED) new_event_comment = EventComment.objects.latest('updated_at') self.assertEqual(new_event_comment.comment, str_comment) def test_not_create_event_comment_to_private_event(self): """Test not creating a new comment to private event""" payload = { 'comment': fake.text(max_nb_chars=500), } url = detail_url(self.private_event.id) res = self.client.post(url, payload) self.assertEqual(res.status_code, status.HTTP_403_FORBIDDEN) def test_change_event_comment_status(self): """Test change event comment status""" self.client.force_authenticate(self.comment_user) url = status_url(self.event.id, self.event_comment.id) res = self.client.patch(url) self.assertEqual(res.status_code, status.HTTP_200_OK) self.event_comment.refresh_from_db() self.assertEqual(self.event_comment.status, EventComment.Status.EDITED.value) def test_not_create_event_comment_to_deleted_event(self): """Test not creating a new comment to deleted event""" self.event.delete() payload = { 'comment': fake.text(max_nb_chars=500), } url = detail_url(self.event.id) res = self.client.post(url, payload) self.assertEqual(res.status_code, status.HTTP_403_FORBIDDEN) def test_delete_event_comment_false(self): """Test delete the event comment by unsuitable user""" url = delete_url(self.event.id, self.organizer_comment.id) res = self.client.delete(url) self.assertEqual(res.status_code, status.HTTP_403_FORBIDDEN) self.assertTrue(self.event.is_active) def test_delete_event_comment_successful(self): """Test delete the event comment by authenticated user""" self.organizer.is_staff = True self.organizer.save() url = delete_url(self.event.id, self.organizer_comment.id) res = self.client.delete(url) self.assertEqual(res.status_code, status.HTTP_204_NO_CONTENT) self.organizer_comment.refresh_from_db() self.assertFalse(self.organizer_comment.is_active) def test_not_delete_event_comment_for_anyone(self): """Test not delete an event comment by not suitable user""" url = delete_url(self.event.id, self.event_comment.id) res = self.client.delete(url) self.assertEqual(res.status_code, status.HTTP_403_FORBIDDEN)
class PublicParticipantApiTests(TestCase): """Test that publcly available participant API""" def setUp(self): self.organizer = UserFactory(email='*****@*****.**') self.first_event = EventFactory( organizer=self.organizer, event_time=make_aware(datetime.datetime.now()) ) self.second_event = EventFactory( organizer=self.organizer, title=fake.text(max_nb_chars=255), event_time=make_aware( datetime.datetime.now() + datetime.timedelta(days=1)), description=fake.text(max_nb_chars=2000), fee='700' ) self.client = APIClient() def test_retrieve_event_list_success(self): """Test retrieving event list""" self.first_event.status = Event.Status.PUBLIC.value self.first_event.save() self.second_event.status = Event.Status.PUBLIC.value self.second_event.save() today = datetime.date.today() tomorrow = today + timedelta(days=1) res = self.client.get(EVENT_URL, {'start': today, 'end': tomorrow}) self.assertEqual(res.status_code, status.HTTP_200_OK) expected_json_dict_list = [ { 'id': self.first_event.id, 'title': self.first_event.title, 'image': self.first_event.image_url, 'event_time': self.first_event.event_time.strftime('%Y-%m-%d %H:%M:%S'), 'address': self.first_event.address, 'participant_count': 0 }, { 'id': self.second_event.id, 'title': self.second_event.title, 'image': self.second_event.image_url, 'event_time': self.second_event.event_time.strftime('%Y-%m-%d %H:%M:%S'), 'address': self.second_event.address, 'participant_count': 0 } ] expected_json = { "count": 2, "next": None, "previous": None, "results": expected_json_dict_list } self.assertJSONEqual(res.content, expected_json) def test_retrieve_event_pagination_success(self): """Test retrieving event with pagination""" self.first_event.status = Event.Status.PUBLIC.value self.first_event.save() self.second_event.status = Event.Status.PUBLIC.value self.second_event.save() count = 0 while count < 30: EventFactory( organizer=self.organizer, status=Event.Status.PUBLIC.value ) count += 1 today = datetime.date.today() tomorrow = today + timedelta(days=1) res = self.client.get(EVENT_URL, {'start': today, 'end': tomorrow}) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(len(res.data['results']), 30) res = self.client.get( EVENT_URL, {'start': today, 'end': tomorrow, 'page': 2}) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(len(res.data['results']), 2) def test_retrieving_events_for_a_day_successful(self): """Test retrieving events for a day""" self.first_event.status = Event.Status.PUBLIC.value self.first_event.save() self.second_event.status = Event.Status.PUBLIC.value self.second_event.save() EventFactory( organizer=self.organizer, event_time=make_aware( datetime.datetime.now() + datetime.timedelta(days=2)), status=Event.Status.PUBLIC.value ) today = datetime.date.today() tomorrow = today + timedelta(days=1) res = self.client.get(EVENT_URL, {'start': today, 'end': tomorrow}) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(len(res.data['results']), 2) def test_not_retrieving_events_by_wrong_query_parameters_name(self): """Test not retrieving events by wrong query parameter name""" EventFactory( organizer=self.organizer, event_time=make_aware( datetime.datetime.now() + datetime.timedelta(days=2)) ) today = datetime.date.today() tomorrow = today + timedelta(days=1) res = self.client.get(EVENT_URL, {'test1': today, 'test2': tomorrow}) self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST) def test_not_retrieving_events_by_wrong_type_query_parameters(self): """Test not retrieving events by wrong type query parameters""" EventFactory( organizer=self.organizer, event_time=make_aware( datetime.datetime.now() + datetime.timedelta(days=2)) ) res = self.client.get(EVENT_URL, {'start': 'test', 'end': 'test'}) self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST) def test_not_retrieving_events_by_wrong_query_parameters_number(self): """Test not retrieving events by wrong query parameters number""" EventFactory( organizer=self.organizer, event_time=make_aware( datetime.datetime.now() + datetime.timedelta(days=2)) ) today = datetime.date.today() res = self.client.get(EVENT_URL, {'start': today}) self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST) def test_retrieve_event_success(self): """Test retrieving event""" url = detail_url(self.second_event.id) res = self.client.get(url) self.assertEqual(res.status_code, status.HTTP_200_OK) event = Event.objects.get(id=self.second_event.id) organizer = get_user_model().objects.get(id=event.organizer_id) expected_json_dict = { 'id': event.id, 'title': event.title, 'description': event.description, 'organizer': event.organizer_id, 'organizer_full_name': 'noname', 'organizer_icon': organizer.icon_url, 'image': event.image_url, 'event_time': event.brief_event_time, 'address': event.address, 'fee': event.fee, 'status': event.status, 'brief_updated_at': event.brief_updated_at } self.assertJSONEqual(res.content, expected_json_dict) def test_create_event_for_unauthorized_user(self): """Test false creating a new event""" payload = { 'title': fake.text(max_nb_chars=255), 'description': fake.text(max_nb_chars=2000), 'image': '', 'organizer_id': self.organizer.id, 'event_time': make_aware(datetime.datetime.now()) .strftime('%Y-%m-%d %H:%M:%S'), 'address': fake.address(), 'fee': 500, } res = self.client.post(EVENT_URL, payload) self.assertEqual(res.status_code, status.HTTP_401_UNAUTHORIZED) def test_delete_event_for_unauthorized_user(self): """Test false logically deleting an event for not authenticated user""" url = detail_url(self.first_event.id) res = self.client.delete(url) self.assertEqual(res.status_code, status.HTTP_401_UNAUTHORIZED) def test_update_event_for_unauthorized_user(self): """Test false updating an event for not authenticated user""" payload = { 'title': fake.text(max_nb_chars=255), 'description': fake.text(max_nb_chars=2000), 'image': '', 'event_time': make_aware(datetime.datetime.now()), 'address': fake.address(), 'fee': '600', } url = detail_url(self.first_event.id) res = self.client.patch(url, payload) self.assertEqual(res.status_code, status.HTTP_401_UNAUTHORIZED)