|
| 1 | +"""Test for transcoding API""" |
| 2 | + |
| 3 | +import json |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import boto3 |
| 7 | +import pytest |
| 8 | +from mitol.transcoding.api import ( |
| 9 | + add_group_settings, |
| 10 | + get_destination_path, |
| 11 | + make_media_convert_job, |
| 12 | + media_convert_job, |
| 13 | +) |
| 14 | +from mitol.transcoding.constants import GroupSettings |
| 15 | +from mitol.transcoding.utils import FileConfig |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture |
| 19 | +def mock_file_config(): |
| 20 | + """Create a mock file config instance""" |
| 21 | + return FileConfig( |
| 22 | + "uploads/test-video.mp4", |
| 23 | + source_prefix="uploads/", |
| 24 | + source_bucket="source-bucket", |
| 25 | + destination_prefix="transcodes/", |
| 26 | + destination_bucket="destination-bucket", |
| 27 | + ) |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture |
| 31 | +def mock_job_dict(): |
| 32 | + """Create a mock MediaConvert job dictionary""" |
| 33 | + return { |
| 34 | + "UserMetadata": {"filter": "default"}, |
| 35 | + "Queue": "", |
| 36 | + "Role": "", |
| 37 | + "Settings": { |
| 38 | + "Inputs": [{"FileInput": ""}], |
| 39 | + "OutputGroups": [ |
| 40 | + { |
| 41 | + "OutputGroupSettings": { |
| 42 | + "Type": GroupSettings.HLS_GROUP_SETTINGS, |
| 43 | + GroupSettings.HLS_GROUP_SETTINGS_KEY: { |
| 44 | + "SegmentLength": 6, |
| 45 | + "AdditionalManifests": [{"ManifestNameModifier": ""}], |
| 46 | + }, |
| 47 | + "Destination": "", |
| 48 | + }, |
| 49 | + "Outputs": [ |
| 50 | + {"VideoDescription": {"CodecSettings": {"Codec": "H_264"}}} |
| 51 | + ], |
| 52 | + }, |
| 53 | + { |
| 54 | + "OutputGroupSettings": { |
| 55 | + "Type": GroupSettings.FILE_GROUP_SETTINGS, |
| 56 | + GroupSettings.FILE_GROUP_SETTINGS_KEY: {"Destination": ""}, |
| 57 | + }, |
| 58 | + "Outputs": [ |
| 59 | + { |
| 60 | + "VideoDescription": { |
| 61 | + "CodecSettings": {"Codec": "FRAME_CAPTURE"} |
| 62 | + }, |
| 63 | + "ContainerSettings": {"Container": "RAW"}, |
| 64 | + } |
| 65 | + ], |
| 66 | + }, |
| 67 | + { |
| 68 | + "OutputGroupSettings": { |
| 69 | + "Type": GroupSettings.FILE_GROUP_SETTINGS, |
| 70 | + GroupSettings.FILE_GROUP_SETTINGS_KEY: {"Destination": ""}, |
| 71 | + }, |
| 72 | + "Outputs": [{"ContainerSettings": {"Container": "MP4"}}], |
| 73 | + }, |
| 74 | + ], |
| 75 | + }, |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | +@pytest.mark.parametrize( |
| 80 | + ("source_prefix", "expected_destination"), |
| 81 | + [ |
| 82 | + ("uploads/", "transcodes/test-video"), |
| 83 | + ("", "transcodes/uploads/test-video"), |
| 84 | + ("other-prefix/", "uploads/test-video"), |
| 85 | + ], |
| 86 | +) |
| 87 | +def test_get_destination_path(mock_file_config, source_prefix, expected_destination): |
| 88 | + """Test get_destination_path with various source prefixes""" |
| 89 | + mock_file_config.video_source_key = "uploads/test-video.mp4" |
| 90 | + mock_file_config.source_prefix = source_prefix |
| 91 | + mock_file_config.destination_prefix = "transcodes/" |
| 92 | + |
| 93 | + destination_path = get_destination_path(mock_file_config) |
| 94 | + assert destination_path == expected_destination |
| 95 | + |
| 96 | + |
| 97 | +def test_add_group_settings_basic(mock_file_config, mock_job_dict): |
| 98 | + """Test add_group_settings with basic settings""" |
| 99 | + mock_file_config.destination = "transcodes/test-video" |
| 100 | + |
| 101 | + add_group_settings(mock_job_dict, mock_file_config) |
| 102 | + |
| 103 | + output_groups = mock_job_dict["Settings"]["OutputGroups"] |
| 104 | + assert len(output_groups) == 3 # noqa: PLR2004 |
| 105 | + |
| 106 | + hls_settings = output_groups[0]["OutputGroupSettings"][ |
| 107 | + GroupSettings.HLS_GROUP_SETTINGS_KEY |
| 108 | + ] |
| 109 | + assert hls_settings["SegmentLength"] == 10 # noqa: PLR2004 |
| 110 | + assert hls_settings["AdditionalManifests"][0]["ManifestNameModifier"] == "__index" |
| 111 | + |
| 112 | + assert output_groups[0]["OutputGroupSettings"][ |
| 113 | + GroupSettings.HLS_GROUP_SETTINGS_KEY |
| 114 | + ]["Destination"].startswith("s3://destination-bucket/transcodes/test-video") |
| 115 | + assert output_groups[1]["OutputGroupSettings"][ |
| 116 | + GroupSettings.FILE_GROUP_SETTINGS_KEY |
| 117 | + ]["Destination"].startswith("s3://test-bucket/transcodes/test-video") |
| 118 | + assert output_groups[2]["OutputGroupSettings"][ |
| 119 | + GroupSettings.FILE_GROUP_SETTINGS_KEY |
| 120 | + ]["Destination"].startswith("s3://destination-bucket/transcodes/test-video") |
| 121 | + |
| 122 | + |
| 123 | +def test_add_group_settings_with_filters(mock_file_config, mock_job_dict): |
| 124 | + """Test add_group_settings with filters""" |
| 125 | + mock_file_config.destination = "transcodes/test-video" |
| 126 | + mock_file_config.group_settings = {"exclude_mp4": True, "exclude_thumbnail": True} |
| 127 | + |
| 128 | + add_group_settings(mock_job_dict, mock_file_config) |
| 129 | + |
| 130 | + output_groups = mock_job_dict["Settings"]["OutputGroups"] |
| 131 | + assert len(output_groups) == 1 |
| 132 | + assert ( |
| 133 | + output_groups[0]["OutputGroupSettings"]["Type"] |
| 134 | + == GroupSettings.HLS_GROUP_SETTINGS |
| 135 | + ) |
| 136 | + |
| 137 | + |
| 138 | +def test_add_group_settings_with_custom_settings(mock_file_config, mock_job_dict): |
| 139 | + """Test add_group_settings with custom settings""" |
| 140 | + mock_file_config.destination = "transcodes/test-video" |
| 141 | + mock_file_config.group_settings = { |
| 142 | + "SegmentLength": 15, |
| 143 | + "ManifestNameModifier": "__custom", |
| 144 | + } |
| 145 | + |
| 146 | + add_group_settings(mock_job_dict, mock_file_config) |
| 147 | + |
| 148 | + hls_settings = mock_job_dict["Settings"]["OutputGroups"][0]["OutputGroupSettings"][ |
| 149 | + GroupSettings.HLS_GROUP_SETTINGS_KEY |
| 150 | + ] |
| 151 | + assert hls_settings["SegmentLength"] == 15 # noqa: PLR2004 |
| 152 | + assert hls_settings["AdditionalManifests"][0]["ManifestNameModifier"] == "__custom" |
| 153 | + |
| 154 | + |
| 155 | +def test_add_group_settings_invalid_type(mock_file_config, mock_job_dict): |
| 156 | + """Test add_group_settings with invalid group type""" |
| 157 | + mock_file_config.destination = "transcodes/test-video" |
| 158 | + |
| 159 | + mock_job_dict["Settings"]["OutputGroups"][0]["OutputGroupSettings"]["Type"] = ( |
| 160 | + "INVALID_TYPE" |
| 161 | + ) |
| 162 | + |
| 163 | + with pytest.raises(ValueError, match="Unsupported group settings type"): |
| 164 | + add_group_settings(mock_job_dict, mock_file_config) |
| 165 | + |
| 166 | + |
| 167 | +def test_make_media_convert_job(mock_file_config, mocker): |
| 168 | + """Test make_media_convert_job""" |
| 169 | + mocker.patch("django.conf.settings.TRANSCODE_JOB_TEMPLATE", "mock_template.json") |
| 170 | + mocker.patch("django.conf.settings.VIDEO_TRANSCODE_QUEUE", "default") |
| 171 | + mocker.patch("django.conf.settings.AWS_REGION", "us-east-1") |
| 172 | + mocker.patch("django.conf.settings.AWS_ACCOUNT_ID", "123456789012") |
| 173 | + mocker.patch("django.conf.settings.AWS_ROLE_NAME", "MediaConvertRole") |
| 174 | + |
| 175 | + mock_template = { |
| 176 | + "UserMetadata": {"filter": ""}, |
| 177 | + "Queue": "", |
| 178 | + "Role": "", |
| 179 | + "Settings": { |
| 180 | + "Inputs": [{"FileInput": ""}], |
| 181 | + "OutputGroups": [ |
| 182 | + { |
| 183 | + "OutputGroupSettings": { |
| 184 | + "Type": GroupSettings.HLS_GROUP_SETTINGS, |
| 185 | + GroupSettings.HLS_GROUP_SETTINGS_KEY: { |
| 186 | + "SegmentLength": 6, |
| 187 | + "AdditionalManifests": [{"ManifestNameModifier": ""}], |
| 188 | + }, |
| 189 | + "Destination": "", |
| 190 | + }, |
| 191 | + "Outputs": [ |
| 192 | + {"VideoDescription": {"CodecSettings": {"Codec": "H_264"}}} |
| 193 | + ], |
| 194 | + }, |
| 195 | + ], |
| 196 | + }, |
| 197 | + } |
| 198 | + |
| 199 | + mock_open = mocker.mock_open(read_data=json.dumps(mock_template)) |
| 200 | + mocker.patch("pathlib.Path.open", mock_open) |
| 201 | + mocker.patch("pathlib.Path.cwd", return_value=Path("/")) |
| 202 | + |
| 203 | + job_dict = make_media_convert_job(mock_file_config) |
| 204 | + |
| 205 | + assert job_dict["UserMetadata"]["filter"] == "default" |
| 206 | + assert ( |
| 207 | + job_dict["Queue"] |
| 208 | + == "arn:aws:mediaconvert:us-east-1:123456789012:queues/default" |
| 209 | + ) |
| 210 | + assert job_dict["Role"] == "arn:aws:iam::123456789012:role/MediaConvertRole" |
| 211 | + assert ( |
| 212 | + job_dict["Settings"]["Inputs"][0]["FileInput"] |
| 213 | + == "s3://source-bucket/uploads/test-video.mp4" |
| 214 | + ) |
| 215 | + |
| 216 | + |
| 217 | +@pytest.mark.parametrize( |
| 218 | + ("exclude_mp4", "exclude_thumbnail"), |
| 219 | + [ |
| 220 | + (True, True), |
| 221 | + (True, False), |
| 222 | + (False, True), |
| 223 | + (False, False), |
| 224 | + ], |
| 225 | +) |
| 226 | +def test_media_convert_job(mocker, exclude_mp4, exclude_thumbnail): |
| 227 | + """Test media_convert_job function with different filter combinations""" |
| 228 | + mock_client = mocker.MagicMock() |
| 229 | + mocker.patch("boto3.client", return_value=mock_client) |
| 230 | + |
| 231 | + mock_job_dict = {"job": "config"} |
| 232 | + mocker.patch( |
| 233 | + "mitol.transcoding.api.make_media_convert_job", return_value=mock_job_dict |
| 234 | + ) |
| 235 | + |
| 236 | + mocker.patch( |
| 237 | + "django.conf.settings.VIDEO_S3_TRANSCODE_ENDPOINT", |
| 238 | + "https://mediaconvert.us-east-1.amazonaws.com", |
| 239 | + ) |
| 240 | + mocker.patch("django.conf.settings.AWS_REGION", "us-east-1") |
| 241 | + |
| 242 | + group_settings = {} |
| 243 | + if exclude_mp4: |
| 244 | + group_settings["exclude_mp4"] = True |
| 245 | + if exclude_thumbnail: |
| 246 | + group_settings["exclude_thumbnail"] = True |
| 247 | + |
| 248 | + media_convert_job( |
| 249 | + "uploads/test-video.mp4", |
| 250 | + group_settings=group_settings, |
| 251 | + ) |
| 252 | + |
| 253 | + boto3.client.assert_called_once_with( |
| 254 | + "mediaconvert", |
| 255 | + region_name="us-east-1", |
| 256 | + endpoint_url="https://mediaconvert.us-east-1.amazonaws.com", |
| 257 | + ) |
| 258 | + |
| 259 | + mock_client.create_job.assert_called_once_with(**mock_job_dict) |
0 commit comments