markproof 0.2.0__tar.gz → 0.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: markproof
3
- Version: 0.2.0
3
+ Version: 0.3.0
4
4
  Summary: Python client for the Markproof API - EU AI Act Article 50 watermarking and verification.
5
5
  Author-email: Markproof <support@markproof.co>
6
6
  License-Expression: MIT
@@ -49,7 +49,7 @@ print(result.summary)
49
49
 
50
50
  - `Markproof(api_key, base_url=DEFAULT_BASE_URL)` — `base_url` defaults to the production API; override for local dev.
51
51
  - `client.mark(file, filename=None, generator_name=None, model_name=None, response_format="file", visible_label=None, label_position=None)` → `MarkResult` — `file` is a path (`str`/`os.PathLike`) or raw `bytes` (in which case `filename` is required). `response_format="file"` (default) returns the marked bytes inline (`result.file`/`result.content_type`); `"url"` returns a short-lived signed download URL instead (`result.file_url`, ~1 hour — download promptly rather than caching it). Either way you also get `asset_id`, `watermark_code`, `verify_url`, `c2pa_manifest_label`. `visible_label` (images only): `True` draws a visible "AI-generated" label (EU AI Act Art. 50(4)) before marking, or pass your own text (max 40 characters); `label_position` picks the corner (`"bottom-right"` default, `"bottom-left"`, `"top-right"`, `"top-left"`).
52
- - `client.verify(file, filename=None)` → `VerifyResult` — `watermark`/`c2pa` detection results, `matched_asset` if the watermark matches a record in Markproof's database, and a human-readable `summary`.
52
+ - `client.verify(file, filename=None)` or `client.verify(url=...)` → `VerifyResult` — `watermark`/`c2pa` detection results, `matched_asset` if the watermark matches a record in Markproof's database, and a human-readable `summary`. Direct uploads are limited to about 4.5 MB; pass a public `url` (up to 25 MB) for bigger files, such as marked audio, which comes back as WAV (e.g. a batch item's `file_url`).
53
53
  - `client.get_asset(asset_id)` → `Asset` — requires an API key on the same project that created the asset.
54
54
 
55
55
  All methods raise `MarkproofError` (with `.status` and `.body`) on a non-2xx response.
