Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/redfetch/sync_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ def _root_sources_for_full_sync(
spec.sources.add("watching")
spec.payload = payload

license_validity: dict[str, bool] = {}

for license_info in licenses:
if not license_info.get("active", False):
continue
Expand All @@ -171,9 +173,18 @@ def _root_sources_for_full_sync(
resource_id = str(payload["resource_id"])
spec = specs.setdefault(resource_id, _RootSpec())
spec.sources.add("licensed")
if is_expired:
had_valid_license = license_validity.get(resource_id, False)
has_valid_license = not is_expired
license_validity[resource_id] = had_valid_license or has_valid_license

# Prefer payload from a valid license when duplicate license rows exist.
if spec.payload is None or (has_valid_license and not had_valid_license):
spec.payload = payload

if license_validity[resource_id]:
spec.discovery_block = None
else:
spec.discovery_block = "license_expired"
spec.payload = payload

settings_for_env = config.settings.from_env(settings_env)
for resource_id, resource_info in settings_for_env.SPECIAL_RESOURCES.items():
Expand Down
16 changes: 16 additions & 0 deletions tests/test_licensed_resources_filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,22 @@ def test_unlimited_license_has_no_discovery_block():
assert target.discovery_block is None


def test_current_license_overrides_expired_duplicate_for_same_resource():
desired_set = asyncio.run(
_discover_from_licenses(
[
make_license(9998, 8, title="Expired Copy", end_date=PAST),
make_license(9998, 8, title="Current Copy", end_date=FAR_FUTURE),
],
"LIVE",
)
)
target = desired_set.install_targets["/9998/"]
assert target.sources == {"licensed"}
assert target.discovery_block is None
assert target.title == "Current Copy"


# --- expired license planner tests ---


Expand Down