Esempio n. 1
0
    def test_restricted(self):

        db.session.delete(self.post)

        user = User(username="******", email="*****@*****.**")

        db.session.add(user)

        user2 = User(username="******", email="*****@*****.**")

        db.session.add(user2)
    
        db.session.commit()
        
        admin = User(username="******", 
                     email="*****@*****.**", 
                     role=User.MODERATOR)

        
        assert user.id

        post = Post(title="test",
                    author=user,
                    access=Post.PRIVATE)

        db.session.add(post)
        db.session.commit()

        posts = Post.query.restricted(user)

        assert Post.query.restricted(user).count() == 1
        assert Post.query.restricted(admin).count() == 1
        assert Post.query.restricted(None).count() == 0
        assert Post.query.restricted(user2).count() == 0

        post.access = Post.PUBLIC
        db.session.commit()
    
        posts = Post.query.restricted(user)

        assert Post.query.restricted(user).count() == 1
        assert Post.query.restricted(admin).count() == 1
        assert Post.query.restricted(None).count() == 1
        assert Post.query.restricted(user2).count() == 1
        
        post.access = Post.FRIENDS

        db.session.commit()
        
        assert Post.query.restricted(user).count() == 1
        assert Post.query.restricted(admin).count() == 1
        assert Post.query.restricted(None).count() == 0
        assert Post.query.restricted(user2).count() == 0
    
        user2.follow(user)
        user.follow(user2)

        db.session.commit()

        assert Post.query.restricted(user2).count() == 1
Esempio n. 2
0
    def test_restricted(self):

        db.session.delete(self.post)
        db.session.delete(self.comment)

        user = User(username="******", email="*****@*****.**")

        db.session.add(user)

        user2 = User(username="******", email="*****@*****.**")

        db.session.add(user2)

        db.session.commit()

        admin = User(username="******",
                     email="*****@*****.**",
                     role=User.MODERATOR)

        post = Post(title="test", author=user, access=Post.PRIVATE)

        db.session.add(post)
        db.session.commit()

        comment = Comment(author=user, post=post, comment="test")

        db.session.add(comment)
        db.session.commit()

        assert Comment.query.restricted(user).count() == 1
        assert Comment.query.restricted(admin).count() == 1
        assert Comment.query.restricted(None).count() == 0
        assert Comment.query.restricted(user2).count() == 0

        post.access = Post.PUBLIC
        db.session.commit()

        posts = Post.query.restricted(user)

        assert Comment.query.restricted(user).count() == 1
        assert Comment.query.restricted(admin).count() == 1
        assert Comment.query.restricted(None).count() == 1
        assert Comment.query.restricted(user2).count() == 1

        post.access = Post.FRIENDS

        db.session.commit()

        assert Comment.query.restricted(user).count() == 1
        assert Comment.query.restricted(admin).count() == 1
        assert Comment.query.restricted(None).count() == 0
        assert Comment.query.restricted(user2).count() == 0

        user2.follow(user)
        user.follow(user2)

        db.session.commit()

        assert Comment.query.restricted(user2).count() == 1
Esempio n. 3
0
    def test_delete_post_logged_in_as_admin(self):
    
        user = self.create_user(False)
        
        admin_user = User(username="******",
                          email="*****@*****.**",
                          password="******",
                          role=User.ADMIN)

        db.session.add(admin_user)
        db.session.commit()

        self.login(login="******", password="******")

        post = Post(author=user,
                    title="test",
                    description="test")

        db.session.add(post)
        db.session.commit()

        with mail.record_messages() as outbox:
            response = self.client.post("/post/%d/delete/" % post.id)
        
            assert response.json['success']
            assert Post.query.count() == 0
            assert len(outbox) == 1
Esempio n. 4
0
    def setUp(self):
        super(TestPost, self).setUp()

        self.user = User(username="******",
                         email="*****@*****.**",
                         password="******")

        db.session.add(self.user)

        self.post = Post(title="testing",
                         link="http://reddit.com",
                         author=self.user)

        db.session.add(self.post)

        db.session.commit()
Esempio n. 5
0
    def create_post(self):
        
        post = Post(author=self.create_user(),
                    title="test")

        db.session.add(post)
        db.session.commit()

        return post
