|
| 1 | +"""Tests for /control/reserve and /control/mode POST handlers. |
| 2 | +
|
| 3 | +Covers the single-value (legacy) form and the optional companion-parameter |
| 4 | +form (mode= on /control/reserve, level= on /control/mode) added to let a |
| 5 | +caller update both reserve and mode in a single set_operation() invocation. |
| 6 | +""" |
| 7 | +import json |
| 8 | +import unittest |
| 9 | +from http import HTTPStatus |
| 10 | +from io import BytesIO |
| 11 | +from unittest.mock import Mock, patch |
| 12 | + |
| 13 | +from proxy.tests.test_csv_endpoints import UnittestHandler, common_patches |
| 14 | + |
| 15 | + |
| 16 | +SECRET = "test-secret" |
| 17 | + |
| 18 | + |
| 19 | +def _encode(params): |
| 20 | + return "&".join(f"{k}={v}" for k, v in params.items()).encode("utf-8") |
| 21 | + |
| 22 | + |
| 23 | +def _passthrough_safe_pw_call(fn, *args, **kwargs): |
| 24 | + return fn(*args, **kwargs) |
| 25 | + |
| 26 | + |
| 27 | +class BaseDoPostTest(unittest.TestCase): |
| 28 | + def setUp(self): |
| 29 | + self.handler = UnittestHandler() |
| 30 | + self.handler.command = "POST" |
| 31 | + |
| 32 | + def _post(self, path, params): |
| 33 | + body = _encode(params) |
| 34 | + self.handler.path = path |
| 35 | + self.handler.rfile = BytesIO(body) |
| 36 | + self.handler.headers = {"Content-Length": str(len(body))} |
| 37 | + self.handler.do_POST() |
| 38 | + |
| 39 | + def _response_body(self): |
| 40 | + return self.handler.wfile.getvalue().decode("utf-8") |
| 41 | + |
| 42 | + def _make_pw_control(self, set_reserve=None, set_mode=None, set_operation=None): |
| 43 | + pw_control = Mock() |
| 44 | + pw_control.client = Mock() # not None -> passes cloud-mode connectivity check |
| 45 | + pw_control.set_reserve.return_value = set_reserve if set_reserve is not None else {"set_reserve": "ok"} |
| 46 | + pw_control.set_mode.return_value = set_mode if set_mode is not None else {"set_mode": "ok"} |
| 47 | + pw_control.set_operation.return_value = set_operation if set_operation is not None else {"set_operation": "ok"} |
| 48 | + return pw_control |
| 49 | + |
| 50 | + |
| 51 | +class TestControlReserve(BaseDoPostTest): |
| 52 | + """POST /control/reserve — single-value and combined-with-mode forms.""" |
| 53 | + |
| 54 | + @common_patches |
| 55 | + @patch("proxy.server.control_secret", SECRET) |
| 56 | + @patch("proxy.server.pw") |
| 57 | + @patch("proxy.server.pw_control") |
| 58 | + @patch("proxy.server.safe_pw_call", side_effect=_passthrough_safe_pw_call) |
| 59 | + def test_value_only_calls_set_reserve(self, _proxystats_lock, mock_safe, mock_pw_control, mock_pw): |
| 60 | + """value= alone -> set_reserve(level), set_operation not called (legacy behaviour).""" |
| 61 | + pw_control = self._make_pw_control() |
| 62 | + mock_pw_control.client = pw_control.client |
| 63 | + mock_pw_control.set_reserve = pw_control.set_reserve |
| 64 | + mock_pw_control.set_operation = pw_control.set_operation |
| 65 | + mock_pw.tedapi = None |
| 66 | + |
| 67 | + self._post("/control/reserve", {"value": "50", "token": SECRET}) |
| 68 | + |
| 69 | + pw_control.set_reserve.assert_called_once_with(50) |
| 70 | + pw_control.set_operation.assert_not_called() |
| 71 | + self.handler.send_response.assert_called_with(HTTPStatus.OK) |
| 72 | + self.assertEqual(json.loads(self._response_body()), {"set_reserve": "ok"}) |
| 73 | + |
| 74 | + @common_patches |
| 75 | + @patch("proxy.server.control_secret", SECRET) |
| 76 | + @patch("proxy.server.pw") |
| 77 | + @patch("proxy.server.pw_control") |
| 78 | + @patch("proxy.server.safe_pw_call", side_effect=_passthrough_safe_pw_call) |
| 79 | + def test_value_with_valid_mode_calls_set_operation(self, _proxystats_lock, mock_safe, mock_pw_control, mock_pw): |
| 80 | + """value= + valid mode= -> set_operation(level, mode), neither set_reserve nor set_mode.""" |
| 81 | + pw_control = self._make_pw_control() |
| 82 | + mock_pw_control.client = pw_control.client |
| 83 | + mock_pw_control.set_reserve = pw_control.set_reserve |
| 84 | + mock_pw_control.set_mode = pw_control.set_mode |
| 85 | + mock_pw_control.set_operation = pw_control.set_operation |
| 86 | + mock_pw.tedapi = None |
| 87 | + |
| 88 | + self._post( |
| 89 | + "/control/reserve", |
| 90 | + {"value": "5", "mode": "self_consumption", "token": SECRET}, |
| 91 | + ) |
| 92 | + |
| 93 | + pw_control.set_operation.assert_called_once_with(5, "self_consumption") |
| 94 | + pw_control.set_reserve.assert_not_called() |
| 95 | + pw_control.set_mode.assert_not_called() |
| 96 | + self.handler.send_response.assert_called_with(HTTPStatus.OK) |
| 97 | + self.assertEqual(json.loads(self._response_body()), {"set_operation": "ok"}) |
| 98 | + |
| 99 | + @common_patches |
| 100 | + @patch("proxy.server.control_secret", SECRET) |
| 101 | + @patch("proxy.server.pw") |
| 102 | + @patch("proxy.server.pw_control") |
| 103 | + @patch("proxy.server.safe_pw_call", side_effect=_passthrough_safe_pw_call) |
| 104 | + def test_value_with_invalid_mode_returns_error(self, _proxystats_lock, mock_safe, mock_pw_control, mock_pw): |
| 105 | + """value= + invalid mode= -> 400 error, no Powerwall call (no silent fallback to set_reserve).""" |
| 106 | + pw_control = self._make_pw_control() |
| 107 | + mock_pw_control.client = pw_control.client |
| 108 | + mock_pw_control.set_reserve = pw_control.set_reserve |
| 109 | + mock_pw_control.set_operation = pw_control.set_operation |
| 110 | + mock_pw.tedapi = None |
| 111 | + |
| 112 | + self._post( |
| 113 | + "/control/reserve", |
| 114 | + {"value": "5", "mode": "garbage", "token": SECRET}, |
| 115 | + ) |
| 116 | + |
| 117 | + pw_control.set_reserve.assert_not_called() |
| 118 | + pw_control.set_operation.assert_not_called() |
| 119 | + self.handler.send_response.assert_called_with(HTTPStatus.BAD_REQUEST) |
| 120 | + self.assertIn("error", json.loads(self._response_body())) |
| 121 | + |
| 122 | + |
| 123 | +class TestControlMode(BaseDoPostTest): |
| 124 | + """POST /control/mode — single-value and combined-with-level forms.""" |
| 125 | + |
| 126 | + @common_patches |
| 127 | + @patch("proxy.server.control_secret", SECRET) |
| 128 | + @patch("proxy.server.pw") |
| 129 | + @patch("proxy.server.pw_control") |
| 130 | + @patch("proxy.server.safe_pw_call", side_effect=_passthrough_safe_pw_call) |
| 131 | + def test_value_only_calls_set_mode(self, _proxystats_lock, mock_safe, mock_pw_control, mock_pw): |
| 132 | + """value= alone -> set_mode(mode), set_operation not called (legacy behaviour).""" |
| 133 | + pw_control = self._make_pw_control() |
| 134 | + mock_pw_control.client = pw_control.client |
| 135 | + mock_pw_control.set_mode = pw_control.set_mode |
| 136 | + mock_pw_control.set_operation = pw_control.set_operation |
| 137 | + mock_pw.tedapi = None |
| 138 | + |
| 139 | + self._post("/control/mode", {"value": "backup", "token": SECRET}) |
| 140 | + |
| 141 | + pw_control.set_mode.assert_called_once_with("backup") |
| 142 | + pw_control.set_operation.assert_not_called() |
| 143 | + self.handler.send_response.assert_called_with(HTTPStatus.OK) |
| 144 | + self.assertEqual(json.loads(self._response_body()), {"set_mode": "ok"}) |
| 145 | + |
| 146 | + @common_patches |
| 147 | + @patch("proxy.server.control_secret", SECRET) |
| 148 | + @patch("proxy.server.pw") |
| 149 | + @patch("proxy.server.pw_control") |
| 150 | + @patch("proxy.server.safe_pw_call", side_effect=_passthrough_safe_pw_call) |
| 151 | + def test_value_with_valid_level_calls_set_operation(self, _proxystats_lock, mock_safe, mock_pw_control, mock_pw): |
| 152 | + """value= + valid level= -> set_operation(level, mode), neither set_mode nor set_reserve.""" |
| 153 | + pw_control = self._make_pw_control() |
| 154 | + mock_pw_control.client = pw_control.client |
| 155 | + mock_pw_control.set_mode = pw_control.set_mode |
| 156 | + mock_pw_control.set_reserve = pw_control.set_reserve |
| 157 | + mock_pw_control.set_operation = pw_control.set_operation |
| 158 | + mock_pw.tedapi = None |
| 159 | + |
| 160 | + self._post( |
| 161 | + "/control/mode", |
| 162 | + {"value": "backup", "level": "80", "token": SECRET}, |
| 163 | + ) |
| 164 | + |
| 165 | + pw_control.set_operation.assert_called_once_with(80, "backup") |
| 166 | + pw_control.set_mode.assert_not_called() |
| 167 | + pw_control.set_reserve.assert_not_called() |
| 168 | + self.handler.send_response.assert_called_with(HTTPStatus.OK) |
| 169 | + self.assertEqual(json.loads(self._response_body()), {"set_operation": "ok"}) |
| 170 | + |
| 171 | + @common_patches |
| 172 | + @patch("proxy.server.control_secret", SECRET) |
| 173 | + @patch("proxy.server.pw") |
| 174 | + @patch("proxy.server.pw_control") |
| 175 | + @patch("proxy.server.safe_pw_call", side_effect=_passthrough_safe_pw_call) |
| 176 | + def test_value_with_invalid_level_returns_error(self, _proxystats_lock, mock_safe, mock_pw_control, mock_pw): |
| 177 | + """value= + non-numeric level= -> 400 error, no Powerwall call (no silent fallback to set_mode).""" |
| 178 | + pw_control = self._make_pw_control() |
| 179 | + mock_pw_control.client = pw_control.client |
| 180 | + mock_pw_control.set_mode = pw_control.set_mode |
| 181 | + mock_pw_control.set_operation = pw_control.set_operation |
| 182 | + mock_pw.tedapi = None |
| 183 | + |
| 184 | + self._post( |
| 185 | + "/control/mode", |
| 186 | + {"value": "backup", "level": "not-a-number", "token": SECRET}, |
| 187 | + ) |
| 188 | + |
| 189 | + pw_control.set_mode.assert_not_called() |
| 190 | + pw_control.set_operation.assert_not_called() |
| 191 | + self.handler.send_response.assert_called_with(HTTPStatus.BAD_REQUEST) |
| 192 | + self.assertIn("error", json.loads(self._response_body())) |
| 193 | + |
| 194 | + |
| 195 | +if __name__ == "__main__": |
| 196 | + unittest.main() |
0 commit comments