示例#1
0
    def test_cannot_react_to_followed_user_encircled_post(self):
        """
          should be able to reaction in the encircled post of a followed user return 400
        """
        user = make_user()
        headers = make_authentication_headers_for_user(user)

        user_to_follow = make_user()
        circle = make_circle(creator=user_to_follow)

        user.follow_user_with_id(user_to_follow.pk)

        followed_user_post = user_to_follow.create_encircled_post(
            text=make_fake_post_text(), circles_ids=[circle.pk])

        emoji_group = make_reactions_emoji_group()

        post_reaction_emoji_id = make_emoji(group=emoji_group).pk

        data = self._get_create_post_reaction_request_data(
            post_reaction_emoji_id, emoji_group.pk)

        url = self._get_url(followed_user_post)
        response = self.client.put(url, data, **headers)

        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        self.assertTrue(
            PostReaction.objects.filter(post_id=followed_user_post.pk,
                                        emoji_id=post_reaction_emoji_id,
                                        reactor_id=user.pk).count() == 0)
    def test_cannot_react_to_foreign_encircled_post_comment(self):
        """
         should not be able to react in a foreign encircled post comment and return 400
         """
        user = make_user()
        headers = make_authentication_headers_for_user(user)

        foreign_user = make_user()
        circle = make_circle(creator=foreign_user)
        post = foreign_user.create_encircled_post(text=make_fake_post_text(), circles_ids=[circle.pk])
        post_comment = foreign_user.comment_post(post=post, text=make_fake_post_comment_text())

        emoji_group = make_reactions_emoji_group()

        post_comment_reaction_emoji_id = make_emoji(group=emoji_group).pk

        data = self._get_create_post_comment_reaction_request_data(post_comment_reaction_emoji_id)

        url = self._get_url(post=post, post_comment=post_comment)
        response = self.client.put(url, data, **headers)

        self.assertEqual(status.HTTP_400_BAD_REQUEST, response.status_code)
        self.assertTrue(
            PostCommentReaction.objects.filter(post_comment_id=post_comment.pk, emoji_id=post_comment_reaction_emoji_id,
                                               reactor_id=user.pk).count() == 0)
示例#3
0
    def test_does_not_retrieve_encircled_post_with_hashtag(self):
        """
        should not retrieve an encircled post with a givne hashtag and return 200
        """
        user = make_user()
        headers = make_authentication_headers_for_user(user)

        post_creator = make_user()
        circle = make_circle(creator=post_creator)

        hashtag = make_hashtag()

        fake_post_text = make_fake_post_text(
        ) + ' and a little hashtag #%s' % hashtag.name
        post_creator.create_encircled_post(circles_ids=[circle.pk],
                                           text=fake_post_text)

        url = self._get_url(hashtag_name=hashtag.name)

        response = self.client.get(url, **headers)

        self.assertEqual(response.status_code, status.HTTP_200_OK)

        parsed_response = json.loads(response.content)

        self.assertEqual(len(parsed_response), 0)
示例#4
0
    def test_can_retrieve_encircled_posts_of_confirmed_connection(self):
        """
        should be able to retrieve the encircled posts of a confirmed connection
        """
        user = make_user()
        user_to_connect_to = make_user()

        user.connect_with_user_with_id(user_id=user_to_connect_to.pk)

        user_to_connect_to_circle = make_circle(creator=user_to_connect_to)
        user_to_connect_to.confirm_connection_with_user_with_id(user_id=user.pk,
                                                                circles_ids=[user_to_connect_to_circle.pk])

        post = user_to_connect_to.create_encircled_post(text=make_fake_post_text(),
                                                        circles_ids=[user_to_connect_to_circle.pk])

        url = self._get_url()
        headers = make_authentication_headers_for_user(user)

        response = self.client.get(url, **headers)

        self.assertEqual(response.status_code, status.HTTP_200_OK)

        response_posts = json.loads(response.content)

        self.assertEqual(1, len(response_posts))

        response_post = response_posts[0]

        self.assertEqual(response_post['id'], post.pk)
    def test_cannot_delete_foreign_reaction_in_connected_user_encircled_post_not_part_of(
            self):
        """
           should NOT be able to delete foreign reaction in a connected user encircled post NOT part of and return 400
         """
        user = make_user()

        user_to_connect = make_user()
        circle = make_circle(creator=user_to_connect)

        foreign_user = make_user()
        foreign_user.connect_with_user_with_id(user_to_connect.pk)
        user_to_connect.confirm_connection_with_user_with_id(
            foreign_user.pk, circles_ids=[circle.pk])

        post = user_to_connect.create_encircled_post(
            text=make_fake_post_text(), circles_ids=[circle.pk])

        emoji_group = make_reactions_emoji_group()

        post_reaction_emoji_id = make_emoji(group=emoji_group).pk

        post_reaction = foreign_user.react_to_post_with_id(
            post.pk,
            emoji_id=post_reaction_emoji_id,
            emoji_group_id=emoji_group.pk)

        url = self._get_url(post_reaction=post_reaction, post=post)

        headers = make_authentication_headers_for_user(user)
        response = self.client.delete(url, **headers)

        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        self.assertTrue(
            PostReaction.objects.filter(id=post_reaction.pk).count() == 1)
