def test02_delete_a_comment(self):
        """
        Delete a comment
        """

        with self.repo_app.app_context():

            # get a post
            post = (db_session.query(Post)
                              .filter(Post.is_published)
                              .first())

            # create a comment
            data = {'text': 'This is a comment'}
            rv = self.app.post('/comment?path={}'.format(post.path),
                               headers=self.headers,
                               data=json.dumps(data),
                               content_type='application/json')

            # get comment id
            comment_id = (db_session.query(Comment)
                                    .filter(Comment.post_id == post.id)
                                    .filter(Comment.user_id == self.user_id)
                                    .filter(Comment.text == data['text'])
                                    .first()).id

            # test deleting a commment
            rv = self.app.get('/delete_comment?comment_id={}'.format(comment_id),
                              headers=self.headers)
            assert rv.status == '200 OK'
            delete_comment = (db_session.query(Comment)
                                        .filter(Comment.id == comment_id)
                                        .all())
            assert not delete_comment, 'comments found after deletion'
    def test01_webeditor_route(self):
        """Test the /webposts route.

        This will need to test the security as well.
        The user 'Knowledge Default; is able to see all webposts,
        whereas 'Test User' should only see posts they've authored
        """

        with self.app.app_context():
            posts = db_session.query(Post).all()
            rv = self.client.get(
                '/webposts', headers={'user_header': 'test_knowledge_editor'})

            assert rv.status == '200 OK'

            data = rv.data.decode('utf-8')
            soup = BeautifulSoup(data, 'html.parser')

            table_rows = soup.findAll("tr")

            # Subtract one for the header of the table
            assert len(table_rows) - 1 == len(posts)

            rv = self.client.get(
                '/webposts', headers={'user_header': 'webeditor_test_user'})

            data = rv.data.decode('utf-8', 'html.parser')
            soup = BeautifulSoup(data, 'html.parser')
            table_rows = soup.findAll("tr")

            assert len(table_rows) - 1 == 1
Beispiel #3
0
    def test04_test_review_button(self):
        """Test the review functionality.

        Ensure that submitting a post for review changes the status
        And that going back to the form editor changes the button text
        And a review comment area is added
        """
        rv = self.client.post('/submit?post_id={}'.format(self.post_id),
                              data=json.dumps(
                                  {'post_reviewers': 'test_post_reviewers'}),
                              content_type='application/json')

        assert rv.status == "200 OK"

        rv = self.client.get('/posteditor?post_id={}'.format(self.post_id))

        assert rv.status == "200 OK"

        data = rv.data.decode("utf-8")
        soup = BeautifulSoup(data, 'html.parser')

        comments_textarea = soup.findAll("textarea", {"id": "comment-text"})
        assert comments_textarea

        btn_in_review = soup.findAll("button", {"id": "btn_in_review"})
        assert btn_in_review and btn_in_review[0].text.strip(
        ) == "In Review Phase"

        with self.app.app_context():
            post = (db_session.query(Post).filter(
                Post.id == self.post_id).first())
            assert post.status == self.repo.PostStatus.SUBMITTED
Beispiel #4
0
    def test06_test_publish_button(self):
        """Test the publish functionality.

        Test that clicking the publish button changes the status
        and the button text.

        Test that clicking it again changes the status back
        and the button text
        """
        # test that clicking the publish button changes the status
        rv = self.client.post('/publish_post?post_id={}'.format(self.post_id))

        assert rv.status == "200 OK"
        with self.app.app_context():
            post = (db_session.query(Post).filter(
                Post.id == self.post_id).first())
            assert post.status == self.repo.PostStatus.PUBLISHED
            rv = self.client.get('/posteditor?post_id={}'.format(self.post_id))

            assert rv.status == "200 OK"

            data = rv.data.decode("utf-8")
            soup = BeautifulSoup(data, 'html.parser')

            # go to the posteditor form to check the button
            btn_publish = soup.findAll("button", {"id": "btn_publish"})
            assert btn_publish and btn_publish[0].text.strip() == "Unpublish"

            rv = self.client.post('/unpublish_post?post_id={}'.format(
                self.post_id))
            assert rv.status == "200 OK"

            post = (db_session.query(Post).filter(
                Post.id == self.post_id).first())

            assert post.status == self.repo.PostStatus.UNPUBLISHED

            rv = self.client.get('/posteditor?post_id={}'.format(self.post_id))

            assert rv.status == "200 OK"

            data = rv.data.decode("utf-8")
            soup = BeautifulSoup(data, 'html.parser')

            # go to the posteditor form to check the button
            btn_publish = soup.findAll("button", {"id": "btn_publish"})
            assert btn_publish and btn_publish[0].text.strip() == "Publish"
