Ejemplo n.º 1
0
    def test_delete_success(self):
        root_resource = mock_resource(name='root')
        root_resource.allowed_methods.add('DELETE')

        savory_dispatch(root_resource, method='DELETE')
        self.assertTrue(root_resource.delete.called)
        self.assertIsNotNone(root_resource.delete.call_args_list[0].request)
Ejemplo n.º 2
0
    def test_child_resolution(self):
        child_resource = mock_resource(name='child')
        child_resource.allowed_methods.add('GET')
        child_resource.get = Mock(return_value={})

        root_resource = mock_resource(name='root', child_resource=child_resource)

        savory_dispatch(root_resource, method='GET', resource_path='child')

        self.assertEqual(call_args_sans_context(root_resource.get_child_resource), ['child'])
        self.assertTrue(child_resource.get.called)
        self.assertIsNotNone(child_resource.get.call_args_list[0].request)
Ejemplo n.º 3
0
 def test_get_success(self):
     response = savory_dispatch(api_resource, method='GET', resource_path='users/1')
     self.assertEqual(response.status_code, 200)
     self.assertEqual(
         json.loads(response.content),
         {u'resourceUri': u'http://localhost/api/users/1',
          u'age': 31,
          u'name': 'Alice'}
     )
Ejemplo n.º 4
0
    def test_get_success(self):
        root_resource = mock_resource(name='root')
        root_resource.allowed_methods.add('GET')
        root_resource.get = Mock(return_value={'foo': 'bar'})

        response = savory_dispatch(root_resource, method='GET')

        self.assertEqual(response.content, '{"foo": "bar"}')
        self.assertTrue(root_resource.get.called)
        self.assertIsNotNone(root_resource.get.call_args_list[0].request)
Ejemplo n.º 5
0
    def test_exception_handling(self):
        root_resource = mock_resource(name='root')
        root_resource.allowed_methods.add('GET')
        root_resource.get.side_effect = Exception('Fail')

        response = savory_dispatch(root_resource, method='GET')

        response_json = json.loads(response.content)
        self.assertIn('error', response_json)
        self.assertEqual(response.status_code, 500)
Ejemplo n.º 6
0
    def test_unauthorized(self):
        root_resource = mock_resource(name='root')
        root_resource.allowed_methods.add('PUT')
        root_resource.put.side_effect = AuthorizationError('foo')

        response = savory_dispatch(root_resource, method='PUT', body='{"foo": "bar"}')
        response_json = json.loads(response.content)

        self.assertIn('validation_errors', response_json)
        self.assertEqual(response_json['validation_errors'], ['Modification of field foo not authorized'])
        self.assertEqual(response.status_code, 403)
Ejemplo n.º 7
0
    def test_get_with_apiexception(self, logger):
        root_resource = mock_resource(name='root')
        root_resource.allowed_methods.add('GET')

        root_resource.get = Mock(side_effect=ApiException('Some kind of server error'))

        response = savory_dispatch(root_resource, method='GET')
        self.assertEqual(response.status_code, 500)
        content = json.loads(response.content)
        self.assertTrue('message' in content)
        self.assertEqual(content['message'], 'Some kind of server error')
        self.assertTrue(logger.error.called)
Ejemplo n.º 8
0
    def test_put_with_exception(self, logger):
        root_resource = mock_resource(name='root')
        root_resource.allowed_methods.add('PUT')

        root_resource.put = Mock(side_effect=Exception('Some kind of server error'))

        response = savory_dispatch(root_resource, method='PUT', body='{"foo": "bar"}')
        self.assertEqual(response.status_code, 500)
        content = json.loads(response.content)
        self.assertTrue('message' in content)
        self.assertEqual(content['message'], 'Some kind of server error')
        self.assertTrue(logger.exception.called)
Ejemplo n.º 9
0
    def test_validation_handling(self):
        root_resource = mock_resource(name='root')
        root_resource.allowed_methods.add('PUT')
        root_resource.put.side_effect = validators.ValidationError(
            Mock(),
            {
                'class.field': 'broken',
            }
        )

        response = savory_dispatch(root_resource, method='PUT', body='{}')

        response_json = json.loads(response.content)
        self.assertIn('validation_errors', response_json)
        self.assertEqual(response_json['validation_errors']['class.field'], 'broken')
        self.assertEqual(response.status_code, 400)
Ejemplo n.º 10
0
    def test_post_with_exception(self, logger):
        root_resource = mock_resource(name='root')
        root_resource.allowed_methods.add('POST')

        def side_effect(*args):
            raise Exception('Some kind of server error')

        root_resource.post = Mock(side_effect=side_effect)

        response = savory_dispatch(root_resource, method='POST', body='{}')
        self.assertEqual(response.status_code, 500)
        content = json.loads(response.content)
        self.assertTrue('error' in content)
        self.assertTrue(content['error'].startswith('Traceback (most recent call last):'))
        self.assertTrue('Some kind of server error' in content['error'])
        self.assertTrue(logger.exception.called)
