Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
100 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Event subscribers.""" | ||
from collective.behavior.amp.logger import logger | ||
from plone import api | ||
from urlparse import urlparse | ||
|
||
import requests | ||
|
||
|
||
def validate_amp(obj, event): | ||
"""Validate @@amp view using Cloudflare's AMP linter API endpoint.""" | ||
request = obj.REQUEST | ||
|
||
if event.status['review_state'] not in ('published', ): | ||
return | ||
|
||
# remove the scheme from the URL | ||
url = ''.join(urlparse(obj.absolute_url())[1:]) | ||
|
||
r = requests.get('https://amp.cloudflare.com/q/' + url + '/@@amp') | ||
validation = r.json() | ||
|
||
if validation['valid']: | ||
msg = u'Valid AMP page' | ||
logger.info(msg) | ||
api.portal.show_message(message=msg, request=request, type='info') | ||
else: | ||
msg = u'Not a valid AMP page' | ||
logger.warn(msg) | ||
api.portal.show_message(message=msg, request=request, type='warn') | ||
|
||
# TODO: include information about the errors |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# -*- coding: utf-8 -*- | ||
from collective.behavior.amp.testing import INTEGRATION_TESTING | ||
from plone import api | ||
from Products.statusmessages.interfaces import IStatusMessage | ||
|
||
import requests_mock | ||
import unittest | ||
|
||
|
||
class AMPValidationTestCase(unittest.TestCase): | ||
|
||
layer = INTEGRATION_TESTING | ||
|
||
def setUp(self): | ||
self.portal = self.layer['portal'] | ||
self.request = self.layer['request'] | ||
|
||
with api.env.adopt_roles(['Manager']): | ||
self.obj = api.content.create(self.portal, 'News Item', 'foo') | ||
|
||
@requests_mock.mock() | ||
def test_validate_amp_valid(self, m): | ||
RESPONSE_VALID = """{"source":"http://nohost/plone/foo/@@amp","valid":true}""" | ||
m.get('https://amp.cloudflare.com/q/nohost/plone/foo/@@amp', text=RESPONSE_VALID) | ||
|
||
with api.env.adopt_roles(['Manager']): | ||
api.content.transition(self.obj, 'publish') | ||
|
||
messages = IStatusMessage(self.request).show() | ||
self.assertEqual(len(messages), 1) | ||
self.assertEqual(messages[0].message, u'Valid AMP page') | ||
self.assertEqual(messages[0].type, u'info') | ||
|
||
@requests_mock.mock() | ||
def test_validate_amp_invalid(self, m): | ||
RESPONSE_INVALID = """{"source":"http://nohost/plone/foo/@@amp","valid":false,"errors":[{"line":169,"col":0,"code":"GENERAL_DISALLOWED_TAG","error":"The tag 'script' is disallowed except in specific forms."}]}""" | ||
m.get('https://amp.cloudflare.com/q/nohost/plone/foo/@@amp', text=RESPONSE_INVALID) | ||
|
||
with api.env.adopt_roles(['Manager']): | ||
api.content.transition(self.obj, 'publish') | ||
|
||
messages = IStatusMessage(self.request).show() | ||
self.assertEqual(len(messages), 1) | ||
self.assertEqual(messages[0].message, u'Not a valid AMP page') | ||
self.assertEqual(messages[0].type, u'warn') | ||
|
||
@unittest.expectedFailure # FIXME | ||
def test_validate_amp_with_package_uninstalled(self): | ||
from collective.behavior.amp.config import PROJECTNAME | ||
qi = self.portal['portal_quickinstaller'] | ||
qi.uninstallProducts(products=[PROJECTNAME]) | ||
|
||
# don't fail if package uninstalled | ||
with api.env.adopt_roles(['Manager']): | ||
api.content.transition(self.obj, 'publish') |