Beispiel #5
0
    def test07_test_delete_button(self):
        """Test webpost deletion."""
        rv = self.client.get('/delete_post?post_id={}'.format(self.post_id))
        assert rv.status == "200 OK"

        with self.app.app_context():
            post = (db_session.query(Post).filter(
                Post.id == self.post_id).first())
            assert not post
    def test01_comment_on_a_post(self):
        """
        Comment on a post
        """
        with self.repo_app.app_context():
            # get the number of comments on a given post
            post = (db_session.query(Post)
                              .filter(Post.is_published)
                              .first())
            n_comments = len(db_session.query(Comment)
                                       .filter(Comment.post_id == post.id)
                                       .all())

            assert post.status == self.repo.PostStatus.PUBLISHED

            # test commenting on a post
            data = {'text': 'This is a comment'}
            rv = self.app.post('/comment?path={}'.format(post.path),
                               headers=self.headers,
                               data=json.dumps(data),
                               content_type='application/json')

            assert rv.status == '200 OK'

            assert post.status == self.repo.PostStatus.PUBLISHED

            # check that new comment exists in db
            comment_exists = (db_session.query(Comment)
                                        .filter(Comment.post_id == post.id)
                                        .filter(Comment.user_id == self.user_id)
                                        .filter(Comment.text == data['text'])
                                        .first())
            assert comment_exists

            assert post.status == self.repo.PostStatus.PUBLISHED

            # check that the number of comments rendered increased by 1
            rv = self.app.get('/post/' + post.path,
                              headers=self.headers)
            soup = BeautifulSoup(rv.data.decode('utf-8'), 'html.parser')
            comments = soup.findAll("div", {"class": "post_comment"})
            assert comments, 'comments found in '
            assert len(comments) == n_comments + 1
Beispiel #7
0
    def test_favorites(self):
        """
        test the favorites route, and make sure the number of posts = number of votes with that user_id
        """
        with self.repo_app.app_context():
            rv = self.app.get("/favorites", headers=self.headers)
            assert rv.status == "200 OK"
            data = rv.data.decode('utf-8')
            soup = BeautifulSoup(data, 'html.parser')
            all_posts = soup.findAll(
                'div', {'class': 'row row-space-4 panel feed-post'})

            user = (db_session.query(User)
                    .filter(User.username == self.knowledge_username)
                    .first())
            votes = (db_session.query(Vote)
                     .filter(Vote.user_id == user.id)
                     .all())
            assert len(votes) == len(all_posts)
Beispiel #8
0
    def test03_test_post_stats(self):
        """
        Test post_stats
        """
        posts = self.data

        for post in posts:
            title_link = post.findAll("a")[0]
            title_href = title_link['href']
            find_equal = title_href.find("=")
            page_id = title_href[find_equal + 1:]

            # get pageviews
            # TODO: refine this for latest pageview model
            pageviews = post.findAll(
                "i", id=lambda x: x and x.startswith('tooltip-pageviews'))
            assert (len(pageviews) == 1)
            pageview_value = int(pageviews[0].text)
            pageview_query = (db_session.query(func.count(
                PageView.id)).filter(PageView.page == page_id).scalar())
            assert (pageview_query == pageview_value + 1)
            # get likes
            likes = post.findAll(
                "i", id=lambda x: x and x.startswith('tooltip-likes'))
            assert (len(likes) == 1)

            likes_value = int(likes[0].text)
            likes_query = (db_session.query(Vote).filter(
                Vote.page == page_id).all())
            assert (likes_value == len(likes_query))

            # get comments
            comments = post.findAll(
                "i", id=lambda x: x and x.startswith('tooltip-comments'))
            assert (len(comments) == 1)

            comments_values = int(comments[0].text)
            comments_query = (db_session.query(Comment).filter(
                Comment.post_id == page_id).all())
            assert (comments_values == len(comments_query))
