File: test_parental_control.py

package info (click to toggle)
python-asusrouter 1.21.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,856 kB
  • sloc: python: 20,497; makefile: 3
file content (93 lines) | stat: -rw-r--r-- 2,486 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""Tests for the parental control module."""

from unittest import mock
from unittest.mock import AsyncMock

import pytest

from asusrouter.modules.parental_control import (
    KEY_PC_BLOCK_ALL,
    KEY_PC_STATE,
    AsusBlockAll,
    AsusParentalControl,
    ParentalControlRule,
    PCRuleType,
    set_state,
)

async_callback = AsyncMock()


@pytest.mark.asyncio
@pytest.mark.parametrize(
    (
        "state",
        "expect_modify",
        "expect_call",
        "expected_args",
        "expect_call_rule",
    ),
    [
        # Correct states
        (AsusParentalControl.ON, True, True, {KEY_PC_STATE: 1}, False),
        (AsusParentalControl.OFF, False, True, {KEY_PC_STATE: 0}, False),
        (AsusBlockAll.ON, True, True, {KEY_PC_BLOCK_ALL: 1}, False),
        (AsusBlockAll.OFF, False, True, {KEY_PC_BLOCK_ALL: 0}, False),
        # ParentalControlRule
        (
            ParentalControlRule(
                mac="00:00:00:00:00:00",
                name="test",
                timemap="",
                type=PCRuleType.BLOCK,
            ),
            False,
            False,
            {},
            True,
        ),
        # Wrong states
        (AsusParentalControl.UNKNOWN, False, False, {}, False),
        (None, False, False, {}, False),
    ],
)
async def test_set_state(
    state: AsusParentalControl | AsusBlockAll | ParentalControlRule | None,
    expect_modify: bool,
    expect_call: bool,
    expected_args: dict[str, int],
    expect_call_rule: bool,
) -> None:
    """Test set_state."""

    with mock.patch(
        "asusrouter.modules.parental_control.set_rule"
    ) as mock_set_rule:
        # Call the set_state function
        await set_state(
            callback=async_callback, state=state, expect_modify=expect_modify
        )

        # Check if the mock function was called
        if expect_call_rule:
            mock_set_rule.assert_called_once_with(
                async_callback,
                state,
                expect_modify=expect_modify,
            )
        else:
            mock_set_rule.assert_not_called()

    # Check if the callback function was called
    if expect_call:
        async_callback.assert_called_once_with(
            service="restart_firewall",
            arguments=expected_args,
            apply=True,
            expect_modify=expect_modify,
        )
    else:
        async_callback.assert_not_called()

    # Reset the mock callback function
    async_callback.reset_mock()