2525VALID_API_KEY = "12345678-1234-1234-1234-123456789abc"
2626ANOTHER_VALID_API_KEY = "abcdefab-cdef-abcd-efab-cdefabcdef12"
2727ASYNC_API_KEY = "fedcba98-7654-3210-fedc-ba9876543210"
28+ DEMO_RESTRICTION_MESSAGE = (
29+ "Output has been watermarked or redacted. This API request was processed "
30+ "with a free account. Visit https://pdfrest.com/pricing/ to upgrade your "
31+ "plan and receive outputs without watermarks or redactions."
32+ )
2833
2934
3035def _build_up_response () -> dict [str , Any ]:
@@ -709,6 +714,107 @@ def handler(_: httpx.Request) -> httpx.Response:
709714 assert exc .value .response_content == "not-json"
710715
711716
717+ def test_client_logs_demo_restriction_message_warning (
718+ monkeypatch : pytest .MonkeyPatch , caplog : pytest .LogCaptureFixture
719+ ) -> None :
720+ monkeypatch .setenv ("PDFREST_API_KEY" , VALID_API_KEY )
721+ caplog .set_level ("WARNING" , logger = "pdfrest.client" )
722+
723+ def handler (_ : httpx .Request ) -> httpx .Response :
724+ return httpx .Response (
725+ 200 ,
726+ json = {** _build_up_response (), "message" : DEMO_RESTRICTION_MESSAGE },
727+ )
728+
729+ transport = httpx .MockTransport (handler )
730+ with PdfRestClient (transport = transport ) as client :
731+ _ = client .up ()
732+
733+ assert "Demo mode restriction message in response" in caplog .text
734+ assert "field=message" in caplog .text
735+ assert DEMO_RESTRICTION_MESSAGE in caplog .text
736+
737+
738+ @pytest .mark .parametrize (
739+ ("field_name" , "body_value" ),
740+ [
741+ pytest .param ("message" , DEMO_RESTRICTION_MESSAGE , id = "message" ),
742+ pytest .param ("warning" , DEMO_RESTRICTION_MESSAGE , id = "warning" ),
743+ pytest .param ("keyMessage" , DEMO_RESTRICTION_MESSAGE , id = "key-message" ),
744+ ],
745+ )
746+ def test_client_logs_demo_restriction_message_warning_all_fields (
747+ monkeypatch : pytest .MonkeyPatch ,
748+ caplog : pytest .LogCaptureFixture ,
749+ field_name : str ,
750+ body_value : str ,
751+ ) -> None :
752+ monkeypatch .setenv ("PDFREST_API_KEY" , VALID_API_KEY )
753+ caplog .set_level ("WARNING" , logger = "pdfrest.client" )
754+
755+ def handler (_ : httpx .Request ) -> httpx .Response :
756+ return httpx .Response (
757+ 200 ,
758+ json = {** _build_up_response (), field_name : body_value },
759+ )
760+
761+ transport = httpx .MockTransport (handler )
762+ with PdfRestClient (transport = transport ) as client :
763+ _ = client .up ()
764+
765+ assert "Demo mode restriction message in response" in caplog .text
766+ assert f"field={ field_name } " in caplog .text
767+ assert DEMO_RESTRICTION_MESSAGE in caplog .text
768+
769+
770+ def test_client_logs_demo_restriction_message_once_when_duplicated (
771+ monkeypatch : pytest .MonkeyPatch , caplog : pytest .LogCaptureFixture
772+ ) -> None :
773+ monkeypatch .setenv ("PDFREST_API_KEY" , VALID_API_KEY )
774+ caplog .set_level ("WARNING" , logger = "pdfrest.client" )
775+
776+ def handler (_ : httpx .Request ) -> httpx .Response :
777+ return httpx .Response (
778+ 200 ,
779+ json = {
780+ ** _build_up_response (),
781+ "message" : DEMO_RESTRICTION_MESSAGE ,
782+ "warning" : DEMO_RESTRICTION_MESSAGE ,
783+ "keyMessage" : DEMO_RESTRICTION_MESSAGE ,
784+ },
785+ )
786+
787+ transport = httpx .MockTransport (handler )
788+ with PdfRestClient (transport = transport ) as client :
789+ _ = client .up ()
790+
791+ demo_logs = [
792+ record .message
793+ for record in caplog .records
794+ if "Demo mode restriction message in response" in record .message
795+ ]
796+ assert len (demo_logs ) == 1
797+
798+
799+ def test_client_does_not_log_non_demo_key_message_warning (
800+ monkeypatch : pytest .MonkeyPatch , caplog : pytest .LogCaptureFixture
801+ ) -> None :
802+ monkeypatch .setenv ("PDFREST_API_KEY" , VALID_API_KEY )
803+ caplog .set_level ("WARNING" , logger = "pdfrest.client" )
804+
805+ def handler (_ : httpx .Request ) -> httpx .Response :
806+ return httpx .Response (
807+ 200 ,
808+ json = {** _build_up_response (), "keyMessage" : "This is a test key" },
809+ )
810+
811+ transport = httpx .MockTransport (handler )
812+ with PdfRestClient (transport = transport ) as client :
813+ _ = client .up ()
814+
815+ assert "Demo mode restriction message in response" not in caplog .text
816+
817+
712818@pytest .mark .asyncio
713819async def test_async_client_raises_for_non_json_success_response (
714820 monkeypatch : pytest .MonkeyPatch ,
@@ -728,6 +834,61 @@ def handler(_: httpx.Request) -> httpx.Response:
728834 assert exc .value .response_content == "not-json"
729835
730836
837+ @pytest .mark .asyncio
838+ async def test_async_client_logs_demo_restriction_message_warning (
839+ monkeypatch : pytest .MonkeyPatch , caplog : pytest .LogCaptureFixture
840+ ) -> None :
841+ monkeypatch .setenv ("PDFREST_API_KEY" , ASYNC_API_KEY )
842+ caplog .set_level ("WARNING" , logger = "pdfrest.client" )
843+
844+ def handler (_ : httpx .Request ) -> httpx .Response :
845+ return httpx .Response (
846+ 200 ,
847+ json = {** _build_up_response (), "message" : DEMO_RESTRICTION_MESSAGE },
848+ )
849+
850+ transport = httpx .MockTransport (handler )
851+ async with AsyncPdfRestClient (transport = transport ) as client :
852+ _ = await client .up ()
853+
854+ assert "Demo mode restriction message in response" in caplog .text
855+ assert "field=message" in caplog .text
856+ assert DEMO_RESTRICTION_MESSAGE in caplog .text
857+
858+
859+ @pytest .mark .asyncio
860+ @pytest .mark .parametrize (
861+ ("field_name" , "body_value" ),
862+ [
863+ pytest .param ("message" , DEMO_RESTRICTION_MESSAGE , id = "message" ),
864+ pytest .param ("warning" , DEMO_RESTRICTION_MESSAGE , id = "warning" ),
865+ pytest .param ("keyMessage" , DEMO_RESTRICTION_MESSAGE , id = "key-message" ),
866+ ],
867+ )
868+ async def test_async_client_logs_demo_restriction_message_warning_all_fields (
869+ monkeypatch : pytest .MonkeyPatch ,
870+ caplog : pytest .LogCaptureFixture ,
871+ field_name : str ,
872+ body_value : str ,
873+ ) -> None :
874+ monkeypatch .setenv ("PDFREST_API_KEY" , ASYNC_API_KEY )
875+ caplog .set_level ("WARNING" , logger = "pdfrest.client" )
876+
877+ def handler (_ : httpx .Request ) -> httpx .Response :
878+ return httpx .Response (
879+ 200 ,
880+ json = {** _build_up_response (), field_name : body_value },
881+ )
882+
883+ transport = httpx .MockTransport (handler )
884+ async with AsyncPdfRestClient (transport = transport ) as client :
885+ _ = await client .up ()
886+
887+ assert "Demo mode restriction message in response" in caplog .text
888+ assert f"field={ field_name } " in caplog .text
889+ assert DEMO_RESTRICTION_MESSAGE in caplog .text
890+
891+
731892def test_client_uses_text_for_non_json_error_payload (
732893 monkeypatch : pytest .MonkeyPatch ,
733894) -> None :
0 commit comments