|
1 | 1 | """Test utilities for creating namespaced keys using class constants.""" |
2 | 2 |
|
| 3 | +from types import SimpleNamespace |
| 4 | +from unittest.mock import MagicMock, patch |
| 5 | + |
| 6 | +from ddt import data, ddt, unpack |
| 7 | +from django.test import TestCase |
| 8 | + |
3 | 9 | from openedx_authz.api.data import ( |
4 | 10 | GLOBAL_SCOPE_WILDCARD, |
5 | 11 | ActionData, |
|
9 | 15 | ScopeData, |
10 | 16 | UserData, |
11 | 17 | ) |
| 18 | +from openedx_authz.utils import get_waffle_flag_states |
| 19 | + |
| 20 | +FLAG_NAME = "authz.enable_course_authoring" |
12 | 21 |
|
13 | 22 |
|
14 | 23 | def make_policy(role_key: str, action_key: str, scope_key: str, effect: str = "allow") -> list[str]: |
@@ -187,3 +196,129 @@ def make_wildcard_key(namespace: str) -> str: |
187 | 196 | str: Wildcard pattern (e.g., 'lib^*', 'org^*', 'course^*') |
188 | 197 | """ |
189 | 198 | return f"{namespace}{ScopeData.SEPARATOR}{GLOBAL_SCOPE_WILDCARD}" |
| 199 | + |
| 200 | + |
| 201 | +@ddt |
| 202 | +class TestGetWaffleFlagStates(TestCase): |
| 203 | + """Test get_waffle_flag_states, which reports the course-authoring flag's state at each tier.""" |
| 204 | + |
| 205 | + def _mock_override_model(self, override_rows: list): |
| 206 | + """Build a mock override model. override_rows is a list of (key, override_choice) tuples.""" |
| 207 | + mock_model = MagicMock() |
| 208 | + mock_model.objects.current_set.return_value.filter.return_value.values_list.return_value = override_rows |
| 209 | + return mock_model |
| 210 | + |
| 211 | + @data(True, False) |
| 212 | + def test_global_tier_follows_the_platform_flag(self, platform_enabled: bool): |
| 213 | + """Test get_waffle_flag_states' global key. |
| 214 | +
|
| 215 | + Expected result: |
| 216 | + - Matches global waffle flag result. |
| 217 | + """ |
| 218 | + with patch( |
| 219 | + "openedx_authz.utils.Flag", |
| 220 | + MagicMock(objects=MagicMock( |
| 221 | + filter=MagicMock(return_value=MagicMock(first=MagicMock( |
| 222 | + return_value=SimpleNamespace(everyone=platform_enabled) |
| 223 | + ))) |
| 224 | + )), |
| 225 | + ), patch( |
| 226 | + "openedx_authz.utils.AUTHZ_COURSE_AUTHORING_FLAG", SimpleNamespace(name=FLAG_NAME) |
| 227 | + ), patch( |
| 228 | + "openedx_authz.utils.WaffleFlagOrgOverrideModel", self._mock_override_model([]) |
| 229 | + ), patch( |
| 230 | + "openedx_authz.utils.WaffleFlagCourseOverrideModel", self._mock_override_model([]) |
| 231 | + ): |
| 232 | + self.assertEqual(get_waffle_flag_states()["global"], platform_enabled) |
| 233 | + |
| 234 | + @data( |
| 235 | + ([("Org1", "on")], {"on": ["Org1"], "off": []}), |
| 236 | + ([("Org1", "off")], {"on": [], "off": ["Org1"]}), |
| 237 | + ([("Org1", "on"), ("Org2", "off")], {"on": ["Org1"], "off": ["Org2"]}), |
| 238 | + ([], {"on": [], "off": []}), |
| 239 | + ) |
| 240 | + @unpack |
| 241 | + def test_org_tier_splits_active_overrides_by_choice(self, override_rows: list, expected: dict): |
| 242 | + """Test get_waffle_flag_states' org key. |
| 243 | +
|
| 244 | + Expected result: |
| 245 | + - Orgs with an enabled override are split into 'on' and 'off' lists, |
| 246 | + by the override's choice. |
| 247 | + """ |
| 248 | + with patch( |
| 249 | + "openedx_authz.utils.Flag", |
| 250 | + MagicMock(objects=MagicMock( |
| 251 | + filter=MagicMock(return_value=MagicMock(first=MagicMock( |
| 252 | + return_value=SimpleNamespace(everyone=False) |
| 253 | + ))) |
| 254 | + )), |
| 255 | + ), patch( |
| 256 | + "openedx_authz.utils.AUTHZ_COURSE_AUTHORING_FLAG", SimpleNamespace(name=FLAG_NAME) |
| 257 | + ), patch( |
| 258 | + "openedx_authz.utils.WaffleFlagOrgOverrideModel", self._mock_override_model(override_rows) |
| 259 | + ), patch( |
| 260 | + "openedx_authz.utils.WaffleFlagCourseOverrideModel", self._mock_override_model([]) |
| 261 | + ): |
| 262 | + self.assertEqual(get_waffle_flag_states()["org_overrides"], expected) |
| 263 | + |
| 264 | + @data( |
| 265 | + ([("course-v1:Org1+COURSE1+2024", "on")], {"on": ["course-v1:Org1+COURSE1+2024"], "off": []}), |
| 266 | + ([("course-v1:Org1+COURSE1+2024", "off")], {"on": [], "off": ["course-v1:Org1+COURSE1+2024"]}), |
| 267 | + ( |
| 268 | + [("course-v1:Org1+COURSE1+2024", "on"), ("course-v1:Org1+COURSE2+2024", "off")], |
| 269 | + {"on": ["course-v1:Org1+COURSE1+2024"], "off": ["course-v1:Org1+COURSE2+2024"]}, |
| 270 | + ), |
| 271 | + ([], {"on": [], "off": []}), |
| 272 | + ) |
| 273 | + @unpack |
| 274 | + def test_course_tier_splits_active_overrides_by_choice(self, override_rows: list, expected: dict): |
| 275 | + """Test get_waffle_flag_states' course key. |
| 276 | +
|
| 277 | + Expected result: |
| 278 | + - Courses with an enabled override are split into 'on' and 'off' lists, |
| 279 | + by the override's choice. Course keys are stringified. |
| 280 | + """ |
| 281 | + with patch( |
| 282 | + "openedx_authz.utils.Flag", |
| 283 | + MagicMock(objects=MagicMock( |
| 284 | + filter=MagicMock(return_value=MagicMock(first=MagicMock( |
| 285 | + return_value=SimpleNamespace(everyone=False) |
| 286 | + ))) |
| 287 | + )), |
| 288 | + ), patch( |
| 289 | + "openedx_authz.utils.AUTHZ_COURSE_AUTHORING_FLAG", SimpleNamespace(name=FLAG_NAME) |
| 290 | + ), patch( |
| 291 | + "openedx_authz.utils.WaffleFlagOrgOverrideModel", self._mock_override_model([]) |
| 292 | + ), patch( |
| 293 | + "openedx_authz.utils.WaffleFlagCourseOverrideModel", self._mock_override_model(override_rows) |
| 294 | + ): |
| 295 | + self.assertEqual(get_waffle_flag_states()["course_overrides"], expected) |
| 296 | + |
| 297 | + def test_all_three_tiers_are_independent(self): |
| 298 | + """Test get_waffle_flag_states with each tier in a different state. |
| 299 | +
|
| 300 | + Expected result: |
| 301 | + - Each key reflects only its own tier, not a blend of the others. |
| 302 | + """ |
| 303 | + with patch( |
| 304 | + "openedx_authz.utils.Flag", |
| 305 | + MagicMock(objects=MagicMock( |
| 306 | + filter=MagicMock(return_value=MagicMock(first=MagicMock( |
| 307 | + return_value=SimpleNamespace(everyone=False) |
| 308 | + ))) |
| 309 | + )), |
| 310 | + ), patch( |
| 311 | + "openedx_authz.utils.AUTHZ_COURSE_AUTHORING_FLAG", SimpleNamespace(name=FLAG_NAME) |
| 312 | + ), patch( |
| 313 | + "openedx_authz.utils.WaffleFlagOrgOverrideModel", self._mock_override_model([("Org1", "on")]) |
| 314 | + ), patch( |
| 315 | + "openedx_authz.utils.WaffleFlagCourseOverrideModel", self._mock_override_model([]) |
| 316 | + ): |
| 317 | + self.assertEqual( |
| 318 | + get_waffle_flag_states(), |
| 319 | + { |
| 320 | + "global": False, |
| 321 | + "org_overrides": {"on": ["Org1"], "off": []}, |
| 322 | + "course_overrides": {"on": [], "off": []}, |
| 323 | + }, |
| 324 | + ) |
0 commit comments