def setUp(self): self.client = APIClient() self.organizer = UserFactory(email='*****@*****.**') self.organizer.is_guide = True self.organizer.save() self.user_one = UserFactory(email='*****@*****.**') self.event = EventFactory(organizer=self.organizer) self.client.force_authenticate(self.organizer)
def setUp(self): self.user = UserFactory(email=fake.safe_email()) self.another_user = UserFactory(email=fake.safe_email()) self.client = APIClient() self.client.force_authenticate(user=self.user) self.event = EventFactory(organizer=self.user) self.participant = ParticipantFactory(event=self.event, user=self.user) self.event_comment = EventCommentFactory(event=self.event, user=self.user)
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 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 setUp(self): 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.event_two = EventFactory(organizer=self.organizer) self.participant_one = ParticipantFactory(event=self.event, user=self.organizer) self.participant_two = ParticipantFactory(event=self.event, user=self.follower) self.participant_three = ParticipantFactory(event=self.event_two, user=self.organizer) self.client = APIClient()
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 setUp(self): self.user = UserFactory(first_name=fake.first_name()) self.event = EventFactory(organizer=self.user) self.event_comment = EventCommentFactory(event=self.event, user=self.user) self.deleted_comment = EventCommentFactory(event=self.event, user=self.user) self.deleted_comment.delete() self.deleted_comment.refresh_from_db() self.client = APIClient()
def setUp(self): self.existed_user = UserFactory(email=fake.safe_email(), first_name=fake.first_name()) self.existed_user.is_guide = True self.existed_user.save() self.event = EventFactory(organizer=self.existed_user, event_time=make_aware( datetime.datetime.now())) self.participant = ParticipantFactory(event=self.event, user=self.existed_user) self.client = APIClient()
def setUp(self): self.email = fake.safe_email() self.organizer = UserFactory(email=self.email) self.organizer.first_name = fake.first_name() self.organizer.family_name = fake.last_name() self.organizer.introduction = fake.text(max_nb_chars=1000) self.organizer.is_guide = True self.organizer.save() self.event = EventFactory(organizer=self.organizer, event_time=make_aware( datetime.datetime.now()))
def setUp(self): self.organizer_user = UserFactory(email=fake.safe_email()) self.organizer_user.first_name = fake.first_name() self.organizer_user.is_guide = True self.organizer_user.save() self.participant_user = UserFactory(email=fake.safe_email()) self.participant_user.first_name = fake.first_name() self.participant_user.save() self.event = EventFactory(organizer=self.organizer_user) self.organizer = ParticipantFactory(event=self.event, user=self.organizer_user) self.participant = ParticipantFactory(event=self.event, user=self.participant_user)
def test_retrieve_organized_event_pagination(self): """Test retrieving organized events""" count = 0 while count < 10: EventFactory(organizer=self.existed_user) count += 1 url = organized_event_url(self.existed_user.id) res = self.client.get(url) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(len(res.data['results']), 10) res = self.client.get(url, {'page': 2}) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(len(res.data['results']), 1)
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 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_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 setUp(self): self.organizer = UserFactory(email='*****@*****.**') self.event = EventFactory(organizer=self.organizer)
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 PrivateParticipantApiTests(TestCase): """Test the authorized user event API""" def setUp(self): self.client = APIClient() self.organizer = UserFactory(email='*****@*****.**') self.organizer.is_guide = True self.organizer.save() self.user_one = UserFactory(email='*****@*****.**') self.event = EventFactory(organizer=self.organizer) self.client.force_authenticate(self.organizer) def test_create_event_successful(self): """Test create a new event""" payload = { 'title': fake.text(max_nb_chars=255), 'description': fake.text(max_nb_chars=2000), 'organizer': self.organizer.id, 'image': '', 'event_time': make_aware(datetime.datetime.now()), 'address': fake.address(), 'fee': 500, 'status': '1' } res = self.client.post(EVENT_URL, payload) self.assertEqual(res.status_code, status.HTTP_201_CREATED) def test_not_creating_event_by_tourist(self): """Test not creating a new event by tourist""" self.client.force_authenticate(self.user_one) 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': 500, 'status': '1' } res = self.client.post(EVENT_URL, payload) self.assertEqual(res.status_code, status.HTTP_403_FORBIDDEN) def test_logically_delete_event_false(self): """Test logically disable to delete an event for unsuitable user""" url = detail_url(self.event.id) res = self.client.delete(url) self.assertEqual(res.status_code, status.HTTP_403_FORBIDDEN) self.assertTrue(self.event.is_active) def test_logically_delete_event_successful(self): """Test logically delete an event for authenticated user""" self.organizer.is_staff = True self.organizer.save() url = detail_url(self.event.id) res = self.client.delete(url) self.assertEqual(res.status_code, status.HTTP_204_NO_CONTENT) self.event.refresh_from_db() self.assertFalse(self.event.is_active) def test_false_logically_deleting_event_for_not_organizer(self): """Test false logically deleting an event for not organizer""" self.client.force_authenticate(self.user_one) url = detail_url(self.event.id) res = self.client.delete(url) self.assertEqual(res.status_code, status.HTTP_403_FORBIDDEN) def test_update_event_successful(self): """Test updating an event successful""" with tempfile.NamedTemporaryFile(suffix='.jpg') as ntf: img = Image.new('RGB', (10, 10)) img.save(ntf, format='JPEG') ntf.seek(0) payload = { 'title': fake.text(max_nb_chars=255), 'description': fake.text(max_nb_chars=2000), 'image': ntf, 'event_time': make_aware(datetime.datetime.now()), 'address': fake.address(), 'fee': '600', 'status': '2' } url = detail_url(self.event.id) res = self.client.patch(url, payload, format='multipart') self.assertEqual(res.status_code, status.HTTP_200_OK) self.event.image.delete() def test_false_updating_event_for_anyone(self): """Test false updating an event for not organizer""" self.client.force_authenticate(self.user_one) 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', 'status': '2' } url = detail_url(self.event.id) res = self.client.patch(url, payload) 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)
def setUp(self): self.organaizer = UserFactory() self.event = EventFactory(organizer=self.organaizer) self.event_comment = EventCommentFactory(event=self.event, user=self.organaizer)
class PrivateUserApiTests(TestCase): """Test API requests that require authentication""" def setUp(self): self.user = UserFactory(email=fake.safe_email()) self.another_user = UserFactory(email=fake.safe_email()) self.client = APIClient() self.client.force_authenticate(user=self.user) self.event = EventFactory(organizer=self.user) self.participant = ParticipantFactory(event=self.event, user=self.user) self.event_comment = EventCommentFactory(event=self.event, user=self.user) def test_retrieve_designated_user_short_name(self): """Test retrieving a user short name""" url = short_name_url(self.user.id) res = self.client.get(url) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertJSONEqual(res.content, {'short_name': 'noname'}) familyName = 'family' self.user.family_name = familyName self.user.save() res = self.client.get(url) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertJSONEqual(res.content, {'short_name': familyName}) firstName = 'first' self.user.first_name = firstName self.user.save() res = self.client.get(url) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertJSONEqual(res.content, {'short_name': firstName}) def test_retrieve_user_email(self): """Test success retrive user e-mail""" url = email_url(self.user.id) res = self.client.get(url) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(res.data['email'], self.user.email) def test_update_user_email(self): """Test success updating the user e-mail""" email = fake.safe_email() url = email_url(self.user.id) res = self.client.patch(url, {'email': email}) self.assertEqual(res.status_code, status.HTTP_200_OK) updated_user = get_user_model().objects.latest('updated_at') self.assertEqual(email, updated_user.email) def test_full_update_user_profile(self): """Test full updating the user profile for authenticated user""" change_first_name = fake.first_name() change_family_name = fake.last_name() change_introduction = fake.text(max_nb_chars=1000) with tempfile.NamedTemporaryFile(suffix='.jpg') as ntf: img = Image.new('RGB', (10, 10)) img.save(ntf, format='JPEG') ntf.seek(0) payload = { 'first_name': change_first_name, 'family_name': change_family_name, 'introduction': change_introduction, 'icon': ntf, 'is_guide': True, } url = detail_url(self.user.id) res = self.client.patch(url, payload, format='multipart') self.assertEqual(res.status_code, status.HTTP_200_OK) self.user.refresh_from_db() self.assertEqual(self.user.first_name, change_first_name) self.assertEqual(self.user.family_name, change_family_name) self.assertEqual(self.user.introduction, change_introduction) self.assertTrue(self.user.is_guide) self.assertTrue(os.path.exists(self.user.icon.path)) self.user.icon.delete() def test_update_email_bad_request(self): """Test updating email with wrong method""" url = detail_url(self.user.id) res = self.client.patch(url, {'email': 'badrequest'}) self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST) def test_update_password_bad_request(self): """Test updating password with wrong method""" url = detail_url(self.user.id) res = self.client.patch(url, {'password': '******'}) self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST) def test_upload_image_bad_request(self): """Test uploading an invalid image""" url = detail_url(self.user.id) res = self.client.patch(url, {'icon': 'notimage'}, format='multipart') self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST) def test_delete_user_successful(self): """Test logically deleting the user""" self.user.is_guide = True self.user.save() url = detail_url(self.user.id) res = self.client.delete(url) self.assertEqual(res.status_code, status.HTTP_204_NO_CONTENT) self.user.refresh_from_db() self.event.refresh_from_db() self.participant.refresh_from_db() self.event_comment.refresh_from_db() self.assertFalse(self.user.is_active) self.assertFalse(self.event.is_active) self.assertFalse(self.participant.is_active) self.assertFalse(self.event_comment.is_active) def test_retrieve_user_email_by_another_user(self): """Test false retrieving user e-mail by another user""" url = email_url(self.another_user.id) res = self.client.get(url) self.assertEqual(res.status_code, status.HTTP_403_FORBIDDEN) def test_update_user_profile_by_another_user(self): """Test false updating the user profile by another user""" url = detail_url(self.another_user.id) res = self.client.patch(url, {'first_name': 'firstname'}) self.assertEqual(res.status_code, status.HTTP_403_FORBIDDEN) def test_delete_user_by_another_user(self): """Test false logically deleting the user by another user""" url = detail_url(self.another_user.id) res = self.client.delete(url) self.assertEqual(res.status_code, status.HTTP_403_FORBIDDEN)
def setUp(self): self.user = UserFactory() self.event = EventFactory(organizer=self.user)
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)