def test_permission_more(self): p1 = Permission({ "target": 'reassignment-change-finished', "allocation": '*', "driver": '*', "truck": '*', }) p2 = Permission({ "target": 'reassignment-change-finished', "allocation": 1, "driver": '*', "truck": '*', }) self.assertTrue(p2 < p1) self.assertTrue(p2 <= p1) self.assertFalse(p2 > p1) self.assertFalse(p2 >= p1) self.assertFalse(p2 == p1) self.assertFalse(p1 < p2) self.assertFalse(p1 <= p2) self.assertTrue(p1 >= p2) self.assertFalse(p1 == p2) self.assertFalse(p1 > p1) self.assertFalse(p1 < p1)
def test_star_permission(self): p1 = Permission({"target": "message", "customer": "1"}) p2 = Permission({"target": "message", "customer": "*"}) self.assertTrue(p1 < p2) self.assertTrue(p1 <= p2) self.assertFalse(p1 == p2) self.assertFalse(p1 >= p2) self.assertFalse(p1 > p2)
def test_permission_not_orderable_same_keys(self): p1 = Permission({"target": "message", "customer": "1"}) p2 = Permission({"target": "message", "customer": "2"}) self.assertFalse(p2 == p1) self.assertFalse(p2 > p1) self.assertFalse(p2 >= p1) self.assertFalse(p1 < p2) self.assertFalse(p1 <= p2) self.assertFalse(p1 > p2) self.assertFalse(p1 >= p2) self.assertFalse(p2 < p1) self.assertFalse(p2 <= p1)
async def get_authentication(self, websocket) -> Authentication: for key, value in self.header_definition.items(): try: value = value.get_value(websocket) except header.NoValue as e: self.app.logger.debug('Can\'t find header {} {} {}'.format( key, value, e)) else: self.headers[key] = value self.app.logger.debug(websocket.args) self.app.logger.debug(websocket.args.get('session_token')) response = await self.get('bootstrap/') if response.status != 200: raise NoBackendConnectionException() content = await response.json() self.app.logger.debug( "Binder: Proxy bootstrap. Response: {}".format(content)) try: return Authentication(allowed_rooms=list( map(lambda x: Permission(x), content.get('allowed_rooms', [])))) except (AttributeError, ValueError): self.app.logger.debug( "Binder: could not understand permissions {}".format( content.get('allowed_rooms'))) raise UnparsableBackendPermissionsException()
def test_permission_ordering(self): p1 = Permission({"target": "message", "customer": "1"}) p2 = Permission({"target": "message", "customer": "*"}) # p2 has strictly more permissions that p1, so it hold that p1 > p2 self.assertFalse(p1 == p2) self.assertTrue(p2 > p1) self.assertTrue(p2 >= p1) self.assertTrue(p1 < p2) self.assertTrue(p1 <= p2) self.assertFalse(p1 > p2) self.assertFalse(p1 >= p2) self.assertFalse(p2 < p1) self.assertFalse(p2 <= p1)
async def dispatch_message(self, message): self.hub_status.rabbitmq_messages_received += 1 try: self.app.logger.debug("Dispatch message: {}".format(message)) content = json.loads(message) message_permissions = list( map(lambda x: Permission(x), content['rooms'])) data = content['data'] # Send the message to all connections as a seperate event send_data_futures = [] async def send_message(subscription, data): connection = subscription.connection connection.app.hub.hub_status.ws_messages_send_queued += 1 try: await connection.send({ "type": "publish", "data": data, "requestId": subscription.request_id }) connection.app.hub.hub_status.ws_messages_send += 1 except Exception as e: connection.app.hub.hub_status.ws_messages_send_error += 1 connection.app.logger.warning( "Error when sending message: {}".format(e)) for permission in message_permissions: if permission.hash() in self.rooms: for subscription in self.rooms[ permission.hash()].subscriptions: try: send_data_futures.append( send_message(subscription, data)) except KeyError: # Happens if the connection with connection_id has been disconnected. pass await asyncio.gather(*send_data_futures) except Exception as e: """ Make sure that all errors are caught, such that we do not crash the whole thread """ self.app.logger.error(e) return
async def subscribe(connection, message): from high_templar.hub import NoPermissionException permission = Permission(message['room']) if 'requestId' not in message: return await connection.send({ "code": "error", "message": "no-request-id" }) try: connection.app.hub.subscribe( Subscription(connection=connection, permission=permission, request_id=message['requestId'])) except NoPermissionException: return await connection.send({ "code": "error", "message": "room-not-found" }) await connection.send({ 'code': 'success', })
def test_permission_equal(self): p1 = Permission({"target": "message", "customer": "1"}) p2 = Permission({"target": "message", "customer": "1"}) self.assertEqual(p1, p2)