diff --git a/contentcuration/contentcuration/tests/viewsets/test_channel.py b/contentcuration/contentcuration/tests/viewsets/test_channel.py index 8421848da3..b4eb5d4b4b 100644 --- a/contentcuration/contentcuration/tests/viewsets/test_channel.py +++ b/contentcuration/contentcuration/tests/viewsets/test_channel.py @@ -1193,6 +1193,12 @@ def setUp(self): self.channel.language_id = "en" self.channel.save() + self.editor = testdata.user(email="language-editor@user.com") + self.channel.editors.add(self.editor) + self.viewer = testdata.user(email="language-viewer@user.com") + self.channel.viewers.add(self.viewer) + self.non_member = testdata.user(email="language-nonmember@user.com") + self.channel_id = self.channel.id self.node_id = "00000000000000000000000000000003" self.public_node = PublicContentNode.objects.create( @@ -1276,9 +1282,33 @@ def test_get_languages_in_channel_invalid_request(self): response = self._perform_action("channel-languages", None) self.assertEqual(response.status_code, 404, response.content) - def _perform_action(self, url_path, channel_id): - user = testdata.user() - self.client.force_authenticate(user=user) + def test_language_actions_viewer_has_access(self): + ContentNode.objects.filter(node_id=self.public_node.id).update(language_id="en") + response = self._perform_action( + "channel-language-exists", self.channel.id, user=self.viewer + ) + self.assertEqual(response.status_code, 200, response.content) + self.assertTrue(response.json()["exists"]) + + response = self._perform_action( + "channel-languages", self.channel.id, user=self.viewer + ) + self.assertEqual(response.status_code, 200, response.content) + + def test_language_actions_nonexistent_channel_matches_non_member(self): + for url_path in ("channel-language-exists", "channel-languages"): + missing = self._perform_action( + url_path, uuid.uuid4().hex, user=self.non_member + ) + denied = self._perform_action( + url_path, self.channel.id, user=self.non_member + ) + self.assertEqual(missing.status_code, 404, missing.content) + self.assertEqual(missing.status_code, denied.status_code) + self.assertEqual(missing.content, denied.content) + + def _perform_action(self, url_path, channel_id, user=None): + self.client.force_authenticate(user=user or self.editor) response = self.client.get( reverse(url_path, kwargs={"pk": channel_id}), format="json" ) diff --git a/contentcuration/contentcuration/viewsets/channel.py b/contentcuration/contentcuration/viewsets/channel.py index 3f9a866105..09c8558d03 100644 --- a/contentcuration/contentcuration/viewsets/channel.py +++ b/contentcuration/contentcuration/viewsets/channel.py @@ -1,7 +1,6 @@ import logging from functools import reduce from operator import or_ -from typing import Dict from typing import List from typing import Union @@ -15,8 +14,8 @@ from django.db.models import Value from django.db.models.functions import Coalesce from django.http import HttpResponse -from django.http import HttpResponseNotFound from django.http import JsonResponse +from django.shortcuts import get_object_or_404 from django.utils.decorators import method_decorator from django.views.decorators.cache import cache_page from django_cte import With @@ -883,12 +882,17 @@ def channel_language_exists( :return: JsonResponse with exists=True if the language exists, False otherwise :rtype: JsonResponse """ - if not self._channel_exists(pk): - return HttpResponseNotFound("No channel matching: {}".format(pk)) + channel = get_object_or_404( + models.Channel.filter_view_queryset( + models.Channel.objects.all(), request.user + ).values("id", "language_id", "main_tree_id"), + pk=pk, + ) - channel_lang, main_tree_id = self._get_channel_details(pk).values() - langs_in_channel = self._get_channel_content_languages(pk, main_tree_id) - lang_exists = channel_lang in langs_in_channel + langs_in_channel = self._get_channel_content_languages( + channel["id"], channel["main_tree_id"] + ) + lang_exists = channel["language_id"] in langs_in_channel return JsonResponse({"exists": lang_exists}) @@ -904,12 +908,16 @@ def get_languages_in_channel( :return: JsonResponse with a list of languages present in the channel :rtype: JsonResponse """ - if not self._channel_exists(pk): - return HttpResponseNotFound("No channel matching: {}".format(pk)) + channel = get_object_or_404( + models.Channel.filter_view_queryset( + models.Channel.objects.all(), request.user + ).values("id", "language_id", "main_tree_id"), + pk=pk, + ) - channel_details = self._get_channel_details(pk) - main_tree_id = channel_details.get("main_tree_id") - langs_in_content = self._get_channel_content_languages(pk, main_tree_id) + langs_in_content = self._get_channel_content_languages( + channel["id"], channel["main_tree_id"] + ) return JsonResponse({"languages": langs_in_content}) @action( @@ -979,43 +987,6 @@ def has_community_library_submission(self, request, pk=None) -> Response: ).exists() return Response({"has_community_library_submission": has_submission}) - def _channel_exists(self, channel_id) -> bool: - """ - Check if a channel exists. - - :param channel_id: The ID of the channel - :return: True if the channel exists, False otherwise - :rtype: bool - """ - try: - return Channel.objects.filter(pk=channel_id).exists() - except Exception as e: - logging.error(f"Error checking if channel exists: {e}") - return False - - def _get_channel_details(self, channel_id) -> Dict[str, any]: - """ - Get the language set for a channel. - - :param channel_id: The ID of the channel - :return: The language code set for the channel - :rtype: str - """ - try: - channel_details = ( - Channel.objects.filter(pk=channel_id) - .values("language_id", "main_tree_id") - .first() - ) - except Channel.DoesNotExist as e: - logging.error(str(e)) - channel_details = None - - if not channel_details: - channel_details = dict(language_id=None, main_tree_id=None) - - return channel_details - def _get_channel_content_languages( self, channel_id, main_tree_id=None ) -> List[str]: