|
| 1 | +"""Test the StepRenderConfiguration class.""" |
| 2 | +import logging |
| 3 | +import os |
| 4 | +from io import StringIO |
| 5 | +from unittest import mock |
| 6 | + |
| 7 | +from pi_portal.installation.templates import config_file, motion_templates |
| 8 | +from pi_portal.modules.configuration import state |
| 9 | +from ..step_configure_motion import StepConfigureMotion |
| 10 | + |
| 11 | + |
| 12 | +class TestStepRenderTemplates: |
| 13 | + """Test the StepRenderConfiguration class.""" |
| 14 | + |
| 15 | + def test__initialize__attrs( |
| 16 | + self, |
| 17 | + step_configure_motion_instance: StepConfigureMotion, |
| 18 | + ) -> None: |
| 19 | + assert isinstance(step_configure_motion_instance.log, logging.Logger) |
| 20 | + assert step_configure_motion_instance.templates == motion_templates |
| 21 | + |
| 22 | + def test__invoke__success__logging( |
| 23 | + self, |
| 24 | + step_configure_motion_instance: StepConfigureMotion, |
| 25 | + mocked_state: state.State, |
| 26 | + mocked_stream: StringIO, |
| 27 | + mocked_system: mock.Mock, |
| 28 | + ) -> None: |
| 29 | + mocked_system.return_value = 0 |
| 30 | + camera_config = mocked_state.user_config["MOTION"]["CAMERAS"] |
| 31 | + expected_log_messages = "" |
| 32 | + for camera in camera_config: |
| 33 | + expected_log_messages += \ |
| 34 | + f"test - INFO - Creating template for '{camera['DEVICE']}' ...\n" |
| 35 | + |
| 36 | + step_configure_motion_instance.invoke() |
| 37 | + |
| 38 | + assert mocked_stream.getvalue() == ( |
| 39 | + "test - INFO - Rendering motion configuration ...\n" + |
| 40 | + expected_log_messages + |
| 41 | + "test - INFO - Done rendering motion configuration.\n" |
| 42 | + ) |
| 43 | + |
| 44 | + def test__invoke__render_call( |
| 45 | + self, |
| 46 | + step_configure_motion_instance: StepConfigureMotion, |
| 47 | + mocked_template_render: mock.Mock, |
| 48 | + ) -> None: |
| 49 | + step_configure_motion_instance.invoke() |
| 50 | + |
| 51 | + mocked_template_render.assert_called_once_with() |
| 52 | + |
| 53 | + def test__generate_camera_templates__updates_templates( |
| 54 | + self, |
| 55 | + step_configure_motion_instance: StepConfigureMotion, |
| 56 | + mocked_state: state.State, |
| 57 | + ) -> None: |
| 58 | + camera_config = mocked_state.user_config["MOTION"]["CAMERAS"] |
| 59 | + motion_template_count = len(motion_templates) |
| 60 | + assert step_configure_motion_instance.templates == \ |
| 61 | + motion_templates |
| 62 | + |
| 63 | + step_configure_motion_instance.generate_camera_templates() |
| 64 | + |
| 65 | + assert len(step_configure_motion_instance.templates) == \ |
| 66 | + motion_template_count + len(camera_config) |
| 67 | + |
| 68 | + def test__generate_camera_templates__creates_valid_templates( |
| 69 | + self, |
| 70 | + step_configure_motion_instance: StepConfigureMotion, |
| 71 | + mocked_state: state.State, |
| 72 | + ) -> None: |
| 73 | + camera_config = mocked_state.user_config["MOTION"]["CAMERAS"] |
| 74 | + motion_template_count = len(motion_templates) |
| 75 | + |
| 76 | + step_configure_motion_instance.generate_camera_templates() |
| 77 | + |
| 78 | + camera_templates = step_configure_motion_instance.\ |
| 79 | + templates[motion_template_count:] |
| 80 | + for index, template in enumerate(camera_templates): |
| 81 | + assert template.source == os.path.join( |
| 82 | + os.path.dirname(config_file.__file__), |
| 83 | + "motion/camera.conf", |
| 84 | + ) |
| 85 | + assert template.destination == f'/etc/motion/camera{index}.conf' |
| 86 | + assert template.permissions == "600" |
| 87 | + assert template.user == "root" |
| 88 | + assert template.context["CAMERA"] == camera_config[index] |
| 89 | + assert template.context["CAMERA"]["NAME"] == f"CAMERA-{index}" |
| 90 | + assert template.context["CAMERA"]["ID"] == index |
0 commit comments