def call(self, rpcmethod, *params, **kwparams): """ Call the web rpc method. According to the json-rpc specification one can have either argument based parameters or keyword arguments. :param method:string: The method to call on volttron central. :param params:list: :param kwparams: :return: """ print(params) print(kwparams) if params and kwparams: raise ValueError('jsonrpc requires either args or kwargs not both!') if params: data = json_method(str(uuid.uuid4()), rpcmethod, params, None) else: data = json_method(str(uuid.uuid4()), rpcmethod, None, kwparams) if rpcmethod != 'get_authorization': data['authorization'] = self._auth_token resp = requests.post(self._url, json=data) if resp.ok: d = resp.json() if 'result' in d: return d['result'] return d['error'] return resp
def call(self, rpcmethod, *params, **kwparams): """ Call the web rpc method. According to the json-rpc specification one can have either argument based parameters or keyword arguments. :param method:string: The method to call on volttron central. :param params:list: :param kwparams: :return: """ print(params) print(kwparams) if params and kwparams: raise ValueError( 'jsonrpc requires either args or kwargs not both!') if params: data = json_method(str(uuid.uuid4()), rpcmethod, params, None) else: data = json_method(str(uuid.uuid4()), rpcmethod, None, kwparams) if rpcmethod != 'get_authorization': data['authorization'] = self._auth_token resp = requests.post(self._url, json=data) if resp.ok: d = resp.json() if 'result' in d: return d['result'] return d['error'] return resp
def test_jsonrpc_get_authorization(mock_response, mock_vc, mock_jsonrpc_env, monkeypatch): mock_claims = {"groups": ["test_admin"]} mock_vc.vip.web.configure_mock( **{"get_user_claims.return_value": mock_claims}) data = jsonrpc.json_method("12345", "get_authorization", { "username": "******", "password": "******" }, None) assert len(mock_vc._authenticated_sessions._sessions) == 0 mock_vc.jsonrpc(mock_jsonrpc_env, data) assert len(mock_vc._authenticated_sessions._sessions) == 1 data = jsonrpc.json_method("12345", "get_authorization", { "username": "******", "password": "******" }, None) response = mock_vc.jsonrpc(mock_jsonrpc_env, data) assert response['error'][ 'message'] == "Invalid username/password specified."
def mock_vc_jsonrpc(mock_response, mock_vc, mock_jsonrpc_env, monkeypatch): mock_claims = {"groups": ["test_admin"]} mock_vc.vip.web.configure_mock( **{"get_user_claims.return_value": mock_claims}) # mock_vc.vip.web.configure_mock(**{"register_websocket.return_value": VolttronWebSocket}) data = jsonrpc.json_method("12345", "get_authorization", { "username": "******", "password": "******" }, None) mock_vc.jsonrpc(mock_jsonrpc_env, data) #mock_vc_env = {"mock_vc": mock_vc, "mock_env": mock_jsonrpc_env} yield mock_vc
def _distribute(self, peer, topic, headers, message=None, bus=''): #self._check_if_protected_topic(topic) subscriptions = self._peer_subscriptions[bus] subscribers = set() for prefix, subscription in subscriptions.items(): if subscription and topic.startswith(prefix): subscribers |= subscription if subscribers: sender = encode_peer(peer) json_msg = jsonapi.dumps(jsonrpc.json_method( None, 'pubsub.push', [sender, bus, topic, headers, message], None)) frames = [zmq.Frame(b''), zmq.Frame(b''), zmq.Frame(b'RPC'), zmq.Frame(json_msg)] socket = self.core.socket for subscriber in subscribers: socket.send(subscriber, flags=SNDMORE) socket.send_multipart(frames, copy=False) return len(subscribers)
def _distribute(self, peer, topic, headers, message=None, bus=''): #self._check_if_protected_topic(topic) subscriptions = self._peer_subscriptions[bus] subscribers = set() for prefix, subscription in subscriptions.iteritems(): if subscription and topic.startswith(prefix): subscribers |= subscription if subscribers: sender = encode_peer(peer) json_msg = jsonapi.dumps(jsonrpc.json_method( None, 'pubsub.push', [sender, bus, topic, headers, message], None)) frames = [zmq.Frame(b''), zmq.Frame(b''), zmq.Frame(b'RPC'), zmq.Frame(json_msg)] socket = self.core.socket for subscriber in subscribers: socket.send(subscriber, flags=SNDMORE) socket.send_multipart(frames, copy=False) return len(subscribers)
def test_jsonrpc_is_unauthorized(mock_vc_jsonrpc, mock_jsonrpc_env): data = jsonrpc.json_method("12345", "list_platforms", None, None) data['authorization'] = "really_bad_access_token" response = mock_vc_jsonrpc.jsonrpc(mock_jsonrpc_env, data) assert response['error']['message'] == "Invalid authentication token"
def test_jsonrpc_is_authorized(mock_vc_jsonrpc, mock_jsonrpc_env): data = jsonrpc.json_method("12345", "list_platforms", None, None) data[ 'authorization'] = '{"refresh_token": "super_secret_refresh_token", "access_token": "super_secret_access_token"}' response = mock_vc_jsonrpc.jsonrpc(mock_jsonrpc_env, data) assert len(response['result']) is 0 and type(response['result']) is list