Esempio n. 6
0
    def test_tag_cloud_with_posts(self):

        user = User(username="******",
                    email="*****@*****.**",
                    password="******")

        db.session.add(user)
        db.session.commit()

        for i in xrange(20):
            post = Post(author=user,
                        title="test",
                        tags="Music, comedy, IT crowd")

            db.session.add(post)
            db.session.commit()

        for i in xrange(10):
            post = Post(author=user,
                        title="test",
                        tags="Twitter, YouTube, funny")

            db.session.add(post)
            db.session.commit()

        post = Post(author=user, title="test", tags="Beer, parties, kegs")

        db.session.add(post)
        db.session.commit()

        assert Tag.query.count() == 9

        tags = Tag.query.cloud()

        for tag in tags:

            if tag.name in ("it crowd", "music", "comedy"):
                assert tag.size == 10

            elif tag.name in ("twitter", "youtube", "funny"):
                assert tag.size == 5

            elif tag.name in ("beer", "parties", "kegs"):
                assert tag.size == 1
Esempio n. 7
0
    def test_edit_existing_post_logged_in_as_author(self):

        post = Post(author=self.create_user(True),
                    title="test",
                    description="test")

        db.session.add(post)
        db.session.commit()

        response = self.client.get("/post/%d/edit/" % post.id)
        self.assert_200(response)
Esempio n. 8
0
    def test_delete_existing_post_not_logged_in(self):

        user = self.create_user(False)
        
        post = Post(author=user,
                    title="test",
                    description="test")

        db.session.add(post)
        db.session.commit()

        response = self.client.post("/post/%d/delete/" % post.id)
        self.assert_401(response)
Esempio n. 9
0
def importfeed(url, username):
    """
    Bulk import news from a feed. For testing only !
    """

    user = User.query.filter_by(username=username).first()
    if not user:
        print "User %s does not exist" % username
        sys.exit(1)
    d = feedparser.parse(url)
    for entry in d['entries']:
        post = Post(author=user, title=entry.title[:200], link=entry.link)

        db.session.add(post)
    db.session.commit()
Esempio n. 10
0
    def setUp(self):
        super(TestPost, self).setUp()

        self.user = User(username="******",
                         email="*****@*****.**",
                         password="******")
        
        db.session.add(self.user)

        self.post = Post(title="testing",
                         link="http://reddit.com",
                         author=self.user)

        db.session.add(self.post)

        db.session.commit()
Esempio n. 11
0
def submit():

    form = PostForm()

    if form.validate_on_submit():

        post = Post(author=g.user)
        form.populate_obj(post)

        db.session.add(post)
        db.session.commit()

        flash(_("Thank you for posting"), "success")

        return redirect(url_for("frontend.latest"))

    return render_template("submit.html", form=form)
Esempio n. 12
0
    def test_latest(self):

        
        user = User(username="******",
                    password="******",
                    email="*****@*****.**")

        db.session.add(user)
        
        for i in xrange(100):
            post =  Post(author=user,
                         link="http://reddit.com",
                         title="test post")

            db.session.add(post)

        db.session.commit()
Esempio n. 13
0
    def test_delete_existing_post_logged_in_as_author(self):

        user = self.create_user(True)
        
        post = Post(author=user,
                    title="test",
                    description="test")

        db.session.add(post)
        db.session.commit()

        response = self.client.post("/post/%d/delete/" % post.id)

        self.assert_200(response)

        assert response.json['success']
        assert Post.query.count() == 0
Esempio n. 14
0
    def create_comment(self):

        user = User(username="******",
                    email="*****@*****.**",
                    password="******")

        post = Post(author=user,
                    title="test")

        comment = Comment(post=post,
                          author=user,
                          comment="test")

        db.session.add_all([user, post, comment])
        db.session.commit()

        comment_added.send(post)
        return comment
Esempio n. 15
0
    def test_tags(self):

        assert self.post.taglist == []

        self.post.tags = "Music, comedy, IT crowd"

        db.session.commit()

        assert self.post.taglist == ["Music", "comedy", "IT crowd"]

        assert self.post.linked_taglist == [
            ("Music", "/tags/music/"),
            ("comedy", "/tags/comedy/"),
            ("IT crowd", "/tags/it-crowd/"),
        ]

        assert Tag.query.count() == 3

        for tag in Tag.query.all():

            assert tag.num_posts == 1
            assert tag.posts[0].id == self.post.id

        post = Post(title="testing again",
                    link="http://reddit.com/r/programming",
                    author=self.user,
                    tags="comedy, it Crowd, Ubuntu")

        db.session.add(post)
        db.session.commit()

        assert post.taglist == ["comedy", "it Crowd", "Ubuntu"]
        assert Tag.query.count() == 4

        for tag in Tag.query.all():

            if tag.name.lower() in ("comedy", "it crowd"):
                assert tag.num_posts == 2
                assert tag.posts.count() == 2

            else:
                assert tag.num_posts == 1
                assert tag.posts.count() == 1
