Esempio n. 1
0
    def test_follow_toggle(self):
        post = PostFactory(user=self.user1)

        community_attrs = {
            'content_type_id': ContentType.objects.get_for_model(Post).id,
            'object_id': post.id,
        }
        url_follow_toggle = reverse('follow-toggle', kwargs=community_attrs)

        # Try to follow as unauthenticated
        response = self.client.get(url_follow_toggle)
        self.assertEqual(response.status_code, 302)

        # Try again as authenticated
        self.client.force_login(self.user2)
        response = self.client.get(url_follow_toggle)
        self.assertEqual(response.status_code, 200)
        self.assertJSONEqual(response.content, {
            'status': 'ok',
            'action': 'Started Following'
        })

        # Now unfollow
        response = self.client.get(url_follow_toggle)
        self.assertEqual(response.status_code, 200)
        self.assertJSONEqual(response.content, {
            'status': 'ok',
            'action': 'Unfollowed'
        })
Esempio n. 2
0
 def setUp(self) -> None:
     self.user_harry = UserFactory(username='******')
     self.user_hermione = UserFactory(username='******')
     self.post = PostFactory(user=self.user_harry,
                             status='published',
                             title='Velocità con #animato')
     self.client = Client()
Esempio n. 3
0
class NotificationViewsTest(TestViewsMixin):
    def setUp(self):
        super().setUp()
        self.post = PostFactory(user=self.user1, title='Velocità con #animato')
        self.post.refresh_from_db()

    def test_notification_feed_view(self):
        # Ensure that anonymous access is not allowed
        response = self.client.get(reverse('notifications'))
        self.assertEqual(response.status_code, 302)
        # TODO(fsiddi) ensure that redirect url is correct
        # Ensure that notifications are listed
        self.client.force_login(self.user1)
        response = self.client.get(reverse('notifications'))
        self.assertEqual(len(response.context['notifications']), 0)
        # Generate one like activity
        self.post.like_toggle(self.user2)
        response = self.client.get(reverse('notifications'))
        # Ensure that one notification is found
        self.assertEqual(len(response.context['notifications']), 1)

    def test_mark_as_read(self):
        mark_as_read_url = reverse('api-notifications-mark-all-as-read')
        # Ensure that anonymous access is not allowed
        response = self.client.post(mark_as_read_url)
        self.assertEqual(response.status_code, 302)
        # Ensure that GET is not allowed
        self.client.force_login(self.user1)
        response = self.client.get(mark_as_read_url)
        self.assertEqual(response.status_code, 405)

        # Generate one like activity
        self.post.like_toggle(self.user2)

        # Ensure that we have one unread notification
        notifications_count = dillo.models.feeds.FeedEntry.objects.filter(
            user=self.user1,
            category='notification',
            is_read=False,
        ).count()
        self.assertEqual(notifications_count, 1)

        # Perform POST request
        response = self.client.post(mark_as_read_url)
        # Ensure response in 'success'
        self.assertJSONEqual(response.content, {'status': 'success'})

        # Check that unread notifications count is 0
        notifications_count = dillo.models.feeds.FeedEntry.objects.filter(
            user=self.user1,
            category='notification',
            is_read=False,
        ).count()
        self.assertEqual(notifications_count, 0)
Esempio n. 4
0
    def setUp(self):

        self.user1 = UserFactory(username='******')
        self.user2 = UserFactory(username='******')
        self.post = PostFactory(user=self.user1, title='Velocità con #animato')
