Skip to content

Commit

Permalink
Merge pull request #635 from plone/fix-email-notification-errors
Browse files Browse the repository at this point in the history
Standardize errors data structure of email-notification endpoint
  • Loading branch information
jensens committed Nov 14, 2018
2 parents 3fbcdde + 53d0d67 commit 0f87ed2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
5 changes: 4 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ Changelog
3.5.1 (unreleased)
------------------

- Nothing changed yet.
Bugfixes:

- Standardize errors data structure of email-notification endpoint.
[cekk]


3.5.0 (2018-11-06)
Expand Down
21 changes: 8 additions & 13 deletions src/plone/restapi/services/email_notification/post.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# -*- coding: utf-8 -*-
from plone.restapi.deserializer import json_body
from plone.restapi.services import Service
from zExceptions import BadRequest
from zope.component import getMultiAdapter
from plone.restapi.deserializer import json_body
from zope.interface import alsoProvides

import plone
Expand All @@ -18,18 +19,12 @@ def reply(self):
subject = data.get('subject', '')

if not sender_from_address or not message:
self.request.response.setStatus(400)
return dict(error=dict(
type='BadRequest',
message='Missing from or message parameters'))
raise BadRequest('Missing from or message parameters')

overview_controlpanel = getMultiAdapter((self.context, self.request),
name='overview-controlpanel')
if overview_controlpanel.mailhost_warning():
self.request.response.setStatus(400)
return dict(error=dict(
type='BadRequest',
message='MailHost is not configured.'))
raise BadRequest('MailHost is not configured.')

# Disable CSRF protection
if 'IDisableCSRFProtection' in dir(plone.protect.interfaces):
Expand All @@ -41,10 +36,10 @@ def reply(self):

contact_info_view.send_message(
dict(
message=message,
subject=subject,
sender_from_address=sender_from_address,
sender_fullname=sender_fullname
message=message,
subject=subject,
sender_from_address=sender_from_address,
sender_fullname=sender_fullname
)
)

Expand Down
15 changes: 14 additions & 1 deletion src/plone/restapi/tests/test_services_email_notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
PLONE5 = True


@unittest.skipIf(not PLONE5, 'email notification not implemented for Plone < 5.') # noqa
@unittest.skipIf(not PLONE5, 'email notification not implemented for Plone < 5.') # noqa
class EmailNotificationEndpoint(unittest.TestCase):

layer = PLONE_RESTAPI_DX_FUNCTIONAL_TESTING
Expand All @@ -46,6 +46,19 @@ def setUp(self):

transaction.commit()

def test_email_notification_missing_parameters(self):
response = self.api_session.post(
'/@email-notification',
json={
'message': 'Just want to say hi.'
})
transaction.commit()
self.assertEqual(response.status_code, 400)
self.assertEqual(self.mailhost.messages, [])
error = response.json()
self.assertEqual(error['message'],
'Missing from or message parameters')

def test_email_notification(self):
response = self.api_session.post(
'/@email-notification',
Expand Down

0 comments on commit 0f87ed2

Please sign in to comment.