Esempio n. 16
0
    def setUp(self):
        super(TestFeeds, self).setUp()

        user = User(username="******",
                    email="*****@*****.**",
                    password="******")

        db.session.add(user)
        db.session.commit()

        for i in xrange(20):

            post = Post(author=user,
                        tags="programming",
                        title="TESTING",
                        description="test")


            db.session.add(post)
            db.session.commit()
Esempio n. 17
0
    def test_edit_existing_post_not_logged_in_as_author(self):

        post = Post(author=self.create_user(False),
                    title="test",
                    description="test")

        db.session.add(post)
        db.session.commit()

        user = User(username="******",
                    password="******",
                    email="*****@*****.**")

        db.session.add(user)
        db.session.commit()

        self.login(login="******", password="******")
 
        response = self.client.get("/post/%d/edit/" % post.id)
        self.assert_403(response)
Esempio n. 18
0
    def test_tags(self):

        user = User(username="******",
                    email="*****@*****.**",
                    password="******")

        db.session.add(user)
        db.session.commit()

        for i in xrange(20):

            post = Post(author=user,
                        tags="IT Crowd, funny, TV",
                        title="test")

            db.session.add(post)
            db.session.commit()
        
        response = self.client.get("/tags/")
        self.assert_200(response)
Esempio n. 19
0
    def test_index(self):
        
        response = self.client.get("/")
        self.assert_200(response)
        
        user = User(username="******",
                    password="******",
                    email="*****@*****.**")

        db.session.add(user)
        
        for i in xrange(100):
            post =  Post(author=user,
                         link="http://reddit.com",
                         title="test post")

            db.session.add(post)

        db.session.commit()

        response = self.client.get("/")
        self.assert_200(response)
Esempio n. 20
0
    def test_update_existing_post_logged_in_as_author(self):

        post = Post(author=self.create_user(True),
                    title="test",
                    description="test")

        db.session.add(post)
        db.session.commit()


        data = {
                "title" : "testing 123",
                "description" : "a test 123"
                }


        response = self.client.post("/post/%d/edit/" % post.id, data=data)
        self.assert_redirects(response, "/post/%d/" % post.id)
        
        post = Post.query.first()

        assert post.title == "testing 123"
        assert post.description == "a test 123"
Esempio n. 21
0
    def setUp(self):
        super(TestUser, self).setUp()

        self.user = User(username="******", 
                         email="*****@*****.**",
                         password="******")

        db.session.add(self.user)

        for i in xrange(10):

            post = Post(author=self.user,
                        title="test")

            db.session.add(post)

            comment = Comment(post=post,
                              author=self.user,
                              comment="test comment")

            db.session.add(comment)

        db.session.commit()
Esempio n. 22
0
    def test_view_post(self):

        response = self.client.get("/post/1/")
        self.assert_404(response)

        user = User(username="******",
                    password="******",
                    email="*****@*****.**")

        db.session.add(user)
        db.session.commit()

        post = Post(author=user,
                    title="test",
                    description="test")

        db.session.add(post)
        db.session.commit()

        response = self.client.get("/post/%d/" % post.id)
        self.assert_200(response)

        for i in xrange(100):
            user = User(username="******" % i,
                        email="tester=%d.gmail.com" % i,
                        password="******")

            comment = Comment(post=post,
                              author=user,
                              comment="a comment")
            db.session.add(user)
            db.session.add(comment)

        db.session.commit()

        response = self.client.get("/post/%d/" % post.id)
        self.assert_200(response)
Esempio n. 23
0
    def test_update_existing_post_logged_in_as_admin(self):

        post = Post(author=self.create_user(False),
                    title="test",
                    description="test")

        db.session.add(post)
        db.session.commit()

        admin_user = User(username="******",
                          email="*****@*****.**",
                          password="******",
                          role=User.ADMIN)

        db.session.add(admin_user)
        db.session.commit()

        self.login(login="******", password="******")
 
 
        data = {
                "title" : "testing 123",
                "description" : "a test 123"
                }


        with mail.record_messages() as outbox:
            response = self.client.post("/post/%d/edit/" % post.id, data=data)
        
            self.assert_redirects(response, "/post/%d/" % post.id)
            assert len(outbox) == 1

        post = Post.query.first()

        assert post.title == "testing 123"
        assert post.description == "a test 123"
Esempio n. 24
0
    def test_can_access(self):

        user = User(username="******", email="*****@*****.**")

        db.session.add(user)

        user2 = User(username="******", email="*****@*****.**")

        db.session.add(user2)
    
        db.session.commit()
        
        admin = User(username="******", 
                     email="*****@*****.**", 
                     role=User.MODERATOR)

        
        post = Post(title="test",
                    author_id=user.id,
                    access=Post.PRIVATE)


        assert post.can_access(user)
        assert post.can_access(admin)

        assert not post.can_access(user2)
        assert not post.can_access(None)

        post.access = Post.PUBLIC

        assert post.can_access(user)
        assert post.can_access(admin)

        assert post.can_access(user2)
        assert post.can_access(None)

        post.access = Post.FRIENDS

        assert post.can_access(user)
        assert post.can_access(admin)

        assert not post.can_access(user2)
        assert not post.can_access(None)

        user.follow(user2)
        user2.follow(user)

        assert post.can_access(user2)