Esempio n. 5
0
class FeedElementModelTest(TestCase):
    def setUp(self):

        self.user1 = UserFactory(username='******')
        self.user2 = UserFactory(username='******')
        self.post = PostFactory(user=self.user1, title='Velocità con #animato')

    def test_user_follows_own_post(self):
        self.assertIn(self.user1, models_actstream.followers(self.post))

    def test_user_liked_your_post_notification(self):
        """When user2 likes user1's post. User1 gets a notification."""
        # We publish the post created in setUp
        self.post.publish()
        self.post.like_toggle(self.user2)
        # Check that a like notification was generated for user1
        notification = self.user1.feed_entries.first()
        self.assertEqual('notification', notification.category)
        self.assertEqual('liked', notification.action.verb)
        # User2 does not have any notification
        self.assertEqual(0, self.user2.feed_entries.count())
        # Test that one email message has been sent
        # TODO(fsiddi) Improve email sending
        self.assertEqual(len(mail.outbox), 1)

    def test_user_commented_on_your_post_notification(self):
        # Create comment
        comment_content = 'Nice idea! #idea'
        comment = CommentForPostFactory(user=self.user2,
                                        content=comment_content,
                                        entity=self.post)
        notification = self.user1.feed_entries.first()
        self.assertEqual('notification', notification.category)
        self.assertEqual('commented', notification.action.verb)
        self.assertEqual(comment.id, notification.action.action_object.id)

        # Own comments do not generate notifications
        comment_content = 'My own comment'
        CommentForPostFactory(user=self.user1,
                              content=comment_content,
                              entity=self.post)

        notifications_count = self.user1.feed_entries.count()
        # We still have one notification, from the previous activity
        self.assertEqual(1, notifications_count)

        # If user does not follow post, do not generate notifications
        unfollow(self.user1, self.post)
        comment_content = 'A second comment!'
        CommentForPostFactory(user=self.user1,
                              content=comment_content,
                              entity=self.post)

        notifications_count = self.user1.feed_entries.count()
        # We still have one notification, from the previous activity
        self.assertEqual(1, notifications_count)

    def test_user_liked_your_comment_notification(self):
        # Create comment
        comment_content = 'Nice idea! #idea'
        comment = CommentForPostFactory(user=self.user1,
                                        content=comment_content,
                                        entity=self.post)

        comment.like_toggle(self.user2)
        notification = self.user1.feed_entries.first()
        # Verify that notification exists
        self.assertEqual('notification', notification.category)
        self.assertEqual('liked', notification.action.verb)
        self.assertEqual(comment_content,
                         notification.action.action_object.content)

    def test_user_replied_to_your_comment_notification(self):
        # We publish the post created in setUp
        self.post.publish()
        # Create comment
        comment_content = 'Nice idea! #idea'
        comment = CommentForPostFactory(user=self.user1,
                                        content=comment_content,
                                        entity=self.post)

        reply_content = 'I concur.'
        # Add reply from the same user
        CommentForPostFactory(user=self.user1,
                              content=reply_content,
                              entity=self.post,
                              parent_comment=comment)

        # This generates no notifications
        notifications_count = self.user1.feed_entries.filter(
            category='notification').count()
        self.assertEqual(0, notifications_count)

        # Add reply from another user
        reply = CommentForPostFactory(user=self.user2,
                                      content=reply_content,
                                      entity=self.post,
                                      parent_comment=comment)
        notification = self.user1.feed_entries.first()
        # Verify that notification exists
        self.assertEqual('notification', notification.category)
        self.assertEqual('replied', notification.action.verb)
        self.assertEqual(reply.content,
                         notification.action.action_object.content)
        # Verify that one email notification was sent
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].subject,
                         'Your comment has a new reply!')

    def test_user_mentioned_you_in_post(self):
        pass

    def test_user_mentioned_you_in_comment(self):
        pass

    def test_user_liked_a_post_where_you_are_mentioned(self):
        pass

    def test_user_liked_a_comment_where_you_are_mentioned(self):
        pass

    def test_user_follows_you_notification(self):
        # We publish the post created in setUp
        self.post.publish()
        follow(self.user2, self.user1)
        notification = self.user1.feed_entries.first()
        # Verify that notification exists
        self.assertEqual('notification', notification.category)
        self.assertEqual('started following', notification.action.verb)
        self.assertEqual(self.user2, notification.action.actor)
        # Verify that unfollow/follow does not create a new notifications
        unfollow(self.user2, self.user1)
        follow(self.user2, self.user1)
        self.assertEqual(2, self.user1.feed_entries.count())
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].subject, 'You have a new follower!')

    def test_post_in_timeline_for_user_followed(self):
        from taggit.models import Tag

        # We publish the post created in setUp
        self.post.publish()

        follow(self.user2, self.user1)
        follow(self.user2, Tag.objects.get(name='animato'))
        new_post = dillo.models.posts.Post.objects.create(
            user=self.user1, title='Follow me with #animato')
        new_post.publish()

        # Verify that the post appears in the user timeline
        expected_timeline_post = (self.user2.feed_entries.filter(
            category='timeline').first().action.action_object)
        self.assertEqual(new_post, expected_timeline_post)

        # Verify that the there is only one entry in the timeline,
        # despite user2 follows both user1 and the animato tag
        self.assertEqual(
            2,
            self.user2.feed_entries.filter(category='timeline').count())

    def test_post_in_timeline_for_tag_followed(self):
        from taggit.models import Tag

        # We publish the post created in setUp
        self.post.publish()

        follow(self.user2, Tag.objects.get(name='animato'))
        follow(self.user2, Tag.objects.create(name='b3d'))
        new_post = dillo.models.posts.Post.objects.create(
            user=self.user1, title='Follow me with #animato #b3d')
        new_post.publish()

        # Verify that the post appears in the user timeline
        expected_timeline_post = (self.user2.feed_entries.filter(
            category='timeline').first().action.action_object)
        self.assertEqual(new_post, expected_timeline_post)

        # Verify that the there is only one entry in the timeline,
        # despite user2 follows both the animato and the b3d tag
        self.assertEqual(
            2,
            self.user2.feed_entries.filter(category='timeline').count())

    def test_post_in_timeline_is_deleted(self):
        follow(self.user2, self.user1)
        new_post = dillo.models.posts.Post.objects.create(
            user=self.user1, title='Follow me with #animato')
        new_post.publish()

        # Verify that the there is one entry in the timeline
        self.assertEqual(
            1,
            self.user2.feed_entries.filter(category='timeline').count())

        # Delete the post
        new_post.delete()

        # There will be no entry in the timeline
        self.assertEqual(
            0,
            self.user2.feed_entries.filter(category='timeline').count())

    def test_post_in_own_timeline(self):
        # User 1 timeline contains one post
        self.assertEqual(
            0,
            self.user1.feed_entries.filter(category='timeline').count())
        # We publish the post created in setUp
        self.post.publish()
        # The timeline features one entry
        self.assertEqual(
            1,
            self.user1.feed_entries.filter(category='timeline').count())
