Anonymous avatar Anonymous committed f83f8a4

started tests refactoring

Comments (0)

Files changed (1)

tests/test_views.py

 
 class TestApi(TestCase):
 
-    def setUp(self):
+    def create_user(self):
+        user = User(username="tester",
+                    email="tester@example.com",
+                    password="test")
+
+        db.session.add(user)
+        db.session.commit()
+
+        return user
+
+    def create_post(self):
         
-        super(TestApi, self).setUp()
+        post = Post(author=self.create_user(),
+                    title="test")
 
-        self.user = User(username="tester",
-                         email="tester@example.com",
-                         password="test")
+        db.session.add(post)
+        db.session.commit()
 
-        self.post = Post(author=self.user,
-                         title="test")
-
-        db.session.add_all([self.user, self.post])
-        db.session.commit()
+        return post
 
     def test_get_post(self):
 
-        response = self.client.get("/api/post/%d/" % self.post.id)
+        post = self.create_post()
+
+        response = self.client.get("/api/post/%d/" % post.id)
         self.assert_200(response)
 
-        assert response.json['post_id'] == self.post.id
+        assert response.json['post_id'] == post.id
         assert response.json['title'] == "test"
         assert response.json['author'] == "tester"
 
     def test_search(self):
 
+        self.create_post()
+
         response = self.client.get("/api/search/?keywords=test") 
 
         self.assert_200(response)
 
     def test_user(self):
 
+        self.create_post()
+
         response = self.client.get("/api/user/tester/")
         self.assert_200(response)
 
         assert len(response.json['posts']) == 1
 
+
 class TestComment(TestCase):
 
-    def setUp(self):
-        super(TestComment, self).setUp()
+    def create_comment(self):
 
-        self.user = User(username="tester",
-                         email="tester@example.com",
-                         password="test")
+        user = User(username="tester",
+                    email="tester@example.com",
+                    password="test")
 
-        self.post = Post(author=self.user,
-                         title="test")
+        post = Post(author=user,
+                    title="test")
 
-        self.comment = Comment(post=self.post,
-                               author=self.user,
-                               comment="test")
+        comment = Comment(post=post,
+                          author=user,
+                          comment="test")
 
-        db.session.add_all([self.user, self.post, self.comment])
+        db.session.add_all([user, post, comment])
         db.session.commit()
 
-        comment_added.send(self.post)
+        comment_added.send(post)
+        return comment
 
-    def test_edit_comment(self):
+    def test_edit_comment_not_logged_in(self):
 
-        response = self.client.get("/comment/%d/edit/" % self.comment.id)
+        comment = self.create_comment()
+        response = self.client.get("/comment/%d/edit/" % comment.id)
         self.assert_401(response)
 
+    def test_edit_comment_not_logged_in_as_author(self):
+
+        comment = self.create_comment()
         user = User(username="tester2",
                     email="tester2@example.com",
                     password="test")
 
         self.login(login="tester2", password="test")
 
-        response = self.client.get("/comment/%d/edit/" % self.comment.id)
+        response = self.client.get("/comment/%d/edit/" % comment.id)
         self.assert_403(response)
 
+    def test_edit_comment_logged_in_as_author(self):
+
+        comment = self.create_comment()
+        self.login(login="tester", password="test")
+
+        response = self.client.get("/comment/%d/edit/" % comment.id)
+        self.assert_200(response)
+
+    def test_update_comment_logged_in_as_author(self):
+
+        comment = self.create_comment()
+
         self.login(login="tester", password="test")
 
