def test_not_creating_participant_with_wrong_user_id_type(self):
     """Test not creating participant with worng user id type"""
     new_participant = UserFactory(email=fake.safe_email())
     new_participant.first_name = fake.first_name()
     new_participant.save()
     data = {'event': self.event.id, 'user': new_participant}
     serializer = ListCreateParticipantSerializer(data=data)
     self.assertFalse(serializer.is_valid())
     self.assertCountEqual(serializer.errors.keys(), ['user'])
Пример #2
0
    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)
Пример #3
0
    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)
Пример #4
0
 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)
Пример #7
0
 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_creating_participant_successful(self):
        """Test creating participant successful"""
        new_participant = UserFactory(email='*****@*****.**')
        new_participant.first_name = 'newparticipant'
        new_participant.save()
        data = {'event': self.event.id, 'user': new_participant.id}
        serializer = ListCreateParticipantSerializer(data=data)
        self.assertTrue(serializer.is_valid())
        serializer.save()

        expected_dict = {
            'user': new_participant.id,
            'first_name': new_participant.first_name,
            'icon': '/static/images/no_user_image.png'
        }
        self.assertEqual(serializer.data, expected_dict)
 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 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()
Пример #11
0
 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()
Пример #12
0
 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_designated_user_short_name(self):
        """Test retrieving a user short name"""
        noname_user = UserFactory(email=fake.safe_email())

        serializer = UserShortNameSerializer(instance=noname_user)
        self.assertEqual(serializer.data, {'short_name': 'noname'})

        familyName = 'family'
        noname_user.family_name = familyName
        noname_user.save()
        serializer = UserShortNameSerializer(instance=noname_user)
        self.assertEqual(serializer.data, {'short_name': familyName})

        firstName = 'first'
        noname_user.first_name = firstName
        noname_user.save()
        serializer = UserShortNameSerializer(instance=noname_user)
        self.assertEqual(serializer.data, {'short_name': firstName})
Пример #14
0
class PublicUserApiTests(TestCase):
    """Test the users API (public)"""
    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 test_retrieve_designated_user(self):
        """Test retrieving a user"""
        url = show_user_url(self.existed_user.id)

        res = self.client.get(url)
        self.assertEqual(res.status_code, status.HTTP_200_OK)

        user = get_user_model().objects.get(pk=res.data['id'])
        expected_json_dict = {
            'id': user.id,
            'short_name': user.first_name,
            'introduction': user.introduction,
            'icon_url': user.icon_url,
            'is_guide': user.is_guide
        }
        self.assertJSONEqual(res.content, expected_json_dict)

    def test_retrieve_designated_user_for_update(self):
        """Test retrieving a user for update"""
        url = detail_url(self.existed_user.id)
        res = self.client.get(url)
        self.assertEqual(res.status_code, status.HTTP_200_OK)

        user = get_user_model().objects.get(pk=res.data['id'])
        expected_json_dict = {
            'id': user.id,
            'first_name': user.first_name,
            'family_name': user.family_name,
            'introduction': user.introduction,
            'icon_url': user.icon_url,
            'is_guide': user.is_guide,
        }
        self.assertJSONEqual(res.content, expected_json_dict)

    def test_retrieve_organized_event(self):
        """Test retrieving organized events"""
        url = organized_event_url(self.existed_user.id)
        res = self.client.get(url)
        self.assertEqual(res.status_code, status.HTTP_200_OK)

        expected_json_dict = {
            "count":
            1,
            "next":
            None,
            "previous":
            None,
            "results": [{
                'id':
                self.event.id,
                'title':
                self.event.title,
                'image':
                self.event.image_url,
                'event_time':
                self.event.event_time.strftime('%Y-%m-%d %H:%M:%S'),
                'address':
                self.event.address,
                "participant_count":
                1
            }]
        }
        self.assertJSONEqual(res.content, expected_json_dict)

    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_retrieve_joined_event(self):
        """Test retrieving joined events"""
        url = joined_event_url(self.existed_user.id)
        res = self.client.get(url)
        self.assertEqual(res.status_code, status.HTTP_200_OK)

        expected_json_dict = {
            "count":
            1,
            "next":
            None,
            "previous":
            None,
            "results": [{
                'id':
                self.event.id,
                'title':
                self.event.title,
                'image':
                self.event.image_url,
                'event_time':
                self.event.event_time.strftime('%Y-%m-%d %H:%M:%S'),
                'address':
                self.event.address,
                "participant_count":
                1
            }]
        }
        self.assertJSONEqual(res.content, expected_json_dict)

    def test_retrieve_joined_event_pagination(self):
        """Test retrieving joined events"""
        count = 0
        while count < 10:
            ParticipantFactory(event=EventFactory(organizer=self.existed_user),
                               user=self.existed_user)
            count += 1

        url = joined_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_retrieve_user_email_by_unauthorized_user(self):
        """Test false retrieving user e-mail by unauthorized user"""
        url = email_url(self.existed_user.id)
        res = self.client.get(url)
        self.assertEqual(res.status_code, status.HTTP_401_UNAUTHORIZED)

    def test_update_user_profile_by_unauthorized_user(self):
        """Test false updating the user profile by unauthorized user"""
        url = detail_url(self.existed_user.id)
        res = self.client.patch(url, {'first_name': 'firstname'})

        self.assertEqual(res.status_code, status.HTTP_401_UNAUTHORIZED)

    def test_delete_event_by_unauthorized_user(self):
        """Test false logically deleting the user by unauthenticated user"""
        url = detail_url(self.existed_user.id)
        res = self.client.delete(url)
        self.assertEqual(res.status_code, status.HTTP_401_UNAUTHORIZED)