Esempio n. 6
0
    def setUp(self):

        self.post: Post = PostFactory()
        self.user: User = self.post.user
Esempio n. 7
0
    def setUp(self):

        self.user = UserFactory(username='******')
        self.post = PostFactory(user=self.user, title='Looking for #animato')
Esempio n. 8
0
class PostModelTest(TestCase):
    def setUp(self):

        self.user = UserFactory(username='******')
        self.post = PostFactory(user=self.user, title='Looking for #animato')

    def test_post_creation(self):

        # Create Post with a hasthag
        saved_post = Post.objects.get(id=self.post.id)
        # Ensure the tag has been assigned to the Post
        self.assertIn('animato', saved_post.tags.names())

    def test_post_publish_set_published_at(self):

        # Create Post with a hasthag
        saved_post = Post.objects.get(id=self.post.id)
        self.assertEqual('draft', saved_post.status)
        self.assertIsNone(saved_post.published_at)
        saved_post.publish()
        self.assertEqual('published', saved_post.status)
        self.assertIsNotNone(saved_post.published_at)

    def test_multiple_tags(self):

        # Create Post with two hasthags
        post = Post.objects.create(user=self.user,
                                   title='Velocità #con #animato')
        saved_post = Post.objects.get(id=post.id)
        # Ensure the tags have been assigned to the Post
        self.assertIn('animato', saved_post.tags.names())
        self.assertIn('con', saved_post.tags.names())

    def test_post_no_tags(self):

        # Create Post without a title
        post = Post.objects.create(user=self.user)
        saved_post = Post.objects.get(id=post.id)
        self.assertEqual(saved_post.user, self.user)
        self.assertIsNone(saved_post.title)

    def test_post_delete(self):

        # Add Comment to Post
        comment = CommentForPostFactory(entity=self.post)
        # TODO(fsiddi) Add image
        self.post.delete()
        # Ensure that Post and Comment are deleted
        with self.assertRaises(Comment.DoesNotExist):
            Comment.objects.get(pk=comment.id)
        with self.assertRaises(Post.DoesNotExist):
            Post.objects.get(pk=self.post.id)

    def test_post_like(self):
        from dillo.models.mixins import Likes

        # Test creating like
        self.post.like_toggle(self.user)
        post_like = Likes.objects.get(
            user=self.user,
            content_type_id=self.post.content_type_id,
            object_id=self.post.id)
        self.assertEqual(post_like.user, self.user)

        # Test removing like
        self.post.like_toggle(self.user)
        with self.assertRaises(Likes.DoesNotExist):
            Likes.objects.get(user=self.user,
                              content_type_id=self.post.content_type_id,
                              object_id=self.post.id)

    def test_post_like_count(self):
        from django.contrib.auth.models import User

        other_user = User.objects.create_user(username='******',
                                              password='******')
        self.post.like_toggle(self.user)
        self.post.like_toggle(other_user)
        self.assertEqual(2, self.post.likes.count())

    def test_post_like_labels(self):
        """Test the output of like_toggle.

        The tuple output is used in the UI.
        """
        from django.contrib.auth.models import User

        action, action_label, likes_count, likes_word = self.post.like_toggle(
            self.user)
        self.assertEqual('liked', action)
        self.assertEqual('Liked', action_label)
        self.assertEqual('1 LIKE', f'{likes_count} {likes_word}')
        other_user = User.objects.create_user(username='******',
                                              password='******')
        _, _, likes_count, likes_word = self.post.like_toggle(other_user)
        self.assertEqual('2 LIKES', f'{likes_count} {likes_word}')
        self.post.like_toggle(other_user)
        _, _, likes_count, likes_word = self.post.like_toggle(self.user)
        self.assertEqual('0 LIKES', f'{likes_count} {likes_word}')

    def test_post_absolute_url(self):
        absolute_url = reverse('post_detail',
                               kwargs={'hash_id': self.post.hash_id})
        self.assertEqual(f'http://example.com{absolute_url}',
                         self.post.absolute_url)

    def test_post_edit_tags(self):
        self.post.title = "Here we have go with #b3d"
        self.post.save()
        self.assertIn('b3d', self.post.tags.names())

    def test_post_edit_mentions(self):
        self.post.title = "Here @testuser goes with animato"
        self.post.save()
        self.assertIn(self.user, self.post.mentioned_users)
        self.post.title = "Here testuser is gone"
        self.post.save()
        self.assertEqual([], self.post.mentioned_users)

    def test_trending_tags(self):
        dillo.models.posts.Post.objects.create(user=self.user,
                                               title='Post with #b3d')
        # Ensure that there are 2 trending tags
        self.assertEqual(len(dillo.models.posts.get_trending_tags()), 2)
        dillo.models.posts.Post.objects.create(user=self.user,
                                               title='Post with #b3d')
        # Ensure that #b3d is at the first place
        self.assertEqual(dillo.models.posts.get_trending_tags()[0].slug, 'b3d')
        # Now add 2 more posts with #animato
        dillo.models.posts.Post.objects.create(user=self.user,
                                               title='Post with #animato')
        dillo.models.posts.Post.objects.create(user=self.user,
                                               title='Post with #animato')
        # Ensure that #animato is at first place
        self.assertEqual(dillo.models.posts.get_trending_tags()[0].slug,
                         'animato')
Esempio n. 9
0
 def setUp(self):
     self.client = Client()
     self.user = UserFactory(username='******')
     self.post_title = 'Velocità con #animato'
     self.post = Post.objects.get(pk=PostFactory(user=self.user, title=self.post_title).pk)
Esempio n. 10
0
 def setUp(self):
     super().setUp()
     self.post = PostFactory(user=self.user1, title='Velocità con #animato')
     self.post.refresh_from_db()
Esempio n. 11
0
 def setUp(self):
     self.client = Client()
     self.user = UserFactory()
     self.comment = CommentForPostFactory(user=self.user)
     self.post = PostFactory(user=self.user)