Просмотр исходного кода

fix #741: cleaned response json handling in tests a little

Rafał Pitoń 8 лет назад
Родитель
Сommit
4f67c08bd2

+ 4 - 6
misago/threads/tests/test_attachments_api.py

@@ -1,11 +1,9 @@
-import json
 import os
 
 from PIL import Image
 
 from django.urls import reverse
 from django.utils import six
-from django.utils.encoding import smart_str
 
 from misago.acl.models import Role
 from misago.acl.testutils import override_acl
@@ -193,7 +191,7 @@ class AttachmentsApiTestCase(AuthenticatedUserTestCase):
             })
         self.assertEqual(response.status_code, 200)
 
-        response_json = json.loads(smart_str(response.content))
+        response_json = response.json()
         attachment = Attachment.objects.get(id=response_json['id'])
 
         self.assertEqual(attachment.filename, 'document.pdf')
@@ -232,7 +230,7 @@ class AttachmentsApiTestCase(AuthenticatedUserTestCase):
             })
         self.assertEqual(response.status_code, 200)
 
-        response_json = json.loads(smart_str(response.content))
+        response_json = response.json()
         attachment = Attachment.objects.get(id=response_json['id'])
 
         self.assertEqual(attachment.filename, 'small.jpg')
@@ -269,7 +267,7 @@ class AttachmentsApiTestCase(AuthenticatedUserTestCase):
             })
         self.assertEqual(response.status_code, 200)
 
-        response_json = json.loads(smart_str(response.content))
+        response_json = response.json()
         attachment = Attachment.objects.get(id=response_json['id'])
 
         self.assertEqual(attachment.filename, 'large.png')
@@ -320,7 +318,7 @@ class AttachmentsApiTestCase(AuthenticatedUserTestCase):
             })
         self.assertEqual(response.status_code, 200)
 
-        response_json = json.loads(smart_str(response.content))
+        response_json = response.json()
         attachment = Attachment.objects.get(id=response_json['id'])
 
         self.assertEqual(attachment.filename, 'animated.gif')

+ 2 - 5
misago/threads/tests/test_thread_editreply_api.py

@@ -1,11 +1,8 @@
 # -*- coding: utf-8 -*-
 from __future__ import unicode_literals
 
-import json
-
 from django.test.client import BOUNDARY, MULTIPART_CONTENT, encode_multipart
 from django.urls import reverse
-from django.utils.encoding import smart_str
 
 from misago.acl.testutils import override_acl
 from misago.categories.models import Category
@@ -154,7 +151,7 @@ class EditReplyTests(AuthenticatedUserTestCase):
         response = self.put(self.api_link, data={})
 
         self.assertEqual(response.status_code, 400)
-        self.assertEqual(json.loads(smart_str(response.content)), {
+        self.assertEqual(response.json(), {
             'post': [
                 "You have to enter a message."
             ]
@@ -180,7 +177,7 @@ class EditReplyTests(AuthenticatedUserTestCase):
         })
 
         self.assertEqual(response.status_code, 400)
-        self.assertEqual(json.loads(smart_str(response.content)), {
+        self.assertEqual(response.json(), {
             'post': [
                 "Posted message should be at least 5 characters long (it has 1)."
             ]

+ 13 - 14
misago/threads/tests/test_thread_postsplit_api.py

@@ -4,7 +4,6 @@ from __future__ import unicode_literals
 import json
 
 from django.urls import reverse
-from django.utils.encoding import smart_str
 from django.utils.six.moves import range
 
 from misago.acl.testutils import override_acl
@@ -189,7 +188,7 @@ class ThreadPostSplitApiTestCase(AuthenticatedUserTestCase):
         }), content_type="application/json")
         self.assertEqual(response.status_code, 400)
 
-        response_json = json.loads(smart_str(response.content))
+        response_json = response.json()
         self.assertEqual(response_json, {
             'title': ['This field is required.'],
             'category': ['This field is required.'],
@@ -204,7 +203,7 @@ class ThreadPostSplitApiTestCase(AuthenticatedUserTestCase):
         }), content_type="application/json")
         self.assertEqual(response.status_code, 400)
 
-        response_json = json.loads(smart_str(response.content))
+        response_json = response.json()
         self.assertEqual(response_json, {
             'title': ["Thread title should be at least 5 characters long (it has 3)."]
         })
@@ -222,7 +221,7 @@ class ThreadPostSplitApiTestCase(AuthenticatedUserTestCase):
         }), content_type="application/json")
         self.assertEqual(response.status_code, 400)
 
