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"
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
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
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"
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
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