From 0525df595e2252f7165271b62cb52dd0d8eff651 Mon Sep 17 00:00:00 2001 From: Jeandre Le Roux Date: Thu, 22 Mar 2018 16:11:03 +0200 Subject: [PATCH] Add unit test for rocket.chat notifications Signed-off-by: Jeandre Le Roux --- .../unit/notifications/test_rocketchat.py | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 awx/main/tests/unit/notifications/test_rocketchat.py diff --git a/awx/main/tests/unit/notifications/test_rocketchat.py b/awx/main/tests/unit/notifications/test_rocketchat.py new file mode 100644 index 0000000000..7cd68b4734 --- /dev/null +++ b/awx/main/tests/unit/notifications/test_rocketchat.py @@ -0,0 +1,37 @@ +import mock +import pytest +from django.core.mail.message import EmailMessage + +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: + backend = rocketchat_backend.RocketChatBackend() + message = EmailMessage('test subject', 'test body', [], ['http://example.com', ]) + sent_messages = backend.send_messages([message, ]) + requests_mock.post.assert_called_once_with('http://example.com', data='{"text": "test subject"}', verify=True) + assert sent_messages == 1 + +def test_send_messages_with_username(): + with mock.patch('awx.main.notifications.rocketchat_backend.requests') as requests_mock: + backend = rocketchat_backend.RocketChatBackend(rocketchat_username='testuser') + message = EmailMessage('test subject', 'test body', [], ['http://example.com', ]) + sent_messages = backend.send_messages([message, ]) + requests_mock.post.assert_called_once_with('http://example.com', data='{"username": "testuser", "text": "test subject"}', verify=True) + assert sent_messages == 1 + +def test_send_messages_with_icon_url(): + with mock.patch('awx.main.notifications.rocketchat_backend.requests') as requests_mock: + backend = rocketchat_backend.RocketChatBackend(rocketchat_icon_url='http://example.com') + message = EmailMessage('test subject', 'test body', [], ['http://example.com', ]) + sent_messages = backend.send_messages([message, ]) + requests_mock.post.assert_called_once_with('http://example.com', data='{"text": "test subject", "icon_url": "http://example.com"}', verify=True) + assert sent_messages == 1 + +def test_send_messages_with_no_verify_ssl(): + with mock.patch('awx.main.notifications.rocketchat_backend.requests') as requests_mock: + backend = rocketchat_backend.RocketChatBackend(rocketchat_no_verify_ssl=True) + message = EmailMessage('test subject', 'test body', [], ['http://example.com', ]) + sent_messages = backend.send_messages([message, ]) + requests_mock.post.assert_called_once_with('http://example.com', data='{"text": "test subject"}', verify=False) + assert sent_messages == 1