def test_to_json(self):
        self.assertEqual(to_json(123), '123')
        self.assertEqual(to_json('123'), '"123"')
        self.assertEqual(to_json({'a': '3', 'b': 5}), '{"a": "3", "b": 5}')
        self.assertEqual(to_json(Decimal('10.33')), '"10.33"')
        self.assertEqual(to_json(ugettext_lazy('ignore this also')),
                         '"ignore this also"')
        self.assertEqual(to_json(datetime.datetime(2015, 1, 20)),
                         '"2015-01-20T00:00:00"')
        self.assertEqual(to_json(datetime.date(2015, 1, 20)), '"2015-01-20"')

        @six.python_2_unicode_compatible
        class TestClass(object):
            def splitlines(self):
                return ('line 1', 'line 2')

            def __str__(self):
                return 'hello'

        class TestClass2(object):
            pass

        self.assertEqual(to_json(TestClass()), '"hello"')
        self.assertEqual(to_json(TestClass2()), 'null')

        with self.settings(DEBUG=True):
            self.assertEqual(to_json({
                'a': '3',
                'b': 5
            }), '{\n  "a": "3", \n  "b": 5\n}')
 def test_FOO(self):
     request_body = to_json({
         'method': 'FOO',
         'view_params': {'view_slug': 'post'},
         'params': {}
     })
     with self.assertRaises(NotImplementedError):
         test_backoffice.api_post_view_action(RequestWithViewParams(request_body, self.user, post=True))
 def test_post_view_action_non_existing_method(self):
     request_body = to_json({
         'method': 'non_existing_method',
         'view_params': {'view_slug': 'post'},
         'params': {}
     })
     with self.assertRaises(Http404):
          test_backoffice.api_post_view_action(RequestWithViewParams(request_body, self.user, post=True))
 def test_post_view_action_not_allowed(self):
     request_body = to_json({
         'method': 'do_something',
         'view_params': {'view_slug': 'post'},
         'params': {}
     })
     r = test_backoffice.api_post_view_action(RequestWithViewParams(request_body, self.user2, post=True))
     self.assertEqual(r.content, 'You are not allowed to post data to the view "post".')
 def test_post_view_action(self):
     request_body = to_json({
         'method': 'do_something',
         'view_params': {'view_slug': 'post'},
         'params': {}
     })
     r = test_backoffice.api_post_view_action(RequestWithViewParams(request_body, self.user, post=True))
     self.assertEqual(r, 'something')
 def test_FOO(self):
     request_body = to_json({
         'method': 'FOO',
         'view_params': {
             'view_slug': 'post'
         },
         'params': {}
     })
     with self.assertRaises(NotImplementedError):
         test_backoffice.api_post_view_action(
             RequestWithViewParams(request_body, self.user, post=True))
 def test_post_view_action_non_existing_method(self):
     request_body = to_json({
         'method': 'non_existing_method',
         'view_params': {
             'view_slug': 'post'
         },
         'params': {}
     })
     with self.assertRaises(Http404):
         test_backoffice.api_post_view_action(
             RequestWithViewParams(request_body, self.user, post=True))
 def test_post_view_action(self):
     request_body = to_json({
         'method': 'do_something',
         'view_params': {
             'view_slug': 'post'
         },
         'params': {}
     })
     r = test_backoffice.api_post_view_action(
         RequestWithViewParams(request_body, self.user, post=True))
     self.assertEqual(r, 'something')
 def test_post_view_action_not_allowed(self):
     request_body = to_json({
         'method': 'do_something',
         'view_params': {
             'view_slug': 'post'
         },
         'params': {}
     })
     r = test_backoffice.api_post_view_action(
         RequestWithViewParams(request_body, self.user2, post=True))
     self.assertEqual(
         r.content, 'You are not allowed to post data to the view "post".')
    def test_to_json(self):
        self.assertEqual(to_json(123), '123')
        self.assertEqual(to_json('123'), '"123"')
        self.assertEqual(to_json({'a': '3', 'b': 5}), '{"a": "3", "b": 5}')
        self.assertEqual(to_json(Decimal('10.33')), '"10.33"')
        self.assertEqual(to_json(ugettext_lazy('ignore this also')), '"ignore this also"')
        self.assertEqual(to_json(datetime.datetime(2015, 1, 20)), '"2015-01-20T00:00:00"')
        self.assertEqual(to_json(datetime.date(2015, 1, 20)), '"2015-01-20"')

        @six.python_2_unicode_compatible
        class TestClass(object):
            def splitlines(self):
                return ('line 1', 'line 2')
            def __str__(self):
                return 'hello'

        class TestClass2(object):
            pass

        self.assertEqual(to_json(TestClass()), '"hello"')
        self.assertEqual(to_json(TestClass2()), 'null')

        with self.settings(DEBUG=True):
            self.assertEqual(to_json({'a': '3', 'b': 5}), '{\n  "a": "3", \n  "b": 5\n}')