-
Notifications
You must be signed in to change notification settings - Fork 292
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inbound email improvements (continued) #5263
Merged
+115
−44
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -47,6 +47,10 @@ | |
) | ||
AMAZON_SNS_TOPIC_ARN = "arn:aws:sns:us-east-2:123456789012:test" | ||
SIGNING_CERT_URL = "https://sns.us-east-2.amazonaws.com/SimpleNotificationService-example.pem" | ||
SENDER_EMAIL = "[email protected]" | ||
TO_EMAIL = "[email protected]" | ||
SUBJECT = "Test email" | ||
MESSAGE = "This is a test email message body." | ||
|
||
|
||
def _sns_inbound_email_payload_and_headers(sender_email, to_email, subject, message): | ||
|
@@ -439,15 +443,11 @@ def test_amazon_ses_pass(create_alert_mock, settings, make_organization, make_al | |
token="test-token", | ||
) | ||
|
||
sender_email = "[email protected]" | ||
to_email = "[email protected]" | ||
subject = "Test email" | ||
message = "This is a test email message body." | ||
sns_payload, sns_headers = _sns_inbound_email_payload_and_headers( | ||
sender_email=sender_email, | ||
to_email=to_email, | ||
subject=subject, | ||
message=message, | ||
sender_email=SENDER_EMAIL, | ||
to_email=TO_EMAIL, | ||
subject=SUBJECT, | ||
message=MESSAGE, | ||
) | ||
|
||
client = APIClient() | ||
|
@@ -460,16 +460,16 @@ def test_amazon_ses_pass(create_alert_mock, settings, make_organization, make_al | |
|
||
assert response.status_code == status.HTTP_200_OK | ||
create_alert_mock.assert_called_once_with( | ||
title=subject, | ||
message=message, | ||
title=SUBJECT, | ||
message=MESSAGE, | ||
alert_receive_channel_pk=alert_receive_channel.pk, | ||
image_url=None, | ||
link_to_upstream_details=None, | ||
integration_unique_data=None, | ||
raw_request_data={ | ||
"subject": subject, | ||
"message": message, | ||
"sender": sender_email, | ||
"subject": SUBJECT, | ||
"message": MESSAGE, | ||
"sender": SENDER_EMAIL, | ||
}, | ||
received_at=ANY, | ||
) | ||
|
@@ -493,15 +493,11 @@ def test_amazon_ses_validated_pass( | |
token="test-token", | ||
) | ||
|
||
sender_email = "[email protected]" | ||
to_email = "[email protected]" | ||
subject = "Test email" | ||
message = "This is a test email message body." | ||
sns_payload, sns_headers = _sns_inbound_email_payload_and_headers( | ||
sender_email=sender_email, | ||
to_email=to_email, | ||
subject=subject, | ||
message=message, | ||
sender_email=SENDER_EMAIL, | ||
to_email=TO_EMAIL, | ||
subject=SUBJECT, | ||
message=MESSAGE, | ||
) | ||
|
||
client = APIClient() | ||
|
@@ -514,23 +510,100 @@ def test_amazon_ses_validated_pass( | |
|
||
assert response.status_code == status.HTTP_200_OK | ||
mock_create_alert.assert_called_once_with( | ||
title=subject, | ||
message=message, | ||
title=SUBJECT, | ||
message=MESSAGE, | ||
alert_receive_channel_pk=alert_receive_channel.pk, | ||
image_url=None, | ||
link_to_upstream_details=None, | ||
integration_unique_data=None, | ||
raw_request_data={ | ||
"subject": subject, | ||
"message": message, | ||
"sender": sender_email, | ||
"subject": SUBJECT, | ||
"message": MESSAGE, | ||
"sender": SENDER_EMAIL, | ||
}, | ||
received_at=ANY, | ||
) | ||
|
||
mock_requests_get.assert_called_once_with(SIGNING_CERT_URL, timeout=5) | ||
|
||
|
||
@patch("requests.get", return_value=Mock(content=CERTIFICATE)) | ||
@patch.object(create_alert, "delay") | ||
@pytest.mark.django_db | ||
def test_amazon_ses_validated_fail_wrong_sns_topic_arn( | ||
mock_create_alert, mock_requests_get, settings, make_organization, make_alert_receive_channel | ||
): | ||
settings.INBOUND_EMAIL_ESP = "amazon_ses_validated,mailgun" | ||
settings.INBOUND_EMAIL_DOMAIN = "inbound.example.com" | ||
settings.INBOUND_EMAIL_WEBHOOK_SECRET = "secret" | ||
settings.INBOUND_EMAIL_AMAZON_SNS_TOPIC_ARN = "arn:aws:sns:us-east-2:123456789013:test" | ||
|
||
organization = make_organization() | ||
make_alert_receive_channel( | ||
organization, | ||
integration=AlertReceiveChannel.INTEGRATION_INBOUND_EMAIL, | ||
token="test-token", | ||
) | ||
|
||
sns_payload, sns_headers = _sns_inbound_email_payload_and_headers( | ||
sender_email=SENDER_EMAIL, | ||
to_email=TO_EMAIL, | ||
subject=SUBJECT, | ||
message=MESSAGE, | ||
) | ||
|
||
client = APIClient() | ||
response = client.post( | ||
reverse("integrations:inbound_email_webhook"), | ||
data=sns_payload, | ||
headers=sns_headers, | ||
format="json", | ||
) | ||
|
||
assert response.status_code == status.HTTP_400_BAD_REQUEST | ||
mock_create_alert.assert_not_called() | ||
mock_requests_get.assert_not_called() | ||
|
||
|
||
@patch("requests.get", return_value=Mock(content=CERTIFICATE)) | ||
@patch.object(create_alert, "delay") | ||
@pytest.mark.django_db | ||
def test_amazon_ses_validated_fail_wrong_signature( | ||
mock_create_alert, mock_requests_get, settings, make_organization, make_alert_receive_channel | ||
): | ||
settings.INBOUND_EMAIL_ESP = "amazon_ses_validated,mailgun" | ||
settings.INBOUND_EMAIL_DOMAIN = "inbound.example.com" | ||
settings.INBOUND_EMAIL_WEBHOOK_SECRET = "secret" | ||
settings.INBOUND_EMAIL_AMAZON_SNS_TOPIC_ARN = AMAZON_SNS_TOPIC_ARN | ||
|
||
organization = make_organization() | ||
make_alert_receive_channel( | ||
organization, | ||
integration=AlertReceiveChannel.INTEGRATION_INBOUND_EMAIL, | ||
token="test-token", | ||
) | ||
|
||
sns_payload, sns_headers = _sns_inbound_email_payload_and_headers( | ||
sender_email=SENDER_EMAIL, | ||
to_email=TO_EMAIL, | ||
subject=SUBJECT, | ||
message=MESSAGE, | ||
) | ||
sns_payload["Signature"] = "invalid-signature" | ||
|
||
client = APIClient() | ||
response = client.post( | ||
reverse("integrations:inbound_email_webhook"), | ||
data=sns_payload, | ||
headers=sns_headers, | ||
format="json", | ||
) | ||
|
||
assert response.status_code == status.HTTP_400_BAD_REQUEST | ||
mock_create_alert.assert_not_called() | ||
mock_requests_get.assert_called_once_with(SIGNING_CERT_URL, timeout=5) | ||
|
||
|
||
@patch.object(create_alert, "delay") | ||
@pytest.mark.django_db | ||
def test_mailgun_pass(create_alert_mock, settings, make_organization, make_alert_receive_channel): | ||
|
@@ -545,16 +618,11 @@ def test_mailgun_pass(create_alert_mock, settings, make_organization, make_alert | |
token="test-token", | ||
) | ||
|
||
sender_email = "[email protected]" | ||
to_email = "[email protected]" | ||
subject = "Test email" | ||
message = "This is a test email message body." | ||
|
||
mailgun_payload = _mailgun_inbound_email_payload( | ||
sender_email=sender_email, | ||
to_email=to_email, | ||
subject=subject, | ||
message=message, | ||
sender_email=SENDER_EMAIL, | ||
to_email=TO_EMAIL, | ||
subject=SUBJECT, | ||
message=MESSAGE, | ||
) | ||
|
||
client = APIClient() | ||
|
@@ -566,16 +634,16 @@ def test_mailgun_pass(create_alert_mock, settings, make_organization, make_alert | |
|
||
assert response.status_code == status.HTTP_200_OK | ||
create_alert_mock.assert_called_once_with( | ||
title=subject, | ||
message=message, | ||
title=SUBJECT, | ||
message=MESSAGE, | ||
alert_receive_channel_pk=alert_receive_channel.pk, | ||
image_url=None, | ||
link_to_upstream_details=None, | ||
integration_unique_data=None, | ||
raw_request_data={ | ||
"subject": subject, | ||
"message": message, | ||
"sender": sender_email, | ||
"subject": SUBJECT, | ||
"message": MESSAGE, | ||
"sender": SENDER_EMAIL, | ||
}, | ||
received_at=ANY, | ||
) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
call to
super().validate_request
is unnecessary here because django-anymail collects thesevalidate_request
methods from the whole superclass chain and runs them all inrun_validators
: https://github.com/anymail/django-anymail/blob/35383c7140289e82b39ada5980077898aa07d18d/anymail/webhooks/base.py#L28