Skip to content

Commit 4834be4

Browse files
authored
Addons: small improvements and privacy considerations (#11561)
- We had a special case in a permissions class, I moved this into the view itself. - We are allowing some requests to not have a version, so our normal permission check doesn't work in this case. In case that we don't have a version, we just check for the project permission. - If we have a version, we are attaching its latest build, this code was duplicated, and wasn't taking into considerations permissions. In case of temporal sharing tokens, they don't have access to builds. - Translations now take into consideration the current user permissions - The list of active versions can now be overridden from .com to include versions granted by a temporal access. Some notes: - We are returning some custom 404 responses, but DRF already handles that when using get_object_or_404. Anyway, users won't see any of the messages, since our 404 handler is catching those in production. - Returning the full serialized version of translations takes like 40 extra queries, probably it generates more per extra translation. From what I found, most of the queries are due to the serializer returning more related projects fully serialized (main translation, superproject). - We are allowing users to override the whole response, we should just allow to override the `addons` key. - This also suffers from readthedocs/readthedocs-corporate#1845 Since to really run the whole code from permissions checks organizations are needed, I'll add tests on .com. ref readthedocs/readthedocs-corporate#1773
1 parent 70cefc0 commit 4834be4

File tree

3 files changed

+123
-100
lines changed

3 files changed

+123
-100
lines changed

readthedocs/api/v2/permissions.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,6 @@ class IsAuthorizedToViewVersion(permissions.BasePermission):
4040
def has_permission(self, request, view):
4141
project = view._get_project()
4242
version = view._get_version()
43-
44-
# I had to add this condition here because I want to return a 400 when
45-
# the `project-slug` or `version-slug` are not sent to the API
46-
# endpoint. In those cases, we don't have a Project/Version and this
47-
# function was failing.
48-
#
49-
# I think it's a valid use case when Project/Version is invalid to be
50-
# able to return a good response from the API view.
51-
if project is None or version is None:
52-
return True
53-
5443
has_access = (
5544
Version.objects.public(
5645
user=request.user,

readthedocs/proxito/tests/test_hosting.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
SINGLE_VERSION_WITHOUT_TRANSLATIONS,
2020
)
2121
from readthedocs.projects.models import AddonsConfig, Domain, Project
22-
from readthedocs.proxito.views.hosting import ClientError
2322

2423

2524
@override_settings(
@@ -284,6 +283,7 @@ def test_flyout_translations(self):
284283
slug="translation",
285284
main_language_project=self.project,
286285
language="ja",
286+
privacy_level=PUBLIC,
287287
)
288288
translation_ja.versions.update(
289289
built=True,
@@ -530,20 +530,24 @@ def test_flyout_subproject_urls(self):
530530
slug="translation",
531531
language="es",
532532
repo="https://github.com/readthedocs/subproject",
533+
privacy_level=PUBLIC,
533534
)
534535
translation.versions.update(
535536
built=True,
536537
active=True,
537538
)
538539
subproject = fixture.get(
539-
Project, slug="subproject", repo="https://github.com/readthedocs/subproject"
540+
Project,
541+
slug="subproject",
542+
repo="https://github.com/readthedocs/subproject",
543+
privacy_level=PUBLIC,
540544
)
541545
self.project.add_subproject(subproject)
542546
subproject.translations.add(translation)
543547
subproject.save()
544548

545-
fixture.get(Version, slug="v1", project=subproject)
546-
fixture.get(Version, slug="v2.3", project=subproject)
549+
fixture.get(Version, slug="v1", project=subproject, privacy_level=PUBLIC)
550+
fixture.get(Version, slug="v2.3", project=subproject, privacy_level=PUBLIC)
547551
subproject.versions.update(
548552
privacy_level=PUBLIC,
549553
built=True,
@@ -710,9 +714,7 @@ def test_non_existent_project(self):
710714
},
711715
)
712716
assert r.status_code == 404
713-
assert r.json() == {
714-
"error": ClientError.PROJECT_NOT_FOUND,
715-
}
717+
assert r.json() == {"detail": "No Project matches the given query."}
716718

717719
def test_number_of_queries_project_version_slug(self):
718720
# The number of queries should not increase too much, even if we change
@@ -734,7 +736,7 @@ def test_number_of_queries_project_version_slug(self):
734736
active=True,
735737
)
736738

737-
with self.assertNumQueries(23):
739+
with self.assertNumQueries(22):
738740
r = self.client.get(
739741
reverse("proxito_readthedocs_docs_addons"),
740742
{
@@ -825,7 +827,7 @@ def test_number_of_queries_url_translations(self):
825827
language=language,
826828
)
827829

828-
with self.assertNumQueries(56):
830+
with self.assertNumQueries(60):
829831
r = self.client.get(
830832
reverse("proxito_readthedocs_docs_addons"),
831833
{

0 commit comments

Comments
 (0)