Esempio n. 25
0
    def test_can_access(self):

        user = User(username="******", email="*****@*****.**")

        db.session.add(user)

        user2 = User(username="******", email="*****@*****.**")

        db.session.add(user2)

        db.session.commit()

        admin = User(username="******",
                     email="*****@*****.**",
                     role=User.MODERATOR)

        post = Post(title="test", author_id=user.id, access=Post.PRIVATE)

        assert post.can_access(user)
        assert post.can_access(admin)

        assert not post.can_access(user2)
        assert not post.can_access(None)

        post.access = Post.PUBLIC

        assert post.can_access(user)
        assert post.can_access(admin)

        assert post.can_access(user2)
        assert post.can_access(None)

        post.access = Post.FRIENDS

        assert post.can_access(user)
        assert post.can_access(admin)

        assert not post.can_access(user2)
        assert not post.can_access(None)

        user.follow(user2)
        user2.follow(user)

        assert post.can_access(user2)
Esempio n. 26
0
class TestPost(TestCase):
    def setUp(self):
        super(TestPost, self).setUp()

        self.user = User(username="******",
                         email="*****@*****.**",
                         password="******")

        db.session.add(self.user)

        self.post = Post(title="testing",
                         link="http://reddit.com",
                         author=self.user)

        db.session.add(self.post)

        db.session.commit()

    def test_url(self):

        assert self.post.url == "/post/1/s/testing/"

    def test_permanlink(self):

        assert self.post.permalink == "http://localhost/post/1/s/testing/"

    def test_popular(self):

        assert Post.query.popular().count() == 1
        self.post.score = 0
        db.session.commit()
        assert Post.query.popular().count() == 0

    def test_deadpooled(self):

        assert Post.query.deadpooled().count() == 0
        self.post.score = 0
        db.session.commit()
        assert Post.query.deadpooled().count() == 1

    def test_jsonify(self):

        d = self.post.json
        assert d['title'] == self.post.title

        json = list(Post.query.jsonify())

        assert json[0]['title'] == self.post.title

    def test_tags(self):

        assert self.post.taglist == []

        self.post.tags = "Music, comedy, IT crowd"

        db.session.commit()

        assert self.post.taglist == ["Music", "comedy", "IT crowd"]

        assert self.post.linked_taglist == [
            ("Music", "/tags/music/"),
            ("comedy", "/tags/comedy/"),
            ("IT crowd", "/tags/it-crowd/"),
        ]

        assert Tag.query.count() == 3

        for tag in Tag.query.all():

            assert tag.num_posts == 1
            assert tag.posts[0].id == self.post.id

        post = Post(title="testing again",
                    link="http://reddit.com/r/programming",
                    author=self.user,
                    tags="comedy, it Crowd, Ubuntu")

        db.session.add(post)
        db.session.commit()

        assert post.taglist == ["comedy", "it Crowd", "Ubuntu"]
        assert Tag.query.count() == 4

        for tag in Tag.query.all():

            if tag.name.lower() in ("comedy", "it crowd"):
                assert tag.num_posts == 2
                assert tag.posts.count() == 2

            else:
                assert tag.num_posts == 1
                assert tag.posts.count() == 1

    def test_restricted(self):

        db.session.delete(self.post)

        user = User(username="******", email="*****@*****.**")

        db.session.add(user)

        user2 = User(username="******", email="*****@*****.**")

        db.session.add(user2)

        db.session.commit()

        admin = User(username="******",
                     email="*****@*****.**",
                     role=User.MODERATOR)

        assert user.id

        post = Post(title="test", author=user, access=Post.PRIVATE)

        db.session.add(post)
        db.session.commit()

        posts = Post.query.restricted(user)

        assert Post.query.restricted(user).count() == 1
        assert Post.query.restricted(admin).count() == 1
        assert Post.query.restricted(None).count() == 0
        assert Post.query.restricted(user2).count() == 0

        post.access = Post.PUBLIC
        db.session.commit()

        posts = Post.query.restricted(user)

        assert Post.query.restricted(user).count() == 1
        assert Post.query.restricted(admin).count() == 1
        assert Post.query.restricted(None).count() == 1
        assert Post.query.restricted(user2).count() == 1

        post.access = Post.FRIENDS

        db.session.commit()

        assert Post.query.restricted(user).count() == 1
        assert Post.query.restricted(admin).count() == 1
        assert Post.query.restricted(None).count() == 0
        assert Post.query.restricted(user2).count() == 0

        user2.follow(user)
        user.follow(user2)

        db.session.commit()

        assert Post.query.restricted(user2).count() == 1

    def test_can_access(self):

        user = User(username="******", email="*****@*****.**")

        db.session.add(user)

        user2 = User(username="******", email="*****@*****.**")

        db.session.add(user2)

        db.session.commit()

        admin = User(username="******",
                     email="*****@*****.**",
                     role=User.MODERATOR)

        post = Post(title="test", author_id=user.id, access=Post.PRIVATE)

        assert post.can_access(user)
        assert post.can_access(admin)

        assert not post.can_access(user2)
        assert not post.can_access(None)

        post.access = Post.PUBLIC

        assert post.can_access(user)
        assert post.can_access(admin)

        assert post.can_access(user2)
        assert post.can_access(None)

        post.access = Post.FRIENDS

        assert post.can_access(user)
        assert post.can_access(admin)

        assert not post.can_access(user2)
        assert not post.can_access(None)

        user.follow(user2)
        user2.follow(user)

        assert post.can_access(user2)

    def test_edit_tags(self):

        self.post.tags = "Music, comedy, IT crowd"

        db.session.commit()

        assert self.post.taglist == ["Music", "comedy", "IT crowd"]

        assert self.post.linked_taglist == [
            ("Music", "/tags/music/"),
            ("comedy", "/tags/comedy/"),
            ("IT crowd", "/tags/it-crowd/"),
        ]

        def _count_post_tags():
            s = db.select([db.func.count(post_tags)])
            return db.engine.execute(s).scalar()

        assert _count_post_tags() == 3

        self.post.tags = "music, iPhone, books"
        db.session.commit()

        for t in Tag.query.all():
            if t.name in ("music", "iphone", "books"):
                assert t.num_posts == 1

            if t.name in ("comedy", "it crowd"):
                assert t.num_posts == 0

        assert _count_post_tags() == 3

        self.post.tags = ""

        assert _count_post_tags() == 0

    def test_update_num_comments(self):

        comment = Comment(post=self.post, author=self.user, comment="test")

        db.session.add(comment)
        db.session.commit()

        signals.comment_added.send(self.post)

        post = Post.query.get(self.post.id)

        assert post.num_comments == 1

        db.session.delete(comment)
        db.session.commit()

        signals.comment_deleted.send(post)

        post = Post.query.get(post.id)

        assert post.num_comments == 0

    def test_votes(self):

        assert self.post.votes == set([])
        user = User(username="******", email="*****@*****.**")

        db.session.add(user)
        db.session.commit()

        self.post.vote(user)

        assert user.id in self.post.votes

        post = Post.query.get(self.post.id)
        assert user.id in post.votes

    def test_can_vote(self):

        assert not self.post.permissions.vote.allows(AnonymousIdentity())

        identity = Identity(self.user.id)
        identity.provides.update(self.user.provides)
        assert not self.post.permissions.vote.allows(identity)

        user = User(username="******", email="*****@*****.**")

        db.session.add(user)
        db.session.commit()

        identity = Identity(user.id)
        identity.provides.update(user.provides)

        assert self.post.permissions.vote.allows(identity)

        votes = self.post.votes
        votes.add(user.id)
        self.post.votes = votes

        del self.post.permissions

        assert not self.post.permissions.vote.allows(identity)

    def test_can_edit(self):

        assert not self.post.permissions.edit.allows(AnonymousIdentity())

        identity = Identity(self.user.id)
        identity.provides.update(self.user.provides)
        assert self.post.permissions.edit.allows(identity)

        user = User(username="******", email="*****@*****.**")

        db.session.add(user)
        db.session.commit()

        identity = Identity(user.id)
        assert not self.post.permissions.edit.allows(identity)

        user.role = User.MODERATOR

        identity.provides.update(user.provides)
        assert self.post.permissions.edit.allows(identity)

        user.role = User.ADMIN
        del user.provides

        identity.provides.update(user.provides)
        assert self.post.permissions.edit.allows(identity)

    def test_can_delete(self):

        assert not self.post.permissions.delete.allows(AnonymousIdentity())

        identity = Identity(self.user.id)
        identity.provides.update(self.user.provides)
        assert self.post.permissions.delete.allows(identity)

        user = User(username="******", email="*****@*****.**")

        db.session.add(user)
        db.session.commit()

        identity = Identity(user.id)
        assert not self.post.permissions.delete.allows(identity)

        user.role = User.MODERATOR

        identity.provides.update(user.provides)
        assert self.post.permissions.delete.allows(identity)

        user.role = User.ADMIN
        del user.provides

        identity.provides.update(user.provides)
        assert self.post.permissions.delete.allows(identity)

    def test_search(self):

        posts = Post.query.search("testing")
        assert posts.count() == 1

        posts = Post.query.search("reddit")
        assert posts.count() == 1

        posts = Post.query.search("digg")
        assert posts.count() == 0

        posts = Post.query.search("testing reddit")
        assert posts.count() == 1

        posts = Post.query.search("testing digg")
        assert posts.count() == 0

        posts = Post.query.search("tester")
        assert posts.count() == 1

    def test_get_comments(self):

        parent = Comment(comment="parent comment",
                         author=self.user,
                         post=self.post)

        child1 = Comment(parent=parent,
                         post=self.post,
                         author=self.user,
                         comment="child1")

        child2 = Comment(parent=parent,
                         post=self.post,
                         author=self.user,
                         comment="child2")

        child3 = Comment(parent=child1,
                         post=self.post,
                         author=self.user,
                         comment="child3")

        db.session.add_all([parent, child1, child2, child3])
        db.session.commit()

        num_queries = len(get_debug_queries())

        comments = self.post.comments

        assert len(get_debug_queries()) == num_queries + 1

        assert comments[0].id == parent.id
        assert comments[0].depth == 0

        comments = comments[0].comments

        assert comments[0].id == child1.id
        assert comments[1].id == child2.id

        assert comments[0].depth == 1

        comments = comments[0].comments

        assert comments[0].id == child3.id

        assert comments[0].depth == 2
