From 927ccc72641c8d103dcbc6ef73720626ea5e62e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20=C3=81lvaro?= Date: Thu, 21 Mar 2024 21:59:32 +0100 Subject: [PATCH] feat: Add tests to highstate returner --- changelog/66251.added.md | 1 + .../unit/returners/test_highstate_return.py | 73 ++++++++++++++----- 2 files changed, 56 insertions(+), 18 deletions(-) create mode 100644 changelog/66251.added.md diff --git a/changelog/66251.added.md b/changelog/66251.added.md new file mode 100644 index 00000000000..bade17c00c7 --- /dev/null +++ b/changelog/66251.added.md @@ -0,0 +1 @@ +Added port, tls, username and password to the `smtp` configuration of the highstate returner. diff --git a/tests/pytests/unit/returners/test_highstate_return.py b/tests/pytests/unit/returners/test_highstate_return.py index 4fcef347062..cd2fbc4d681 100644 --- a/tests/pytests/unit/returners/test_highstate_return.py +++ b/tests/pytests/unit/returners/test_highstate_return.py @@ -9,6 +9,7 @@ import pytest import salt.returners.highstate_return as highstate import salt.utils.files +from tests.support.mock import MagicMock, patch @pytest.fixture @@ -30,24 +31,9 @@ def configure_loader_modules(output_file): } -def test_generate_table_should_correctly_escape_html_characters_when_data_contains_both_list_and_dict(): - unescaped_fnord = "&fnord&" - unescaped_dronf = "" - expected_escaped_fnord = "&fnord&" - expected_escaped_dronf = "<dronf>" - data = [["something", "or", "another", unescaped_fnord, {"cool": unescaped_dronf}]] - - out = io.StringIO() - highstate._generate_html_table(data=data, out=out) - out.seek(0) - actual_table = out.read() - - assert expected_escaped_fnord in actual_table - assert expected_escaped_dronf in actual_table - - -def test_pipe_in_name(output_file): - ret = { +@pytest.fixture +def ret(): + return { "fun_args": ["test"], "jid": "20180308201402941603", "return": { @@ -74,6 +60,25 @@ def test_pipe_in_name(output_file): "id": "salt", "out": "highstate", } + + +def test_generate_table_should_correctly_escape_html_characters_when_data_contains_both_list_and_dict(): + unescaped_fnord = "&fnord&" + unescaped_dronf = "" + expected_escaped_fnord = "&fnord&" + expected_escaped_dronf = "<dronf>" + data = [["something", "or", "another", unescaped_fnord, {"cool": unescaped_dronf}]] + + out = io.StringIO() + highstate._generate_html_table(data=data, out=out) + out.seek(0) + actual_table = out.read() + + assert expected_escaped_fnord in actual_table + assert expected_escaped_dronf in actual_table + + +def test_pipe_in_name(output_file, ret): expected = [ { "stats": [ @@ -120,3 +125,35 @@ def test_pipe_in_name(output_file): highstate.returner(ret) with salt.utils.files.fopen(str(output_file)) as fh_: assert json.load(fh_) == expected + + +def test_smtp_options(ret): + """ + Test to see if the highstate returner uses smtp options + """ + smtp_username = "alice" + smtp_password = "p4ssw0rd" + smtp_server = "salt.stack.test" + smtp_port = 587 + smtp_tls = True + + options = { + "smtp_username": smtp_username, + "smtp_password": smtp_password, + "smtp_server": smtp_server, + "smtp_port": smtp_port, + "smtp_tls": smtp_tls, + "smtp_recipients": "bob.salt.test", + "smtp_sender": "alice.salt.test", + "report_delivery": "smtp", + } + + with patch( + "salt.returners.highstate_return._get_options", MagicMock(return_value=options) + ), patch("salt.returners.smtp_return.smtplib.SMTP") as mocked_smtplib: + highstate.returner(ret) + mocked_smtplib.assert_called_with(host=smtp_server, port=smtp_port) + mocked_smtplib.return_value.login.assert_called_with( + smtp_username, smtp_password + ) + assert mocked_smtplib.return_value.starttls.called is smtp_tls