-        response_json = json.loads(smart_str(response.content))
+        response_json = response.json()
         self.assertEqual(response_json, {
             'category': ["Requested category could not be found."]
         })
@@ -240,7 +239,7 @@ class ThreadPostSplitApiTestCase(AuthenticatedUserTestCase):
         }), content_type="application/json")
         self.assertEqual(response.status_code, 400)
 
-        response_json = json.loads(smart_str(response.content))
+        response_json = response.json()
         self.assertEqual(response_json, {
             'category': [
                 "You can't create new threads in selected category."
@@ -257,7 +256,7 @@ class ThreadPostSplitApiTestCase(AuthenticatedUserTestCase):
         }), content_type="application/json")
         self.assertEqual(response.status_code, 400)
 
-        response_json = json.loads(smart_str(response.content))
+        response_json = response.json()
         self.assertEqual(response_json, {
             'weight': ["Ensure this value is less than or equal to 2."]
         })
@@ -272,7 +271,7 @@ class ThreadPostSplitApiTestCase(AuthenticatedUserTestCase):
         }), content_type="application/json")
         self.assertEqual(response.status_code, 400)
 
-        response_json = json.loads(smart_str(response.content))
+        response_json = response.json()
         self.assertEqual(response_json, {
             'weight': [
                 "You don't have permission to pin threads globally in this category."
@@ -289,7 +288,7 @@ class ThreadPostSplitApiTestCase(AuthenticatedUserTestCase):
         }), content_type="application/json")
         self.assertEqual(response.status_code, 400)
 
-        response_json = json.loads(smart_str(response.content))
+        response_json = response.json()
         self.assertEqual(response_json, {
             'weight': [
                 "You don't have permission to pin threads in this category."
@@ -310,7 +309,7 @@ class ThreadPostSplitApiTestCase(AuthenticatedUserTestCase):
         }), content_type="application/json")
         self.assertEqual(response.status_code, 400)
 
-        response_json = json.loads(smart_str(response.content))
+        response_json = response.json()
         self.assertEqual(response_json, {
             'title': ["Thread title should be at least 5 characters long (it has 3)."]
         })
@@ -329,7 +328,7 @@ class ThreadPostSplitApiTestCase(AuthenticatedUserTestCase):
         }), content_type="application/json")
         self.assertEqual(response.status_code, 400)
 
-        response_json = json.loads(smart_str(response.content))
+        response_json = response.json()
         self.assertEqual(response_json, {
             'title': ["Thread title should be at least 5 characters long (it has 3)."]
         })
@@ -344,7 +343,7 @@ class ThreadPostSplitApiTestCase(AuthenticatedUserTestCase):
         }), content_type="application/json")
         self.assertEqual(response.status_code, 400)
 
-        response_json = json.loads(smart_str(response.content))
+        response_json = response.json()
         self.assertEqual(response_json, {
             'is_closed': [
                 "You don't have permission to close threads in this category."
@@ -366,7 +365,7 @@ class ThreadPostSplitApiTestCase(AuthenticatedUserTestCase):
         }), content_type="application/json")
         self.assertEqual(response.status_code, 400)
 
-        response_json = json.loads(smart_str(response.content))
+        response_json = response.json()
         self.assertEqual(response_json, {
             'title': ["Thread title should be at least 5 characters long (it has 3)."]
         })
@@ -381,7 +380,7 @@ class ThreadPostSplitApiTestCase(AuthenticatedUserTestCase):
         }), content_type="application/json")
         self.assertEqual(response.status_code, 400)
 
-        response_json = json.loads(smart_str(response.content))
+        response_json = response.json()
         self.assertEqual(response_json, {
             'is_hidden': [
                 "You don't have permission to hide threads in this category."
@@ -403,7 +402,7 @@ class ThreadPostSplitApiTestCase(AuthenticatedUserTestCase):
         }), content_type="application/json")
         self.assertEqual(response.status_code, 400)
 
-        response_json = json.loads(smart_str(response.content))
+        response_json = response.json()
         self.assertEqual(response_json, {
             'title': ["Thread title should be at least 5 characters long (it has 3)."]
         })

+ 2 - 5
misago/threads/tests/test_thread_reply_api.py

@@ -1,10 +1,7 @@
 # -*- coding: utf-8 -*-
 from __future__ import unicode_literals
 
-import json
-
 from django.urls import reverse
-from django.utils.encoding import smart_str
 
 from misago.acl.testutils import override_acl
 from misago.categories.models import Category
@@ -114,7 +111,7 @@ class ReplyThreadTests(AuthenticatedUserTestCase):
 
         response = self.client.post(self.api_link, data={})
         self.assertEqual(response.status_code, 400)
