from pathlib import Path import pytest from pytest_django.fixtures import DjangoAssertNumQueries from pytest_django.fixtures import Settings from rest_framework import status from rest_framework.exceptions import ValidationError from rest_framework.test import APIClient from paperless.serialisers import GroupSerializer from paperless.serialisers import UserSerializer class TestNullByteInput: @pytest.mark.parametrize("db_only", ["true", "false"]) def test_global_search_rejects_null_byte( self, admin_client: APIClient, db_only: str, ) -> None: """ GIVEN: - An authenticated client WHEN: - The global search query contains a null byte THEN: - The response is a 400, the query never reaches the database """ response = admin_client.get( "/api/search/", {"query": "invoice\x00number", "db_only": db_only}, ) assert response.status_code == status.HTTP_400_BAD_REQUEST @pytest.mark.parametrize( ("serializer", "field"), [ pytest.param(GroupSerializer, "permissions", id="group"), pytest.param(UserSerializer, "user_permissions", id="user"), ], ) def test_codename_with_null_byte_is_rejected_without_a_query( self, db: None, django_assert_num_queries: DjangoAssertNumQueries, serializer: type, field: str, ) -> None: """ GIVEN: - A permission codename field WHEN: - A value containing a null byte is validated THEN: - It is rejected without querying, Postgres raises an error for it where SQLite does not """ codename_field = serializer().fields[field].child_relation with django_assert_num_queries(0), pytest.raises(ValidationError): codename_field.to_internal_value("view_document\x00") class TestLogLimit: def test_limit_larger_than_any_file_returns_everything( self, admin_client: APIClient, settings: Settings, tmp_path: Path, ) -> None: """ GIVEN: - A log file WHEN: - The limit is larger than a platform integer can hold THEN: - The whole file is returned, as for any limit above its length """ settings.LOGGING_DIR = tmp_path (tmp_path / "paperless.log").write_text("one\ntwo\nthree\n") response = admin_client.get("/api/logs/paperless/", {"limit": 2**70}) assert response.status_code == status.HTTP_200_OK assert response.data == ["one", "two", "three"] class TestTotpView: def test_options_does_not_fail(self, admin_client: APIClient) -> None: """ GIVEN: - An authenticated client WHEN: - OPTIONS is sent to the TOTP endpoint THEN: - The request succeeds, DRF's metadata needs a serializer on the view """ response = admin_client.options("/api/profile/totp/") assert response.status_code == status.HTTP_200_OK @pytest.mark.parametrize( "body", [{}, {"secret": "abc"}, {"code": "123456"}, {"secret": None, "code": None}], ) def test_activation_with_missing_fields_is_a_400( self, admin_client: APIClient, body: dict, ) -> None: """ GIVEN: - An authenticated client WHEN: - TOTP is activated without both the secret and the code THEN: - The response is a 400, not a server error """ response = admin_client.post("/api/profile/totp/", body, format="json") assert response.status_code == status.HTTP_400_BAD_REQUEST