Browse Source

Remove assertContains from thread posts move api tests

rafalp 6 years ago
parent
commit
e2de5db6f0
1 changed files with 109 additions and 59 deletions
  1. 109 59
      misago/threads/tests/test_thread_postmove_api.py

+ 109 - 59
misago/threads/tests/test_thread_postmove_api.py

@@ -92,43 +92,67 @@ class ThreadPostMoveApiTestCase(AuthenticatedUserTestCase):
 
 
         response = self.client.post(self.api_link, json.dumps({}), content_type="application/json")
         response = self.client.post(self.api_link, json.dumps({}), content_type="application/json")
         self.assertEqual(response.status_code, 403)
         self.assertEqual(response.status_code, 403)
+        self.assertEqual(response.json(), {
+            "detail": "This action is not available to guests.",
+        })
 
 
     def test_invalid_data(self):
     def test_invalid_data(self):
         """api handles post that is invalid type"""
         """api handles post that is invalid type"""
         self.override_acl()
         self.override_acl()
         response = self.client.post(self.api_link, '[]', content_type="application/json")
         response = self.client.post(self.api_link, '[]', content_type="application/json")
-        self.assertContains(response, "Invalid data. Expected a dictionary", status_code=400)
+        self.assertEqual(response.status_code, 400)
+        self.assertEqual(response.json(), {
+            "detail": "Invalid data. Expected a dictionary, but got list.",
+        })
 
 
         self.override_acl()
         self.override_acl()
         response = self.client.post(self.api_link, '123', content_type="application/json")
         response = self.client.post(self.api_link, '123', content_type="application/json")
-        self.assertContains(response, "Invalid data. Expected a dictionary", status_code=400)
+        self.assertEqual(response.status_code, 400)
+        self.assertEqual(response.json(), {
+            "detail": "Invalid data. Expected a dictionary, but got int.",
+        })
 
 
         self.override_acl()
         self.override_acl()
         response = self.client.post(self.api_link, '"string"', content_type="application/json")
         response = self.client.post(self.api_link, '"string"', content_type="application/json")
-        self.assertContains(response, "Invalid data. Expected a dictionary", status_code=400)
+        self.assertEqual(response.status_code, 400)
+        self.assertEqual(response.json(), {
+            "detail": "Invalid data. Expected a dictionary, but got str.",
+        })
 
 
         self.override_acl()
         self.override_acl()
         response = self.client.post(self.api_link, 'malformed', content_type="application/json")
         response = self.client.post(self.api_link, 'malformed', content_type="application/json")
-        self.assertContains(response, "JSON parse error", status_code=400)
+        self.assertEqual(response.status_code, 400)
+        self.assertEqual(response.json(), {
+            "detail": "JSON parse error - Expecting value: line 1 column 1 (char 0)",
+        })
 
 
     def test_no_permission(self):
     def test_no_permission(self):
         """api validates permission to move"""
         """api validates permission to move"""
         self.override_acl({'can_move_posts': 0})
         self.override_acl({'can_move_posts': 0})
 
 
         response = self.client.post(self.api_link, json.dumps({}), content_type="application/json")
         response = self.client.post(self.api_link, json.dumps({}), content_type="application/json")
-        self.assertContains(response, "You can't move posts in this thread.", status_code=403)
+        self.assertEqual(response.status_code, 403)
+        self.assertEqual(response.json(), {
+            "detail": "You can't move posts in this thread.",
+        })
 
 
     def test_move_no_new_thread_url(self):
     def test_move_no_new_thread_url(self):
         """api validates if new thread url was given"""
         """api validates if new thread url was given"""
         response = self.client.post(self.api_link)
         response = self.client.post(self.api_link)
-        self.assertContains(response, "Enter link to new thread.", status_code=400)
+        self.assertEqual(response.status_code, 400)
+        self.assertEqual(response.json(), {
+            "detail": "Enter link to new thread.",
+        })
 
 
     def test_invalid_new_thread_url(self):
     def test_invalid_new_thread_url(self):
         """api validates new thread url"""
         """api validates new thread url"""
         response = self.client.post(self.api_link, {
         response = self.client.post(self.api_link, {
             'new_thread': self.user.get_absolute_url(),
             'new_thread': self.user.get_absolute_url(),
         })
         })