示例#6
0
    def test_can_react_to_connected_user_encircled_post_part_of(self):
        """
          should be able to reaction in the encircled post of a connected user which the user is part of and return 201
        """
        user = make_user()
        headers = make_authentication_headers_for_user(user)

        user_to_connect = make_user()
        circle = make_circle(creator=user_to_connect)

        user.connect_with_user_with_id(user_to_connect.pk)
        user_to_connect.confirm_connection_with_user_with_id(
            user.pk, circles_ids=[circle.pk])

        connected_user_post = user_to_connect.create_encircled_post(
            text=make_fake_post_text(), circles_ids=[circle.pk])

        emoji_group = make_reactions_emoji_group()

        post_reaction_emoji_id = make_emoji(group=emoji_group).pk

        data = self._get_create_post_reaction_request_data(
            post_reaction_emoji_id, emoji_group.pk)

        url = self._get_url(connected_user_post)
        response = self.client.put(url, data, **headers)

        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertTrue(
            PostReaction.objects.filter(post_id=connected_user_post.pk,
                                        emoji_id=post_reaction_emoji_id,
                                        reactor_id=user.pk).count() == 1)
示例#7
0
    def test_cant_retrieve_pending_connection_user_user_encircled_post_media_video(
            self):
        """
        should be able to retrieve an pending_connection_user_user encircled post media video
        """
        user = make_user()
        pending_connection_user_user = make_user()

        pending_connection_user_user.connect_with_user_with_id(user_id=user.pk)

        headers = make_authentication_headers_for_user(user=user)

        test_video = get_test_video()

        with open(test_video['path'], 'rb') as file:
            file = File(file)
            circle = make_circle(creator=pending_connection_user_user)
            post = pending_connection_user_user.create_encircled_post(
                video=file, circles_ids=[circle.pk])

        get_worker('high', worker_class=SimpleWorker).work(burst=True)

        url = self._get_url(post=post)

        response = self.client.get(url, **headers, format='multipart')

        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
    def test_cannot_react_to_connected_user_encircled_post_comment_not_part_of(self):
        """
             should NOT be able to react in the encircled post comment of a connected user which the user is NOT part of and return 400
        """
        user = make_user()
        headers = make_authentication_headers_for_user(user)

        user_to_connect = make_user()
        circle = make_circle(creator=user_to_connect)

        user.connect_with_user_with_id(user_to_connect.pk)
        # Note there is no confirmation of the connection on the other side

        connected_user_post = user_to_connect.create_encircled_post(text=make_fake_post_text(), circles_ids=[circle.pk])
        connected_user_post_comment = user_to_connect.comment_post(post=connected_user_post,
                                                                   text=make_fake_post_comment_text())

        emoji_group = make_reactions_emoji_group()

        post_comment_reaction_emoji_id = make_emoji(group=emoji_group).pk

        data = self._get_create_post_comment_reaction_request_data(post_comment_reaction_emoji_id)

        url = self._get_url(post=connected_user_post, post_comment=connected_user_post_comment)
        response = self.client.put(url, data, **headers)

        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

        self.assertTrue(
            PostCommentReaction.objects.filter(post_comment_id=connected_user_post.pk,
                                               emoji_id=post_comment_reaction_emoji_id,
                                               reactor_id=user.pk).count() == 0)
