Skip to content

Commit

Permalink
Addons: return 404 when the project does not exist in the DB (#11302)
Browse files Browse the repository at this point in the history
  • Loading branch information
humitos committed Apr 24, 2024
1 parent 1840922 commit 5a303de
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
20 changes: 20 additions & 0 deletions readthedocs/proxito/tests/test_hosting.py
Expand Up @@ -19,6 +19,7 @@
SINGLE_VERSION_WITHOUT_TRANSLATIONS,
)
from readthedocs.projects.models import AddonsConfig, Domain, Project
from readthedocs.proxito.views.hosting import ClientError


@override_settings(
Expand Down Expand Up @@ -666,6 +667,25 @@ def test_custom_domain_url(self):
== "https://docs.example.com/en/latest/"
)

def test_non_existent_project(self):
r = self.client.get(
reverse("proxito_readthedocs_docs_addons"),
{
"api-version": "1.0.0",
"client-version": "0.6.0",
"project-slug": "non-existent-project",
"version-slug": "latest",
},
secure=True,
headers={
"host": "project.dev.readthedocs.io",
},
)
assert r.status_code == 404
assert r.json() == {
"error": ClientError.PROJECT_NOT_FOUND,
}

def test_number_of_queries_project_version_slug(self):
# The number of queries should not increase too much, even if we change
# some of the responses from the API. This test will help us to
Expand Down
6 changes: 6 additions & 0 deletions readthedocs/proxito/views/hosting.py
Expand Up @@ -47,6 +47,7 @@ class ClientError(Exception):
"The version specified in 'api-version' is currently not supported"
)
VERSION_INVALID = "The version specifified in 'api-version' is invalid"
PROJECT_NOT_FOUND = "There is no project with the 'project-slug' requested"


class BaseReadTheDocsConfigJson(CDNCacheTagsMixin, APIView):
Expand Down Expand Up @@ -189,6 +190,11 @@ def get(self, request, format=None):
)

project, version, build, filename = self._resolve_resources()
if not project:
return JsonResponse(
{"error": ClientError.PROJECT_NOT_FOUND},
status=404,
)

data = AddonsResponse().get(
addons_version,
Expand Down

0 comments on commit 5a303de

Please sign in to comment.