-        self.assertContains(response, "This is not a valid thread link.", status_code=400)
+        self.assertEqual(response.status_code, 400)
+        self.assertEqual(response.json(), {
+            "detail": "This is not a valid thread link.",
+        })
 
 
     def test_current_new_thread_url(self):
     def test_current_new_thread_url(self):
         """api validates if new thread url points to current thread"""
         """api validates if new thread url points to current thread"""
@@ -137,9 +161,10 @@ class ThreadPostMoveApiTestCase(AuthenticatedUserTestCase):
                 'new_thread': self.thread.get_absolute_url(),
                 'new_thread': self.thread.get_absolute_url(),
             }
             }
         )
         )
-        self.assertContains(
-            response, "Thread to move posts to is same as current one.", status_code=400
-        )
+        self.assertEqual(response.status_code, 400)
+        self.assertEqual(response.json(), {
+            "detail": "Thread to move posts to is same as current one.",
+        })
 
 
     def test_other_thread_exists(self):
     def test_other_thread_exists(self):
         """api validates if other thread exists"""
         """api validates if other thread exists"""
@@ -152,9 +177,13 @@ class ThreadPostMoveApiTestCase(AuthenticatedUserTestCase):
         response = self.client.post(self.api_link, {
         response = self.client.post(self.api_link, {
             'new_thread': other_new_thread,
             'new_thread': other_new_thread,
         })
         })
-        self.assertContains(
-            response, "The thread you have entered link to doesn't exist", status_code=400
-        )
+        self.assertEqual(response.status_code, 400)
+        self.assertEqual(response.json(), {
+            "detail": (
+                "The thread you have entered link to doesn't exist "
+                "or you don't have permission to see it."
+            ),
+        })
 
 
     def test_other_thread_is_invisible(self):
     def test_other_thread_is_invisible(self):
         """api validates if other thread is visible"""
         """api validates if other thread is visible"""
@@ -167,9 +196,13 @@ class ThreadPostMoveApiTestCase(AuthenticatedUserTestCase):
                 'new_thread': other_thread.get_absolute_url(),
                 'new_thread': other_thread.get_absolute_url(),
             }
             }
         )
         )
-        self.assertContains(
-            response, "The thread you have entered link to doesn't exist", status_code=400
-        )
+        self.assertEqual(response.status_code, 400)
+        self.assertEqual(response.json(), {
+            "detail": (
+                "The thread you have entered link to doesn't exist "
+                "or you don't have permission to see it."
+            ),
+        })
 
 
     def test_other_thread_isnt_replyable(self):
     def test_other_thread_isnt_replyable(self):
         """api validates if other thread can be replied"""
         """api validates if other thread can be replied"""
@@ -182,18 +215,20 @@ class ThreadPostMoveApiTestCase(AuthenticatedUserTestCase):
                 'new_thread': other_thread.get_absolute_url(),
                 'new_thread': other_thread.get_absolute_url(),
             }
             }
         )
         )
-        self.assertContains(
-            response, "You can't move posts to threads you can't reply.", status_code=400
-        )
+        self.assertEqual(response.status_code, 400)
+        self.assertEqual(response.json(), {
+            "detail": "You can't move posts to threads you can't reply.",
+        })
 
 
     def test_empty_data(self):
     def test_empty_data(self):
         """api handles empty data"""
         """api handles empty data"""
         other_thread = testutils.post_thread(self.category)
         other_thread = testutils.post_thread(self.category)
 
 
         response = self.client.post(self.api_link)
         response = self.client.post(self.api_link)
-        self.assertContains(
-            response, "Enter link to new thread.", status_code=400
-        )
+        self.assertEqual(response.status_code, 400)
+        self.assertEqual(response.json(), {
+            "detail": "Enter link to new thread.",
+        })
 
 
     def test_empty_posts_data_json(self):
     def test_empty_posts_data_json(self):
         """api handles empty json data"""
         """api handles empty json data"""
