Skip to content

Commit

Permalink
GET/PUT registry_: Replace mock with a basic actual implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasgraf committed May 15, 2016
1 parent bc3da24 commit f9dfe58
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 6 deletions.
4 changes: 2 additions & 2 deletions docs/source/_json/registry_get.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
GET /plone/registry_/plone.app.event.first_weekday
GET /plone/registry_/plone.app.querystring.field.path.title
Accept: application/json

HTTP 200 OK
content-type: application/json

"value"
"Location"
2 changes: 1 addition & 1 deletion docs/source/_json/registry_update.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PUT /plone/registry_/
Accept: application/json

{"key": "value"}
{"plone.app.querystring.field.path.title": "Value"}

HTTP 204 No Content

15 changes: 14 additions & 1 deletion src/plone/restapi/services/registry/get.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# -*- coding: utf-8 -*-
from plone.registry.interfaces import IRegistry
from plone.rest import Service
from zope.component import getUtility
from zope.interface import implements
from zope.publisher.interfaces import IPublishTraverse

Expand All @@ -17,5 +19,16 @@ def publishTraverse(self, request, name):
self.params.append(name)
return self

@property
def _get_record_name(self):
if len(self.params) != 1:
raise Exception(
"Must supply exactly one parameter (dotted name of"
"the record to be retrieved)")

return self.params[0]

def render(self):
return 'value'
registry = getUtility(IRegistry)
value = registry[self._get_record_name]
return value
13 changes: 13 additions & 0 deletions src/plone/restapi/services/registry/update.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
# -*- coding: utf-8 -*-
from plone.registry.interfaces import IRegistry
from plone.rest import Service
from zope.component import getUtility

import json


class RegistryUpdate(Service):

def render(self):
records_to_update = json.loads(self.request.get('BODY', '{}'))
registry = getUtility(IRegistry)

for key, value in records_to_update.items():
if key not in registry:
raise Exception(
"This endpoint is only intended to update existing "
"records! Couldn't find key %r" % key)
registry[key] = value
self.request.response.setStatus(204)
return None
5 changes: 3 additions & 2 deletions src/plone/restapi/tests/test_documentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,12 +282,13 @@ def test_documentation_search(self):

def test_documentation_registry_get(self):
response = self.api_session.get(
'/registry_/plone.app.event.first_weekday')
'/registry_/plone.app.querystring.field.path.title')
save_response_for_documentation('registry_get.json', response)

def test_documentation_registry_update(self):
response = self.api_session.put(
'/registry_/', json={'key': 'value'})
'/registry_/',
json={'plone.app.querystring.field.path.title': 'Value'})
save_response_for_documentation('registry_update.json', response)

def test_documentation_theme(self):
Expand Down
51 changes: 51 additions & 0 deletions src/plone/restapi/tests/test_registry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# -*- coding: utf-8 -*-
from plone.app.testing import setRoles
from plone.app.testing import SITE_OWNER_NAME
from plone.app.testing import SITE_OWNER_PASSWORD
from plone.app.testing import TEST_USER_ID
from plone.registry import field
from plone.registry.interfaces import IRegistry
from plone.registry.record import Record
from plone.restapi.testing import PLONE_RESTAPI_DX_FUNCTIONAL_TESTING
from plone.restapi.testing import RelativeSession
from zope.component import getUtility

import transaction
import unittest


class TestRegistry(unittest.TestCase):

layer = PLONE_RESTAPI_DX_FUNCTIONAL_TESTING

def setUp(self):
self.app = self.layer['app']
self.portal = self.layer['portal']
self.portal_url = self.portal.absolute_url()
setRoles(self.portal, TEST_USER_ID, ['Manager'])

self.api_session = RelativeSession(self.portal_url)
self.api_session.headers.update({'Accept': 'application/json'})
self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD)

registry = getUtility(IRegistry)
record = Record(field.TextLine(title=u"Foo Bar"), u"Lorem Ipsum")
registry.records['foo.bar'] = record
transaction.commit()

def test_get_registry_record(self):

transaction.commit()

response = self.api_session.get('/registry_/foo.bar')

self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), 'Lorem Ipsum')

def test_update_registry_record(self):
registry = getUtility(IRegistry)
payload = {'foo.bar': 'Lorem Ipsum'}
response = self.api_session.put('/registry_', json=payload)

self.assertEqual(response.status_code, 204)
self.assertEqual(registry['foo.bar'], 'Lorem Ipsum')

0 comments on commit f9dfe58

Please sign in to comment.