Esempio n. 27
0
class TestPost(TestCase):

    def setUp(self):
        super(TestPost, self).setUp()

        self.user = User(username="******",
                         email="*****@*****.**",
                         password="******")
        
        db.session.add(self.user)

        self.post = Post(title="testing",
                         link="http://reddit.com",
                         author=self.user)

        db.session.add(self.post)

        db.session.commit()

    def test_url(self):

        assert self.post.url == "/post/1/s/testing/"

    def test_permanlink(self):

        assert self.post.permalink == "http://localhost/post/1/s/testing/"

    def test_popular(self):

        assert Post.query.popular().count() == 1
        self.post.score = 0
        db.session.commit()
        assert Post.query.popular().count() == 0

    def test_deadpooled(self):

        assert Post.query.deadpooled().count() == 0
        self.post.score = 0
        db.session.commit()
        assert Post.query.deadpooled().count() == 1

    def test_jsonify(self):

        d = self.post.json
        assert d['title'] == self.post.title

        json = list(Post.query.jsonify())

        assert json[0]['title'] == self.post.title

    def test_tags(self):

        assert self.post.taglist == []

        self.post.tags = "Music, comedy, IT crowd"

        db.session.commit()

        assert self.post.taglist == ["Music", "comedy", "IT crowd"]
        
        assert self.post.linked_taglist == [
            ("Music", "/tags/music/"),
            ("comedy", "/tags/comedy/"),
            ("IT crowd", "/tags/it-crowd/"),
        ]


        assert Tag.query.count() == 3

        for tag in Tag.query.all():

            assert tag.num_posts == 1
            assert tag.posts[0].id == self.post.id

        post = Post(title="testing again",
                    link="http://reddit.com/r/programming",
                    author=self.user,
                    tags="comedy, it Crowd, Ubuntu")

        db.session.add(post)
        db.session.commit()

        assert post.taglist == ["comedy", "it Crowd", "Ubuntu"]
        assert Tag.query.count() == 4

        for tag in Tag.query.all():

            if tag.name.lower() in ("comedy", "it crowd"):
                assert tag.num_posts == 2
                assert tag.posts.count() == 2

            else:
                assert tag.num_posts == 1
                assert tag.posts.count() == 1

    def test_restricted(self):

        db.session.delete(self.post)

        user = User(username="******", email="*****@*****.**")

        db.session.add(user)

        user2 = User(username="******", email="*****@*****.**")

        db.session.add(user2)
    
        db.session.commit()
        
        admin = User(username="******", 
                     email="*****@*****.**", 
                     role=User.MODERATOR)

        
        assert user.id

        post = Post(title="test",
                    author=user,
                    access=Post.PRIVATE)

        db.session.add(post)
        db.session.commit()

        posts = Post.query.restricted(user)

        assert Post.query.restricted(user).count() == 1
        assert Post.query.restricted(admin).count() == 1
        assert Post.query.restricted(None).count() == 0
        assert Post.query.restricted(user2).count() == 0

        post.access = Post.PUBLIC
        db.session.commit()
    
        posts = Post.query.restricted(user)

        assert Post.query.restricted(user).count() == 1
        assert Post.query.restricted(admin).count() == 1
        assert Post.query.restricted(None).count() == 1
        assert Post.query.restricted(user2).count() == 1
        
        post.access = Post.FRIENDS

        db.session.commit()
        
        assert Post.query.restricted(user).count() == 1
        assert Post.query.restricted(admin).count() == 1
        assert Post.query.restricted(None).count() == 0
        assert Post.query.restricted(user2).count() == 0
    
        user2.follow(user)
        user.follow(user2)

        db.session.commit()

        assert Post.query.restricted(user2).count() == 1

    def test_can_access(self):

        user = User(username="******", email="*****@*****.**")

        db.session.add(user)

        user2 = User(username="******", email="*****@*****.**")

        db.session.add(user2)
    
        db.session.commit()
        
        admin = User(username="******", 
                     email="*****@*****.**", 
                     role=User.MODERATOR)

        
        post = Post(title="test",
                    author_id=user.id,
                    access=Post.PRIVATE)


        assert post.can_access(user)
        assert post.can_access(admin)

        assert not post.can_access(user2)
        assert not post.can_access(None)

        post.access = Post.PUBLIC

        assert post.can_access(user)
        assert post.can_access(admin)

        assert post.can_access(user2)
        assert post.can_access(None)

        post.access = Post.FRIENDS

        assert post.can_access(user)
        assert post.can_access(admin)

        assert not post.can_access(user2)
        assert not post.can_access(None)

        user.follow(user2)
        user2.follow(user)

        assert post.can_access(user2)

    def test_edit_tags(self):

        self.post.tags = "Music, comedy, IT crowd"

        db.session.commit()

        assert self.post.taglist == ["Music", "comedy", "IT crowd"]
        
        assert self.post.linked_taglist == [
            ("Music", "/tags/music/"),
            ("comedy", "/tags/comedy/"),
            ("IT crowd", "/tags/it-crowd/"),
        ]

        def _count_post_tags():
            s = db.select([db.func.count(post_tags)])
            return db.engine.execute(s).scalar()

        assert _count_post_tags() == 3

        self.post.tags = "music, iPhone, books"
        db.session.commit()

        for t in Tag.query.all():
            if t.name in ("music", "iphone", "books"):
                assert t.num_posts == 1
            
            if t.name in ("comedy", "it crowd"):
                assert t.num_posts == 0

        assert _count_post_tags() == 3
        
        self.post.tags = ""

        assert _count_post_tags() == 0

    def test_update_num_comments(self):

        comment = Comment(post=self.post,
                          author=self.user,
                          comment="test")

        db.session.add(comment)
        db.session.commit()

        signals.comment_added.send(self.post)

        post = Post.query.get(self.post.id)

        assert post.num_comments == 1

        db.session.delete(comment)
        db.session.commit()

        signals.comment_deleted.send(post)

        post = Post.query.get(post.id)

        assert post.num_comments == 0

    def test_votes(self):

        assert self.post.votes == set([])
        user = User(username="******",
                    email="*****@*****.**")

        db.session.add(user)
        db.session.commit()
        
        self.post.vote(user)

        assert user.id in self.post.votes

        post = Post.query.get(self.post.id)
        assert user.id in post.votes

    def test_can_vote(self):

        assert not self.post.permissions.vote.allows(AnonymousIdentity())

        identity = Identity(self.user.id)
        identity.provides.update(self.user.provides)
        assert not self.post.permissions.vote.allows(identity)

        user = User(username="******",
                    email="*****@*****.**")

        db.session.add(user)
        db.session.commit()

        identity = Identity(user.id)
        identity.provides.update(user.provides)

        assert self.post.permissions.vote.allows(identity)

        votes = self.post.votes
        votes.add(user.id)
        self.post.votes = votes

        del self.post.permissions

        assert not self.post.permissions.vote.allows(identity)

    def test_can_edit(self):

        assert not self.post.permissions.edit.allows(AnonymousIdentity())

        identity = Identity(self.user.id)
        identity.provides.update(self.user.provides)
        assert self.post.permissions.edit.allows(identity)

        user = User(username="******",
                    email="*****@*****.**")

        db.session.add(user)
        db.session.commit()

        identity = Identity(user.id)
        assert not self.post.permissions.edit.allows(identity)

        user.role = User.MODERATOR

        identity.provides.update(user.provides)
        assert self.post.permissions.edit.allows(identity)

        user.role = User.ADMIN
        del user.provides

        identity.provides.update(user.provides)
        assert self.post.permissions.edit.allows(identity)

    def test_can_delete(self):

        assert not self.post.permissions.delete.allows(AnonymousIdentity())

        identity = Identity(self.user.id)
        identity.provides.update(self.user.provides)
        assert self.post.permissions.delete.allows(identity)

        user = User(username="******",
                    email="*****@*****.**")

        db.session.add(user)
        db.session.commit()

        identity = Identity(user.id)
        assert not self.post.permissions.delete.allows(identity)

        user.role = User.MODERATOR

        identity.provides.update(user.provides)
        assert self.post.permissions.delete.allows(identity)

        user.role = User.ADMIN
        del user.provides

        identity.provides.update(user.provides)
        assert self.post.permissions.delete.allows(identity)

    def test_search(self):

        posts = Post.query.search("testing")
        assert posts.count() == 1

        posts = Post.query.search("reddit")
        assert posts.count() == 1

        posts = Post.query.search("digg")
        assert posts.count() == 0

        posts = Post.query.search("testing reddit")
        assert posts.count() == 1

        posts = Post.query.search("testing digg")
        assert posts.count() == 0
    
        posts = Post.query.search("tester")
        assert posts.count() == 1

    def test_get_comments(self):

        parent = Comment(comment="parent comment",
                         author=self.user,
                         post=self.post)


        child1 = Comment(parent=parent,
                         post=self.post,
                         author=self.user,
                         comment="child1")

        child2 = Comment(parent=parent,
                         post=self.post,
                         author=self.user,
                         comment="child2")

        child3 = Comment(parent=child1,
                         post=self.post, 
                         author=self.user,
                         comment="child3")

        db.session.add_all([parent, child1, child2, child3])
        db.session.commit()

        num_queries = len(get_debug_queries())

        comments = self.post.comments

        assert len(get_debug_queries()) == num_queries + 1

        assert comments[0].id == parent.id
        assert comments[0].depth == 0
        
        comments = comments[0].comments

        assert comments[0].id == child1.id
        assert comments[1].id == child2.id

        assert comments[0].depth == 1

        comments = comments[0].comments

        assert comments[0].id == child3.id

        assert comments[0].depth == 2
