Skip to content

Commit 7864b54

Browse files
author
Dan Hertz
committed
add tests for chunking
1 parent 4c45e6e commit 7864b54

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

nightfall/api.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44
~~~~~~~~~~~~~
55
This module provides a class which abstracts the Nightfall REST API.
66
"""
7-
import time
87
from datetime import datetime, timedelta
98
import hmac
109
import hashlib
11-
import json
1210
import logging
1311
import os
1412
from typing import Dict, List, Tuple, Optional
@@ -103,7 +101,7 @@ def scan_text(self, texts: List[str], detection_rules: Optional[List[DetectionRu
103101
return findings, parsed_response.get("redactedPayload")
104102

105103
def _scan_text_v3(self, data: dict):
106-
response = self.session.post(url=self.TEXT_SCAN_ENDPOINT_V3, data=json.dumps(data))
104+
response = self.session.post(url=self.TEXT_SCAN_ENDPOINT_V3, json=data)
107105

108106
self.logger.debug(f"HTTP Request URL: {response.request.url}")
109107
self.logger.debug(f"HTTP Request Body: {response.request.body}")
@@ -166,7 +164,7 @@ def _file_scan_initialize(self, location: str):
166164
data = {
167165
"fileSizeBytes": os.path.getsize(location)
168166
}
169-
response = self.session.post(url=self.FILE_SCAN_INITIALIZE_ENDPOINT, data=json.dumps(data))
167+
response = self.session.post(url=self.FILE_SCAN_INITIALIZE_ENDPOINT, json=data)
170168

171169
return response
172170

@@ -216,7 +214,7 @@ def _file_scan_scan(self, session_id: str, detection_rules: Optional[List[Detect
216214
if request_metadata:
217215
data["requestMetadata"] = request_metadata
218216

219-
response = self.session.post(url=self.FILE_SCAN_SCAN_ENDPOINT.format(session_id), data=json.dumps(data))
217+
response = self.session.post(url=self.FILE_SCAN_SCAN_ENDPOINT.format(session_id), json=data)
220218
return response
221219

222220
def validate_webhook(self, request_signature: str, request_timestamp: str, request_data: str) -> bool:

tests/test_api.py

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,17 +269,49 @@ def test_scan_file(tmpdir):
269269
for call in responses.calls:
270270
assert call.request.headers.get("Authorization") == "Bearer NF-NOT_REAL"
271271

272-
assert responses.calls[0].request.body == '{"fileSizeBytes": 44}'
272+
assert responses.calls[0].request.body == b'{"fileSizeBytes": 44}'
273273
assert responses.calls[1].request.body == b"4916-6734-7572-5015 is"
274274
assert responses.calls[1].request.headers.get("X-UPLOAD-OFFSET") == '0'
275275
assert responses.calls[2].request.body == b" my credit card number"
276276
assert responses.calls[2].request.headers.get("X-UPLOAD-OFFSET") == '22'
277-
assert responses.calls[4].request.body == '{"policy": {"webhookURL": "https://my-website.example/callback", ' \
278-
'"detectionRuleUUIDs": ["a_uuid"]}, "requestMetadata": "some test data"}'
277+
assert responses.calls[4].request.body == b'{"policy": {"webhookURL": "https://my-website.example/callback", ' \
278+
b'"detectionRuleUUIDs": ["a_uuid"]}, "requestMetadata": "some test data"}'
279279
assert id == 1
280280
assert message == "scan_started"
281281

282282

283+
@responses.activate
284+
def test_file_scan_upload_short(tmpdir):
285+
file = tmpdir.mkdir("test_data").join("file.txt")
286+
287+
file.write("4916-6734-7572-5015 is my credit card number")
288+
289+
nightfall = Nightfall("NF-NOT_REAL")
290+
291+
responses.add(responses.PATCH, 'https://api.nightfall.ai/v3/upload/1', status=204)
292+
293+
assert nightfall._file_scan_upload(1, file, 200)
294+
assert len(responses.calls) == 1
295+
296+
297+
@responses.activate
298+
def test_file_scan_upload_long(tmpdir):
299+
file = tmpdir.mkdir("test_data").join("file.txt")
300+
test_str = b"4916-6734-7572-5015 is my credit card number"
301+
file.write_binary(test_str)
302+
303+
responses.add(responses.PATCH, 'https://api.nightfall.ai/v3/upload/1', status=204)
304+
305+
nightfall = Nightfall("NF-NOT_REAL")
306+
307+
assert nightfall._file_scan_upload(1, file, 1)
308+
assert len(responses.calls) == 44
309+
for i, call in enumerate(responses.calls):
310+
assert call.request.headers.get("Authorization") == "Bearer NF-NOT_REAL"
311+
assert call.request.body.decode('utf-8') == test_str.decode('utf-8')[i]
312+
assert call.request.headers.get("X-UPLOAD-OFFSET") == str(i)
313+
314+
283315
@freeze_time("2021-10-04T17:30:50Z")
284316
def test_validate_webhook(nightfall):
285317
nightfall.signing_secret = "super-secret-shhhh"

0 commit comments

Comments
 (0)