File size: 6,633 Bytes
851e5ba | 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 | """Feature flag evaluation, platform detection and caching (no database)."""
from contextlib import asynccontextmanager
from types import SimpleNamespace
import pytest
from starlette.requests import Request
from src import feature_flags
from src.config import Config, set_config_override
from src.feature_flags import FeatureFlag, client_platform, evaluate
from src.workflows.contracts import WORKFLOWS
from src.workflows.options import allowed_workflows, workflow_allowed_for
def request_with(headers: dict[str, str]) -> Request:
return Request(
{"type": "http", "headers": [(k.lower().encode(), v.encode()) for k, v in headers.items()]}
)
class FakeDb:
"""Answers the flags query and the is_admin lookup; counts both."""
def __init__(self, flags=None, admins=(), fail=False):
self.flags = flags or []
self.admins = set(admins)
self.fail = fail
self.flag_reads = 0
self.user_reads = 0
@asynccontextmanager
async def begin_nested(self):
yield
async def execute(self, statement, params=None):
sql = str(statement)
if "FROM feature_flags" in sql:
self.flag_reads += 1
if self.fail:
raise RuntimeError('relation "feature_flags" does not exist')
return [SimpleNamespace(**row) for row in self.flags]
if "FROM users" in sql:
self.user_reads += 1
user_id = params["user_id"]
row = {"is_admin": user_id in self.admins, "email": None, "email_verified": False}
class _Result:
def fetchone(self_inner):
return SimpleNamespace(**row)
return _Result()
raise AssertionError(sql)
def workflows_row(web=False, ios=False, bypass=True):
return {"key": "workflows", "web_enabled": web, "ios_enabled": ios, "admins_bypass": bypass, "description": ""}
@pytest.fixture(autouse=True)
def fresh_cache():
feature_flags.clear_cache()
yield
feature_flags.clear_cache()
@pytest.fixture
def workflows_for_all():
config = Config()
config.workflows_access = "all"
set_config_override(config)
yield config
set_config_override(None)
@pytest.mark.parametrize(
("headers", "expected"),
[
({}, "web"),
({"x-supoclip-client": "ios"}, "ios"),
({"X-SupoClip-Client": " iOS "}, "ios"),
({"x-supoclip-client": "web"}, "web"),
({"x-supoclip-client": "android"}, "web"),
({"authorization": "Bearer sk_test"}, "web"),
],
)
def test_platform_comes_from_the_client_header(headers, expected):
assert client_platform(request_with(headers)) == expected
def test_no_request_counts_as_web():
assert client_platform(None) == "web"
@pytest.mark.parametrize(
("flag", "platform", "is_admin", "expected"),
[
(FeatureFlag("x", web_enabled=True), "web", False, True),
(FeatureFlag("x", web_enabled=True), "ios", False, False),
(FeatureFlag("x", ios_enabled=True), "ios", False, True),
(FeatureFlag("x"), "web", True, True),
(FeatureFlag("x", admins_bypass=False), "web", True, False),
(FeatureFlag("x"), "ios", False, False),
],
)
def test_evaluate(flag, platform, is_admin, expected):
assert evaluate(flag, platform, is_admin) is expected
def test_unknown_flags_default_to_closed_with_the_admin_bypass():
assert feature_flags.DEFAULT_FLAGS["workflows"] == FeatureFlag("workflows", False, False, True)
@pytest.mark.asyncio
async def test_on_platform_skips_the_admin_lookup():
db = FakeDb([workflows_row(web=True)])
assert await feature_flags.is_enabled(db, "workflows", platform="web", user_id="u") is True
assert db.user_reads == 0
@pytest.mark.asyncio
async def test_off_platform_lets_admins_through():
db = FakeDb([workflows_row()], admins={"boss"})
assert await feature_flags.is_enabled(db, "workflows", platform="web", user_id="boss") is True
assert await feature_flags.is_enabled(db, "workflows", platform="ios", user_id="someone") is False
assert await feature_flags.is_enabled(db, "workflows", platform="web", user_id=None) is False
@pytest.mark.asyncio
async def test_flags_are_cached():
db = FakeDb([workflows_row(web=True)])
for _ in range(5):
await feature_flags.is_enabled(db, "workflows", platform="web")
assert db.flag_reads == 1
feature_flags._cache = (feature_flags._cache[0] - feature_flags.CACHE_TTL_SECONDS - 1, feature_flags._cache[1])
await feature_flags.is_enabled(db, "workflows", platform="web")
assert db.flag_reads == 2
@pytest.mark.asyncio
async def test_missing_row_or_unreadable_table_uses_the_defaults():
assert await feature_flags.is_enabled(FakeDb([]), "workflows", platform="web", user_id="u") is False
feature_flags.clear_cache()
db = FakeDb(fail=True, admins={"boss"})
assert await feature_flags.is_enabled(db, "workflows", platform="web", user_id="u") is False
assert await feature_flags.is_enabled(db, "workflows", platform="web", user_id="boss") is True
@pytest.mark.asyncio
async def test_evaluated_flags_looks_up_the_user_once():
db = FakeDb(
[workflows_row(), {"key": "other", "web_enabled": False, "ios_enabled": False, "admins_bypass": True, "description": ""}],
admins={"boss"},
)
assert await feature_flags.evaluated_flags(db, platform="web", user_id="boss") == {"other": True, "tiktok_publishing": False, "workflows": True}
assert db.user_reads == 1
@pytest.mark.asyncio
async def test_workflow_gate_needs_the_flag_and_the_env_gate(workflows_for_all):
db = FakeDb([workflows_row(web=False, ios=True)])
assert await workflow_allowed_for(db, "u", "series", platform="web") is False
assert await allowed_workflows(db, "u", platform="web") == frozenset()
assert await workflow_allowed_for(db, "u", "series", platform="ios") is True
assert await allowed_workflows(db, "u", platform="ios") == frozenset(WORKFLOWS)
workflows_for_all.workflows_access = "off"
assert await workflow_allowed_for(db, "u", "series", platform="ios") is False
assert await allowed_workflows(db, "u", platform="ios") == frozenset()
@pytest.mark.asyncio
async def test_public_workflows_also_wait_for_the_flag(workflows_for_all):
workflows_for_all.workflows_access = "admins"
workflows_for_all.workflows_public = frozenset({"recap"})
db = FakeDb([workflows_row(web=False, ios=True)])
assert await allowed_workflows(db, "u", platform="web") == frozenset()
assert await allowed_workflows(db, "u", platform="ios") == frozenset({"recap"})
|