Beispiel #9
0
    def test05_test_author_can_publish_button(self):
        """Test changing the permission for authors.

        Clicking the author_can_publish checkbox
        should change the relevant field in the db
        """
        url = "/author_publish?post_id=" + str(self.post_id)
        rv = self.client.post(url)

        assert rv.status == "200 OK"
        with self.app.app_context():
            post = (db_session.query(Post).filter(
                Post.id == self.post_id).first())
            kp = self.repo.post(post.path)
            assert kp.is_accepted is True
Beispiel #10
0
    def test_like_and_unlike_a_post(self):
        """
        Like and then unlike a post
        """
        with self.repo_app.app_context():
            post = (db_session.query(Post)
                    .filter(Post.is_published)
                    .first())

            old_likes = (db_session.query(Vote)
                         .filter(Vote.object_id == post.id)
                         .filter(Vote.object_type == 'post')
                         .all())
            rv = self.app.get("/like?post_id=" + str(post.id), headers=self.headers)

            assert rv.status == '200 OK', post.path + rv.status

            new_likes = (db_session.query(Vote)
                         .filter(Vote.object_id == post.id)
                         .filter(Vote.object_type == 'post')
                         .all())

            assert len(old_likes) + 1 == len(new_likes)

            # assert that if you re-like the page, the number of likes doesn't
            # change
            rv = self.app.get("/like?post_id=" + str(post.id), headers=self.headers)

            assert rv.status == '200 OK'

            like_again = (db_session.query(Vote)
                          .filter(Vote.object_id == post.id)
                          .filter(Vote.object_type == 'post')
                          .all())
            assert len(like_again) == len(new_likes)

            """
            Let's unlike it again
            """
            old_likes = (db_session.query(Vote)
                         .filter(Vote.object_id == post.id)
                         .filter(Vote.object_type == 'post')
                         .all())
            rv = self.app.get("/unlike?post_id=" + str(post.id), headers=self.headers)

            assert rv.status == '200 OK'

            new_likes = (db_session.query(Vote)
                         .filter(Vote.object_id == post.id)
                         .filter(Vote.object_type == 'post')
                         .all())
            assert len(new_likes) == len(old_likes) - 1
Beispiel #11
0
    def test03_test_save_button(self):
        """Test the save button on the webeditor form.

        Change the post text and make sure that
        the relevant field is updated in the db
        """
        new_post_text = "Here is the new post_text"

        with self.app.app_context():
            post = (db_session.query(Post).filter(
                Post.id == self.post_id).first())

            str_created_at = datetime.datetime.strftime(
                post.created_at, '%Y-%m-%d')
            str_updated_at = datetime.datetime.strftime(
                post.updated_at, '%Y-%m-%d')

            post_json_object = {
                'title': post.title,
                'project': post.project,
                'feed_image': "",  # TODO where do we store this?
                'tags': "",  # TODO pass tags
                'tldr': post.tldr,
                'status': post.status.value,
                'created_at': str_created_at,
                'updated_at': str_updated_at,
                'author': [auth.username for auth in post.authors],
                'markdown': new_post_text
            }

            url = "/save_post?post_id=" + str(post.id)
            rv = self.client.post(url,
                                  data=json.dumps(post_json_object),
                                  content_type='application/json')
            assert rv.status == "200 OK"

            kp = self.repo.post(self.post_path)

            assert kp.read(headers=False).strip() == new_post_text