Ejemplo n.º 1
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)
Ejemplo n.º 2
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()
Ejemplo n.º 3
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)
Ejemplo n.º 4
0
    def test_retrieve_event_comment_pagination_success(self):
        """Test retrieving event comments with pagination"""
        count = 0
        while count < 15:
            EventCommentFactory(event=self.event, user=self.user)
            count += 1

        url = detail_url(self.event.id)
        res = self.client.get(url, {'page': 1})
        self.assertEqual(res.status_code, status.HTTP_200_OK)
        self.assertEqual(len(res.data['results']), 15)

        res = self.client.get(url, {'page': 2})
        self.assertEqual(res.status_code, status.HTTP_200_OK)
        self.assertEqual(len(res.data['results']), 2)
Ejemplo n.º 5
0
class PublicEventCommentApiTests(TestCase):
    """Test that publcly available event comments API"""
    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 test_retrieve_event_comment_success(self):
        """Test retrieving event comments"""
        url = detail_url(self.event.id)
        res = self.client.get(url, {'page': 1})
        self.assertEqual(res.status_code, status.HTTP_200_OK)

        event_comments = EventComment.objects.all().order_by('updated_at')
        expected_json_dict_list = []
        for event_comment in event_comments:
            expected_json_dict = {
                'id': event_comment.id,
                'event': event_comment.event.id,
                'user': event_comment.user.id,
                'first_name': event_comment.user.first_name,
                'icon': event_comment.user.icon_url,
                'comment': event_comment.display_comment,
                'status': event_comment.status,
                'brief_updated_at': event_comment.brief_updated_at
            }
            expected_json_dict_list.append(expected_json_dict)

        expected_json = {
            "count": len(event_comments),
            "next": None,
            "previous": None,
            "results": expected_json_dict_list
        }
        self.assertJSONEqual(res.content, expected_json)

    def test_retrieve_event_comment_pagination_success(self):
        """Test retrieving event comments with pagination"""
        count = 0
        while count < 15:
            EventCommentFactory(event=self.event, user=self.user)
            count += 1

        url = detail_url(self.event.id)
        res = self.client.get(url, {'page': 1})
        self.assertEqual(res.status_code, status.HTTP_200_OK)
        self.assertEqual(len(res.data['results']), 15)

        res = self.client.get(url, {'page': 2})
        self.assertEqual(res.status_code, status.HTTP_200_OK)
        self.assertEqual(len(res.data['results']), 2)

    def test_retrieve_event_comment_pagination_false(self):
        """Test retrieving event comments false with pagination"""
        url = detail_url(self.event.id)
        res = self.client.get(url, {'page': 0})
        self.assertEqual(res.status_code, status.HTTP_404_NOT_FOUND)

        res = self.client.get(url, {'page': 2})
        self.assertEqual(res.status_code, status.HTTP_404_NOT_FOUND)

    def test_not_retrieve_deleted_comments(self):
        """Test not retrieving deleted comments"""
        url = detail_url(self.event.id)
        res = self.client.get(url, {'page': 1})
        self.assertEqual(res.status_code, status.HTTP_200_OK)

        expected_json_dict = {
            'id': self.event_comment.id,
            'event': self.event_comment.event.id,
            'user': self.event_comment.user.id,
            'first_name': self.event_comment.user.first_name,
            'icon': self.event_comment.user.icon_url,
            'comment': self.event_comment.display_comment,
            'status': self.event_comment.status,
            'brief_updated_at': self.event_comment.brief_updated_at
        }
        self.assertIn(expected_json_dict, list(res.data['results']))
        self.assertEqual(dict(res.data['results'][0]), expected_json_dict)

    def test_create_event_comment_for_unauthorized_user(self):
        """Test creating a new event comment"""
        payload = {
            'comment': 'testcomment',
        }
        url = detail_url(self.event.id)
        res = self.client.post(url, payload)
        self.assertEqual(res.status_code, status.HTTP_401_UNAUTHORIZED)

    def test_delete_event_comment_for_unauthorized_user(self):
        """Test delete an event comment for authenticated user"""
        url = delete_url(self.event.id, self.event_comment.id)
        res = self.client.delete(url)
        self.assertEqual(res.status_code, status.HTTP_401_UNAUTHORIZED)
Ejemplo n.º 6
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)
Ejemplo n.º 7
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)