Пример #15
0
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.organaizer = UserFactory()
     self.event = EventFactory(organizer=self.organaizer)
     self.event_comment = EventCommentFactory(event=self.event,
                                              user=self.organaizer)
Пример #17
0
 def setUp(self):
     self.user = UserFactory()
     self.event = EventFactory(organizer=self.user)
class ParticipantSerializerApiTests(TestCase):
    """Test participant serializer API"""
    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_participant_successful(self):
        """Test retrieving participant successful"""
        participants = Participant.objects.filter(event=self.event)
        serializer = ListCreateParticipantSerializer(instance=participants,
                                                     many=True)

        expected_dict_list = [{
            'user': self.organizer_user.id,
            'first_name': self.organizer_user.first_name,
            'icon': '/static/images/no_user_image.png'
        }, {
            'user': self.participant_user.id,
            'first_name': self.participant_user.first_name,
            'icon': '/static/images/no_user_image.png'
        }]
        self.assertEqual(serializer.data, expected_dict_list)

    def test_creating_participant_successful(self):
        """Test creating participant successful"""
        new_participant = UserFactory(email='*****@*****.**')
        new_participant.first_name = 'newparticipant'
        new_participant.save()
        data = {'event': self.event.id, 'user': new_participant.id}
        serializer = ListCreateParticipantSerializer(data=data)
        self.assertTrue(serializer.is_valid())
        serializer.save()

        expected_dict = {
            'user': new_participant.id,
            'first_name': new_participant.first_name,
            'icon': '/static/images/no_user_image.png'
        }
        self.assertEqual(serializer.data, expected_dict)

    def test_not_creating_participant_with_wrong_event_id_type(self):
        """Test not creating participant with worng event id type"""
        new_participant = UserFactory(email=fake.safe_email())
        new_participant.first_name = fake.first_name()
        new_participant.save()
        data = {'event': self.event, 'user': new_participant.id}
        serializer = ListCreateParticipantSerializer(data=data)
        self.assertFalse(serializer.is_valid())
        self.assertCountEqual(serializer.errors.keys(), ['event'])

    def test_not_creating_participant_with_wrong_user_id_type(self):
        """Test not creating participant with worng user id type"""
        new_participant = UserFactory(email=fake.safe_email())
        new_participant.first_name = fake.first_name()
        new_participant.save()
        data = {'event': self.event.id, 'user': new_participant}
        serializer = ListCreateParticipantSerializer(data=data)
        self.assertFalse(serializer.is_valid())
        self.assertCountEqual(serializer.errors.keys(), ['user'])

    def test_update_participant_status_successful(self):
        """Test updating participant status successful"""
        data = {'status': Participant.Status.CANCEL.value}
        serializer = UpdateParticipantSerializer(instance=self.participant,
                                                 data=data)
        self.assertTrue(serializer.is_valid())
        serializer.save()
        self.assertEqual(serializer.data['status'], data['status'])

        data = {'status': Participant.Status.JOIN.value}
        serializer = UpdateParticipantSerializer(instance=self.participant,
                                                 data=data)
        self.assertTrue(serializer.is_valid())
        serializer.save()
        self.assertEqual(serializer.data['status'], data['status'])

    def test_not_updating_participant_status_with_undesignated_number(self):
        """Test not updating participant status with undesignated number"""
        data = {'status': 2}
        serializer = UpdateParticipantSerializer(instance=self.participant,
                                                 data=data)
        self.assertFalse(serializer.is_valid())
        self.assertCountEqual(serializer.errors.keys(), ['status'])

    def test_updating_wrong_field_with_UpdateParticipantSerializer(self):
        """Test not logically delting with UpdateParticipantSerializer"""
        serializer = UpdateParticipantSerializer(instance=self.participant,
                                                 data={'is_active': False})
        self.assertTrue(serializer.is_valid())
        serializer.save()
        self.assertTrue(self.participant.is_active)
