pyramid_cubicweb/tests/test_rest_api.py
author David Douard <david.douard@logilab.fr>
Tue, 22 Mar 2016 14:01:44 +0100
changeset 11611 9d2bb6bdb5c8
parent 11603 c7bfeb7b0d67
permissions -rw-r--r--
[tests] add a __main__ handler the relative import in test_rest_api.py needs to be modified to prevent a ValueError: Attempted relative import in non-package
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11596
1f2570f1d72a [routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     1
from __future__ import absolute_import
1f2570f1d72a [routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     2
11598
2dc0b0db2329 use CubicWeb request to execute RQL
Rabah Meradi <rabah.meradi@logilab.fr>
parents: 11597
diff changeset
     3
from pyramid_cubicweb.rest_api import EntityResource
2dc0b0db2329 use CubicWeb request to execute RQL
Rabah Meradi <rabah.meradi@logilab.fr>
parents: 11597
diff changeset
     4
from pyramid_cubicweb.core import CubicWebPyramidRequest
2dc0b0db2329 use CubicWeb request to execute RQL
Rabah Meradi <rabah.meradi@logilab.fr>
parents: 11597
diff changeset
     5
from pyramid.view import view_config
2dc0b0db2329 use CubicWeb request to execute RQL
Rabah Meradi <rabah.meradi@logilab.fr>
parents: 11597
diff changeset
     6
11611
9d2bb6bdb5c8 [tests] add a __main__ handler
David Douard <david.douard@logilab.fr>
parents: 11603
diff changeset
     7
from pyramid_cubicweb.tests import PyramidCWTest
9d2bb6bdb5c8 [tests] add a __main__ handler
David Douard <david.douard@logilab.fr>
parents: 11603
diff changeset
     8
11596
1f2570f1d72a [routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     9
11597
286611d1d7a4 [views] Rename entities to rest_api
Rabah Meradi <rabah.meradi@logilab.fr>
parents: 11596
diff changeset
    10
class RestApiTest(PyramidCWTest):
11596
1f2570f1d72a [routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    11
    def includeme(self, config):
11597
286611d1d7a4 [views] Rename entities to rest_api
Rabah Meradi <rabah.meradi@logilab.fr>
parents: 11596
diff changeset
    12
        config.include('pyramid_cubicweb.rest_api')
11598
2dc0b0db2329 use CubicWeb request to execute RQL
Rabah Meradi <rabah.meradi@logilab.fr>
parents: 11597
diff changeset
    13
        config.include('pyramid_cubicweb.tests.test_rest_api')
11596
1f2570f1d72a [routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    14
1f2570f1d72a [routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    15
    def test_delete(self):
1f2570f1d72a [routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    16
        with self.admin_access.repo_cnx() as cnx:
1f2570f1d72a [routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    17
            cnx.create_entity('CWGroup', name=u'tmp')
1f2570f1d72a [routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    18
            cnx.commit()
1f2570f1d72a [routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    19
1f2570f1d72a [routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    20
        self.login()
1f2570f1d72a [routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    21
        res = self.webapp.delete('/cwgroup/tmp')
1f2570f1d72a [routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    22
        self.assertEqual(res.status_int, 204)
1f2570f1d72a [routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    23
1f2570f1d72a [routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    24
        with self.admin_access.repo_cnx() as cnx:
1f2570f1d72a [routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    25
            self.assertEqual(cnx.find('CWGroup', name=u'tmp').rowcount, 0)
11598
2dc0b0db2329 use CubicWeb request to execute RQL
Rabah Meradi <rabah.meradi@logilab.fr>
parents: 11597
diff changeset
    26
2dc0b0db2329 use CubicWeb request to execute RQL
Rabah Meradi <rabah.meradi@logilab.fr>
parents: 11597
diff changeset
    27
    def test_rql_execute(self):
2dc0b0db2329 use CubicWeb request to execute RQL
Rabah Meradi <rabah.meradi@logilab.fr>
parents: 11597
diff changeset
    28
        with self.admin_access.repo_cnx() as cnx:
2dc0b0db2329 use CubicWeb request to execute RQL
Rabah Meradi <rabah.meradi@logilab.fr>
parents: 11597
diff changeset
    29
            cnx.create_entity('CWGroup', name=u'tmp')
2dc0b0db2329 use CubicWeb request to execute RQL
Rabah Meradi <rabah.meradi@logilab.fr>
parents: 11597
diff changeset
    30
            cnx.commit()
2dc0b0db2329 use CubicWeb request to execute RQL
Rabah Meradi <rabah.meradi@logilab.fr>
parents: 11597
diff changeset
    31
        self.login()
2dc0b0db2329 use CubicWeb request to execute RQL
Rabah Meradi <rabah.meradi@logilab.fr>
parents: 11597
diff changeset
    32
        params = {'test_rql_execute': 'test'}
2dc0b0db2329 use CubicWeb request to execute RQL
Rabah Meradi <rabah.meradi@logilab.fr>
parents: 11597
diff changeset
    33
        self.webapp.get('/cwgroup/tmp', params=params)
2dc0b0db2329 use CubicWeb request to execute RQL
Rabah Meradi <rabah.meradi@logilab.fr>
parents: 11597
diff changeset
    34
2dc0b0db2329 use CubicWeb request to execute RQL
Rabah Meradi <rabah.meradi@logilab.fr>
parents: 11597
diff changeset
    35
2dc0b0db2329 use CubicWeb request to execute RQL
Rabah Meradi <rabah.meradi@logilab.fr>
parents: 11597
diff changeset
    36
@view_config(
2dc0b0db2329 use CubicWeb request to execute RQL
Rabah Meradi <rabah.meradi@logilab.fr>
parents: 11597
diff changeset
    37
    route_name='cwentities',
2dc0b0db2329 use CubicWeb request to execute RQL
Rabah Meradi <rabah.meradi@logilab.fr>
parents: 11597
diff changeset
    38
    context=EntityResource,
2dc0b0db2329 use CubicWeb request to execute RQL
Rabah Meradi <rabah.meradi@logilab.fr>
parents: 11597
diff changeset
    39
    request_method='GET',
2dc0b0db2329 use CubicWeb request to execute RQL
Rabah Meradi <rabah.meradi@logilab.fr>
parents: 11597
diff changeset
    40
    request_param=('test_rql_execute',)
2dc0b0db2329 use CubicWeb request to execute RQL
Rabah Meradi <rabah.meradi@logilab.fr>
parents: 11597
diff changeset
    41
)
11603
c7bfeb7b0d67 [tox] Use py.test
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11598
diff changeset
    42
def rql_execute_view(context, request):
11598
2dc0b0db2329 use CubicWeb request to execute RQL
Rabah Meradi <rabah.meradi@logilab.fr>
parents: 11597
diff changeset
    43
    """Return 500 response if rset.req is not a CubicWeb request.
2dc0b0db2329 use CubicWeb request to execute RQL
Rabah Meradi <rabah.meradi@logilab.fr>
parents: 11597
diff changeset
    44
    """
2dc0b0db2329 use CubicWeb request to execute RQL
Rabah Meradi <rabah.meradi@logilab.fr>
parents: 11597
diff changeset
    45
    if isinstance(context.rset.req, CubicWebPyramidRequest):
2dc0b0db2329 use CubicWeb request to execute RQL
Rabah Meradi <rabah.meradi@logilab.fr>
parents: 11597
diff changeset
    46
        request.response.status_int = 204
2dc0b0db2329 use CubicWeb request to execute RQL
Rabah Meradi <rabah.meradi@logilab.fr>
parents: 11597
diff changeset
    47
    else:
2dc0b0db2329 use CubicWeb request to execute RQL
Rabah Meradi <rabah.meradi@logilab.fr>
parents: 11597
diff changeset
    48
        request.response.status_int = 500
2dc0b0db2329 use CubicWeb request to execute RQL
Rabah Meradi <rabah.meradi@logilab.fr>
parents: 11597
diff changeset
    49
        request.response.text = 'rset.req is not a CubicWeb request'
2dc0b0db2329 use CubicWeb request to execute RQL
Rabah Meradi <rabah.meradi@logilab.fr>
parents: 11597
diff changeset
    50
    return request.response
2dc0b0db2329 use CubicWeb request to execute RQL
Rabah Meradi <rabah.meradi@logilab.fr>
parents: 11597
diff changeset
    51
2dc0b0db2329 use CubicWeb request to execute RQL
Rabah Meradi <rabah.meradi@logilab.fr>
parents: 11597
diff changeset
    52
2dc0b0db2329 use CubicWeb request to execute RQL
Rabah Meradi <rabah.meradi@logilab.fr>
parents: 11597
diff changeset
    53
def includeme(config):
2dc0b0db2329 use CubicWeb request to execute RQL
Rabah Meradi <rabah.meradi@logilab.fr>
parents: 11597
diff changeset
    54
    config.scan(__name__)
11611
9d2bb6bdb5c8 [tests] add a __main__ handler
David Douard <david.douard@logilab.fr>
parents: 11603
diff changeset
    55
9d2bb6bdb5c8 [tests] add a __main__ handler
David Douard <david.douard@logilab.fr>
parents: 11603
diff changeset
    56
9d2bb6bdb5c8 [tests] add a __main__ handler
David Douard <david.douard@logilab.fr>
parents: 11603
diff changeset
    57
if __name__ == '__main__':
9d2bb6bdb5c8 [tests] add a __main__ handler
David Douard <david.douard@logilab.fr>
parents: 11603
diff changeset
    58
    from unittest import main
9d2bb6bdb5c8 [tests] add a __main__ handler
David Douard <david.douard@logilab.fr>
parents: 11603
diff changeset
    59
    main()