@@ -1,69 +1,69 @@
1
- # markproof
2
-
3
- Python client for the [Markproof](https://markproof.co) API — EU AI Act Article 50 watermarking and verification in one call. Thin wrapper over `requests`.
4
-
5
- > **Is your AI output Article 50-ready?** Drop a file into the [free checker](https://markproof.co/check) (no signup) to see whether it carries a watermark and signed Content Credentials.
6
-
7
- ## Install
8
-
9
- ```bash
10
- pip install markproof
11
- ```
12
-
13
- ## Usage
14
-
15
- ```python
16
- from markproof import Markproof
17
-
18
- client = Markproof(api_key="lt_live_...")
19
-
20
- marked = client.mark("generated.png", generator_name="my-app", model_name="my-model-v1")
21
- print(marked.watermark_code, marked.verify_url)
22
- with open("generated-marked.png", "wb") as f:
23
- f.write(marked.file)
24
-
25
- result = client.verify("generated-marked.png")
26
- print(result.summary)
27
- ```
28
-
29
- ## API
30
-
31
- - `Markproof(api_key, base_url=DEFAULT_BASE_URL)` — `base_url` defaults to the production API; override for local dev.
32
- - `client.mark(file, filename=None, generator_name=None, model_name=None, response_format="file", visible_label=None, label_position=None)` → `MarkResult` — `file` is a path (`str`/`os.PathLike`) or raw `bytes` (in which case `filename` is required). `response_format="file"` (default) returns the marked bytes inline (`result.file`/`result.content_type`); `"url"` returns a short-lived signed download URL instead (`result.file_url`, ~1 hour — download promptly rather than caching it). Either way you also get `asset_id`, `watermark_code`, `verify_url`, `c2pa_manifest_label`. `visible_label` (images only): `True` draws a visible "AI-generated" label (EU AI Act Art. 50(4)) before marking, or pass your own text (max 40 characters); `label_position` picks the corner (`"bottom-right"` default, `"bottom-left"`, `"top-right"`, `"top-left"`).
33
- - `client.verify(file, filename=None)` → `VerifyResult` — `watermark`/`c2pa` detection results, `matched_asset` if the watermark matches a record in Markproof's database, and a human-readable `summary`.
34
- - `client.get_asset(asset_id)` → `Asset` — requires an API key on the same project that created the asset.
35
-
36
- All methods raise `MarkproofError` (with `.status` and `.body`) on a non-2xx response.
37
-
38
- ## Batches and webhooks
39
-
40
- For volume, queue many files at once instead of calling `mark` per file. Each item counts as one mark.
41
-
42
- ```python
43
- created = client.create_batch(
44
- urls=["https://cdn.example.com/1.png", "https://cdn.example.com/2.png"], # and/or files=["a.png", (data, "b.wav")]
45
- generator_name="your-app",
46
- webhook_url="https://your-app.example.com/hooks/markproof", # optional
47
- )
48
- batch = client.wait_for_batch(created.batch_id) # or get_batch(...), or wait for the webhook
49
- for item in batch.items:
50
- print(item.index, item.status, item.file_url or item.error)
51
- ```
52
-
53
- - `client.create_batch(files=None, urls=None, generator_name=None, model_name=None, webhook_url=None)` → `CreatedBatch` — files go up in one multipart request (keep the total under ~4.5 MB; use `urls` for more). Max items per batch: 10 Free, 100 Indie, 1,000 Pro.
54
- - `client.get_batch(batch_id)` → `Batch` — `status` (`queued`/`processing`/`completed`/`partial`/`failed`), `counts`, and `items` with `asset_id`, `watermark_code`, `verify_url`, `file_url` (signed, ~1 hour; call again for a fresh one) or `error`.
55
- - `client.wait_for_batch(batch_id, interval=3.0, timeout=900, on_progress=None)` — polls until the batch is final.
56
-
57
- Verify webhooks with the raw request body and your signing secret from the dashboard:
58
-
59
- ```python
60
- from markproof import verify_webhook_signature
61
-
62
- ok = verify_webhook_signature(
63
- request.get_data(), # the exact bytes received, not re-serialized JSON
64
- request.headers.get("Markproof-Signature"),
65
- os.environ["MARKPROOF_WEBHOOK_SECRET"],
66
- )
67
- ```
68
-
69
- `parse_webhook_event(...)` does the same and returns the `batch.completed` event, raising `WebhookSignatureError` if the signature doesn't match. Event `id`s are stable across retries, so use them to drop duplicates.
1
+ # markproof
2
+
3
+ Python client for the [Markproof](https://markproof.co) API — EU AI Act Article 50 watermarking and verification in one call. Thin wrapper over `requests`.
4
+
5
+ > **Is your AI output Article 50-ready?** Drop a file into the [free checker](https://markproof.co/check) (no signup) to see whether it carries a watermark and signed Content Credentials.
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ pip install markproof
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```python
16
+ from markproof import Markproof
17
+
18
+ client = Markproof(api_key="lt_live_...")
19
+
20
+ marked = client.mark("generated.png", generator_name="my-app", model_name="my-model-v1")
21
+ print(marked.watermark_code, marked.verify_url)
22
+ with open("generated-marked.png", "wb") as f:
23
+ f.write(marked.file)
24
+
25
+ result = client.verify("generated-marked.png")
26
+ print(result.summary)
27
+ ```
28
+
29
+ ## API
30
+
31
+ - `Markproof(api_key, base_url=DEFAULT_BASE_URL)` — `base_url` defaults to the production API; override for local dev.
32
+ - `client.mark(file, filename=None, generator_name=None, model_name=None, response_format="file", visible_label=None, label_position=None)` → `MarkResult` — `file` is a path (`str`/`os.PathLike`) or raw `bytes` (in which case `filename` is required). `response_format="file"` (default) returns the marked bytes inline (`result.file`/`result.content_type`); `"url"` returns a short-lived signed download URL instead (`result.file_url`, ~1 hour — download promptly rather than caching it). Either way you also get `asset_id`, `watermark_code`, `verify_url`, `c2pa_manifest_label`. `visible_label` (images only): `True` draws a visible "AI-generated" label (EU AI Act Art. 50(4)) before marking, or pass your own text (max 40 characters); `label_position` picks the corner (`"bottom-right"` default, `"bottom-left"`, `"top-right"`, `"top-left"`).
33
+ - `client.verify(file, filename=None)` or `client.verify(url=...)` → `VerifyResult` — `watermark`/`c2pa` detection results, `matched_asset` if the watermark matches a record in Markproof's database, and a human-readable `summary`. Direct uploads are limited to about 4.5 MB; pass a public `url` (up to 25 MB) for bigger files, such as marked audio, which comes back as WAV (e.g. a batch item's `file_url`).
34
+ - `client.get_asset(asset_id)` → `Asset` — requires an API key on the same project that created the asset.
35
+
36
+ All methods raise `MarkproofError` (with `.status` and `.body`) on a non-2xx response.
37
+
38
+ ## Batches and webhooks
39
+
40
+ For volume, queue many files at once instead of calling `mark` per file. Each item counts as one mark.
41
+
42
+ ```python
43
+ created = client.create_batch(
44
+ urls=["https://cdn.example.com/1.png", "https://cdn.example.com/2.png"], # and/or files=["a.png", (data, "b.wav")]
45
+ generator_name="your-app",
46
+ webhook_url="https://your-app.example.com/hooks/markproof", # optional
47
+ )
48
+ batch = client.wait_for_batch(created.batch_id) # or get_batch(...), or wait for the webhook
49
+ for item in batch.items:
50
+ print(item.index, item.status, item.file_url or item.error)
51
+ ```
52
+
53
+ - `client.create_batch(files=None, urls=None, generator_name=None, model_name=None, webhook_url=None)` → `CreatedBatch` — files go up in one multipart request (keep the total under ~4.5 MB; use `urls` for more). Max items per batch: 10 Free, 100 Indie, 1,000 Pro.
54
+ - `client.get_batch(batch_id)` → `Batch` — `status` (`queued`/`processing`/`completed`/`partial`/`failed`), `counts`, and `items` with `asset_id`, `watermark_code`, `verify_url`, `file_url` (signed, ~1 hour; call again for a fresh one) or `error`.
55
+ - `client.wait_for_batch(batch_id, interval=3.0, timeout=900, on_progress=None)` — polls until the batch is final.
56
+
57
+ Verify webhooks with the raw request body and your signing secret from the dashboard:
58
+
59
+ ```python
60
+ from markproof import verify_webhook_signature
61
+
62
+ ok = verify_webhook_signature(
63
+ request.get_data(), # the exact bytes received, not re-serialized JSON
64
+ request.headers.get("Markproof-Signature"),
65
+ os.environ["MARKPROOF_WEBHOOK_SECRET"],
66
+ )
67
+ ```
68
+
69
+ `parse_webhook_event(...)` does the same and returns the `batch.completed` event, raising `WebhookSignatureError` if the signature doesn't match. Event `id`s are stable across retries, so use them to drop duplicates.
@@ -198,14 +198,26 @@ class Markproof:
198
198
  file=response.content,
199
199
  )
200
200
 
201
- def verify(self, file: FileLike, filename: Optional[str] = None) -> VerifyResult:
202
- """Checks a file for our watermark and/or a C2PA manifest."""
203
- data, resolved_filename, content_type = _resolve_file(file, filename)
204
- response = self._session.post(
205
- f"{self._base_url}/verify",
206
- headers=self._headers(),
207
- files={"file": (resolved_filename, data, content_type)},
208
- )
201
+ def verify(
202
+ self, file: Optional[FileLike] = None, filename: Optional[str] = None, *, url: Optional[str] = None
203
+ ) -> VerifyResult:
204
+ """Checks a file for our watermark and/or a C2PA manifest.
205
+
206
+ Pass ``url`` instead of ``file`` for anything over ~4.5 MB (the
207
+ direct-upload limit), e.g. marked audio, which comes back as WAV;
208
+ URLs up to 25 MB work.
209
+ """
210
+ if (file is None) == (url is None):
211
+ raise ValueError("Pass exactly one of file or url.")
212
+ if url is not None:
213
+ response = self._session.post(f"{self._base_url}/verify", headers=self._headers(), json={"url": url})
214
+ else:
215
+ data, resolved_filename, content_type = _resolve_file(file, filename)
216
+ response = self._session.post(
217
+ f"{self._base_url}/verify",
218
+ headers=self._headers(),
219
+ files={"file": (resolved_filename, data, content_type)},
220
+ )
209
221
  self._raise_if_error(response)
210
222
  body = response.json()
211
223
  return VerifyResult(
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: markproof
3
- Version: 0.2.0
3
+ Version: 0.3.0
4
4
  Summary: Python client for the Markproof API - EU AI Act Article 50 watermarking and verification.
5
5
  Author-email: Markproof <support@markproof.co>
6
6
  License-Expression: MIT
@@ -49,7 +49,7 @@ print(result.summary)
49
49
 
50
50
  - `Markproof(api_key, base_url=DEFAULT_BASE_URL)` — `base_url` defaults to the production API; override for local dev.
51
51
  - `client.mark(file, filename=None, generator_name=None, model_name=None, response_format="file", visible_label=None, label_position=None)` → `MarkResult` — `file` is a path (`str`/`os.PathLike`) or raw `bytes` (in which case `filename` is required). `response_format="file"` (default) returns the marked bytes inline (`result.file`/`result.content_type`); `"url"` returns a short-lived signed download URL instead (`result.file_url`, ~1 hour — download promptly rather than caching it). Either way you also get `asset_id`, `watermark_code`, `verify_url`, `c2pa_manifest_label`. `visible_label` (images only): `True` draws a visible "AI-generated" label (EU AI Act Art. 50(4)) before marking, or pass your own text (max 40 characters); `label_position` picks the corner (`"bottom-right"` default, `"bottom-left"`, `"top-right"`, `"top-left"`).
52
- - `client.verify(file, filename=None)` → `VerifyResult` — `watermark`/`c2pa` detection results, `matched_asset` if the watermark matches a record in Markproof's database, and a human-readable `summary`.
52
+ - `client.verify(file, filename=None)` or `client.verify(url=...)` → `VerifyResult` — `watermark`/`c2pa` detection results, `matched_asset` if the watermark matches a record in Markproof's database, and a human-readable `summary`. Direct uploads are limited to about 4.5 MB; pass a public `url` (up to 25 MB) for bigger files, such as marked audio, which comes back as WAV (e.g. a batch item's `file_url`).
53
53
  - `client.get_asset(asset_id)` → `Asset` — requires an API key on the same project that created the asset.
54
54
 
55
55
  All methods raise `MarkproofError` (with `.status` and `.body`) on a non-2xx response.
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "markproof"
7
- version = "0.2.0"
7
+ version = "0.3.0"
8
8
  description = "Python client for the Markproof API - EU AI Act Article 50 watermarking and verification."
9
9
  readme = "README.md"
10
10
  license = "MIT"
@@ -1,249 +1,278 @@
1
- import pytest
2
- import responses
3
-
4
- from markproof import Markproof, MarkproofError
5
-
6
- API_KEY = "lt_test_abc123"
7
- BASE_URL = "http://localhost:3000/api/v1"
8
-
9
-
10
- @pytest.fixture
11
- def client():
12
- return Markproof(api_key=API_KEY, base_url=BASE_URL)
13
-
14
-
15
- def test_requires_api_key():
16
- with pytest.raises(ValueError, match="api_key is required"):
17
- Markproof(api_key="")
18
-
19
-
20
- def test_strips_trailing_slash():
21
- client = Markproof(api_key=API_KEY, base_url="http://localhost:3000/api/v1/")
22
- assert client._base_url == "http://localhost:3000/api/v1"
23
-
24
-
25
- @responses.activate
26
- def test_mark_parses_binary_response_and_headers(client):
27
- file_bytes = b"\x01\x02\x03\x04"
28
- responses.add(
29
- responses.POST,
30
- f"{BASE_URL}/mark",
31
- body=file_bytes,
32
- status=200,
33
- content_type="image/png",
34
- headers={
35
- "x-asset-id": "asset-1",
36
- "x-watermark-code": "ABC12345",
37
- "x-verify-url": "https://markproof.co/v/ABC12345",
38
- "x-c2pa-manifest-label": "urn:c2pa:xyz",
39
- },
40
- )
41
-
42
- result = client.mark(file_bytes, filename="test.png")
43
-
44
- assert result.content_type == "image/png"
45
- assert result.asset_id == "asset-1"
46
- assert result.watermark_code == "ABC12345"
47
- assert result.kind == "image"
48
- assert result.verify_url == "https://markproof.co/v/ABC12345"
49
- assert result.c2pa_manifest_label == "urn:c2pa:xyz"
50
- assert result.file == file_bytes
51
-
52
- sent = responses.calls[0].request
53
- assert sent.headers["Authorization"] == f"Bearer {API_KEY}"
54
-
55
-
56
- @responses.activate
57
- def test_mark_requires_filename_for_raw_bytes(client):
58
- with pytest.raises(ValueError, match="filename is required"):
59
- client.mark(b"abc")
60
-
61
-
62
- @responses.activate
63
- def test_mark_detects_audio_from_content_type(client):
64
- responses.add(
65
- responses.POST,
66
- f"{BASE_URL}/mark",
67
- body=b"\x00",
68
- status=200,
69
- content_type="audio/wav",
70
- headers={
71
- "x-asset-id": "a",
72
- "x-watermark-code": "b",
73
- "x-verify-url": "c",
74
- },
75
- )
76
- result = client.mark(b"fake-wav-bytes", filename="test.wav")
77
- assert result.content_type == "audio/wav"
78
- assert result.kind == "audio"
79
- # Audio isn't self-checked, so the API omits the header.
80
- assert result.watermark_robust is None
81
-
82
-
83
- @responses.activate
84
- def test_mark_parses_watermark_robust_header(client):
85
- responses.add(
86
- responses.POST,
87
- f"{BASE_URL}/mark",
88
- body=b"\x00",
89
- status=200,
90
- content_type="image/png",
91
- headers={"x-asset-id": "a", "x-watermark-code": "b", "x-verify-url": "c", "x-watermark-robust": "false"},
92
- )
93
- assert client.mark(b"img", filename="x.png").watermark_robust is False
94
-
95
-
96
- @responses.activate
97
- def test_mark_raises_markproof_error_with_server_message(client):
98
- responses.add(
99
- responses.POST,
100
- f"{BASE_URL}/mark",
101
- json={"error": "Monthly quota exceeded (10/10)."},
102
- status=429,
103
- )
104
- with pytest.raises(MarkproofError) as exc_info:
105
- client.mark(b"abc", filename="test.png")
106
- assert exc_info.value.status == 429
107
- assert str(exc_info.value) == "Monthly quota exceeded (10/10)."
108
-
109
-
110
- @responses.activate
111
- def test_mark_with_response_format_url(client):
112
- responses.add(
113
- responses.POST,
114
- f"{BASE_URL}/mark",
115
- json={
116
- "asset_id": "asset-1",
117
- "watermark_code": "ABC12345",
118
- "kind": "image",
119
- "verify_url": "https://markproof.co/v/ABC12345",
120
- "c2pa_manifest_label": "urn:c2pa:xyz",
121
- "file_url": "https://storage.example/signed-url",
122
- },
123
- status=200,
124
- )
125
- result = client.mark(b"abc", filename="test.png", response_format="url")
126
-
127
- assert result.file_url == "https://storage.example/signed-url"
128
- assert result.file is None
129
- assert result.asset_id == "asset-1"
130
-
131
- sent = responses.calls[0].request
132
- assert b'name="response_format"' in sent.body
133
-
134
-
135
- def _ok_image_response():
136
- responses.add(
137
- responses.POST,
138
- f"{BASE_URL}/mark",
139
- body=b"\x00",
140
- status=200,
141
- content_type="image/png",
142
- headers={"x-asset-id": "a", "x-watermark-code": "b", "x-verify-url": "c"},
143
- )
144
-
145
-
146
- @responses.activate
147
- def test_mark_sends_visible_label_true_and_position(client):
148
- _ok_image_response()
149
- client.mark(b"img", filename="x.png", visible_label=True, label_position="top-left")
150
- body = responses.calls[0].request.body
151
- assert b'name="visible_label"\r\n\r\ntrue\r\n' in body
152
- assert b'name="label_position"\r\n\r\ntop-left\r\n' in body
153
-
154
-
155
- @responses.activate
156
- def test_mark_sends_custom_visible_label(client):
157
- _ok_image_response()
158
- client.mark(b"img", filename="x.png", visible_label="Généré par IA")
159
- body = responses.calls[0].request.body
160
- assert 'name="visible_label"\r\n\r\nGénéré par IA\r\n'.encode() in body
161
- assert b'name="label_position"' not in body
162
-
163
-
164
- @responses.activate
165
- def test_mark_omits_label_fields_by_default(client):
166
- _ok_image_response()
167
- client.mark(b"img", filename="x.png", label_position="top-left")
168
- body = responses.calls[0].request.body
169
- assert b'name="visible_label"' not in body
170
- assert b'name="label_position"' not in body
171
-
172
-
173
- @responses.activate
174
- def test_verify_converts_snake_case_response(client):
175
- responses.add(
176
- responses.POST,
177
- f"{BASE_URL}/verify",
178
- json={
179
- "watermark": {"detected": True, "watermark_code": "ABC12345", "confidence": None},
180
- "c2pa": {"present": True, "signature_valid": True, "trusted": False, "manifest_label": "urn:c2pa:xyz"},
181
- "matched_asset": {
182
- "id": "asset-1",
183
- "watermark_code": "ABC12345",
184
- "kind": "image",
185
- "generator_name": "midjourney",
186
- "model_name": "v7",
187
- "c2pa_manifest_label": "urn:c2pa:xyz",
188
- "created_at": "2026-10-25T00:00:00Z",
189
- },
190
- "summary": "Watermark detected code ABC12345, matches our record.",
191
- },
192
- status=200,
193
- )
194
- result = client.verify(b"abc", filename="test.png")
195
-
196
- assert result.watermark.detected is True
197
- assert result.watermark.watermark_code == "ABC12345"
198
- assert result.c2pa.trusted is False
199
- assert result.matched_asset is not None
200
- assert result.matched_asset.generator_name == "midjourney"
201
-
202
-
203
- @responses.activate
204
- def test_verify_handles_no_matched_asset(client):
205
- responses.add(
206
- responses.POST,
207
- f"{BASE_URL}/verify",
208
- json={
209
- "watermark": {"detected": False, "watermark_code": None, "confidence": None},
210
- "c2pa": {"present": False, "signature_valid": None, "trusted": None, "manifest_label": None},
211
- "matched_asset": None,
212
- "summary": "No watermark detected.",
213
- },
214
- status=200,
215
- )
216
- result = client.verify(b"abc", filename="test.png")
217
- assert result.matched_asset is None
218
-
219
-
220
- @responses.activate
221
- def test_get_asset(client):
222
- responses.add(
223
- responses.GET,
224
- f"{BASE_URL}/assets/asset-1",
225
- json={
226
- "id": "asset-1",
227
- "watermark_code": "ABC12345",
228
- "kind": "image",
229
- "generator_name": None,
230
- "model_name": None,
231
- "c2pa_manifest_label": None,
232
- "created_at": "2026-10-25T00:00:00Z",
233
- },
234
- status=200,
235
- )
236
- asset = client.get_asset("asset-1")
237
- assert asset.id == "asset-1"
238
-
239
-
240
- @responses.activate
241
- def test_get_asset_url_encodes_id(client):
242
- responses.add(
243
- responses.GET,
244
- f"{BASE_URL}/assets/weird%2Fid",
245
- json={"error": "Not found."},
246
- status=404,
247
- )
248
- with pytest.raises(MarkproofError):
249
- client.get_asset("weird/id")
1
+ import json
2
+
3
+ import pytest
4
+ import responses
5
+
6
+ from markproof import Markproof, MarkproofError
7
+
8
+ API_KEY = "lt_test_abc123"
9
+ BASE_URL = "http://localhost:3000/api/v1"
10
+
11
+
12
+ @pytest.fixture
13
+ def client():
14
+ return Markproof(api_key=API_KEY, base_url=BASE_URL)
15
+
16
+
17
+ def test_requires_api_key():
18
+ with pytest.raises(ValueError, match="api_key is required"):
19
+ Markproof(api_key="")
20
+
21
+
22
+ def test_strips_trailing_slash():
23
+ client = Markproof(api_key=API_KEY, base_url="http://localhost:3000/api/v1/")
24
+ assert client._base_url == "http://localhost:3000/api/v1"
25
+
26
+
27
+ @responses.activate
28
+ def test_mark_parses_binary_response_and_headers(client):
29
+ file_bytes = b"\x01\x02\x03\x04"
30
+ responses.add(
31
+ responses.POST,
32
+ f"{BASE_URL}/mark",
33
+ body=file_bytes,
34
+ status=200,
35
+ content_type="image/png",
36
+ headers={
37
+ "x-asset-id": "asset-1",
38
+ "x-watermark-code": "ABC12345",
39
+ "x-verify-url": "https://markproof.co/v/ABC12345",
40
+ "x-c2pa-manifest-label": "urn:c2pa:xyz",
41
+ },
42
+ )
43
+
44
+ result = client.mark(file_bytes, filename="test.png")
45
+
46
+ assert result.content_type == "image/png"
47
+ assert result.asset_id == "asset-1"
48
+ assert result.watermark_code == "ABC12345"
49
+ assert result.kind == "image"
50
+ assert result.verify_url == "https://markproof.co/v/ABC12345"
51
+ assert result.c2pa_manifest_label == "urn:c2pa:xyz"
52
+ assert result.file == file_bytes
53
+
54
+ sent = responses.calls[0].request
55
+ assert sent.headers["Authorization"] == f"Bearer {API_KEY}"
56
+
57
+
58
+ @responses.activate
59
+ def test_mark_requires_filename_for_raw_bytes(client):
60
+ with pytest.raises(ValueError, match="filename is required"):
61
+ client.mark(b"abc")
62
+
63
+
64
+ @responses.activate
65
+ def test_mark_detects_audio_from_content_type(client):
66
+ responses.add(
67
+ responses.POST,
68
+ f"{BASE_URL}/mark",
69
+ body=b"\x00",
70
+ status=200,
71
+ content_type="audio/wav",
72
+ headers={
73
+ "x-asset-id": "a",
74
+ "x-watermark-code": "b",
75
+ "x-verify-url": "c",
76
+ },
77
+ )
78
+ result = client.mark(b"fake-wav-bytes", filename="test.wav")
79
+ assert result.content_type == "audio/wav"
80
+ assert result.kind == "audio"
81
+ # Audio isn't self-checked, so the API omits the header.
82
+ assert result.watermark_robust is None
83
+
84
+
85
+ @responses.activate
86
+ def test_mark_parses_watermark_robust_header(client):
87
+ responses.add(
88
+ responses.POST,
89
+ f"{BASE_URL}/mark",
90
+ body=b"\x00",
91
+ status=200,
92
+ content_type="image/png",
93
+ headers={"x-asset-id": "a", "x-watermark-code": "b", "x-verify-url": "c", "x-watermark-robust": "false"},
94
+ )
95
+ assert client.mark(b"img", filename="x.png").watermark_robust is False
96
+
97
+
98
+ @responses.activate
99
+ def test_mark_raises_markproof_error_with_server_message(client):
100
+ responses.add(
101
+ responses.POST,
102
+ f"{BASE_URL}/mark",
103
+ json={"error": "Monthly quota exceeded (10/10)."},
104
+ status=429,
105
+ )
106
+ with pytest.raises(MarkproofError) as exc_info:
107
+ client.mark(b"abc", filename="test.png")
108
+ assert exc_info.value.status == 429
109
+ assert str(exc_info.value) == "Monthly quota exceeded (10/10)."
110
+
111
+
112
+ @responses.activate
113
+ def test_mark_with_response_format_url(client):
114
+ responses.add(
115
+ responses.POST,
116
+ f"{BASE_URL}/mark",
117
+ json={
118
+ "asset_id": "asset-1",
119
+ "watermark_code": "ABC12345",
120
+ "kind": "image",
121
+ "verify_url": "https://markproof.co/v/ABC12345",
122
+ "c2pa_manifest_label": "urn:c2pa:xyz",
123
+ "file_url": "https://storage.example/signed-url",
124
+ },
125
+ status=200,
126
+ )
127
+ result = client.mark(b"abc", filename="test.png", response_format="url")
128
+
129
+ assert result.file_url == "https://storage.example/signed-url"
130
+ assert result.file is None
131
+ assert result.asset_id == "asset-1"
132
+
133
+ sent = responses.calls[0].request
134
+ assert b'name="response_format"' in sent.body
135
+
136
+
137
+ def _ok_image_response():
138
+ responses.add(
139
+ responses.POST,
140
+ f"{BASE_URL}/mark",
141
+ body=b"\x00",
142
+ status=200,
143
+ content_type="image/png",
144
+ headers={"x-asset-id": "a", "x-watermark-code": "b", "x-verify-url": "c"},
145
+ )
146
+
147
+
148
+ @responses.activate
149
+ def test_mark_sends_visible_label_true_and_position(client):
150
+ _ok_image_response()
151
+ client.mark(b"img", filename="x.png", visible_label=True, label_position="top-left")
152
+ body = responses.calls[0].request.body
153
+ assert b'name="visible_label"\r\n\r\ntrue\r\n' in body
154
+ assert b'name="label_position"\r\n\r\ntop-left\r\n' in body
155
+
156
+
157
+ @responses.activate
158
+ def test_mark_sends_custom_visible_label(client):
159
+ _ok_image_response()
160
+ client.mark(b"img", filename="x.png", visible_label="Généré par IA")
161
+ body = responses.calls[0].request.body
162
+ assert 'name="visible_label"\r\n\r\nGénéré par IA\r\n'.encode() in body
163
+ assert b'name="label_position"' not in body
164
+
165
+
166
+ @responses.activate
167
+ def test_mark_omits_label_fields_by_default(client):
168
+ _ok_image_response()
169
+ client.mark(b"img", filename="x.png", label_position="top-left")
170
+ body = responses.calls[0].request.body
171
+ assert b'name="visible_label"' not in body
172
+ assert b'name="label_position"' not in body
173
+
174
+
175
+ @responses.activate
176
+ def test_verify_converts_snake_case_response(client):
177
+ responses.add(
178
+ responses.POST,
179
+ f"{BASE_URL}/verify",
180
+ json={
181
+ "watermark": {"detected": True, "watermark_code": "ABC12345", "confidence": None},
182
+ "c2pa": {"present": True, "signature_valid": True, "trusted": False, "manifest_label": "urn:c2pa:xyz"},
183
+ "matched_asset": {
184
+ "id": "asset-1",
185
+ "watermark_code": "ABC12345",
186
+ "kind": "image",
187
+ "generator_name": "midjourney",
188
+ "model_name": "v7",
189
+ "c2pa_manifest_label": "urn:c2pa:xyz",
190
+ "created_at": "2026-10-25T00:00:00Z",
191
+ },
192
+ "summary": "Watermark detected code ABC12345, matches our record.",
193
+ },
194
+ status=200,
195
+ )
196
+ result = client.verify(b"abc", filename="test.png")
197
+
198
+ assert result.watermark.detected is True
199
+ assert result.watermark.watermark_code == "ABC12345"
200
+ assert result.c2pa.trusted is False
201
+ assert result.matched_asset is not None
202
+ assert result.matched_asset.generator_name == "midjourney"
203
+
204
+
205
+ @responses.activate
206
+ def test_verify_handles_no_matched_asset(client):
207
+ responses.add(
208
+ responses.POST,
209
+ f"{BASE_URL}/verify",
210
+ json={
211
+ "watermark": {"detected": False, "watermark_code": None, "confidence": None},
212
+ "c2pa": {"present": False, "signature_valid": None, "trusted": None, "manifest_label": None},
213
+ "matched_asset": None,
214
+ "summary": "No watermark detected.",
215
+ },
216
+ status=200,
217
+ )
218
+ result = client.verify(b"abc", filename="test.png")
219
+ assert result.matched_asset is None
220
+
221
+
222
+ @responses.activate
223
+ def test_verify_by_url_sends_json(client):
224
+ responses.add(
225
+ responses.POST,
226
+ f"{BASE_URL}/verify",
227
+ json={
228
+ "watermark": {"detected": True, "watermark_code": None, "confidence": 0.98},
229
+ "c2pa": {"present": True, "signature_valid": True, "trusted": False, "manifest_label": "urn:c2pa:x"},
230
+ "matched_asset": None,
231
+ "summary": "Watermark detected.",
232
+ },
233
+ status=200,
234
+ )
235
+ result = client.verify(url="https://cdn.example.com/a.wav")
236
+ sent = responses.calls[0].request
237
+ assert sent.headers["Content-Type"] == "application/json"
238
+ assert json.loads(sent.body) == {"url": "https://cdn.example.com/a.wav"}
239
+ assert result.watermark.confidence == 0.98
240
+
241
+
242
+ def test_verify_needs_exactly_one_input(client):
243
+ with pytest.raises(ValueError):
244
+ client.verify()
245
+ with pytest.raises(ValueError):
246
+ client.verify(b"abc", url="https://cdn.example.com/a.wav")
247
+
248
+
249
+ @responses.activate
250
+ def test_get_asset(client):
251
+ responses.add(
252
+ responses.GET,
253
+ f"{BASE_URL}/assets/asset-1",
254
+ json={
255
+ "id": "asset-1",
256
+ "watermark_code": "ABC12345",
257
+ "kind": "image",
258
+ "generator_name": None,
259
+ "model_name": None,
260
+ "c2pa_manifest_label": None,
261
+ "created_at": "2026-10-25T00:00:00Z",
262
+ },
263
+ status=200,
264
+ )
265
+ asset = client.get_asset("asset-1")
266
+ assert asset.id == "asset-1"
267
+
268
+
269
+ @responses.activate
270
+ def test_get_asset_url_encodes_id(client):
271
+ responses.add(
272
+ responses.GET,
273
+ f"{BASE_URL}/assets/weird%2Fid",
274
+ json={"error": "Not found."},
275
+ status=404,
276
+ )
277
+ with pytest.raises(MarkproofError):
278
+ client.get_asset("weird/id")
File without changes
File without changes
File without changes