Ejemplo n.º 11
0
    def test_post_with_collision_one(self):
        root_resource = mock_resource(name='root')
        root_resource.allowed_methods.add('POST')

        def side_effect(*args):
            # This could occur if a slightly earlier POST or PUT still had
            # the database locked during a DB transaction.
            from django.db.transaction import TransactionManagementError

            raise TransactionManagementError()

        root_resource.post = Mock(side_effect=side_effect)

        response = savory_dispatch(root_resource, method='POST', body='{}')
        self.assertEqual(response.status_code, 409)
        self.assertEqual(response.content, '{"resource": "http://localhost/api/"}')
Ejemplo n.º 12
0
    def test_set_expires(self):
        expires = datetime(2005, 7, 14, 12, 30)

        def get(ctx, params):
            ctx.set_expires_header(datetime.utcnow())
            ctx.set_expires_header(expires)
            ctx.set_expires_header(datetime.utcnow())
            return {'foo2': 'bar2'}

        root_resource = mock_resource(name='root')
        root_resource.allowed_methods.add('GET')
        root_resource.get = get

        response = savory_dispatch(root_resource, method='GET')

        self.assertEqual(response['Expires'], expires.isoformat('T'))
        self.assertEqual(response.content, '{"foo2": "bar2"}')
Ejemplo n.º 13
0
    def test_set_header(self):
        """
        Tests the set_header method in the APIContext class
        """

        def get(ctx, params):
            ctx.set_header('foo1', 'bar1')
            return {'foo2': 'bar2'}

        root_resource = mock_resource(name='root')
        root_resource.allowed_methods.add('GET')
        root_resource.get = get

        response = savory_dispatch(root_resource, method='GET')

        self.assertEqual(response['foo1'], 'bar1')
        self.assertEqual(response.content, '{"foo2": "bar2"}')
Ejemplo n.º 14
0
    def test_post_success(self):
        root_resource = mock_resource(name='root')
        root_resource.allowed_methods.add('POST')

        new_resource = mock_resource(name='new', resource_path='foo')
        root_resource.post = Mock(return_value=new_resource)

        response = savory_dispatch(root_resource, method='POST', body='{"foo": "bar"}')

        self.assertTrue(
            call_args_sans_context(root_resource.post),
            [
                {
                    'foo': 'bar'
                }
            ]
        )
        self.assertEqual(response['Location'], 'http://localhost/api/foo')
        self.assertIsNotNone(root_resource.post.call_args_list[0].request)
Ejemplo n.º 15
0
    def test_post_success_returns_resource_when_indicated(self):
        root_resource = mock_resource(name='root')
        root_resource.allowed_methods.add('POST')
        root_resource.return_on_post = True
        root_resource.post.return_value = {'key': 'value'}

        response = savory_dispatch(root_resource, method='POST', body='{"foo": "bar"}')

        self.assertTrue(
            call_args_sans_context(root_resource.post),
            [
                {
                    'foo': 'bar'
                }
            ]
        )
        self.assertEqual(response.status_code, 201)
        self.assertEqual(response.content, '{"key": "value"}')
        self.assertIsNotNone(root_resource.post.call_args_list[0].request)
Ejemplo n.º 16
0
    def test_put_no_content_success(self):
        root_resource = mock_resource(name='root')
        root_resource.allowed_methods.add('PUT')
        root_resource.get.return_value = {}
        root_resource.put.return_value = None

        response = savory_dispatch(root_resource, method='PUT', body='{"foo": "bar"}')

        self.assertTrue(
            call_args_sans_context(root_resource.put),
            [
                {
                    'foo': 'bar'
                }
            ]
        )

        self.assertEqual(response.status_code, 204)
        self.assertEqual(response.content, '')
        self.assertIsNotNone(root_resource.put.call_args_list[0].request)
Ejemplo n.º 17
0
    def test_get_success_streaming(self):
        def get(ctx, params):
            ctx.streaming_response = True
            ctx.formatter = Mock()
            ctx.formatter.write_to = lambda *args: None
            return iter([
                '{"foo": ',
                '"bar"',
                '}',
            ])

        root_resource = mock_resource(name='root')
        root_resource.allowed_methods.add('GET')
        root_resource.get = Mock(side_effect=get)

        response = savory_dispatch(root_resource, method='GET')

        self.assertEqual(response.status_code, 200)
        self.assertEqual(''.join(response.streaming_content), '{"foo": "bar"}')
        self.assertTrue(root_resource.get.called)
        self.assertIsNotNone(root_resource.get.call_args_list[0].request)
Ejemplo n.º 18
0
    def test_child_resolution_fail(self):
        root_resource = mock_resource(name='root')

        response = savory_dispatch(root_resource, method='GET', resource_path='child/grandchild')
        self.assertEqual(response.status_code, 404)
Ejemplo n.º 19
0
    def test_delete_not_supported(self):
        root_resource = mock_resource(name='root')

        response = savory_dispatch(root_resource, method='DELETE')
        self.assertEqual(response.status_code, 405)
Ejemplo n.º 20
0
    def test_post_not_supported(self):
        root_resource = mock_resource(name='root')

        response = savory_dispatch(root_resource, method='POST', body='{}')

        self.assertEqual(response.status_code, 405)