Skip to content

Commit

Permalink
Fix wap webservice (#1987)
Browse files Browse the repository at this point in the history
* reduce log noise - fixes #1985
* reactivate actually working rlink tests
* add WAP webservice tests
* fix WAP webservice running in proxy workers
  • Loading branch information
oberstet committed Apr 7, 2022
1 parent 0bb0c02 commit ca8d383
Show file tree
Hide file tree
Showing 12 changed files with 247 additions and 71 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ test_fixme:
test_cb_proxy:
pytest -sv --no-install test/functests/cbtests/test_cb_proxy.py

test_wap:
trial crossbar.webservice.test

docs:
cd docs && sphinx-build -b html . _build

Expand Down
2 changes: 1 addition & 1 deletion crossbar/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
#
#####################################################################################

__version__ = '22.4.1.dev3'
__version__ = '22.4.1.dev4'
__build__ = '19000101-0000000'
21 changes: 13 additions & 8 deletions crossbar/master/cluster/webcluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from autobahn.wamp.types import CallDetails, PublishOptions, RegisterOptions

from crossbar.common import checkconfig
from crossbar.webservice import archive
from crossbar.webservice import archive, wap
from crossbar._util import hl, hlid, hltype, hlval, get_free_tcp_port
from cfxdb.mrealm import WebCluster, WebClusterNodeMembership, WebService
from cfxdb.mrealm import cluster
Expand Down Expand Up @@ -414,21 +414,26 @@ class WebClusterManager(object):
# publication options for management API events
_PUBOPTS = PublishOptions(acknowledge=True)

# map of allowed web services
# map of allowed web services, see also crossbar.personality.Personality.WEB_SERVICE_CHECKERS
_WEB_SERVICE_CHECKERS = {
# none
'path': checkconfig.check_web_path_service_path,
'static': checkconfig.check_web_path_service_static,
'archive': archive.RouterWebServiceArchive.check,
'json': checkconfig.check_web_path_service_json,
'nodeinfo': checkconfig.check_web_path_service_nodeinfo,
'redirect': checkconfig.check_web_path_service_redirect,
# resource
'reverseproxy': checkconfig.check_web_path_service_reverseproxy,
'websocket': checkconfig.check_web_path_service_websocket,
'websocket-reverseproxy': checkconfig.check_web_path_service_websocket_reverseproxy,
'nodeinfo': checkconfig.check_web_path_service_nodeinfo,
'json': checkconfig.check_web_path_service_json,
'cgi': checkconfig.check_web_path_service_cgi,
'wsgi': checkconfig.check_web_path_service_wsgi,
'static': checkconfig.check_web_path_service_static,
'websocket': checkconfig.check_web_path_service_websocket,
'websocket-reverseproxy': checkconfig.check_web_path_service_websocket_reverseproxy,
# longpoll
'caller': checkconfig.check_web_path_service_caller,
'publisher': checkconfig.check_web_path_service_publisher,
# webhook
'archive': archive.RouterWebServiceArchive.check,
'wap': wap.RouterWebServiceWap.check,
}

def __init__(self, session, globaldb, globalschema, db, schema, reactor=None):
Expand Down
2 changes: 1 addition & 1 deletion crossbar/router/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ def onAuthenticate_error(err):

else:
msg = "{} message received while session is not yet joined".format(str(msg.__class__.__name__).upper())
self.log.warn('{func} {msg}', func=hltype(self.onMessage), msg=msg)
self.log.debug('{func} {msg}', func=hltype(self.onMessage), msg=msg)
# raise ProtocolError(msg)

else:
Expand Down
2 changes: 2 additions & 0 deletions crossbar/webservice/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
from crossbar.worker.proxy import ProxyController
from crossbar.worker.router import RouterController

__all__ = ('RouterWebServiceRestPublisher', 'RouterWebServiceRestCaller', 'RouterWebServiceWebhook')


@inlineCallbacks
def _create_resource(resource_klass: Union[PublisherResource, CallerResource, WebhookResource], worker,
Expand Down
6 changes: 6 additions & 0 deletions crossbar/webservice/test/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#####################################################################################
#
# Copyright (c) Crossbar.io Technologies GmbH
# SPDX-License-Identifier: EUPL-1.2
#
#####################################################################################
10 changes: 10 additions & 0 deletions crossbar/webservice/test/templates/greeting.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html>
<title>Greeting</title>
<body>
<h1>Greeting</H1>
<p>
Hi {{ name }}! This is your personalized greeting message:
</p>
<strong>{{ message }}</strong>
</body>
</html>
10 changes: 10 additions & 0 deletions crossbar/webservice/test/templates/product_report.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html>
<title>Report</title>
<body>
<h1>Product Report</H1>
<div>This is a product report for product id: <b>{{product_id}}</b></div>
<div>Report is: <b>{{report}}</b></div>
<div>For month <b>{{month}}</b> year <b>{{year}}</b>
<div>Flasky: <b>{{flasky}}</b></div>
</body>
</html>
80 changes: 80 additions & 0 deletions crossbar/webservice/test/test_wap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#####################################################################################
#
# Copyright (c) Crossbar.io Technologies GmbH
# SPDX-License-Identifier: EUPL-1.2
#
#####################################################################################

import os
from twisted.trial.unittest import TestCase

from werkzeug.routing import Map, Rule
from jinja2 import Environment, FileSystemLoader
from jinja2.environment import Template

from crossbar.webservice.wap import WapResource


class WapTestCase(TestCase):
"""
Tests for :class:`crossbar.webservice.wap.WapResource`.
"""

_WAP1 = {
"type":
"wap",
"templates":
"../templates",
"sandbox":
True,
"routes": [{
"path": "/greeting/<name>",
"method": "GET",
"call": "com.example.greeting",
"render": "greeting.html"
}, {
"path": "/product/<int:product_id>/<report>/<int:year>/<int:month>",
"method": "GET",
"call": "com.example.get_product_report",
"render": "product_report.html"
}],
"wamp": {
"realm": "realm1",
"authrole": "anonymous"
}
}

def setUp(self):
self._templates_dir = os.path.join(os.path.dirname(__file__), 'templates')
self._jinja_env = Environment(loader=FileSystemLoader(self._templates_dir), autoescape=True)

def test_map_adapter(self):
# https://werkzeug.palletsprojects.com/en/2.1.x/routing/#werkzeug.routing.MapAdapter.match
test_map = Map()
url = '/reports/product/<int:product_id>/<report>/<int:year>/<int:month>'
endpoint = 'endpoint1'
rule = Rule(url, methods=['GET'], endpoint=endpoint)
test_map.add(rule)
test_adapter = test_map.bind('localhost', '/')

test_url = '/reports/product/123/total/2016/12'
test_data = {'product_id': 123, 'report': 'total', 'year': 2016, 'month': 12}

_endpoint, _kwargs = test_adapter.match(test_url, method='GET', query_args={})

self.assertEqual(_endpoint, endpoint)
self.assertEqual(_kwargs, test_data)

def test_map_adapter_factory(self):

map_adapter = WapResource._create_map_adapter(self._jinja_env, self._WAP1, 'localhost', 'reports')

test_url = '/reports/product/123/total/2016/12'
test_data = {'product_id': 123, 'report': 'total', 'year': 2016, 'month': 12}
_endpoint, _kwargs = map_adapter.match(test_url, method='GET', query_args={})

# ('com.example.get_product_report', <Template 'product_report.html'>) != 'localhost'

self.assertEqual(_endpoint[0], 'com.example.get_product_report')
self.assertIsInstance(_endpoint[1], Template)
self.assertEqual(_kwargs, test_data)
Loading

0 comments on commit ca8d383

Please sign in to comment.