"""Stream logic tests.""" import datetime from unittest.mock import MagicMock import pytest from freezegun import freeze_time from pytest_mock import MockerFixture from werkzeug.exceptions import NotFound from video.logic import stream as stream_logic from video.models.sql.classes import product_video @freeze_time(datetime.datetime.fromtimestamp(1000006666, datetime.UTC)) def test_get_hls_streaming_url(mocker: MockerFixture) -> None: """Test get hls streaming url.""" product_id = 1 job_id = 2 mock_get: MagicMock = mocker.patch.object( product_video, "get", return_value={"latest_pipeline_run_id": job_id} ) mocker.patch.object( stream_logic, "get_wowza_secure_access_token", return_value="TOKEN" ) result = stream_logic.get_hls_streaming_url(product_id) mock_get.assert_called_once_with(product_id) assert result == ( "https://qa-aws-wowza.qaorch.com/vods3/_definst_/amazons3/" "mp4:test-orcd-video-assets/streamable-preview/2/1.mp4/" "playlist.m3u8?" "wowzatokenendtime=1000007666&wowzatokenstarttime=1000006566&" "wowzatokenhash=TOKEN" ) @freeze_time(datetime.datetime.fromtimestamp(1000006666, datetime.UTC)) def test_get_hls_streaming_url_oa(mocker: MockerFixture) -> None: """Test get hls streaming url when the request is from OA.""" user_id = "oa:666" product_id = 1 job_id = 2 mock_get: MagicMock = mocker.patch.object( product_video, "get", return_value={"latest_pipeline_run_id": job_id} ) mocker.patch.object( stream_logic, "get_wowza_secure_access_token", return_value="TOKEN" ) result = stream_logic.get_hls_streaming_url(product_id, user_id=user_id) mock_get.assert_called_once_with(product_id) assert result == ( "https://qa-aws-wowza.qaorch.com/vods3oa/" "_definst_/amazons3/" "mp4:test-orcd-video-assets/streamable-preview/2/1.mp4" "/playlist.m3u8?" "wowzatokenendtime=1000007666&wowzatokenstarttime=1000006566&" "wowzatokenhash=TOKEN" ) def test_get_hls_streaming_url_product_error(mocker: MockerFixture) -> None: """Test get hls streaming url when the product call returns an error.""" product_id = 1 mock_get: MagicMock = mocker.patch.object(product_video, "get", return_value=None) with pytest.raises(NotFound): stream_logic.get_hls_streaming_url(product_id) mock_get.assert_called_once_with(product_id) def test_get_hls_streaming_url_job_not_found(mocker: MockerFixture) -> None: """Test get hls streaming url when the job is not found.""" product_id = 1 mock_get: MagicMock = mocker.patch.object(product_video, "get", return_value={}) with pytest.raises(NotFound): stream_logic.get_hls_streaming_url(product_id) mock_get.assert_called_once_with(product_id) def test_get_wowza_secure_access_token() -> None: """Test get wowza secure access token.""" url_prefix = "vod/_definst_/mp4:" bucket = "test-orcd-video-assets" key_path = "test.mp4" start_time = 1000006566 end_time = 1000007666 shared_secret = "abc123" result = stream_logic.get_wowza_secure_access_token( url_prefix, bucket, key_path, start_time, end_time, shared_secret ) assert result == "cP2qYjh5vd4m55Qf_82c5TtaZL_zFyS080-JYtfua9c="