"""A server-CDM licence that gives a track no content key stops the run at the licence step, before any segment.""" from types import SimpleNamespace from uuid import UUID import click import pytest from tests.core.key_resolver import MemoryVault, key_table, resolver, vaults_of from tests.core.test_all_drm import widevine from tests.remote.key_source import MemoryKeySource from unshackle.core.constants import DOWNLOAD_CANCELLED from unshackle.core.manifests.segment_pipeline import license_drm from unshackle.core.remote_protocol import APIErrorCode from unshackle.core.tracks.track import DownloadContext, Track pytestmark = pytest.mark.unit KID = UUID("161219ec3df64e0eadbb3827c0ccc46d") NO_KEY = "^The server sent no Widevine content key for track vid$" @pytest.fixture(autouse=True) def no_cancel(): DOWNLOAD_CANCELLED.clear() yield DOWNLOAD_CANCELLED.clear() def keyless_track(): drm = widevine(KID) track = SimpleNamespace(id="vid", drm=[drm], descriptor=Track.Descriptor.DASH) callback = resolver(vaults_of(MemoryVault()), source=MemoryKeySource()).licence_for(track, None, key_table()) return track, drm, callback @pytest.mark.parametrize("track_kid", [None, KID], ids=["no-track-kid", "track-kid"]) def test_a_server_answer_with_no_key_is_a_drm_error(track_kid): track, drm, callback = keyless_track() with pytest.raises(click.ClickException, match=NO_KEY) as raised: callback(drm, track_kid=track_kid) assert raised.value.error_code == APIErrorCode.DRM_ERROR # type: ignore[attr-defined] def test_a_server_answer_with_no_key_cancels_the_other_tracks(tmp_path): track, drm, callback = keyless_track() ctx = DownloadContext( save_path=tmp_path / "vid.mp4", save_dir=tmp_path / "seg", progress=lambda **kw: None, license_widevine=callback ) with pytest.raises(click.ClickException, match=NO_KEY): license_drm(track, drm, ctx, KID) # type: ignore[arg-type] assert DOWNLOAD_CANCELLED.is_set()