Skip to content

Commit a1dfa7a

Browse files
committed
Merge branch 'feature/fix_build' into bugfix/46
2 parents ecd2c18 + 5326bbe commit a1dfa7a

File tree

14 files changed

+113
-67
lines changed

14 files changed

+113
-67
lines changed

example/urls.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
1. Import the include() function: from django.conf.urls import url, include
1414
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
1515
"""
16-
from django.conf.urls import url, include
16+
from django.urls import path, include
1717
from django.contrib import admin
1818

1919
urlpatterns = [
20-
url(r'^admin/', admin.site.urls),
21-
url(r'^pyas2/', include('pyas2.urls')),
20+
path("admin/", admin.site.urls),
21+
path("pyas2/", include('pyas2.urls')),
2222
]

pyas2/migrations/0001_initial.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,9 @@ class Migration(migrations.Migration):
367367
),
368368
),
369369
],
370-
options={"unique_together": {("message_id", "partner")},},
370+
options={
371+
"unique_together": {("message_id", "partner")},
372+
},
371373
),
372374
migrations.CreateModel(
373375
name="Mdn",

pyas2/models.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from django.core.files.storage import default_storage
1212
from django.db import models
1313
from django.utils import timezone
14-
from django.utils.translation import ugettext as _
14+
from django.utils.translation import gettext as _
1515

1616
from pyas2lib import (
1717
Mdn as As2Mdn,
@@ -531,10 +531,21 @@ class MdnManager(models.Manager):
531531
def create_from_as2mdn(self, as2mdn, message, status, return_url=None):
532532
"""Create the MDN from the pyas2lib's MDN object"""
533533
signed = True if as2mdn.digest_alg else False
534+
535+
# Check for message-id in MDN.
536+
if as2mdn.message_id is None:
537+
message_id = as2mdn.orig_message_id
538+
logger.warning(
539+
f"Received MDN response without a message-id. Using original "
540+
f"message-id as ID instead: {message_id}"
541+
)
542+
else:
543+
message_id = as2mdn.message_id
544+
534545
mdn, _ = self.update_or_create(
535546
message=message,
536547
defaults=dict(
537-
mdn_id=as2mdn.message_id,
548+
mdn_id=message_id,
538549
status=status,
539550
signed=signed,
540551
return_url=return_url,

pyas2/tests/test_advanced.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from django.test import Client, override_settings
66
from django.test import TestCase
77
from pyas2lib import Message as As2Message
8+
from pyas2lib import Mdn as As2Mdn
89

910
from pyas2 import settings
1011
from pyas2.models import Message
@@ -117,7 +118,7 @@ def test_post_send_command(self):
117118

118119
@mock.patch("requests.post")
119120
def test_post_send_command_async(self, mock_request):
120-
""" Test that the command after successful send gets executed with
121+
"""Test that the command after successful send gets executed with
121122
asynchronous MDN."""
122123

123124
partner = Partner.objects.create(
@@ -422,6 +423,31 @@ def test_signature_error(self):
422423
"Failed to verify message signature" in out_message.detailed_status
423424
)
424425

426+
def test_missing_message_id(self):
427+
# Create the client partner and send the command
428+
partner = Partner.objects.create(
429+
name="AS2 Server",
430+
as2_name="as2server",
431+
target_url="http://localhost:8080/pyas2/as2receive",
432+
signature="sha1",
433+
signature_cert=self.server_crt,
434+
encryption="tripledes_192_cbc",
435+
encryption_cert=self.server_crt,
436+
mdn=True,
437+
mdn_mode="ASYNC",
438+
mdn_sign="sha1",
439+
)
440+
out_message = self.build_and_send(partner)
441+
442+
# Create MDN object without message_id
443+
in_message = As2Mdn()
444+
in_message.orig_message_id = out_message.message_id
445+
in_message.message_id = None
446+
mdn_message = Mdn.objects.create_from_as2mdn(in_message, out_message, "R")
447+
448+
# Check that original message id was used to store mdn_id
449+
self.assertEqual(mdn_message.mdn_id, out_message.message_id)
450+
425451
@mock.patch("requests.post")
426452
def build_and_send(self, partner, mock_request, smudge=False):
427453
# Build and send the message to server

pyas2/tests/test_basic.py

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ def testEndpoint(self):
9494
self.assertEqual(response.status_code, 200)
9595

9696
def testNoEncryptMessageNoMdn(self):
97-
""" Test Permutation 1: Sender sends un-encrypted data and does
98-
NOT request a receipt. """
97+
"""Test Permutation 1: Sender sends un-encrypted data and does
98+
NOT request a receipt."""
9999

100100
# Create the partner with appropriate settings for this case
101101
partner = Partner.objects.create(
@@ -119,8 +119,8 @@ def testNoEncryptMessageNoMdn(self):
119119
)
120120

121121
def testNoEncryptMessageMdn(self):
122-
""" Test Permutation 2: Sender sends un-encrypted data and requests an
123-
unsigned receipt. """
122+
"""Test Permutation 2: Sender sends un-encrypted data and requests an
123+
unsigned receipt."""
124124

125125
# Create the partner with appropriate settings for this case
126126
partner = Partner.objects.create(
@@ -147,8 +147,8 @@ def testNoEncryptMessageMdn(self):
147147
)
148148

149149
def testNoEncryptMessageSignMdn(self):
150-
""" Test Permutation 3: Sender sends un-encrypted data and requests a
151-
signed receipt. """
150+
"""Test Permutation 3: Sender sends un-encrypted data and requests a
151+
signed receipt."""
152152

153153
# Create the partner with appropriate settings for this case
154154
partner = Partner.objects.create(
@@ -177,8 +177,8 @@ def testNoEncryptMessageSignMdn(self):
177177
)
178178

179179
def testEncryptMessageNoMdn(self):
180-
""" Test Permutation 4: Sender sends encrypted data and does NOT
181-
request a receipt. """
180+
"""Test Permutation 4: Sender sends encrypted data and does NOT
181+
request a receipt."""
182182

183183
# Create the partner with appropriate settings for this case
184184
partner = Partner.objects.create(
@@ -204,8 +204,8 @@ def testEncryptMessageNoMdn(self):
204204
)
205205

206206
def testEncryptMessageMdn(self):
207-
""" Test Permutation 5: Sender sends encrypted data and requests an
208-
unsigned receipt. """
207+
"""Test Permutation 5: Sender sends encrypted data and requests an
208+
unsigned receipt."""
209209

210210
partner = Partner.objects.create(
211211
name="AS2 Server",
@@ -233,8 +233,8 @@ def testEncryptMessageMdn(self):
233233
)
234234

235235
def testEncryptMessageSignMdn(self):
236-
""" Test Permutation 6: Sender sends encrypted data and requests
237-
an signed receipt. """
236+
"""Test Permutation 6: Sender sends encrypted data and requests
237+
an signed receipt."""
238238

239239
partner = Partner.objects.create(
240240
name="AS2 Server",
@@ -265,8 +265,8 @@ def testEncryptMessageSignMdn(self):
265265
)
266266

267267
def testSignMessageNoMdn(self):
268-
""" Test Permutation 7: Sender sends signed data and does NOT request
269-
a receipt. """
268+
"""Test Permutation 7: Sender sends signed data and does NOT request
269+
a receipt."""
270270

271271
partner = Partner.objects.create(
272272
name="AS2 Server",
@@ -291,8 +291,8 @@ def testSignMessageNoMdn(self):
291291
)
292292

293293
def testSignMessageMdn(self):
294-
""" Test Permutation 8: Sender sends signed data and requests an
295-
unsigned receipt. """
294+
"""Test Permutation 8: Sender sends signed data and requests an
295+
unsigned receipt."""
296296

297297
partner = Partner.objects.create(
298298
name="AS2 Server",
@@ -320,8 +320,8 @@ def testSignMessageMdn(self):
320320
)
321321

322322
def testSignMessageSignMdn(self):
323-
""" Test Permutation 9: Sender sends signed data and requests a
324-
signed receipt. """
323+
"""Test Permutation 9: Sender sends signed data and requests a
324+
signed receipt."""
325325

326326
partner = Partner.objects.create(
327327
name="AS2 Server",
@@ -351,8 +351,8 @@ def testSignMessageSignMdn(self):
351351
)
352352

353353
def testEncryptSignMessageNoMdn(self):
354-
""" Test Permutation 10: Sender sends encrypted and signed data and
355-
does NOT request a receipt. """
354+
"""Test Permutation 10: Sender sends encrypted and signed data and
355+
does NOT request a receipt."""
356356

357357
partner = Partner.objects.create(
358358
name="AS2 Server",
@@ -380,8 +380,8 @@ def testEncryptSignMessageNoMdn(self):
380380
)
381381

382382
def testEncryptSignMessageMdn(self):
383-
""" Test Permutation 11: Sender sends encrypted and signed data and
384-
requests an unsigned receipt. """
383+
"""Test Permutation 11: Sender sends encrypted and signed data and
384+
requests an unsigned receipt."""
385385

386386
partner = Partner.objects.create(
387387
name="AS2 Server",
@@ -412,8 +412,8 @@ def testEncryptSignMessageMdn(self):
412412
)
413413

414414
def testEncryptSignMessageSignMdn(self):
415-
""" Test Permutation 12: Sender sends encrypted and signed data and
416-
requests a signed receipt. """
415+
"""Test Permutation 12: Sender sends encrypted and signed data and
416+
requests a signed receipt."""
417417

418418
partner = Partner.objects.create(
419419
name="AS2 Server",
@@ -446,8 +446,8 @@ def testEncryptSignMessageSignMdn(self):
446446
)
447447

448448
def testCompressEncryptSignMessageSignMdn(self):
449-
""" Test Permutation 13: Sender sends compressed, encrypted and signed
450-
data and requests an signed receipt. """
449+
"""Test Permutation 13: Sender sends compressed, encrypted and signed
450+
data and requests an signed receipt."""
451451

452452
partner = Partner.objects.create(
453453
name="AS2 Server",
@@ -483,8 +483,8 @@ def testCompressEncryptSignMessageSignMdn(self):
483483

484484
@mock.patch("requests.post")
485485
def testEncryptSignMessageAsyncSignMdn(self, mock_request):
486-
""" Test Permutation 14: Sender sends encrypted and signed data and
487-
requests an Asynchronous signed receipt. """
486+
"""Test Permutation 14: Sender sends encrypted and signed data and
487+
requests an Asynchronous signed receipt."""
488488

489489
partner = Partner.objects.create(
490490
name="AS2 Server",
@@ -593,8 +593,13 @@ def __call__(self, *args, **kwargs):
593593
req_response = Response()
594594
req_response._content = response.content
595595

596-
for h_key, h_value in response._headers.values():
597-
req_response.headers[h_key] = h_value
596+
# Django 3.2 stores headers in headers instead of _headers
597+
if hasattr(response, "_headers"):
598+
for h_key, h_value in response._headers.values():
599+
req_response.headers[h_key] = h_value
600+
else:
601+
for h_key, h_value in response.headers._store.values():
602+
req_response.headers[h_key] = h_value
598603

599604
req_response.status_code = response.status_code
600605
return req_response

pyas2/tests/test_views.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ def setUp(self):
2424

2525
with open(os.path.join(TEST_DIR, "testmessage.edi"), "rb") as fp:
2626
self.message = Message.objects.create(
27-
message_id="some-message-id", direction="IN", status="S",
27+
message_id="some-message-id",
28+
direction="IN",
29+
status="S",
2830
)
2931
self.message.payload.save("testmessage.edi", fp)
3032
self.mdn = Mdn.objects.create(

pyas2/urls.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
from django.conf.urls import url
1+
from django.urls import path
22
from django.contrib.auth.decorators import login_required
33

44
from pyas2 import views
55

66

77
urlpatterns = [
8-
url(r"^as2receive/", views.ReceiveAs2Message.as_view(), name="as2-receive"),
8+
path("as2receive/", views.ReceiveAs2Message.as_view(), name="as2-receive"),
99
# Add the url again without slash for backwards compatibility
10-
url(r"^as2receive", views.ReceiveAs2Message.as_view(), name="as2-receive"),
11-
url(r"^as2send/", login_required(views.SendAs2Message.as_view()), name="as2-send"),
12-
url(
13-
r"^download/(?P<obj_type>.+)/(?P<obj_id>.+)/",
10+
path("as2receive", views.ReceiveAs2Message.as_view(), name="as2-receive"),
11+
path("as2send/", login_required(views.SendAs2Message.as_view()), name="as2-send"),
12+
path(
13+
"download/<str:obj_type>/<str:obj_id>/",
1414
login_required(views.DownloadFile.as_view()),
1515
name="download-file",
1616
),

pyas2/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88

99
def run_post_send(message):
10-
""" Execute command after successful send, can be used to notify
11-
successful sends """
10+
"""Execute command after successful send, can be used to notify
11+
successful sends"""
1212

1313
command = message.partner.cmd_send
1414
if command:
@@ -28,7 +28,7 @@ def run_post_send(message):
2828

2929

3030
def run_post_receive(message, full_filename):
31-
""" Execute command after successful receive, can be used to call the
31+
"""Execute command after successful receive, can be used to call the
3232
edi program for further processing"""
3333

3434
command = message.partner.cmd_receive

pyas2/views.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@
3232
@method_decorator(csrf_exempt, name="dispatch")
3333
class ReceiveAs2Message(View):
3434
"""
35-
Class receives AS2 requests from partners.
36-
Checks whether its an AS2 message or an MDN and acts accordingly.
35+
Class receives AS2 requests from partners.
36+
Checks whether its an AS2 message or an MDN and acts accordingly.
3737
"""
3838

3939
@staticmethod
4040
def find_message(message_id, partner_id):
41-
""" Find the message using the message_id and return its
42-
pyas2 version"""
41+
"""Find the message using the message_id and return its
42+
pyas2 version"""
4343
message = Message.objects.filter(
4444
message_id=message_id, partner_id=partner_id.strip()
4545
).first()

requirements/base.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
pyas2lib==1.3.1
1+
pyas2lib==1.3.3
22
requests
3-
django>=2.1.9
3+
django>=2.2.18

0 commit comments

Comments
 (0)