示例#9
0
    def test_does_not_retrieve_duplicate_connections_posts_when_multiple_circles(
            self):
        """
        should not retrieve duplicate connections posts when posted to multiple circles
        """
        user = make_user()
        user_to_connect_to = make_user()

        circle = make_circle(creator=user)

        user.connect_with_user_with_id(user_id=user_to_connect_to.pk,
                                       circles_ids=[circle.pk])

        user_to_connect_to.confirm_connection_with_user_with_id(
            user_id=user.pk, )

        post = user.create_encircled_post(
            text=make_fake_post_text(),
            circles_ids=[circle.pk, user.connections_circle_id])

        url = self._get_url()
        headers = make_authentication_headers_for_user(user)

        response = self.client.get(url, **headers)

        self.assertEqual(response.status_code, status.HTTP_200_OK)

        response_posts = json.loads(response.content)

        self.assertEqual(1, len(response_posts))

        response_post = response_posts[0]

        self.assertEqual(response_post['id'], post.pk)
示例#10
0
    def test_can_retrieve_posts_with_recent_unconfirmed_connection_encircled_post(
            self):
        """
        should be able to retrieve the timeline posts with an unconfirmed connection recent posts
        https://github.com/OpenbookOrg/openbook-api/issues/301
        """
        user = make_user()

        user_timeline_posts_amount = 5

        posts_ids = []

        for i in range(0, user_timeline_posts_amount):
            foreign_user = make_user()
            foreign_post = foreign_user.create_encircled_post(
                text=make_fake_post_text(),
                circles_ids=[foreign_user.connections_circle_id])
            posts_ids.append(foreign_post.pk)
            user.connect_with_user_with_id(user_id=foreign_user.pk)
            foreign_user.confirm_connection_with_user_with_id(user_id=user.pk)

        connection_requester_user = make_user()

        connection_requester_user.connect_with_user_with_id(
            user_id=user.pk,
            circles_ids=[connection_requester_user.connections_circle.pk])

        user.confirm_connection_with_user_with_id(
            user_id=connection_requester_user.pk)

        connection_requester_user.disconnect_from_user_with_id(user_id=user.pk)

        connection_requester_user.connect_with_user_with_id(
            user_id=user.pk,
            circles_ids=[connection_requester_user.connections_circle.pk])

        connection_requester_user_circle = make_circle(
            creator=connection_requester_user)
        connection_requester_user.update_connection_with_user_with_id(
            user_id=user.pk, circles_ids=[connection_requester_user_circle.pk])

        connection_requester_user.create_encircled_post(
            text=make_fake_post_text(),
            circles_ids=[connection_requester_user_circle.pk])

        url = self._get_url()
        headers = make_authentication_headers_for_user(user)

        response = self.client.get(url, **headers)

        self.assertEqual(response.status_code, status.HTTP_200_OK)

        response_posts = json.loads(response.content)

        self.assertEqual(len(posts_ids), len(response_posts))

        response_posts_ids = [post['id'] for post in response_posts]

        for post_id in posts_ids:
            self.assertIn(post_id, response_posts_ids)
示例#11
0
    def test_confirm_connection_in_circle(self):
        """
        should be able to confirm a connection in a custom circle and return 200
        """
        user = make_user()

        user_to_connect = make_user()
        circle = make_circle(creator=user_to_connect)

        user.connect_with_user_with_id(user_to_connect.pk)

        headers = make_authentication_headers_for_user(user_to_connect)

        data = {'username': user.username, 'circles_ids': circle.pk}

        url = self._get_url()

        response = self.client.post(url, data, **headers, format='multipart')

        self.assertEqual(response.status_code, status.HTTP_200_OK)

        self.assertTrue(
            user.is_fully_connected_with_user_with_id(user_to_connect.pk))
        self.assertTrue(
            user_to_connect.is_fully_connected_with_user_with_id(user.pk))

        connection = user_to_connect.get_connection_for_user_with_id(user.pk)
        self.assertTrue(connection.circles.filter(id=circle.pk).exists())
示例#12
0
    def test_retrieves_own_posts_of_own_filtered_circle(self):
        """
        should retrieve own posts when filtering on a circle that is from us
        """
        user = make_user()

        circle = make_circle(creator=user)
        circle_id = circle.pk

        headers = make_authentication_headers_for_user(user)

        amount_of_posts = 10

        posts_ids = []

        for i in range(amount_of_posts):
            post_text = make_fake_post_text()
            post = user.create_encircled_post(text=post_text, circles_ids=[circle_id])
            posts_ids.append(post.pk)

        url = self._get_url()

        response = self.client.get(url, {'circle_id': circle_id}, **headers)

        self.assertEqual(response.status_code, status.HTTP_200_OK)

        response_posts = json.loads(response.content)

        self.assertEqual(len(posts_ids), len(response_posts))

        response_posts_ids = [post['id'] for post in response_posts]

        for post_id in posts_ids:
            self.assertIn(post_id, response_posts_ids)
