diff --git a/tests/test_manager.py b/tests/test_manager.py index ce09a5b..28e8463 100644 --- a/tests/test_manager.py +++ b/tests/test_manager.py @@ -27,6 +27,8 @@ import re import queue import signal import http.server +import logging +import bcrypt from unittest.mock import patch from unittest.mock import MagicMock @@ -560,6 +562,7 @@ class TestManager(unittest.TestCase): self.listEvent = {} self.listQueueIn = {} self.listQueueOut = {} + self.authentification = False @patch.object(http.server.SimpleHTTPRequestHandler, '__init__') def test_run_manage_log_message(self, init): @@ -1039,6 +1042,297 @@ class TestManager(unittest.TestCase): manage._send_command("STATUS") manage.send_error.assert_called_with(400, 'Name unknown') + @patch.object(http.server.SimpleHTTPRequestHandler, '__init__') + def test_run_manage_do_GET_1(self, init): + # Enable timeout + signal.alarm(10) + manage = Manager.ManageHttpRequest(None, None, None) + manage.server = TestManager.MockServer() + manage._command_log = MagicMock() + manage.path = "/STDOUT" + manage.do_GET() + manage._command_log.assert_called_with() + + @patch.object(http.server.SimpleHTTPRequestHandler, '__init__') + def test_run_manage_check_authentication_1(self, init): + # Enable timeout + signal.alarm(10) + manage = Manager.ManageHttpRequest(None, None, None) + manage.server = TestManager.MockServer() + res = manage.check_authentication() + self.assertEqual(True, res) + + @patch.object(http.server.SimpleHTTPRequestHandler, '__init__') + def test_run_manage_check_authentication_2(self, init): + # Enable timeout + signal.alarm(10) + manage = Manager.ManageHttpRequest(None, None, None) + manage.server = TestManager.MockServer() + manage.server.authentification = True + res = manage.check_authentication() + self.assertEqual(False, res) + + @patch.object(http.server.SimpleHTTPRequestHandler, '__init__') + def test_run_manage_check_authentication_3(self, init): + # Enable timeout + signal.alarm(10) + manage = Manager.ManageHttpRequest(None, None, None) + manage.server = TestManager.MockServer() + logging.error = MagicMock() + manage.server.authentification = True + manage.headers = {'Authorization' : 'Other'} + res = manage.check_authentication() + self.assertEqual(False, res) + logging.error.assert_called_with("Authentification with Bad method (Other)") + + @patch.object(http.server.SimpleHTTPRequestHandler, '__init__') + def test_run_manage_check_authentication_4(self, init): + # Enable timeout + signal.alarm(10) + manage = Manager.ManageHttpRequest(None, None, None) + manage.server = TestManager.MockServer() + logging.error = MagicMock() + manage.server.authentification = True + manage.headers = {'Authorization' : 'Basic '} + res = manage.check_authentication() + self.assertEqual(False, res) + logging.error.assert_called_with("Error detected list index out of range") + + @patch.object(http.server.SimpleHTTPRequestHandler, '__init__') + def test_run_manage_check_authentication_5(self, init): + # Enable timeout + signal.alarm(10) + manage = Manager.ManageHttpRequest(None, None, None) + manage.server = TestManager.MockServer() + logging.error = MagicMock() + manage.server.authentification = True + manage.headers = {'Authorization' : 'Basic test:$2b$12$AoRGhrAYsuSlROUDx.0AZ.3SPBVYd8wD.Vyz7jCRXZdwm7ArEN3Oi'} + res = manage.check_authentication() + self.assertEqual(False, res) + logging.error.assert_called_with("Error detected Incorrect padding") + + @patch.object(http.server.SimpleHTTPRequestHandler, '__init__') + def test_run_manage_check_authentication_6(self, init): + # Enable timeout + signal.alarm(10) + manage = Manager.ManageHttpRequest(None, None, None) + manage.server = TestManager.MockServer() + manage.server.users = {"user1": ""} + logging.error = MagicMock() + manage.server.authentification = True + manage.headers = {'Authorization' : 'Basic dGVzdDokMmIkMTIkQW9SR2hyQVlzdVNsUk9VRHguMEFaLjNTUEJWWWQ4d0QuVnl6N2pDUlhaZHdtN0FyRU4zT2k='} + res = manage.check_authentication() + self.assertEqual(False, res) + logging.error.assert_called_with("Authentification with unknown user (test)") + + @patch.object(http.server.SimpleHTTPRequestHandler, '__init__') + def test_run_manage_do_GET_2(self, init): + # Enable timeout + signal.alarm(10) + manage = Manager.ManageHttpRequest(None, None, None) + manage.server = TestManager.MockServer() + manage._send_command = MagicMock() + manage.path = "/STATUS" + manage.do_GET() + manage._send_command.assert_called_with("STATUS") + + @patch.object(http.server.SimpleHTTPRequestHandler, '__init__') + def test_run_manage_do_GET_3(self, init): + # Enable timeout + signal.alarm(10) + manage = Manager.ManageHttpRequest(None, None, None) + manage.server = TestManager.MockServer() + manage._send_list = MagicMock() + manage.path = "/LIST" + manage.do_GET() + manage._send_list.assert_called_with() + + @patch.object(http.server.SimpleHTTPRequestHandler, '__init__') + def test_run_manage_do_GET_4(self, init): + # Enable timeout + signal.alarm(10) + manage = Manager.ManageHttpRequest(None, None, None) + manage.server = TestManager.MockServer() + manage._send_command_all = MagicMock() + manage.path = "/STATUSALL" + manage.do_GET() + manage._send_command_all.assert_called_with("STATUS") + + @patch.object(http.server.SimpleHTTPRequestHandler, '__init__') + def test_run_manage_do_GET_5(self, init): + # Enable timeout + signal.alarm(10) + manage = Manager.ManageHttpRequest(None, None, None) + manage.server = TestManager.MockServer() + manage.send_error = MagicMock() + manage.path = "/BADPATH" + manage.do_GET() + manage.send_error.assert_called_with(400, 'Path unknown') + + @patch.object(http.server.SimpleHTTPRequestHandler, '__init__') + def test_run_manage_do_GET_6(self, init): + # Enable timeout + signal.alarm(10) + manage = Manager.ManageHttpRequest(None, None, None) + manage.server = TestManager.MockServer() + manage.server.authentification = True + manage.send_error = MagicMock() + manage.path = "/BADPATH" + manage.do_GET() + manage.send_error.assert_called_with(403, 'Wrong authentication') + + @patch.object(http.server.SimpleHTTPRequestHandler, '__init__') + def test_run_manage_do_POST_1(self, init): + # Enable timeout + signal.alarm(10) + manage = Manager.ManageHttpRequest(None, None, None) + manage.server = TestManager.MockServer() + manage._send_command = MagicMock() + manage.path = "/START" + manage.do_POST() + manage._send_command.assert_called_with("START") + + @patch.object(http.server.SimpleHTTPRequestHandler, '__init__') + def test_run_manage_do_POST_2(self, init): + # Enable timeout + signal.alarm(10) + manage = Manager.ManageHttpRequest(None, None, None) + manage.server = TestManager.MockServer() + manage._send_command = MagicMock() + manage.path = "/STOP" + manage.do_POST() + manage._send_command.assert_called_with("STOP") + + @patch.object(http.server.SimpleHTTPRequestHandler, '__init__') + def test_run_manage_do_POST_3(self, init): + # Enable timeout + signal.alarm(10) + manage = Manager.ManageHttpRequest(None, None, None) + manage.server = TestManager.MockServer() + manage._send_action = MagicMock() + manage.path = "/STDIN" + manage.do_POST() + manage._send_action.assert_called_with() + + @patch.object(http.server.SimpleHTTPRequestHandler, '__init__') + def test_run_manage_do_POST_4(self, init): + # Enable timeout + signal.alarm(10) + manage = Manager.ManageHttpRequest(None, None, None) + manage.server = TestManager.MockServer() + manage._send_shutdown = MagicMock() + manage.path = "/SHUTDOWN" + manage.do_POST() + manage._send_shutdown.assert_called_with() + + @patch.object(http.server.SimpleHTTPRequestHandler, '__init__') + def test_run_manage_do_POST_5(self, init): + # Enable timeout + signal.alarm(10) + manage = Manager.ManageHttpRequest(None, None, None) + manage.server = TestManager.MockServer() + manage._send_command_all = MagicMock() + manage.path = "/STARTALL" + manage.do_POST() + manage._send_command_all.assert_called_with("START") + + @patch.object(http.server.SimpleHTTPRequestHandler, '__init__') + def test_run_manage_do_POST_6(self, init): + # Enable timeout + signal.alarm(10) + manage = Manager.ManageHttpRequest(None, None, None) + manage.server = TestManager.MockServer() + manage._send_command_all = MagicMock() + manage.path = "/STOPALL" + manage.do_POST() + manage._send_command_all.assert_called_with("STOP") + + @patch.object(http.server.SimpleHTTPRequestHandler, '__init__') + def test_run_manage_do_POST_7(self, init): + # Enable timeout + signal.alarm(10) + manage = Manager.ManageHttpRequest(None, None, None) + manage.server = TestManager.MockServer() + manage.log_message = MagicMock() + manage.send_response = MagicMock() + manage.send_header = MagicMock() + manage.send_error = MagicMock() + manage.end_headers = MagicMock() + manage._send_command = MagicMock() + manage._send_action = MagicMock() + manage._send_shutdown = MagicMock() + manage._send_command_all = MagicMock() + manage.path = "/BADPATH" + manage.do_POST() + manage.send_error.assert_called_with(400, 'Path unknown') + + @patch.object(http.server.SimpleHTTPRequestHandler, '__init__') + def test_run_manage_do_POST_8(self, init): + # Enable timeout + signal.alarm(10) + manage = Manager.ManageHttpRequest(None, None, None) + manage.server = TestManager.MockServer() + manage.server.authentification = True + manage.send_error = MagicMock() + manage.path = "/BADPATH" + manage.do_POST() + manage.send_error.assert_called_with(403, 'Wrong authentication') + + @patch.object(http.server.SimpleHTTPRequestHandler, '__init__') + def test_run_manage_do_HEAD(self, init): + # Enable timeout + signal.alarm(10) + manage = Manager.ManageHttpRequest(None, None, None) + manage.log_message = MagicMock() + manage.send_response = MagicMock() + manage.send_header = MagicMock() + manage.send_error = MagicMock() + manage.end_headers = MagicMock() + manage.path = "test" + manage.do_HEAD() + manage.send_error.assert_called_with(404, 'File Not Found: test') + + @patch.object(http.server.SimpleHTTPRequestHandler, '__init__') + def test_run_manage_do_PUT(self, init): + # Enable timeout + signal.alarm(10) + manage = Manager.ManageHttpRequest(None, None, None) + manage.log_message = MagicMock() + manage.send_response = MagicMock() + manage.send_header = MagicMock() + manage.send_error = MagicMock() + manage.end_headers = MagicMock() + manage.path = "test" + manage.do_PUT() + manage.send_error.assert_called_with(404, 'File Not Found: test') + + @patch.object(http.server.SimpleHTTPRequestHandler, '__init__') + def test_run_manage_do_PATCH(self, init): + # Enable timeout + signal.alarm(10) + manage = Manager.ManageHttpRequest(None, None, None) + manage.log_message = MagicMock() + manage.send_response = MagicMock() + manage.send_header = MagicMock() + manage.send_error = MagicMock() + manage.end_headers = MagicMock() + manage.path = "test" + manage.do_PATCH() + manage.send_error.assert_called_with(404, 'File Not Found: test') + + @patch.object(http.server.SimpleHTTPRequestHandler, '__init__') + def test_run_manage_do_DELETE(self, init): + # Enable timeout + signal.alarm(10) + manage = Manager.ManageHttpRequest(None, None, None) + manage.log_message = MagicMock() + manage.send_response = MagicMock() + manage.send_header = MagicMock() + manage.send_error = MagicMock() + manage.end_headers = MagicMock() + manage.path = "test" + manage.do_DELETE() + manage.send_error.assert_called_with(404, 'File Not Found: test') if __name__ == '__main__': unittest.main()