Merge pull request #11246 from ziegenberg/fix-http-headers-for-rocketchat-notifications

Use the AWX HTTP client headers for rocketchat notifications
This commit is contained in:
Rebeccah Hunter 2021-10-27 10:20:58 -04:00 committed by GitHub
commit e447b667e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 5 deletions

View File

@ -9,6 +9,7 @@ from django.utils.encoding import smart_text
from django.utils.translation import ugettext_lazy as _
from awx.main.notifications.base import AWXBaseEmailBackend
from awx.main.utils import get_awx_http_client_headers
from awx.main.notifications.custom_notification_base import CustomNotificationBase
logger = logging.getLogger('awx.main.notifications.rocketchat_backend')
@ -38,7 +39,9 @@ class RocketChatBackend(AWXBaseEmailBackend, CustomNotificationBase):
if optvalue is not None:
payload[optval] = optvalue.strip()
r = requests.post("{}".format(m.recipients()[0]), data=json.dumps(payload), verify=(not self.rocketchat_no_verify_ssl))
r = requests.post(
"{}".format(m.recipients()[0]), data=json.dumps(payload), headers=get_awx_http_client_headers(), verify=(not self.rocketchat_no_verify_ssl)
)
if r.status_code >= 400:
logger.error(smart_text(_("Error sending notification rocket.chat: {}").format(r.status_code)))

View File

@ -7,8 +7,11 @@ import awx.main.notifications.rocketchat_backend as rocketchat_backend
def test_send_messages():
with mock.patch('awx.main.notifications.rocketchat_backend.requests') as requests_mock:
with mock.patch('awx.main.notifications.rocketchat_backend.requests') as requests_mock, mock.patch(
'awx.main.notifications.rocketchat_backend.get_awx_http_client_headers'
) as version_mock:
requests_mock.post.return_value.status_code = 201
version_mock.return_value = {'Content-Type': 'application/json', 'User-Agent': 'AWX 0.0.1.dev (open)'}
backend = rocketchat_backend.RocketChatBackend()
message = EmailMessage(
'test subject',
@ -23,7 +26,12 @@ def test_send_messages():
message,
]
)
requests_mock.post.assert_called_once_with('http://example.com', data='{"text": "test subject"}', verify=True)
requests_mock.post.assert_called_once_with(
'http://example.com',
data='{"text": "test subject"}',
headers={'Content-Type': 'application/json', 'User-Agent': 'AWX 0.0.1.dev (open)'},
verify=True,
)
assert sent_messages == 1
@ -84,8 +92,11 @@ def test_send_messages_with_icon_url():
def test_send_messages_with_no_verify_ssl():
with mock.patch('awx.main.notifications.rocketchat_backend.requests') as requests_mock:
with mock.patch('awx.main.notifications.rocketchat_backend.requests') as requests_mock, mock.patch(
'awx.main.notifications.rocketchat_backend.get_awx_http_client_headers'
) as version_mock:
requests_mock.post.return_value.status_code = 201
version_mock.return_value = {'Content-Type': 'application/json', 'User-Agent': 'AWX 0.0.1.dev (open)'}
backend = rocketchat_backend.RocketChatBackend(rocketchat_no_verify_ssl=True)
message = EmailMessage(
'test subject',
@ -100,5 +111,10 @@ def test_send_messages_with_no_verify_ssl():
message,
]
)
requests_mock.post.assert_called_once_with('http://example.com', data='{"text": "test subject"}', verify=False)
requests_mock.post.assert_called_once_with(
'http://example.com',
data='{"text": "test subject"}',
headers={'Content-Type': 'application/json', 'User-Agent': 'AWX 0.0.1.dev (open)'},
verify=False,
)
assert sent_messages == 1