def test_run_post_receive_triggers_signal(self, message): callback = Mock() full_filename = "dummy_full_name" post_receive.connect(callback) run_post_receive(message, full_filename) post_receive.disconnect(callback) callback.assert_called_once_with(message=message, full_filename=full_filename, sender=Message, signal=post_receive)
def post(self, request, *args, **kwargs): # extract the headers from the http request as2headers = "" for key in request.META: if key.startswith("HTTP") or key.startswith("CONTENT"): as2headers += ( f'{key.replace("HTTP_", "").replace("_", "-").lower()}: ' f"{request.META[key]}\n") # build the body along with the headers request_body = as2headers.encode() + b"\r\n" + request.body logger.debug( f'Received an HTTP POST from {request.META["REMOTE_ADDR"]} ' f"with payload :\n{request_body}") # First try to see if this is an MDN logger.debug("Check to see if payload is an Asynchronous MDN.") as2mdn = As2Mdn() # Parse the mdn and get the message status status, detailed_status = as2mdn.parse(request_body, self.find_message) if not detailed_status == "mdn-not-found": message = Message.objects.get(message_id=as2mdn.orig_message_id, direction="OUT") logger.info( f"Asynchronous MDN received for AS2 message {as2mdn.message_id} to organization " f"{message.organization.as2_name} from partner {message.partner.as2_name}" ) # Update the message status and return the response if status == "processed": message.status = "S" run_post_send(message) else: message.status = "E" message.detailed_status = ( f"Partner failed to process message: {detailed_status}") # Save the message and create the mdn message.save() Mdn.objects.create_from_as2mdn(as2mdn=as2mdn, message=message, status="R") return HttpResponse(_("AS2 ASYNC MDN has been received")) else: logger.debug("Payload is not an MDN parse it as an AS2 Message") as2message = As2Message() status, exception, as2mdn = as2message.parse( request_body, self.find_organization, self.find_partner, self.check_message_exists, ) logger.info( f'Received an AS2 message with id {as2message.headers.get("message-id")} for ' f'organization {as2message.headers.get("as2-to")} from ' f'partner {as2message.headers.get("as2-from")}.') # In case of duplicates update message id if isinstance(exception[0], DuplicateDocument): as2message.message_id += "_duplicate" # Create the Message and MDN objects message, full_fn = Message.objects.create_from_as2message( as2message=as2message, filename=as2message.payload.get_filename(), payload=as2message.content, direction="IN", status="S" if status == "processed" else "E", detailed_status=exception[1], ) # run post receive command on success if status == "processed": run_post_receive(message, full_fn) # Return the mdn in case of sync else return text message if as2mdn and as2mdn.mdn_mode == "SYNC": message.mdn = Mdn.objects.create_from_as2mdn(as2mdn=as2mdn, message=message, status="S") response = HttpResponse(as2mdn.content) for key, value in as2mdn.headers.items(): response[key] = value return response elif as2mdn and as2mdn.mdn_mode == "ASYNC": Mdn.objects.create_from_as2mdn( as2mdn=as2mdn, message=message, status="P", return_url=as2mdn.mdn_url, ) return HttpResponse(_("AS2 message has been received"))
def post(self, request, *args, **kwargs): # extract the headers from the http request as2headers = '' for key in request.META: if key.startswith('HTTP') or key.startswith('CONTENT'): as2headers += f'{key.replace("HTTP_", "").replace("_", "-").lower()}: ' \ f'{request.META[key]}\n' # build the body along with the headers request_body = as2headers.encode() + b'\r\n' + request.body logger.debug( f'Received an HTTP POST from {request.META["REMOTE_ADDR"]} ' f'with payload :\n{request_body}' ) # First try to see if this is an MDN logger.debug('Check to see if payload is an Asynchronous MDN.') as2mdn = As2Mdn() # Parse the mdn and get the message status status, detailed_status = as2mdn.parse(request_body, self.find_message) if not detailed_status == 'mdn-not-found': message = Message.objects.get(message_id=as2mdn.orig_message_id, direction='OUT') logger.info( f'Asynchronous MDN received for AS2 message {as2mdn.message_id} to organization ' f'{message.organization.as2_name} from partner {message.partner.as2_name}') # Update the message status and return the response if status == 'processed': message.status = 'S' run_post_send(message) else: message.status = 'E' message.detailed_status = f'Partner failed to process message: {detailed_status}' run_post_failure(message) # Save the message and create the mdn message.save() Mdn.objects.create_from_as2mdn(as2mdn=as2mdn, message=message, status='R') return HttpResponse(_('AS2 ASYNC MDN has been received')) else: logger.debug('Payload is not an MDN parse it as an AS2 Message') as2message = As2Message() status, exception, as2mdn = as2message.parse( request_body, self.find_organization, self.find_partner, self.check_message_exists) logger.info( f'Received an AS2 message with id {as2message.headers.get("message-id")} for ' f'organization {as2message.headers.get("as2-to")} from ' f'partner {as2message.headers.get("as2-from")}.' ) # In case of duplicates update message id if isinstance(exception[0], DuplicateDocument): as2message.message_id += '_duplicate' # Create the Message and MDN objects message, full_fn = Message.objects.create_from_as2message( as2message=as2message, filename=as2message.payload.get_filename(), payload=as2message.content, direction='IN', status='S' if status == 'processed' else 'E', detailed_status=exception[1] ) # run post receive command on success if status == 'processed': run_post_receive(message, full_fn) # Return the mdn in case of sync else return text message if as2mdn and as2mdn.mdn_mode == 'SYNC': message.mdn = Mdn.objects.create_from_as2mdn( as2mdn=as2mdn, message=message, status='S') response = HttpResponse(as2mdn.content) for key, value in as2mdn.headers.items(): response[key] = value return response elif as2mdn and as2mdn.mdn_mode == 'ASYNC': Mdn.objects.create_from_as2mdn( as2mdn=as2mdn, message=message, status='P', return_url=as2mdn.mdn_url) return HttpResponse(_('AS2 message has been received'))