Compare commits

...
5 changed files with 201 additions and 16 deletions

No files matched your search

+40
View File
@@ -1,3 +1,5 @@
from typing import Any
import pytest
from django.core.management import call_command
from django.core.management.base import CommandError
@@ -337,3 +339,41 @@ class TestDocumentListObjectFilterSchema:
param = next(p for p in params if p["name"] == param_name)
assert param["schema"] == {"type": "array", "items": {"type": "integer"}}
assert param["explode"] is False
class TestOauthCallbackSchema:
@pytest.mark.parametrize("name", ["code", "state"])
def test_empty_code_and_state_are_not_schema_valid(
self,
api_schema: dict[str, Any],
name: str,
) -> None:
"""
GIVEN:
- The generated schema
WHEN:
- The code and state parameters of the OAuth callback are read
THEN:
- They are required and not empty, the view answers 400 to an empty one
"""
params = api_schema["paths"]["/api/oauth/callback/"]["get"]["parameters"]
param = next(p for p in params if p["name"] == name)
assert param["required"] is True
assert param["schema"]["minLength"] == 1
class TestOcrPageImageSchema:
def test_page_image_is_declared_as_png(self, api_schema: dict[str, Any]) -> None:
"""
GIVEN:
- The generated schema
WHEN:
- The 200 response of the OCR template page image is read
THEN:
- It is a PNG, which is what the view serves
"""
path = "/api/ocr_templates/document-page-image/{doc_id}/{page}/"
content = api_schema["paths"][path]["get"]["responses"]["200"]["content"]
assert list(content) == ["image/png"]
+94 -9
View File
@@ -942,7 +942,7 @@ class EmailDocumentDetailSchema(EmailSerializer):
parameters=[
OpenApiParameter(
name="id",
type=OpenApiTypes.STR,
type=OpenApiTypes.INT,
location=OpenApiParameter.PATH,
),
],
@@ -2886,6 +2886,13 @@ class UnifiedSearchViewSet(DocumentViewSet):
return Response(max_asn + 1)
LOG_FILES = {
"paperless": "paperless.log",
"mail": "mail.log",
"celery": "celery.log",
}
@extend_schema_view(
list=extend_schema(
description="Logs view",
@@ -2903,6 +2910,7 @@ class UnifiedSearchViewSet(DocumentViewSet):
name="id",
type=OpenApiTypes.STR,
location=OpenApiParameter.PATH,
enum=list(LOG_FILES),
),
OpenApiParameter(
name="limit",
@@ -2923,11 +2931,7 @@ class UnifiedSearchViewSet(DocumentViewSet):
class LogViewSet(ViewSet):
permission_classes = (IsAuthenticated, PaperlessAdminPermissions)
ALLOWED_LOG_FILES = {
"paperless": "paperless.log",
"mail": "mail.log",
"celery": "celery.log",
}
ALLOWED_LOG_FILES = LOG_FILES
def get_log_file(self, log_key: str) -> Path:
return Path(settings.LOGGING_DIR) / self.ALLOWED_LOG_FILES[log_key]
@@ -4016,7 +4020,36 @@ class GlobalSearchView(PassUserMixin):
get=extend_schema(
description="Get statistics for the current user",
responses={
(200, "application/json"): OpenApiTypes.OBJECT,
(200, "application/json"): inline_serializer(
name="Statistics",
fields={
"documents_total": serializers.IntegerField(),
"documents_inbox": serializers.IntegerField(allow_null=True),
"inbox_tag": serializers.IntegerField(
allow_null=True,
help_text="The first inbox tag, kept for backwards compatibility",
),
"inbox_tags": serializers.ListField(
child=serializers.IntegerField(),
allow_null=True,
),
"document_file_type_counts": serializers.ListField(
child=inline_serializer(
name="MimeTypeCount",
fields={
"mime_type": serializers.CharField(),
"mime_type_count": serializers.IntegerField(),
},
),
),
"character_count": serializers.IntegerField(),
"tag_count": serializers.IntegerField(),
"correspondent_count": serializers.IntegerField(),
"document_type_count": serializers.IntegerField(),
"storage_path_count": serializers.IntegerField(),
"current_asn": serializers.IntegerField(),
},
),
},
),
)
@@ -4501,7 +4534,13 @@ def get_github_stars() -> int | None:
get=extend_schema(
description="Get the current version of the Paperless-NGX server",
responses={
(200, "application/json"): OpenApiTypes.OBJECT,
(200, "application/json"): inline_serializer(
name="RemoteVersion",
fields={
"version": serializers.CharField(),
"update_available": serializers.BooleanField(),
},
),
},
),
)
@@ -4566,7 +4605,17 @@ class RemoteVersionView(GenericAPIView[Any]):
),
],
responses={
(200, "application/json"): OpenApiTypes.OBJECT,
(200, "application/json"): inline_serializer(
name="ReleaseNotes",
fields={
"version": serializers.CharField(),
"releases": serializers.ListField(
child=serializers.DictField(),
help_text="GitHub release objects",
),
"stars": serializers.IntegerField(allow_null=True),
},
),
},
),
)
@@ -5830,6 +5879,26 @@ class SystemStatusView(PassUserMixin):
)
@extend_schema_view(
get=extend_schema(
description="List the documents in the trash",
responses={(200, "application/json"): DocumentSerializer(many=True)},
),
post=extend_schema(
description="Restore documents from the trash, or delete them permanently",
responses={
(200, "application/json"): inline_serializer(
name="TrashActionResult",
fields={
"result": serializers.CharField(),
"doc_ids": serializers.ListField(
child=serializers.IntegerField(),
),
},
),
},
),
)
class TrashView(ListModelMixin, PassUserMixin):
permission_classes = (IsAuthenticated, TrashPermissions)
serializer_class = TrashSerializer
@@ -5978,6 +6047,22 @@ class OcrTemplateViewSet(BulkPermissionMixin, PassUserMixin, ModelViewSet[OcrTem
]
return super().get_permissions()
@extend_schema(
parameters=[
OpenApiParameter(
name="doc_id",
type=OpenApiTypes.INT,
location=OpenApiParameter.PATH,
),
OpenApiParameter(
name="page",
type={"type": "integer", "minimum": 1},
location=OpenApiParameter.PATH,
description="1-based page number",
),
],
responses={(200, "image/png"): OpenApiTypes.BINARY},
)
@action(
detail=False,
methods=["get"],
+30 -3
View File
@@ -24,6 +24,8 @@ from django_filters.rest_framework import DjangoFilterBackend
from drf_spectacular.types import OpenApiTypes
from drf_spectacular.utils import extend_schema
from drf_spectacular.utils import extend_schema_view
from drf_spectacular.utils import inline_serializer
from rest_framework import serializers
from rest_framework.authtoken.models import Token
from rest_framework.authtoken.views import ObtainAuthToken
from rest_framework.decorators import action
@@ -274,8 +276,16 @@ class ProfileView(GenericAPIView[Any]):
@extend_schema_view(
get=extend_schema(
description="Generate a new TOTP secret, its otpauth URL and a QR code",
responses={
(200, "application/json"): OpenApiTypes.OBJECT,
(200, "application/json"): inline_serializer(
name="TotpSetup",
fields={
"url": serializers.CharField(),
"qr_svg": serializers.CharField(help_text="QR code as an SVG"),
"secret": serializers.CharField(),
},
),
},
),
post=extend_schema(
@@ -290,7 +300,17 @@ class ProfileView(GenericAPIView[Any]):
},
},
responses={
(200, "application/json"): OpenApiTypes.OBJECT,
(200, "application/json"): inline_serializer(
name="TotpActivation",
fields={
"success": serializers.BooleanField(),
"recovery_codes": serializers.ListField(
child=serializers.CharField(),
allow_null=True,
help_text="Only returned when the code was valid",
),
},
),
},
),
delete=extend_schema(
@@ -523,7 +543,14 @@ class DisconnectSocialAccountView(GenericAPIView[Any]):
@extend_schema_view(
get=extend_schema(
responses={
(200, "application/json"): OpenApiTypes.OBJECT,
(200, "application/json"): inline_serializer(
name="SocialAccountProvider",
fields={
"name": serializers.CharField(),
"login_url": serializers.CharField(),
},
many=True,
),
},
),
)
+3 -2
View File
@@ -2,6 +2,7 @@ import datetime
import imaplib
import itertools
import logging
import os
import socket
import ssl
import tempfile
@@ -1135,12 +1136,12 @@ class MailAccountHandler(LoggingMixin):
doc_type,
):
settings.SCRATCH_DIR.mkdir(parents=True, exist_ok=True)
_, temp_filename = tempfile.mkstemp(
fd, temp_filename = tempfile.mkstemp(
prefix="paperless-mail-",
dir=settings.SCRATCH_DIR,
suffix=".eml",
)
with Path(temp_filename).open("wb") as f:
with os.fdopen(fd, "wb") as f:
# Move "From"-header to beginning of file
# TODO: This ugly workaround is needed because the parser is
# chosen only by the mime_type detected via magic
+34 -2
View File
@@ -10,6 +10,7 @@ from django.http import HttpResponseRedirect
from django.utils import timezone
from django_filters.rest_framework import DjangoFilterBackend
from drf_spectacular.types import OpenApiTypes
from drf_spectacular.utils import OpenApiParameter
from drf_spectacular.utils import extend_schema
from drf_spectacular.utils import extend_schema_view
from drf_spectacular.utils import inline_serializer
@@ -254,8 +255,39 @@ class MailRuleViewSet(PassUserMixin, ModelViewSet[MailRule]):
@extend_schema_view(
get=extend_schema(
description="Callback view for OAuth2 authentication",
responses={200: None},
description=(
"Callback view for OAuth2 authentication. Redirects back to the "
"frontend, with the outcome in the query string"
),
parameters=[
OpenApiParameter(
name="code",
# An empty value is rejected
type={"type": "string", "minLength": 1},
location=OpenApiParameter.QUERY,
required=True,
description="Authorization code returned by the OAuth provider",
),
OpenApiParameter(
name="state",
# An empty value is rejected
type={"type": "string", "minLength": 1},
location=OpenApiParameter.QUERY,
required=True,
description="State value, must match the one stored in the session",
),
OpenApiParameter(
name="scope",
type=OpenApiTypes.STR,
location=OpenApiParameter.QUERY,
required=False,
description="Granted scopes, sent by Gmail but not by Outlook",
),
],
responses={
302: None,
(400, "text/html"): OpenApiTypes.STR,
},
),
)
class OauthCallbackView(GenericAPIView[Any]):