mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-11-30 21:15:18 +00:00
150 lines
4.7 KiB
Python
150 lines
4.7 KiB
Python
"""The licence cap module: the setting, the track measure, and the request and track questions."""
|
|
|
|
import pytest
|
|
|
|
from unshackle.core.api import licence_cap
|
|
from unshackle.core.api.errors import APIError
|
|
from unshackle.core.remote_protocol import APIErrorCode
|
|
from unshackle.core.tracks import Audio, Video
|
|
from unshackle.core.tracks.tracks import at_resolution
|
|
|
|
pytestmark = pytest.mark.unit
|
|
|
|
CAP_DETAILS = {"reason": "server_cdm_max_height", "max_height": 1080}
|
|
|
|
|
|
def video(height, width=None):
|
|
track = Video.__new__(Video)
|
|
track.height = height
|
|
track.width = width
|
|
return track
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("setting", "service", "cap"),
|
|
[
|
|
(1080, "EXAMPLE1", 1080),
|
|
({"example1": 720, "default": 1080}, "EXAMPLE1", 720),
|
|
({"example1": 720, "default": 1080}, "EXAMPLE2", 1080),
|
|
({"example1": 720, "default": 1080}, None, 1080),
|
|
({"EXAMPLE1": 1080}, "EXAMPLE2", None),
|
|
({"EXAMPLE1": None, "default": 1080}, "EXAMPLE1", 1080),
|
|
(None, "EXAMPLE1", None),
|
|
(True, "EXAMPLE1", None),
|
|
],
|
|
)
|
|
def test_the_cap_per_service(setting, service, cap):
|
|
assert licence_cap.cap_for(setting, service) == cap
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("value", "problem"),
|
|
[
|
|
("1080", "u.server_cdm_max_height must be"),
|
|
(True, "u.server_cdm_max_height must be"),
|
|
({"EXAMPLE": -1}, "u.server_cdm_max_height values"),
|
|
(None, None),
|
|
(1080, None),
|
|
({"EXAMPLE1": 720, "default": None}, None),
|
|
],
|
|
)
|
|
def test_the_setting_check(value, problem):
|
|
error = licence_cap.setting_error("u", value)
|
|
assert error is None if problem is None else problem in error
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("track", "refused"),
|
|
[
|
|
(video(2160), True),
|
|
(video(1080), False),
|
|
(video(None), False),
|
|
(Audio.__new__(Audio), False),
|
|
(video(1088, 2048), False),
|
|
(video(1600, 3840), True),
|
|
(video(None, 3840), True),
|
|
(video(1080, 2560), False),
|
|
(video(1080, 1920), False),
|
|
],
|
|
)
|
|
def test_a_track_above_the_cap_gets_no_live_licence(track, refused):
|
|
assert (licence_cap.track_refusal(1080, track) is not None) is refused
|
|
|
|
|
|
def test_no_cap_refuses_no_track():
|
|
assert licence_cap.track_refusal(None, video(4320)) is None
|
|
|
|
|
|
def test_the_track_refusal_gives_the_client_the_cap():
|
|
refusal = licence_cap.track_refusal(1080, video(2160))
|
|
assert refusal.error_code is APIErrorCode.SERVER_CDM_CAPPED
|
|
assert refusal.details == CAP_DETAILS
|
|
assert "2160p track" in refusal.message
|
|
|
|
|
|
@pytest.mark.parametrize("size", [(1088, 2048), (1188, 2112), (800, 1920), (1080, 2560), (1080, 1920), (None, 1920)])
|
|
def test_a_track_that_the_cap_quality_selects_gets_a_live_licence(size):
|
|
track = video(*size)
|
|
assert at_resolution(track, 1080)
|
|
assert licence_cap.track_refusal(1080, track) is None
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("cap", "quality", "has_device", "server_cdm"),
|
|
[
|
|
(None, [2160], False, True),
|
|
(1080, [1080], False, True),
|
|
(1080, [], False, True),
|
|
(1080, [2160, 1080], True, False),
|
|
(1080, ["2160p"], True, False),
|
|
],
|
|
)
|
|
def test_the_session_question(cap, quality, has_device, server_cdm):
|
|
assert licence_cap.session_fits(cap, "EXAMPLE1", quality, has_device) is server_cdm
|
|
|
|
|
|
def test_a_session_above_the_cap_without_a_device_is_refused():
|
|
with pytest.raises(APIError) as ei:
|
|
licence_cap.session_fits(1080, "EXAMPLE1", [2160], False)
|
|
assert ei.value.error_code is APIErrorCode.SERVER_CDM_CAPPED
|
|
assert ei.value.details == CAP_DETAILS
|
|
|
|
|
|
@pytest.mark.parametrize("quality", [["best"], [-1], [0], [1080, 0]])
|
|
@pytest.mark.parametrize("cap", [None, 1080])
|
|
def test_a_bad_quality_is_invalid_parameters(quality, cap):
|
|
with pytest.raises(APIError) as ei:
|
|
licence_cap.session_fits(cap, "EXAMPLE1", quality, True)
|
|
assert ei.value.error_code is APIErrorCode.INVALID_PARAMETERS
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"params",
|
|
[
|
|
{"quality": [2160]},
|
|
{"quality": []},
|
|
{},
|
|
{"quality": [1080], "best_available": True},
|
|
{"quality": [1080], "range": "HYBRID"},
|
|
{"quality": [1080], "range": ["SDR", "hybrid"]},
|
|
{"quality": [1080], "range": [Video.Range.HYBRID]},
|
|
],
|
|
)
|
|
def test_a_job_that_could_pass_the_cap_is_refused(params):
|
|
with pytest.raises(APIError) as ei:
|
|
licence_cap.check_job(1080, params)
|
|
assert ei.value.error_code is APIErrorCode.SERVER_CDM_CAPPED
|
|
assert ei.value.details == CAP_DETAILS
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("cap", "params"),
|
|
[
|
|
(1080, {"quality": [1080]}),
|
|
(1080, {"quality": [720, 1080], "range": ["HDR10"]}),
|
|
(None, {"best_available": True}),
|
|
],
|
|
)
|
|
def test_a_job_inside_the_cap_passes(cap, params):
|
|
licence_cap.check_job(cap, params)
|