示例#13
0
    def test_get_all_public_posts_for_connected_user(self):
        """
        should be able to retrieve all the posts of a connected user
        and return 200
        """
        user = make_user()

        user_to_connect_with = make_user()

        user.connect_with_user_with_id(user_to_connect_with.pk)
        user_to_connect_with.confirm_connection_with_user_with_id(user.pk)

        amount_of_public_posts = random.randint(1, 5)
        amount_of_encircled_posts = random.randint(1, 5)

        created_posts_ids = []

        for i in range(amount_of_public_posts):
            post = user_to_connect_with.create_public_post(
                make_fake_post_text())
            created_posts_ids.append(post.pk)

        circle = make_circle(creator=user_to_connect_with)

        user_to_connect_with.update_connection_with_user_with_id(
            user_id=user.pk, circles_ids=[circle.pk])

        for i in range(amount_of_encircled_posts):
            post = user_to_connect_with.create_encircled_post(
                text=make_fake_post_text(), circles_ids=[circle.pk])
            created_posts_ids.append(post.pk)

        headers = make_authentication_headers_for_user(user)

        url = self._get_url()

        response = self.client.get(url,
                                   {'username': user_to_connect_with.username},
                                   **headers)

        self.assertEqual(response.status_code, status.HTTP_200_OK)

        response_posts = json.loads(response.content)

        self.assertEqual(len(response_posts), len(created_posts_ids))

        response_posts_ids = [post['id'] for post in response_posts]

        for post_id in created_posts_ids:
            self.assertIn(post_id, response_posts_ids)
示例#14
0
    def test_cannot_mute_foreign_post_if_encircled_post(self):
        user = make_user()
        foreign_user = make_user()

        headers = make_authentication_headers_for_user(user)

        circle = make_circle(creator=foreign_user)

        post = foreign_user.create_encircled_post(text=make_fake_post_text(), circles_ids=[circle.pk])

        url = self._get_url(post)

        response = self.client.post(url, **headers)

        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

        self.assertFalse(user.has_muted_post_with_id(post.pk))
示例#15
0
    def test_cant_retrieve_user_encircled_post(self):
        """
        should not be able to retrieve a user encircled post and return 400
        """
        user = make_user()
        foreign_user = make_user()

        headers = make_authentication_headers_for_user(user)

        circle = make_circle(creator=foreign_user)
        post = foreign_user.create_encircled_post(text=make_fake_post_text(), circles_ids=[circle.pk])

        url = self._get_url(post)

        response = self.client.get(url, **headers)

        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
示例#16
0
    def test_can_mute_foreign_post_if_part_of_encircled_post(self):
        user = make_user()
        foreign_user = make_user()

        headers = make_authentication_headers_for_user(user)

        circle = make_circle(creator=foreign_user)

        post = foreign_user.create_encircled_post(text=make_fake_post_text(), circles_ids=[circle.pk])

        foreign_user.connect_with_user_with_id(user_id=user.pk, circles_ids=[circle.pk])
        user.confirm_connection_with_user_with_id(user_id=foreign_user.pk)

        url = self._get_url(post)

        response = self.client.post(url, **headers)

        self.assertEqual(response.status_code, status.HTTP_200_OK)

        self.assertTrue(user.has_muted_post_with_id(post.pk))