@@ -206,10 +241,10 @@ class ThreadPostMoveApiTestCase(AuthenticatedUserTestCase):
             }),
             }),
             content_type="application/json",
             content_type="application/json",
         )
         )
-
-        self.assertContains(
-            response, "You have to specify at least one post to move.", status_code=400
-        )
+        self.assertEqual(response.status_code, 400)
+        self.assertEqual(response.json(), {
+            "detail": "You have to specify at least one post to move.",
+        })
 
 
     def test_empty_posts_data_form(self):
     def test_empty_posts_data_form(self):
         """api handles empty form data"""
         """api handles empty form data"""
@@ -221,10 +256,10 @@ class ThreadPostMoveApiTestCase(AuthenticatedUserTestCase):
                 'new_thread': other_thread.get_absolute_url(),
                 'new_thread': other_thread.get_absolute_url(),
             },
             },
         )
         )
-
-        self.assertContains(
-            response, "You have to specify at least one post to move.", status_code=400
-        )
+        self.assertEqual(response.status_code, 400)
+        self.assertEqual(response.json(), {
+            "detail": "You have to specify at least one post to move.",
+        })
 
 
     def test_no_posts_ids(self):
     def test_no_posts_ids(self):
         """api rejects no posts ids"""
         """api rejects no posts ids"""
@@ -238,9 +273,10 @@ class ThreadPostMoveApiTestCase(AuthenticatedUserTestCase):
             }),
             }),
             content_type="application/json",
             content_type="application/json",
         )
         )
-        self.assertContains(
-            response, "You have to specify at least one post to move.", status_code=400
-        )
+        self.assertEqual(response.status_code, 400)
+        self.assertEqual(response.json(), {
+            "detail": "You have to specify at least one post to move.",
+        })
 
 
     def test_invalid_posts_data(self):
     def test_invalid_posts_data(self):
         """api handles invalid data"""
         """api handles invalid data"""
@@ -254,9 +290,10 @@ class ThreadPostMoveApiTestCase(AuthenticatedUserTestCase):
             }),
             }),
             content_type="application/json",
             content_type="application/json",
         )
         )
-        self.assertContains(
-            response, "Expected a list of items", status_code=400
-        )
+        self.assertEqual(response.status_code, 400)
+        self.assertEqual(response.json(), {
+            "detail": 'Expected a list of items but got type "str".',
+        })
 
 
     def test_invalid_posts_ids(self):
     def test_invalid_posts_ids(self):
         """api handles invalid post id"""
         """api handles invalid post id"""
@@ -270,9 +307,10 @@ class ThreadPostMoveApiTestCase(AuthenticatedUserTestCase):
             }),
             }),
             content_type="application/json",
             content_type="application/json",
         )
         )
-        self.assertContains(
-            response, "One or more post ids received were invalid.", status_code=400
-        )
+        self.assertEqual(response.status_code, 400)
+        self.assertEqual(response.json(), {
+            "detail": "One or more post ids received were invalid.",
+        })
 
 
     def test_move_limit(self):
     def test_move_limit(self):
         """api rejects more posts than move limit"""
         """api rejects more posts than move limit"""
@@ -286,9 +324,10 @@ class ThreadPostMoveApiTestCase(AuthenticatedUserTestCase):
             }),
             }),
             content_type="application/json",
             content_type="application/json",
         )
         )
-        self.assertContains(
-            response, "No more than {} posts can be moved".format(POSTS_LIMIT), status_code=400
-        )
+        self.assertEqual(response.status_code, 400)
+        self.assertEqual(response.json(), {
+            "detail": "No more than %s posts can be moved at single time." % POSTS_LIMIT,
+        })
 
 
     def test_move_invisible(self):
     def test_move_invisible(self):
         """api validates posts visibility"""
         """api validates posts visibility"""
@@ -302,9 +341,10 @@ class ThreadPostMoveApiTestCase(AuthenticatedUserTestCase):
             }),
             }),
             content_type="application/json",
             content_type="application/json",
         )
         )
