def dispatch_request(self, *args, **kwargs): output = super(Resource, self).dispatch_request(*args, **kwargs) view_name = '%s.%s' % (self.__class__.__module__, self.__class__.__name__) if view_name in no_permissions or \ view_name in default_access and access_map[request.method.upper()] in default_access[view_name]: return output user_id = None if hasattr(request, 'user'): user_id = request.user.id audit = { 'user_id': user_id, 'ip': request.remote_addr, 'endpoint': request.path, 'headers': json.dumps([{ key: request.environ[key] } for key in request.environ if 'HTTP_' in key]), 'method': request.method, 'payload': json.dumps({ 'json': request.get_json(silent=True), 'query': request.args.to_dict(), 'form': request.form.to_dict(), 'all': request.get_data(as_text=True) }), 'response': {} } if not isinstance(output, Response): response = json.dumps(output) if len(response) <= MAX_MSG_LENGTH - 2400: audit['response'] = response else: audit['response'] = 'truncated original length = %s' % len( response) get_logger('app').info( 'Message response truncated as it was too large') # TODO: Identify if this is the best approach given that the # queue is not active on test unless running queue tests if not hasattr(configs, 'TESTING'): try: mem_queue.send_msg(json.dumps(audit)) except (ConnectionRefusedError, MaxMessageSizeExceededError, FileNotFoundError, timeout): get_logger('app').exception('message error') return output
def test_send_invalid_large_msg(queue_process): from core.mem_queue import send_msg from core.queue_worker import MessageNotQueuedError assert queue_process.name == 'mem_queue_worker' chars = 'A a B b C c D d E e F f G g H h I i J j K k L M m N n O o P p Q q R r S s T t U u V v W w X x Y y Z z' \ .split() msg = '' for i in range(1, 8200): msg += choice(chars) with pytest.raises(MessageNotQueuedError) as ex: send_msg(msg) assert 'maximum size' in str(ex.value)
def test_send_max_length_msg(queue_process): from core.mem_queue import send_msg assert queue_process.name == 'mem_queue_worker' chars = 'A a B b C c D d E e F f G g H h I i J j K k L M m N n O o P p Q q R r S s T t U u V v W w X x Y y Z z' \ .split() msg = '' for i in range(1, 8192): msg += choice(chars) assert send_msg(msg) == True
def test_send_multiple_msgs_to_queue(queue_process): from core.mem_queue import send_msg assert queue_process.name == 'mem_queue_worker' assert send_msg('this is message one') is True assert send_msg('this is message two') is True
def test_send_msg_to_queue(queue_process): from core.mem_queue import send_msg assert queue_process.name == 'mem_queue_worker' assert send_msg('hey bro') is True