Skip to content

Commit

Permalink
Merge pull request #544 from spack971/feature/wrap-service-resource
Browse files Browse the repository at this point in the history
Copy wrapped resource's attributes into the service
  • Loading branch information
leplatrem committed Sep 1, 2020
2 parents 46ebb49 + 6f40768 commit 4f56c9c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cornice/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
import warnings
import functools

import venusian

Expand Down Expand Up @@ -116,6 +117,9 @@ class User(object):
service_name = prefix + service_name
service = services[service_name] = Service(name=service_name,
depth=2, **service_args)
# ensure the service comes with the same properties as the wrapped
# resource
functools.update_wrapper(service, klass)

# initialize views
for verb in ('get', 'post', 'put', 'delete', 'options', 'patch'):
Expand Down
20 changes: 20 additions & 0 deletions tests/test_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
import json
import functools
from unittest import mock

from pyramid import testing
Expand Down Expand Up @@ -29,6 +30,7 @@ def my_collection_acl(request):
@resource(collection_path='/thing', path='/thing/{id}',
name='thing_service')
class Thing(object):
"""This is a thing."""

def __init__(self, request, context=None):
self.request = request
Expand All @@ -45,6 +47,7 @@ def collection_get(self):
@resource(collection_path='/users', path='/users/{id}',
name='user_service', factory=dummy_factory)
class User(object):
"""My user resource."""

def __init__(self, request, context=None):
self.request = request
Expand Down Expand Up @@ -219,6 +222,23 @@ def test_acl_support_authenticated_allowed_thing_get(self):
result = self.app.get('/thing', status=HTTPOk.code)
self.assertEqual("yay", result.json)

def test_service_wrapped_resource(self):
resources = {
'thing_service': Thing,
'user_service': User,
}

for name, service in (
(x.name, x) for x in self.config.registry.cornice_services.values()
if x.name in resources
):
for attr in functools.WRAPPER_ASSIGNMENTS:
with self.subTest(service=name, attribute=attr):
self.assertEqual(
getattr(resources[name], attr, None),
getattr(service, attr, None)
)


class NonAutocommittingConfigurationTestResource(TestCase):
"""
Expand Down

0 comments on commit 4f56c9c

Please sign in to comment.