-        self.assertEqual(json.loads(smart_str(response.content)), {
+        self.assertEqual(response.json(), {
             'post': [
                 "You have to enter a message."
             ]
@@ -129,7 +126,7 @@ class ReplyThreadTests(AuthenticatedUserTestCase):
         })
 
         self.assertEqual(response.status_code, 400)
-        self.assertEqual(json.loads(smart_str(response.content)), {
+        self.assertEqual(response.json(), {
             'post': [
                 "Posted message should be at least 5 characters long (it has 1)."
             ]

+ 9 - 11
misago/threads/tests/test_threads_editor_api.py

@@ -1,8 +1,6 @@
-import json
 import os
 
 from django.urls import reverse
-from django.utils.encoding import smart_str
 
 from misago.acl import add_acl
 from misago.acl.testutils import override_acl
@@ -124,7 +122,7 @@ class ThreadPostEditorApiTests(EditorApiTestCase):
         response = self.client.get(self.api_link)
         self.assertEqual(response.status_code, 200)
 
-        response_json = json.loads(smart_str(response.content))
+        response_json = response.json()
         self.assertEqual(response_json[0], {
             'id': self.category.pk,
             'name': self.category.name,
@@ -145,7 +143,7 @@ class ThreadPostEditorApiTests(EditorApiTestCase):
         response = self.client.get(self.api_link)
         self.assertEqual(response.status_code, 200)
 
-        response_json = json.loads(smart_str(response.content))
+        response_json = response.json()
         self.assertEqual(response_json[0], {
             'id': self.category.pk,
             'name': self.category.name,
@@ -167,7 +165,7 @@ class ThreadPostEditorApiTests(EditorApiTestCase):
         response = self.client.get(self.api_link)
         self.assertEqual(response.status_code, 200)
 
-        response_json = json.loads(smart_str(response.content))
+        response_json = response.json()
         self.assertEqual(response_json[0], {
             'id': self.category.pk,
             'name': self.category.name,
@@ -189,7 +187,7 @@ class ThreadPostEditorApiTests(EditorApiTestCase):
         response = self.client.get(self.api_link)
         self.assertEqual(response.status_code, 200)
 
-        response_json = json.loads(smart_str(response.content))
+        response_json = response.json()
         self.assertEqual(response_json[0], {
             'id': self.category.pk,
             'name': self.category.name,
@@ -211,7 +209,7 @@ class ThreadPostEditorApiTests(EditorApiTestCase):
         response = self.client.get(self.api_link)
         self.assertEqual(response.status_code, 200)
 
-        response_json = json.loads(smart_str(response.content))
+        response_json = response.json()
         self.assertEqual(response_json[0], {
             'id': self.category.pk,
             'name': self.category.name,
@@ -233,7 +231,7 @@ class ThreadPostEditorApiTests(EditorApiTestCase):
         response = self.client.get(self.api_link)
         self.assertEqual(response.status_code, 200)
 
-        response_json = json.loads(smart_str(response.content))
+        response_json = response.json()
         self.assertEqual(response_json[0], {
             'id': self.category.pk,
             'name': self.category.name,
@@ -253,7 +251,7 @@ class ThreadPostEditorApiTests(EditorApiTestCase):
         response = self.client.get(self.api_link)
         self.assertEqual(response.status_code, 200)
 
-        response_json = json.loads(smart_str(response.content))
+        response_json = response.json()
         self.assertEqual(response_json[0], {
             'id': self.category.pk,
             'name': self.category.name,
@@ -411,7 +409,7 @@ class ThreadReplyEditorApiTests(EditorApiTestCase):
         response = self.client.get('{}?reply={}'.format(self.api_link, reply_to.pk))
 
         self.assertEqual(response.status_code, 200)
-        self.assertEqual(json.loads(smart_str(response.content)), {
+        self.assertEqual(response.json(), {
             'id': reply_to.pk,
             'post': reply_to.original,
             'poster': reply_to.poster_name
@@ -654,7 +652,7 @@ class EditReplyEditorApiTests(EditorApiTestCase):
             add_acl(self.user, attachment)
 
         self.assertEqual(response.status_code, 200)
-        self.assertEqual(json.loads(smart_str(response.content)), {
+        self.assertEqual(response.json(), {
             'id': self.post.pk,
             'api': self.post.get_api_url(),
             'post': self.post.original,

+ 2 - 5
misago/users/tests/test_auth_views.py

@@ -1,8 +1,5 @@
-import json
-
 from django.test import TestCase
 from django.urls import reverse
-from django.utils.encoding import smart_str
 
 
 class AuthViewsTests(TestCase):
@@ -48,7 +45,7 @@ class AuthViewsTests(TestCase):
         response = self.client.get('/api/auth/')
         self.assertEqual(response.status_code, 200)
 
-        user_json = json.loads(smart_str(response.content))
+        user_json = response.json()
         self.assertIsNone(user_json['id'])
 
         response = self.client.post(reverse('misago:logout'))
@@ -57,5 +54,5 @@ class AuthViewsTests(TestCase):
         response = self.client.get('/api/auth/')
         self.assertEqual(response.status_code, 200)
 
-        user_json = json.loads(smart_str(response.content))
+        user_json = response.json()
         self.assertIsNone(user_json['id'])

+ 1 - 4
misago/users/tests/test_captcha_api.py

@@ -1,8 +1,5 @@
-import json
-
 from django.test import TestCase
 from django.urls import reverse
-from django.utils.encoding import smart_str
 
 from misago.conf import settings
 
@@ -29,6 +26,6 @@ class AuthenticateAPITests(TestCase):
         response = self.client.get(self.api_link)
         self.assertEqual(response.status_code, 200)
 
-        response_json = json.loads(smart_str(response.content))
+        response_json = response.json()
         self.assertEqual(response_json['question'], 'Do you like pies?')
         self.assertEqual(response_json['help_text'], 'Type in "yes".')

+ 5 - 8
misago/users/tests/test_testutils.py

@@ -1,7 +1,4 @@
-import json
-
 from django.urls import reverse
-from django.utils.encoding import smart_str
 
 from misago.users.testutils import AuthenticatedUserTestCase, SuperUserTestCase, UserTestCase
 
@@ -37,7 +34,7 @@ class UserTestCaseTests(UserTestCase):
         response = self.client.get('/api/auth/')
         self.assertEqual(response.status_code, 200)
 
-        user_json = json.loads(smart_str(response.content))
+        user_json = response.json()
         self.assertEqual(user_json['id'], user.id)
 
     def test_login_superuser(self):
@@ -48,7 +45,7 @@ class UserTestCaseTests(UserTestCase):
         response = self.client.get('/api/auth/')
         self.assertEqual(response.status_code, 200)
 
-        user_json = json.loads(smart_str(response.content))
+        user_json = response.json()
         self.assertEqual(user_json['id'], user.id)
 
     def test_logout_user(self):
@@ -60,7 +57,7 @@ class UserTestCaseTests(UserTestCase):
         response = self.client.get('/api/auth/')
         self.assertEqual(response.status_code, 200)
 
-        user_json = json.loads(smart_str(response.content))
+        user_json = response.json()
         self.assertIsNone(user_json['id'])
 
     def test_logout_superuser(self):
@@ -72,7 +69,7 @@ class UserTestCaseTests(UserTestCase):
         response = self.client.get('/api/auth/')
         self.assertEqual(response.status_code, 200)
 
-        user_json = json.loads(smart_str(response.content))
+        user_json = response.json()
         self.assertIsNone(user_json['id'])
 
 
@@ -99,5 +96,5 @@ class SuperUserTestCaseTests(SuperUserTestCase):
         response = self.client.get('/api/auth/')
         self.assertEqual(response.status_code, 200)
 
-        user_json = json.loads(smart_str(response.content))
+        user_json = response.json()
         self.assertEqual(user_json['id'], self.user.id)

+ 8 - 9
misago/users/tests/test_user_username_api.py

@@ -1,7 +1,6 @@
 import json
 
 from django.contrib.auth import get_user_model
-from django.utils.encoding import smart_str
 from django.utils.six.moves import range
 
 from misago.acl.testutils import override_acl
@@ -25,7 +24,7 @@ class UserUsernameTests(AuthenticatedUserTestCase):
         response = self.client.get(self.link)
         self.assertEqual(response.status_code, 200)
 
-        response_json = json.loads(smart_str(response.content))
+        response_json = response.json()
 
         self.assertIsNotNone(response_json['changes_left'])
         self.assertEqual(response_json['length_min'],
@@ -40,7 +39,7 @@ class UserUsernameTests(AuthenticatedUserTestCase):
         response = self.client.get(self.link)
         self.assertEqual(response.status_code, 200)
 
-        response_json = json.loads(smart_str(response.content))
+        response_json = response.json()
         self.assertEqual(response_json['changes_left'], 0)
         self.assertIsNotNone(response_json['next_on'])
 
@@ -49,12 +48,12 @@ class UserUsernameTests(AuthenticatedUserTestCase):
         response = self.client.get(self.link)
         self.assertEqual(response.status_code, 200)
 
-        response_json = json.loads(smart_str(response.content))
+        response_json = response.json()
         for i in range(response_json['changes_left']):
             self.user.set_username('NewName%s' % i, self.user)
 
         response = self.client.get(self.link)
-        response_json = json.loads(smart_str(response.content))
+        response_json = response.json()
         self.assertEqual(response_json['changes_left'], 0)
 
         response = self.client.post(self.link, data={
@@ -81,7 +80,7 @@ class UserUsernameTests(AuthenticatedUserTestCase):
     def test_change_username(self):
         """api changes username and records change"""
         response = self.client.get(self.link)
-        changes_left = json.loads(smart_str(response.content))['changes_left']
+        changes_left = response.json()['changes_left']
 
         old_username = self.user.username
         new_username = 'NewUsernamu'
@@ -91,7 +90,7 @@ class UserUsernameTests(AuthenticatedUserTestCase):
         })
 
         self.assertEqual(response.status_code, 200)
-        options = json.loads(smart_str(response.content))['options']
+        options = response.json()['options']
         self.assertEqual(changes_left, options['changes_left'] + 1)
 
         self.reload_user()
@@ -139,7 +138,7 @@ class UserUsernameModerationTests(AuthenticatedUserTestCase):
         response = self.client.get(self.link)
         self.assertEqual(response.status_code, 200)
 
-        options = json.loads(smart_str(response.content))
+        options = response.json()
         self.assertEqual(options['length_min'],
                          settings.username_length_min)
         self.assertEqual(options['length_max'],
@@ -199,7 +198,7 @@ class UserUsernameModerationTests(AuthenticatedUserTestCase):
         self.assertEqual('BobBoberson', other_user.username)
         self.assertEqual('bobboberson', other_user.slug)
 
-        options = json.loads(smart_str(response.content))
+        options = response.json()
         self.assertEqual(options['username'], other_user.username)
         self.assertEqual(options['slug'], other_user.slug)
 

+ 5 - 8
misago/users/tests/test_useradmin_views.py

@@ -1,10 +1,7 @@
-import json
-
 from django.contrib.auth import get_user_model
 from django.core import mail
 from django.urls import reverse
 from django.utils import six
-from django.utils.encoding import smart_str
 from django.utils.six.moves import range
 
 from misago.acl.models import Role
@@ -496,14 +493,14 @@ class UserAdminViewsTests(AdminTestCase):
         response = self.client.post(test_link, **self.AJAX_HEADER)
         self.assertEqual(response.status_code, 200)
 
-        response_dict = json.loads(smart_str(response.content))
+        response_dict = response.json()
         self.assertEqual(response_dict['deleted_count'], 10)
         self.assertFalse(response_dict['is_completed'])
 
         response = self.client.post(test_link, **self.AJAX_HEADER)
         self.assertEqual(response.status_code, 200)
 
-        response_dict = json.loads(smart_str(response.content))
+        response_dict = response.json()
         self.assertEqual(response_dict['deleted_count'], 0)
         self.assertTrue(response_dict['is_completed'])
 
@@ -520,14 +517,14 @@ class UserAdminViewsTests(AdminTestCase):
         response = self.client.post(test_link, **self.AJAX_HEADER)
         self.assertEqual(response.status_code, 200)
 
-        response_dict = json.loads(smart_str(response.content))
+        response_dict = response.json()
         self.assertEqual(response_dict['deleted_count'], 10)
         self.assertFalse(response_dict['is_completed'])
 
         response = self.client.post(test_link, **self.AJAX_HEADER)
         self.assertEqual(response.status_code, 200)
 
-        response_dict = json.loads(smart_str(response.content))
+        response_dict = response.json()
         self.assertEqual(response_dict['deleted_count'], 0)
         self.assertTrue(response_dict['is_completed'])
 
@@ -540,5 +537,5 @@ class UserAdminViewsTests(AdminTestCase):
         response = self.client.post(test_link, **self.AJAX_HEADER)
         self.assertEqual(response.status_code, 200)
 
-        response_dict = json.loads(smart_str(response.content))
+        response_dict = response.json()
         self.assertTrue(response_dict['is_completed'])

+ 1 - 1
misago/users/tests/test_users_api.py

@@ -478,7 +478,7 @@ class UserBanTests(AuthenticatedUserTestCase):
         response = self.client.get(self.link)
         self.assertEqual(response.status_code, 200)
 
-        ban_json = json.loads(smart_str(response.content))
+        ban_json = response.json()
         self.assertEqual(ban_json['user_message']['plain'], 'Nope!')
         self.assertEqual(ban_json['user_message']['html'], '<p>Nope!</p>')