Пример #19
0
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)
Пример #20
0
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)
 def setUp(self):
     self.organizer = UserFactory(email='*****@*****.**')
     self.event = EventFactory(organizer=self.organizer)
class UserSerializerApiTests(TestCase):
    """Test user serializer API"""
    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 test_retrieve_user_for_update(self):
        """Test retrieve user fields for update"""
        serializer = UserSerializer(instance=self.organizer)
        expected_dict = {
            'id': self.organizer.id,
            'first_name': self.organizer.first_name,
            'family_name': self.organizer.family_name,
            'introduction': self.organizer.introduction,
            'icon_url': '/static/images/no_user_image.png',
            'is_guide': self.organizer.is_guide
        }
        self.assertEqual(serializer.data, expected_dict)

    def test_retrieve_user_successful(self):
        """Test retrieve user fields successful"""
        serializer = ShowUserSerializer(instance=self.organizer)
        expected_dict = {
            'id': self.organizer.id,
            'short_name': self.organizer.first_name,
            'introduction': self.organizer.introduction,
            'icon_url': '/static/images/no_user_image.png',
            'is_guide': self.organizer.is_guide
        }
        self.assertEqual(serializer.data, expected_dict)

    def test_retrieve_designated_user_short_name(self):
        """Test retrieving a user short name"""
        noname_user = UserFactory(email=fake.safe_email())

        serializer = UserShortNameSerializer(instance=noname_user)
        self.assertEqual(serializer.data, {'short_name': 'noname'})

        familyName = 'family'
        noname_user.family_name = familyName
        noname_user.save()
        serializer = UserShortNameSerializer(instance=noname_user)
        self.assertEqual(serializer.data, {'short_name': familyName})

        firstName = 'first'
        noname_user.first_name = firstName
        noname_user.save()
        serializer = UserShortNameSerializer(instance=noname_user)
        self.assertEqual(serializer.data, {'short_name': firstName})

    def test_update_user_successful(self):
        """Test update user fields successful"""
        data = {'is_guide': False}
        serializer = UserSerializer(instance=self.organizer, data=data)
        self.assertTrue(serializer.is_valid())

        serializer.save()
        self.assertEqual(serializer.data['is_guide'], data['is_guide'])

    def test_long_user_first_name_validate(self):
        """Test retrieve user fields successful"""
        long_first_name = "long_first_name " * 500
        data = {'first_name': long_first_name}
        serializer = UserSerializer(instance=self.organizer, data=data)
        self.assertFalse(serializer.is_valid())
        self.assertCountEqual(serializer.errors.keys(), ['first_name'])

    def test_long_user_family_name_validate(self):
        """Test retrieve user fields successful"""
        long_family_name = "long_family_name " * 500
        data = {'family_name': long_family_name}
        serializer = UserSerializer(instance=self.organizer, data=data)
        self.assertFalse(serializer.is_valid())
        self.assertCountEqual(serializer.errors.keys(), ['family_name'])

    def test_long_introduction_validate(self):
        """Test retrieve user fields successful"""
        long_introduction = "logn_introduction " * 500
        data = {'introduction': long_introduction}
        serializer = UserSerializer(instance=self.organizer, data=data)
        self.assertFalse(serializer.is_valid())
        self.assertCountEqual(serializer.errors.keys(), ['introduction'])

    def test_not_updating_user_icon_validate(self):
        """Test retrieve user fields successful"""
        data = {'icon': 1}
        serializer = UserSerializer(instance=self.organizer, data=data)
        self.assertFalse(serializer.is_valid())
        self.assertCountEqual(serializer.errors.keys(), ['icon'])

    def test_not_updating_is_guide_with_not_boolean(self):
        """Test updating is_guide with not boolean"""
        data = {'is_guide': 'test'}
        serializer = UserSerializer(instance=self.organizer, data=data)
        self.assertFalse(serializer.is_valid())
        self.assertCountEqual(serializer.errors.keys(), ['is_guide'])

    def test_updating_other_field_with_userserialize(self):
        """Test not updating other fields"""
        serializer = UserSerializer(instance=self.organizer,
                                    data={'email': fake.safe_email()})
        self.assertTrue(serializer.is_valid())
        serializer.save()
        self.assertEqual(self.organizer.email, self.email)

    def test_retrieve_user_email_successful(self):
        """Test retrieving user email successful"""
        serializer = UserEmailSerializer(instance=self.organizer)
        expected_dict = {'email': self.email}
        self.assertEqual(serializer.data, expected_dict)

    def test_update_user_email_successful(self):
        """Test validate user email successful"""
        data = {'email': fake.safe_email()}
        serializer = UserEmailSerializer(instance=self.organizer, data=data)
        self.assertTrue(serializer.is_valid())

        serializer.save()
        self.assertEqual(serializer.data, data)

    def test_update_user_email_with_not_email_format(self):
        """Test validate user email false with not email format"""
        data = {'email': 'organizer_changed'}
        serializer = UserEmailSerializer(instance=self.organizer, data=data)
        self.assertFalse(serializer.is_valid())
        self.assertCountEqual(serializer.errors.keys(), ['email'])

    def test_retrieve_user_events_successful(self):
        """Test retrieve user events successful"""
        events = Event.objects.filter(organizer=self.organizer.id,
                                      is_active=True)
        serializer = UserEventsSerializer(instance=events, many=True)

        expected_dict = {
            'id': self.event.id,
            'title': self.event.title,
            'image': self.event.image_url,
            'event_time': self.event.event_time.strftime('%Y-%m-%d %H:%M:%S'),
            'address': self.event.address,
            'participant_count': 0
        }
        self.assertEqual(dict(serializer.data[0]), expected_dict)
Пример #23
0
 def setUp(self):
     self.client = Client()
     self.admin_user = get_user_model().objects.create_superuser(
         email='*****@*****.**', password='******')
     self.client.force_login(self.admin_user)
     self.user = UserFactory(first_name='testfirst', family_name='testlast')