示例#17
0
    def test_cant_retrieve_reported_connected_user_post(self):
        """
        should not be able to retrieve reported connected user post
        """
        user = make_user()

        connected_user = make_user()
        user.connect_with_user_with_id(user_id=connected_user.pk)
        connected_user_post_circle = make_circle(creator=connected_user)
        connected_user.confirm_connection_with_user_with_id(user_id=user.pk,
                                                            circles_ids=[connected_user_post_circle.pk])
        connected_user_post = connected_user.create_encircled_post(text=make_fake_post_text(),
                                                                   circles_ids=[connected_user_post_circle.pk])
        user.report_post(post=connected_user_post, category_id=make_moderation_category().pk)

        url = self._get_url(post=connected_user_post)
        headers = make_authentication_headers_for_user(user)
        response = self.client.get(url, **headers)

        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
    def test_can_delete_own_reaction_in_connected_user_encircled_post_comment_part_of(
            self):
        """
           should be able to delete own reaction in a connected user encircled post comment it's part of and return 200
         """
        user = make_user()

        user_to_connect = make_user()
        circle = make_circle(creator=user_to_connect)

        user.connect_with_user_with_id(user_to_connect.pk)
        user_to_connect.confirm_connection_with_user_with_id(
            user.pk, circles_ids=[circle.pk])

        post = user_to_connect.create_encircled_post(
            text=make_fake_post_text(), circles_ids=[circle.pk])
        post_comment = user_to_connect.comment_post_with_id(
            post_id=post.pk, text=make_fake_post_comment_text())

        emoji_group = make_reactions_emoji_group()

        post_comment_reaction_emoji_id = make_emoji(group=emoji_group).pk

        post_comment_reaction = user.react_to_post_comment_with_id(
            post.pk,
            emoji_id=post_comment_reaction_emoji_id,
        )

        url = self._get_url(post_comment_reaction=post_comment_reaction,
                            post=post,
                            post_comment=post_comment)

        headers = make_authentication_headers_for_user(user)
        response = self.client.delete(url, **headers)

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertTrue(
            PostCommentReaction.objects.filter(
                id=post_comment_reaction.pk).count() == 0)
示例#19
0
    def test_can_retrieve_connected_user_post_media_video(self):
        """
        should be able to retrieve an connected_user post media video
        """
        user = make_user()
        connected_user = make_user()

        circle = make_circle(creator=connected_user)

        connected_user.connect_with_user_with_id(user_id=user.pk,
                                                 circles_ids=[circle.pk])
        user.confirm_connection_with_user_with_id(user_id=connected_user.pk)

        headers = make_authentication_headers_for_user(user=user)

        test_video = get_test_video()

        with open(test_video['path'], 'rb') as file:
            file = File(file)
            post = connected_user.create_encircled_post(
                video=file, circles_ids=[circle.pk])

        get_worker('high', worker_class=SimpleWorker).work(burst=True)

        url = self._get_url(post=post)

        response = self.client.get(url, **headers, format='multipart')

        self.assertEqual(response.status_code, status.HTTP_200_OK)

        response_media = json.loads(response.content)

        post.refresh_from_db()

        post_media = post.get_media().all()

        self._compare_response_media_with_post_media(
            post_media=post_media, response_media=response_media)
示例#20
0
    def test_get_all_public_posts_for_user_unauthenticated(self):
        """
        should be able to retrieve all the public posts of an specific user
        being unauthenticated and return 200
        """
        user = make_user()

        amount_of_user_public_posts = random.randint(1, 5)
        amount_of_user_encircled_posts = random.randint(1, 5)

        public_posts_ids = []

        for i in range(amount_of_user_public_posts):
            post_text = make_fake_post_text()
            public_post = user.create_public_post(text=post_text)
            public_posts_ids.append(public_post.pk)

        for i in range(amount_of_user_encircled_posts):
            post_text = make_fake_post_text()
            circle = make_circle(creator=user)
            user.create_encircled_post(text=post_text, circles_ids=[circle.pk])

        url = self._get_url()

        response = self.client.get(url, {
            'username': user.username
        })

        self.assertEqual(response.status_code, status.HTTP_200_OK)

        response_posts = json.loads(response.content)

        self.assertEqual(len(response_posts), len(public_posts_ids))

        response_posts_ids = [response_post['id'] for response_post in response_posts]

        for public_post_id in public_posts_ids:
            self.assertIn(public_post_id, response_posts_ids)
示例#21
0
    def test_can_retrieve_connected_user_encircled_post(self):
        """
        should be able to retrieve a connected user encircled post and return 200
        """
        user = make_user()
        foreign_user = make_user()

        headers = make_authentication_headers_for_user(user)

        circle = make_circle(creator=foreign_user)
        post = foreign_user.create_encircled_post(text=make_fake_post_text(), circles_ids=[circle.pk])

        user.connect_with_user_with_id(foreign_user.pk)
        foreign_user.confirm_connection_with_user_with_id(user.pk, circles_ids=[circle.pk])

        url = self._get_url(post)

        response = self.client.get(url, **headers)

        self.assertEqual(response.status_code, status.HTTP_200_OK)

        response_post = json.loads(response.content)

        self.assertEqual(response_post['id'], post.pk)