Improve yessssms test notify (#41283)

This commit is contained in:
sycx2 2020-10-06 10:11:36 +02:00 committed by GitHub
parent b6b85d3e0c
commit 956b735be6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,9 +1,7 @@
"""The tests for the notify yessssms platform.""" """The tests for the notify yessssms platform."""
import logging import logging
import unittest
import pytest import pytest
import requests_mock
from homeassistant.components.yessssms.const import CONF_PROVIDER from homeassistant.components.yessssms.const import CONF_PROVIDER
import homeassistant.components.yessssms.notify as yessssms import homeassistant.components.yessssms.notify as yessssms
@ -151,63 +149,65 @@ async def test_connection_error_on_init(hass, caplog, valid_settings, connection
) )
class TestNotifyYesssSMS(unittest.TestCase): @pytest.fixture(name="yessssms")
"""Test the yessssms notify.""" def yessssms_init():
def setUp(self): # pylint: disable=invalid-name
"""Set up things to be run when tests are started.""" """Set up things to be run when tests are started."""
login = "06641234567" login = "06641234567"
passwd = "testpasswd" passwd = "testpasswd"
recipient = "06501234567" recipient = "06501234567"
client = yessssms.YesssSMS(login, passwd) client = yessssms.YesssSMS(login, passwd)
self.yessssms = yessssms.YesssSMSNotificationService(client, recipient) return yessssms.YesssSMSNotificationService(client, recipient)
@requests_mock.Mocker()
def test_login_error(self, mock): async def test_login_error(yessssms, requests_mock, caplog):
"""Test login that fails.""" """Test login that fails."""
mock.register_uri( requests_mock.post(
requests_mock.POST,
# pylint: disable=protected-access # pylint: disable=protected-access
self.yessssms.yesss._login_url, yessssms.yesss._login_url,
status_code=200, status_code=200,
text="BlaBlaBla<strong>Login nicht erfolgreichBlaBla", text="BlaBlaBla<strong>Login nicht erfolgreichBlaBla",
) )
message = "Testing YesssSMS platform :)" message = "Testing YesssSMS platform :)"
with self.assertLogs("homeassistant.components.yessssms.notify", level="ERROR"): with caplog.at_level(logging.ERROR):
self.yessssms.send_message(message) yessssms.send_message(message)
self.assertTrue(mock.called) assert requests_mock.called is True
self.assertEqual(mock.call_count, 1) assert requests_mock.call_count == 1
def test_empty_message_error(self):
async def test_empty_message_error(yessssms, caplog):
"""Test for an empty SMS message error.""" """Test for an empty SMS message error."""
message = "" message = ""
with self.assertLogs("homeassistant.components.yessssms.notify", level="ERROR"): with caplog.at_level(logging.ERROR):
self.yessssms.send_message(message) yessssms.send_message(message)
@requests_mock.Mocker() for record in caplog.records:
def test_error_account_suspended(self, mock): if (
record.levelname == "ERROR"
and record.name == "homeassistant.components.yessssms.notify"
):
assert "Cannot send empty SMS message" in record.message
async def test_error_account_suspended(yessssms, requests_mock, caplog):
"""Test login that fails after multiple attempts.""" """Test login that fails after multiple attempts."""
mock.register_uri( requests_mock.post(
"POST",
# pylint: disable=protected-access # pylint: disable=protected-access
self.yessssms.yesss._login_url, yessssms.yesss._login_url,
status_code=200, status_code=200,
text="BlaBlaBla<strong>Login nicht erfolgreichBlaBla", text="BlaBlaBla<strong>Login nicht erfolgreichBlaBla",
) )
message = "Testing YesssSMS platform :)" message = "Testing YesssSMS platform :)"
with self.assertLogs("homeassistant.components.yessssms.notify", level="ERROR"): yessssms.send_message(message)
self.yessssms.send_message(message) assert requests_mock.called is True
self.assertTrue(mock.called) assert requests_mock.call_count == 1
self.assertEqual(mock.call_count, 1)
mock.register_uri( requests_mock.post(
"POST",
# pylint: disable=protected-access # pylint: disable=protected-access
self.yessssms.yesss._login_url, yessssms.yesss._login_url,
status_code=200, status_code=200,
text="Wegen 3 ungültigen Login-Versuchen ist Ihr Account für " text="Wegen 3 ungültigen Login-Versuchen ist Ihr Account für "
"eine Stunde gesperrt.", "eine Stunde gesperrt.",
@ -215,146 +215,153 @@ class TestNotifyYesssSMS(unittest.TestCase):
message = "Testing YesssSMS platform :)" message = "Testing YesssSMS platform :)"
with self.assertLogs("homeassistant.components.yessssms.notify", level="ERROR"): with caplog.at_level(logging.ERROR):
self.yessssms.send_message(message) yessssms.send_message(message)
self.assertTrue(mock.called) assert requests_mock.called is True
self.assertEqual(mock.call_count, 2) assert requests_mock.call_count == 2
def test_error_account_suspended_2(self):
async def test_error_account_suspended_2(yessssms, caplog):
"""Test login that fails after multiple attempts.""" """Test login that fails after multiple attempts."""
message = "Testing YesssSMS platform :)" message = "Testing YesssSMS platform :)"
# pylint: disable=protected-access # pylint: disable=protected-access
self.yessssms.yesss._suspended = True yessssms.yesss._suspended = True
with self.assertLogs( with caplog.at_level(logging.ERROR):
"homeassistant.components.yessssms.notify", level="ERROR" yessssms.send_message(message)
) as context: for record in caplog.records:
self.yessssms.send_message(message) if (
self.assertIn("Account is suspended, cannot send SMS.", context.output[0]) record.levelname == "ERROR"
and record.name == "homeassistant.components.yessssms.notify"
):
assert "Account is suspended, cannot send SMS." in record.message
@requests_mock.Mocker()
def test_send_message(self, mock): async def test_send_message(yessssms, requests_mock, caplog):
"""Test send message.""" """Test send message."""
message = "Testing YesssSMS platform :)" message = "Testing YesssSMS platform :)"
mock.register_uri( requests_mock.post(
"POST",
# pylint: disable=protected-access # pylint: disable=protected-access
self.yessssms.yesss._login_url, yessssms.yesss._login_url,
status_code=302, status_code=302,
# pylint: disable=protected-access # pylint: disable=protected-access
headers={"location": self.yessssms.yesss._kontomanager}, headers={"location": yessssms.yesss._kontomanager},
) )
# pylint: disable=protected-access # pylint: disable=protected-access
login = self.yessssms.yesss._logindata["login_rufnummer"] login = yessssms.yesss._logindata["login_rufnummer"]
mock.register_uri( requests_mock.get(
"GET",
# pylint: disable=protected-access # pylint: disable=protected-access
self.yessssms.yesss._kontomanager, yessssms.yesss._kontomanager,
status_code=200, status_code=200,
text=f"test...{login}</a>", text=f"test...{login}</a>",
) )
mock.register_uri( requests_mock.post(
"POST",
# pylint: disable=protected-access # pylint: disable=protected-access
self.yessssms.yesss._websms_url, yessssms.yesss._websms_url,
status_code=200, status_code=200,
text="<h1>Ihre SMS wurde erfolgreich verschickt!</h1>", text="<h1>Ihre SMS wurde erfolgreich verschickt!</h1>",
) )
mock.register_uri( requests_mock.get(
"GET",
# pylint: disable=protected-access # pylint: disable=protected-access
self.yessssms.yesss._logout_url, yessssms.yesss._logout_url,
status_code=200, status_code=200,
) )
with self.assertLogs( with caplog.at_level(logging.INFO):
"homeassistant.components.yessssms.notify", level="INFO" yessssms.send_message(message)
) as context: for record in caplog.records:
self.yessssms.send_message(message) if (
self.assertIn("SMS sent", context.output[0]) record.levelname == "INFO"
self.assertTrue(mock.called) and record.name == "homeassistant.components.yessssms.notify"
self.assertEqual(mock.call_count, 4) ):
self.assertIn( assert "SMS sent" in record.message
mock.last_request.scheme
+ "://"
+ mock.last_request.hostname
+ mock.last_request.path
+ "?"
+ mock.last_request.query,
# pylint: disable=protected-access
self.yessssms.yesss._logout_url,
)
def test_no_recipient_error(self): assert requests_mock.called is True
assert requests_mock.call_count == 4
assert (
requests_mock.last_request.scheme
+ "://"
+ requests_mock.last_request.hostname
+ requests_mock.last_request.path
+ "?"
+ requests_mock.last_request.query
) in yessssms.yesss._logout_url # pylint: disable=protected-access
async def test_no_recipient_error(yessssms, caplog):
"""Test for missing/empty recipient.""" """Test for missing/empty recipient."""
message = "Testing YesssSMS platform :)" message = "Testing YesssSMS platform :)"
# pylint: disable=protected-access # pylint: disable=protected-access
self.yessssms._recipient = "" yessssms._recipient = ""
with self.assertLogs( with caplog.at_level(logging.ERROR):
"homeassistant.components.yessssms.notify", level="ERROR" yessssms.send_message(message)
) as context: for record in caplog.records:
self.yessssms.send_message(message) if (
record.levelname == "ERROR"
self.assertIn( and record.name == "homeassistant.components.yessssms.notify"
"You need to provide a recipient for SMS notification", context.output[0] ):
assert (
"You need to provide a recipient for SMS notification" in record.message
) )
@requests_mock.Mocker()
def test_sms_sending_error(self, mock): async def test_sms_sending_error(yessssms, requests_mock, caplog):
"""Test sms sending error.""" """Test sms sending error."""
mock.register_uri( requests_mock.post(
"POST",
# pylint: disable=protected-access # pylint: disable=protected-access
self.yessssms.yesss._login_url, yessssms.yesss._login_url,
status_code=302, status_code=302,
# pylint: disable=protected-access # pylint: disable=protected-access
headers={"location": self.yessssms.yesss._kontomanager}, headers={"location": yessssms.yesss._kontomanager},
) )
# pylint: disable=protected-access # pylint: disable=protected-access
login = self.yessssms.yesss._logindata["login_rufnummer"] login = yessssms.yesss._logindata["login_rufnummer"]
mock.register_uri( requests_mock.get(
"GET",
# pylint: disable=protected-access # pylint: disable=protected-access
self.yessssms.yesss._kontomanager, yessssms.yesss._kontomanager,
status_code=200, status_code=200,
text=f"test...{login}</a>", text=f"test...{login}</a>",
) )
mock.register_uri( requests_mock.post(
"POST",
# pylint: disable=protected-access # pylint: disable=protected-access
self.yessssms.yesss._websms_url, yessssms.yesss._websms_url,
status_code=HTTP_INTERNAL_SERVER_ERROR, status_code=HTTP_INTERNAL_SERVER_ERROR,
) )
message = "Testing YesssSMS platform :)" message = "Testing YesssSMS platform :)"
with self.assertLogs( with caplog.at_level(logging.ERROR):
"homeassistant.components.yessssms.notify", level="ERROR" yessssms.send_message(message)
) as context:
self.yessssms.send_message(message)
self.assertTrue(mock.called) assert requests_mock.called is True
self.assertEqual(mock.call_count, 3) assert requests_mock.call_count == 3
self.assertIn("YesssSMS: error sending SMS", context.output[0]) for record in caplog.records:
if (
record.levelname == "ERROR"
and record.name == "homeassistant.components.yessssms.notify"
):
assert "YesssSMS: error sending SMS" in record.message
@requests_mock.Mocker()
def test_connection_error(self, mock): async def test_connection_error(yessssms, requests_mock, caplog):
"""Test connection error.""" """Test connection error."""
mock.register_uri( requests_mock.post(
"POST",
# pylint: disable=protected-access # pylint: disable=protected-access
self.yessssms.yesss._login_url, yessssms.yesss._login_url,
exc=yessssms.YesssSMS.ConnectionError, exc=yessssms.yesss.ConnectionError,
) )
message = "Testing YesssSMS platform :)" message = "Testing YesssSMS platform :)"
with self.assertLogs( with caplog.at_level(logging.ERROR):
"homeassistant.components.yessssms.notify", level="ERROR" yessssms.send_message(message)
) as context:
self.yessssms.send_message(message)
self.assertTrue(mock.called) assert requests_mock.called is True
self.assertEqual(mock.call_count, 1) assert requests_mock.call_count == 1
self.assertIn("cannot connect to provider", context.output[0]) for record in caplog.records:
if (
record.levelname == "ERROR"
and record.name == "homeassistant.components.yessssms.notify"
):
assert "cannot connect to provider" in record.message