forked from abhishek-ram/pyas2-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add message_id parameter (abhishek-ram#63)
* Add message_id parameter * Validate length of message id when user provided * Formatting
- Loading branch information
1 parent
b9dc0a2
commit af63a9b
Showing
3 changed files
with
50 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
* Abhishek Ram <[email protected]> @abhishek-ram | ||
* Chad Gates @chadgates | ||
* Bruno Ribeiro da Silva <[email protected]> @loop0 | ||
* Robin C Samuel @robincsamuel | ||
- Abhishek Ram <[email protected]> @abhishek-ram | ||
- Chad Gates @chadgates | ||
- Bruno Ribeiro da Silva <[email protected]> @loop0 | ||
- Robin C Samuel @robincsamuel | ||
- Brandon Joyce @brandonjoyce |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -330,6 +330,7 @@ def build( | |
content_type="application/edi-consent", | ||
additional_headers=None, | ||
disposition_notification_to="[email protected]", | ||
message_id=None, | ||
): | ||
|
||
"""Function builds the AS2 message. Compresses, signs and encrypts | ||
|
@@ -354,6 +355,10 @@ def build( | |
:param disposition_notification_to: | ||
Email address for disposition-notification-to header entry. | ||
(default "[email protected]") | ||
:param message_id: | ||
A value to be used for the left side of the message id. If not provided a | ||
unique id is generated. (default None) | ||
""" | ||
|
||
# Validations | ||
|
@@ -372,10 +377,22 @@ def build( | |
"Encryption of messages is enabled but encrypt key is not set for the receiver." | ||
) | ||
|
||
# Generate message id using UUID 1 as it uses both hostname and time | ||
self.message_id = ( | ||
email_utils.make_msgid(domain=self.sender.domain).lstrip("<").rstrip(">") | ||
) | ||
if message_id: | ||
self.message_id = f"{message_id}@{self.sender.domain}" | ||
else: | ||
self.message_id = ( | ||
email_utils.make_msgid(domain=self.sender.domain) | ||
.lstrip("<") | ||
.rstrip(">") | ||
) | ||
|
||
# ensure the total length of the message id is no more than 255 characters | ||
if len(self.message_id) > 255: | ||
raise ValueError( | ||
"Message ID must be no more than 255 characters for " | ||
"compatibility with some AS2 servers. " | ||
f"Current message ID length is {len(self.message_id)}." | ||
) | ||
|
||
# Set up the message headers | ||
as2_headers = { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
"""Module for testing the basic features of pyas2.""" | ||
import pytest | ||
import socket | ||
from pyas2lib import as2 | ||
from . import Pyas2TestCase | ||
|
@@ -200,6 +201,29 @@ def test_plain_message_without_domain(self): | |
out_message.build(self.test_data) | ||
self.assertEqual(out_message.message_id.split("@")[1], socket.getfqdn()) | ||
|
||
def test_plain_message_with_custom_message_id(self): | ||
"""Test Message building with a custom message id""" | ||
|
||
# Build an As2 message to be transmitted to partner | ||
self.org.domain = "example.com" | ||
out_message = as2.Message(self.org, self.partner) | ||
out_message.build(self.test_data, message_id="some_custom_id") | ||
self.assertEqual(out_message.message_id, "[email protected]") | ||
|
||
def test_invalid_message_id_length_raises_error(self): | ||
"""Test Message building with a custom message id that's invalid""" | ||
|
||
# Build an As2 message to be transmitted to partner | ||
self.org.domain = "example.com" | ||
out_message = as2.Message(self.org, self.partner) | ||
very_long_message_id = "a" * 1000 | ||
with pytest.raises(ValueError) as excinfo: | ||
out_message.build(self.test_data, message_id=very_long_message_id) | ||
assert ( | ||
"Message ID must be no more than 255 characters for compatibility" | ||
in str(excinfo.value) | ||
) | ||
|
||
def find_org(self, as2_id): | ||
return self.org | ||
|
||
|