-        self.assertContains(
-            response, "One or more posts to move could not be found.", status_code=400
-        )
+        self.assertEqual(response.status_code, 400)
+        self.assertEqual(response.json(), {
+            "detail": "One or more posts to move could not be found.",
+        })
 
 
     def test_move_other_thread_posts(self):
     def test_move_other_thread_posts(self):
         """api recjects attempt to move other thread's post"""
         """api recjects attempt to move other thread's post"""
@@ -318,9 +358,10 @@ class ThreadPostMoveApiTestCase(AuthenticatedUserTestCase):
             }),
             }),
             content_type="application/json",
             content_type="application/json",
         )
         )
-        self.assertContains(
-            response, "One or more posts to move could not be found.", status_code=400
-        )
+        self.assertEqual(response.status_code, 400)
+        self.assertEqual(response.json(), {
+            "detail": "One or more posts to move could not be found.",
+        })
 
 
     def test_move_event(self):
     def test_move_event(self):
         """api rejects events move"""
         """api rejects events move"""
@@ -334,7 +375,10 @@ class ThreadPostMoveApiTestCase(AuthenticatedUserTestCase):
             }),
             }),
             content_type="application/json",
             content_type="application/json",
         )
         )
-        self.assertContains(response, "Events can't be moved.", status_code=400)
+        self.assertEqual(response.status_code, 400)
+        self.assertEqual(response.json(), {
+            "detail": "Events can't be moved.",
+        })
 
 
     def test_move_first_post(self):
     def test_move_first_post(self):
         """api rejects first post move"""
         """api rejects first post move"""
@@ -348,7 +392,10 @@ class ThreadPostMoveApiTestCase(AuthenticatedUserTestCase):
             }),
             }),
             content_type="application/json",
             content_type="application/json",
         )
         )
-        self.assertContains(response, "You can't move thread's first post.", status_code=400)
+        self.assertEqual(response.status_code, 400)
+        self.assertEqual(response.json(), {
+            "detail": "You can't move thread's first post.",
+        })
 
 
     def test_move_hidden_posts(self):
     def test_move_hidden_posts(self):
         """api recjects attempt to move urneadable hidden post"""
         """api recjects attempt to move urneadable hidden post"""
@@ -362,9 +409,10 @@ class ThreadPostMoveApiTestCase(AuthenticatedUserTestCase):
             }),
             }),
             content_type="application/json",
             content_type="application/json",
         )
         )
-        self.assertContains(
-            response, "You can't move posts the content you can't see.", status_code=400
-        )
+        self.assertEqual(response.status_code, 400)
+        self.assertEqual(response.json(), {
+            "detail": "You can't move posts the content you can't see.",
+        })
 
 
     def test_move_posts_closed_thread_no_permission(self):
     def test_move_posts_closed_thread_no_permission(self):
         """api recjects attempt to move posts from closed thread"""
         """api recjects attempt to move posts from closed thread"""
@@ -383,9 +431,10 @@ class ThreadPostMoveApiTestCase(AuthenticatedUserTestCase):
             }),
             }),
             content_type="application/json",
             content_type="application/json",
         )
         )
-        self.assertContains(
-            response, "This thread is closed. You can't move posts in it.", status_code=400
-        )
+        self.assertEqual(response.status_code, 400)
+        self.assertEqual(response.json(), {
+            "detail": "This thread is closed. You can't move posts in it.",
+        })
 
 
     def test_move_posts_closed_category_no_permission(self):
     def test_move_posts_closed_category_no_permission(self):
         """api recjects attempt to move posts from closed thread"""
         """api recjects attempt to move posts from closed thread"""
@@ -405,9 +454,10 @@ class ThreadPostMoveApiTestCase(AuthenticatedUserTestCase):
             }),
             }),
             content_type="application/json",
             content_type="application/json",
         )
         )
-        self.assertContains(
-            response, "This category is closed. You can't move posts in it.", status_code=400
-        )
+        self.assertEqual(response.status_code, 400)
+        self.assertEqual(response.json(), {
+            "detail": "This category is closed. You can't move posts in it.",
+        })
 
 
     def test_move_posts(self):
     def test_move_posts(self):
         """api moves posts to other thread"""
         """api moves posts to other thread"""