-        response = self.client.get("/comment/%d/edit/" % self.comment.id)
-        self.assert_200(response)
-
-        response = self.client.post("/comment/%d/edit/" % self.comment.id,
+        response = self.client.post("/comment/%d/edit/" % comment.id,
                                     data={"comment":"test2"})
         
-        self.assert_redirects(response, self.comment.url)
+        self.assert_redirects(response, comment.url)
         
-        comment = Comment.query.get(self.comment.id)
+        comment = Comment.query.get(comment.id)
 
         assert comment.comment == "test2"
 
-    def test_delete_comment(self):
+    def test_delete_comment_not_logged_in(self):
 
-        response = self.client.get("/comment/%d/delete/" % self.comment.id)
+        comment = self.create_comment()
+        response = self.client.get("/comment/%d/delete/" % comment.id)
         self.assert_405(response)
        
-        response = self.client.post("/comment/%d/delete/" % self.comment.id)
+    def test_delete_comment_not_logged_in_as_author(self):
+
+        comment = self.create_comment()
+        response = self.client.post("/comment/%d/delete/" % comment.id)
   
         self.assert_401(response)
 
 
         self.login(login="tester2", password="test")
 
-        response = self.client.post("/comment/%d/delete/" % self.comment.id)
+        response = self.client.post("/comment/%d/delete/" % comment.id)
         self.assert_403(response)
 
+    def test_delete_comment_logged_in_as_author(self):
+
+        comment = self.create_comment()
         self.login(login="tester", password="test")
 
-        response = self.client.post("/comment/%d/delete/" % self.comment.id)
+        response = self.client.post("/comment/%d/delete/" % comment.id)
         
         assert Comment.query.count() == 0
 
-        post = Post.query.get(self.post.id)
+        post = Post.query.get(comment.post.id)
         assert post.num_comments == 0
 
         assert response.json['success']
-        assert response.json['comment_id'] == self.comment.id
+        assert response.json['comment_id'] == comment.id
 
     
 class TestFrontend(TestCase):
 
         db.session.commit()
 
-    def test_submit(self):
+    def test_submit_not_logged_in(self):
 
         response = self.client.get("/submit/")
         self.assert_401(response)
 
+    def test_post_submit_not_logged_in(self):
+
+        data = {
+                "title" : "testing",
+                "description" : "a test"
+                }
+
+        response = self.client.post("/submit/", data=data)
+        self.assert_401(response)
+
+    def test_submit_logged_in(self):
+
         user = User(username="tester",
                     password="test",
                     email="tester@example.com")
         self.login(login="tester", password="test")
         response = self.client.get("/submit/")
         self.assert_200(response)
+
+    def test_post_submit_logged_in(self):
+        
+        user = User(username="tester",
+                    password="test",
+                    email="tester@example.com")
+
+        db.session.add(user)
+        db.session.commit()
+
+        self.login(login="tester", password="test")
     
         data = {
                 "title" : "testing",
         
 class TestPost(TestCase):
 
-    def test_delete_post(self):
-
-        response = self.client.get("/post/1/delete/")
-        self.assert_405(response)
-
-        response = self.client.post("/post/1/delete/")
-        self.assert_401(response)
+    def create_user(self, login=False):
 
         user = User(username="tester",
                     password="test",
         db.session.add(user)
         db.session.commit()
 
-        self.login(login="tester", password="test")
+        if login:
+            self.login(login="tester", password="test")
+        return user
+
+    def test_delete_post_via_get(self):
+
+        response = self.client.get("/post/1/delete/")
+        self.assert_405(response)
+
+    def test_delete_non_existing_post_not_logged_in(self):
+
         response = self.client.post("/post/1/delete/")
+        self.assert_401(response)
 
+    def test_delete_non_existing_post_logged_in(self):
+
+        user = self.create_user(True)
+        response = self.client.post("/post/1/delete/")
         self.assert_404(response)
 
+    def test_delete_existing_post_not_logged_in(self):
+
+        user = self.create_user(False)
+        
         post = Post(author=user,
                     title="test",
                     description="test")
         db.session.commit()
 
         response = self.client.post("/post/%d/delete/" % post.id)
+        self.assert_401(response)
         
+    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
 
+    def test_delete_post_not_logged_in_as_author(self):
+
+        user = self.create_user(False)
+        
+        post = Post(author=user,
+                    title="test",
+                    description="test")
+
+        db.session.add(post)
+        db.session.commit()
+
+        user = User(username="tester2",
+                    password="test",
+                    email="tester2@example.com")
+
+        db.session.add(user)
+        db.session.commit()
+
+        self.login(login="tester2", password="test")
+ 
+        response = self.client.post("/post/%d/delete/" % post.id)
+
+        self.assert_403(response)
+
+    def test_delete_post_logged_in_as_admin(self):
+    
+        user = self.create_user(False)
+        
         admin_user = User(username="admin",
                           email="admin@newsmeme.com",
                           password="admin1",
             assert Post.query.count() == 0
             assert len(outbox) == 1
 
-    def test_edit_post(self):
+    def test_edit_non_existing_post_not_logged_in(self):
 
         response = self.client.get("/post/1/edit/")
         self.assert_401(response)
         
-        user = User(username="tester",
-                    password="test",
-                    email="tester@example.com")
+    def test_edit_non_existing_post_logged_in(self):
 
-        db.session.add(user)
-
-        db.session.commit()
-
-        self.login(login="tester", password="test")
+        user = self.create_user(True)
         response = self.client.get("/post/1/edit/")
         
         self.assert_404(response)
 
-        post = Post(author=user,
+    def test_edit_existing_post_not_logged_in(self):
+
+        post = Post(author=self.create_user(False),
+                    title="test",
+                    description="test")
+
+        db.session.add(post)
+        db.session.commit()
+
+        response = self.client.get("/post/%d/edit/" % post.id)
+        self.assert_401(response)
+
+    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="tester2",
+                    password="test",
+                    email="tester2@example.com")
+
+        db.session.add(user)
+        db.session.commit()
+
+        self.login(login="tester2", password="test")
+ 
+        response = self.client.get("/post/%d/edit/" % post.id)
+        self.assert_403(response)
+
+    def test_edit_existing_post_logged_in_as_author(self):
+
+        post = Post(author=self.create_user(True),
                     title="test",
                     description="test")
 
         response = self.client.get("/post/%d/edit/" % post.id)
         self.assert_200(response)
 
+    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)
         
         assert post.title == "testing 123"
         assert post.description == "a test 123"
 
+    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="admin",
                           email="admin@newsmeme.com",
                           password="admin1",
         db.session.commit()
 
         self.login(login="admin", password="admin1")
+ 
+ 
+        data = {
+                "title" : "testing 123",
+                "description" : "a test 123"
+                }
 
-        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/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_view_post(self):
 
         response = self.client.get("/post/1/")
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.