-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_oon_policy.py
More file actions
300 lines (254 loc) · 11.1 KB
/
Copy pathtest_oon_policy.py
File metadata and controls
300 lines (254 loc) · 11.1 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
"""Tests for OON policy support (model, API mixin, switch, mappings)."""
from unittest.mock import AsyncMock, Mock
import pytest
from custom_components.unifi_network_rules.models.oon_policy import OONPolicy
from custom_components.unifi_network_rules.udm.oon import OONMixin
from custom_components.unifi_network_rules.unified_change_detector import UnifiedChangeDetector
from custom_components.unifi_network_rules.unified_trigger import VALID_CHANGE_TYPES
# Note: Switch imports commented out - require Home Assistant to be installed
# from custom_components.unifi_network_rules.switches.oon_policy import (
# UnifiOONPolicySwitch,
# UnifiOONPolicyKillSwitch,
# )
@pytest.fixture
def oon_policy_payload():
"""Sample OON policy payload."""
return {
"_id": "69134f9a2046c56ca159e502",
"name": "Test OON Policy",
"enabled": True,
"target_type": "CLIENTS",
"targets": ["68b6eef7dd411xxxxxxxxx"],
"qos": {
"enabled": True,
"bandwidth_limit": 1000000,
},
"route": {
"enabled": True,
"kill_switch": False,
},
"secure": {
"enabled": False,
},
}
@pytest.fixture
def oon_policy_with_kill_switch():
"""OON policy with kill switch enabled."""
return {
"_id": "69134f9a2046c56ca159e503",
"name": "VPN Route Policy",
"enabled": True,
"target_type": "CLIENTS",
"targets": ["68b6eef7dd411xxxxxxxxx"],
"qos": {
"enabled": False,
},
"route": {
"enabled": True,
"kill_switch": True,
},
"secure": {
"enabled": False,
},
}
class TestOONPolicyModel:
"""Test OONPolicy model creation and properties."""
def test_oon_policy_defaults_and_properties(self, oon_policy_payload):
"""Test OONPolicy model creation and basic properties."""
policy = OONPolicy(oon_policy_payload)
assert policy.id == oon_policy_payload["_id"]
assert policy.name == "Test OON Policy"
assert policy.enabled is True
assert policy.target_type == "CLIENTS"
assert len(policy.targets) == 1
assert policy.qos["enabled"] is True
assert policy.route["enabled"] is True
def test_oon_policy_minimal_data(self):
"""Test OONPolicy with minimal required data."""
minimal_data = {
"_id": "test123",
"name": "Minimal Policy",
}
policy = OONPolicy(minimal_data)
assert policy.id == "test123"
assert policy.name == "Minimal Policy"
assert policy.enabled is False # Default
assert policy.target_type == "CLIENTS" # Default
assert policy.targets == [] # Default
assert policy.qos == {} # Default
assert policy.route == {} # Default
def test_has_kill_switch_with_kill_switch(self, oon_policy_with_kill_switch):
"""Test has_kill_switch() returns True when route enabled and kill_switch is boolean."""
policy = OONPolicy(oon_policy_with_kill_switch)
assert policy.has_kill_switch() is True
def test_has_kill_switch_with_route_enabled(self, oon_policy_payload):
"""Test has_kill_switch() returns True when route enabled and kill_switch exists (even if False)."""
policy = OONPolicy(oon_policy_payload)
# kill_switch is False (boolean), but route.enabled is True, so should return True
# The method checks if kill_switch exists as a boolean, not its value
assert policy.has_kill_switch() is True
def test_has_kill_switch_route_disabled(self):
"""Test has_kill_switch() returns False when route is disabled."""
data = {
"_id": "test123",
"name": "No Route Policy",
"route": {
"enabled": False,
"kill_switch": True,
},
}
policy = OONPolicy(data)
assert policy.has_kill_switch() is False
def test_has_kill_switch_no_route(self):
"""Test has_kill_switch() returns False when route is missing."""
data = {
"_id": "test123",
"name": "No Route Policy",
}
policy = OONPolicy(data)
assert policy.has_kill_switch() is False
def test_to_api_dict(self, oon_policy_payload):
"""Test to_api_dict() returns correct dictionary."""
policy = OONPolicy(oon_policy_payload)
api_dict = policy.to_api_dict()
assert isinstance(api_dict, dict)
assert api_dict["_id"] == oon_policy_payload["_id"]
assert api_dict["name"] == oon_policy_payload["name"]
assert api_dict["enabled"] == oon_policy_payload["enabled"]
assert "qos" in api_dict
assert "route" in api_dict
class TestOONMixin:
"""Test OONMixin API methods."""
@pytest.mark.asyncio
async def test_get_oon_policies_success(self):
"""Test get_oon_policies() with successful response."""
mixin = OONMixin()
mixin.controller = AsyncMock()
# Mock create_api_request to return a request dict
mixin.create_api_request = Mock(return_value={"method": "GET", "path": "/test"})
response_data = {
"data": [
{"_id": "test1", "name": "Policy 1", "enabled": True},
{"_id": "test2", "name": "Policy 2", "enabled": False},
]
}
mixin.controller.request = AsyncMock(return_value=response_data)
policies = await mixin.get_oon_policies()
assert len(policies) == 2
assert isinstance(policies[0], OONPolicy)
assert policies[0].id == "test1"
assert policies[1].id == "test2"
@pytest.mark.asyncio
async def test_get_oon_policies_404_handling(self):
"""Test get_oon_policies() gracefully handles 404 errors."""
mixin = OONMixin()
mixin.controller = AsyncMock()
mixin.create_api_request = Mock(return_value={"method": "GET", "path": "/test"})
mixin.controller.request = AsyncMock(side_effect=Exception("404 Not Found"))
policies = await mixin.get_oon_policies()
assert policies == []
assert isinstance(policies, list)
@pytest.mark.asyncio
async def test_update_oon_policy_success(self, oon_policy_payload):
"""Test update_oon_policy() with successful update."""
mixin = OONMixin()
mixin.controller = AsyncMock()
mixin.create_api_request = Mock(return_value={"method": "PUT", "path": "/test"})
mixin.controller.request = AsyncMock(return_value={})
policy = OONPolicy(oon_policy_payload)
result = await mixin.update_oon_policy(policy)
assert result is True
@pytest.mark.asyncio
async def test_toggle_oon_policy_success(self, oon_policy_payload):
"""Test toggle_oon_policy() sets enabled state."""
mixin = OONMixin()
mixin.controller = AsyncMock()
mixin.create_api_request = Mock(return_value={"method": "PUT", "path": "/test"})
mixin.controller.request = AsyncMock(return_value={})
policy = OONPolicy(oon_policy_payload)
original_state = policy.enabled
target_state = not original_state
result = await mixin.toggle_oon_policy(policy, target_state)
assert result is True
# Verify that the API call was made with the target state
assert mixin.controller.request.await_count == 1
@pytest.mark.asyncio
async def test_add_oon_policy_success(self):
"""Test add_oon_policy() creates new policy."""
mixin = OONMixin()
mixin.controller = AsyncMock()
mixin.create_api_request = Mock(return_value={"method": "POST", "path": "/test"})
# API may return data as dict or list
response_data = {
"data": {
"_id": "new123",
"name": "New Policy",
"enabled": True,
}
}
mixin.controller.request = AsyncMock(return_value=response_data)
policy_data = {"name": "New Policy", "enabled": True}
policy = await mixin.add_oon_policy(policy_data)
assert policy is not None
assert isinstance(policy, OONPolicy)
assert policy.id == "new123"
@pytest.mark.asyncio
async def test_add_oon_policy_with_list_response(self):
"""Test add_oon_policy() handles list response format."""
mixin = OONMixin()
mixin.controller = AsyncMock()
mixin.create_api_request = Mock(return_value={"method": "POST", "path": "/test"})
# Some APIs return data as a list
response_data = {
"data": [
{
"_id": "new456",
"name": "New Policy List",
"enabled": True,
}
]
}
mixin.controller.request = AsyncMock(return_value=response_data)
policy_data = {"name": "New Policy List", "enabled": True}
policy = await mixin.add_oon_policy(policy_data)
assert policy is not None
assert isinstance(policy, OONPolicy)
assert policy.id == "new456"
@pytest.mark.asyncio
async def test_remove_oon_policy_success(self):
"""Test remove_oon_policy() deletes policy."""
mixin = OONMixin()
mixin.controller = AsyncMock()
mixin.create_api_request = Mock(return_value={"method": "DELETE", "path": "/test"})
mixin.controller.request = AsyncMock(return_value=None) # 204 No Content
result = await mixin.remove_oon_policy("test123")
assert result is True
class TestOONPolicyIntegration:
"""Test OON policy integration with change detection and triggers."""
def test_oon_policy_in_valid_change_types(self):
"""Test that oon_policy is in VALID_CHANGE_TYPES."""
assert "oon_policy" in VALID_CHANGE_TYPES
def test_change_detector_mapping(self):
"""Test that unified change detector maps oon_policies correctly."""
detector = UnifiedChangeDetector(Mock(), Mock())
assert "oon_policies" in detector._rule_type_mapping
assert detector._rule_type_mapping["oon_policies"] == "oon_policy"
def test_deletion_check_does_not_orphan_oon_policies(self, oon_policy_payload):
"""Regression for #154: OON policies present in current data must not
be flagged as deletions every cycle. The deletion-check iteration list
previously omitted "oon_policies", causing every OON ID in
known_unique_ids to be treated as a ghost deletion on every poll."""
from custom_components.unifi_network_rules.coordination.entity_manager import (
CoordinatorEntityManager,
)
from custom_components.unifi_network_rules.helpers.rule import get_rule_id
policy = OONPolicy(oon_policy_payload)
oon_unique_id = get_rule_id(policy)
assert oon_unique_id and oon_unique_id.startswith("unr_oon_")
coordinator = Mock()
coordinator._initial_update_done = True
coordinator.known_unique_ids = {oon_unique_id}
manager = CoordinatorEntityManager(Mock(), coordinator)
manager._process_deleted_rules = Mock()
manager.check_for_deleted_rules({"oon_policies": [policy]})
manager._process_deleted_rules.assert_not_called()