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
35 changes: 35 additions & 0 deletions contentcuration/contentcuration/tests/viewsets/test_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -1478,6 +1478,21 @@ def test_get_version_detail_returns_all_fields(self):
for field in expected_fields:
self.assertIn(field, data, f"Field '{field}' should be in response")

def test_get_version_detail_dedupes_duplicated_licenses(self):
"""Test that duplicated license ids stored on the ChannelVersion (e.g. from a
backfill bug) are deduped in the response."""
self.channel_version.included_licenses = [1, 2, 2, 1]
self.channel_version.non_distributable_licenses_included = [1, 1]
self.channel_version.save()

url = reverse("channel-version-detail", kwargs={"pk": self.channel.id})
response = self.client.get(url)

self.assertEqual(response.status_code, 200)
data = response.json()
self.assertEqual(data["included_licenses"], [1, 2])
self.assertEqual(data["non_distributable_licenses_included"], [1])

def test_get_version_detail_excludes_special_permissions_included(self):
"""Test that special_permissions_included is not in the response."""
special_license = AuditedSpecialPermissionsLicense.objects.create(
Expand Down Expand Up @@ -1587,6 +1602,26 @@ def test_get_specific_channel_version(self):
self.assertEqual(data["channel"], self.channel.id)
self.assertEqual(data["version"], channel_version.version)

def test_get_channel_version_dedupes_duplicated_licenses(self):
"""Test that duplicated license ids stored on the ChannelVersion (e.g. from a
backfill bug) are deduped in both list and detail responses."""
channel_version = ChannelVersion.objects.filter(channel=self.channel).first()
channel_version.included_licenses = [2, 1, 2, 1]
channel_version.save()

detail_url = reverse("channelversion-detail", kwargs={"pk": channel_version.id})
detail_response = self.client.get(detail_url)
self.assertEqual(detail_response.status_code, 200)
self.assertEqual(detail_response.json()["included_licenses"], [1, 2])

list_url = reverse("channelversion-list") + f"?channel={self.channel.id}"
list_response = self.client.get(list_url)
self.assertEqual(list_response.status_code, 200)
data = list_response.json()
results = data["results"] if "results" in data else data
version_data = next(r for r in results if r["id"] == channel_version.id)
self.assertEqual(version_data["included_licenses"], [1, 2])

def test_get_channel_versions_ordering(self):
"""Test ordering of channel versions."""
url = (
Expand Down
17 changes: 17 additions & 0 deletions contentcuration/contentcuration/viewsets/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,17 @@ def get_version_detail(self, request, pk=None) -> Response:
if not version_data:
return Response({})

# Older ChannelVersion rows may have been backfilled with duplicate
# license IDs - dedupe defensively.
if version_data.get("included_licenses") is not None:
version_data["included_licenses"] = sorted(
set(version_data["included_licenses"])
)
if version_data.get("non_distributable_licenses_included") is not None:
version_data["non_distributable_licenses_included"] = sorted(
set(version_data["non_distributable_licenses_included"])
)

return Response(version_data)

@action(
Expand Down Expand Up @@ -1073,6 +1084,12 @@ class ChannelVersionViewSet(ReadOnlyValuesViewset):
ordering_fields = ["version"]
ordering = "-version"

# Older ChannelVersion rows may have been backfilled with duplicate
# license IDs - dedupe defensively.
field_map = {
"included_licenses": lambda item: sorted(set(item["included_licenses"] or []))
}

values = (
"id",
"channel",
Expand Down
Loading