Esempio n. 28
0
    def test_add_comment(self):

        response = self.client.get("/post/1/addcomment/")
        self.assert_401(response)

        user = User(username="******",
                    email="*****@*****.**",
                    password="******")

        db.session.add(user)
        db.session.commit()

        self.login(login="******", password="******")

        response = self.client.get("/post/1/addcomment/")
        self.assert_404(response)

        post = Post(author=user,
                    title="test",
                    link="http://reddit.com")

        db.session.add(post)
        db.session.commit()
        

        response = self.client.get("/post/%d/addcomment/" % post.id)
        self.assert_200(response)

        response = self.client.get("/post/%d/1/reply/" % post.id)

        with mail.record_messages() as outbox:
            response = self.client.post("/post/%d/addcomment/" % post.id,
                data={"comment" : "testing"})
        
            assert len(outbox) == 0

        comment = Comment.query.first()

        self.assert_redirects(response, comment.url)

        # reply to this comment

        response = self.client.get("/post/%d/%d/reply/" % (post.id, comment.id))

        self.assert_200(response)
        
        with mail.record_messages() as outbox:
            response = self.client.post("/post/%d/%d/reply/" % (
                post.id, comment.id), data={'comment':'hello'})

            assert len(outbox) == 0

        assert Comment.query.count() == 2

        reply = Comment.query.filter(
            Comment.parent_id==comment.id).first()

        assert reply.comment == "hello"

        self.assert_redirects(response, reply.url)


        # another user

        user2 = User(username="******",
                     email="*****@*****.**",
                     password="******")

        db.session.add(user2)
        db.session.commit()

        self.login(login="******", password="******")

        with mail.record_messages() as outbox:
            response = self.client.post("/post/%d/addcomment/" % post.id,
                data={"comment" : "testing"})
        
            assert len(outbox) == 0

        user.email_alerts = True
        db.session.add(user)
        db.session.commit()

        assert User.query.filter(User.email_alerts==True).count() == 1

        with mail.record_messages() as outbox:
            response = self.client.post("/post/%d/addcomment/" % post.id,
                data={"comment" : "testing"})
        
            assert len(outbox) == 1


        with mail.record_messages() as outbox:
            response = self.client.post(
                "/post/%d/%d/reply/" % (post.id, comment.id),
                data={"comment" : "testing"})
        
            assert len(outbox) == 1

        # double check author doesn't receive own emails

        self.login(login="******", password="******")

        with mail.record_messages() as outbox:
            response = self.client.post("/post/%d/%d/reply/" % (
                post.id, comment.id), data={'comment':'hello'})

            assert len(outbox) == 0