sbt-idp/tests/utils.py

98 lines
3.0 KiB
Python
Raw Permalink Normal View History

2023-12-26 07:09:31 +00:00
import requests
import traceback
import logging
import time
from requests_toolbelt import MultipartEncoderMonitor
def login(host, username, password):
print(host, username, password)
response = requests.post(f'{host}/api/ctel/login/', json={
'username': username,
'password': password
})
try:
token = response.json().get('token', '')
except:
logging.error("Failed to login")
traceback.print_exc()
raise ValueError("Could not login. Please check host, username or password.")
if not token:
logging.error(response.content)
raise ValueError("Could not get a valid token. Please check host, username or password.")
return token
2024-04-08 07:45:16 +00:00
def make_sbt_request(host, token, invoice_files=None, imei_files=None, ensure_success=True, sub=None):
2023-12-26 07:09:31 +00:00
files = []
if invoice_files:
for file in invoice_files:
files.append(('invoice_file', (file, open(file, "rb"), 'application/octet-stream')))
if imei_files:
for file in imei_files:
files.append(('imei_files', (file, open(file, "rb"), 'application/octet-stream')))
num_files = len(files)
files.append(('processType', '12'))
2024-01-23 07:16:22 +00:00
files.append(('is_test_request', 'True'))
2024-04-08 07:45:16 +00:00
if sub:
files.append(("subsidiary", sub))
2023-12-26 07:09:31 +00:00
start_time = time.time()
end_of_upload_time = 0
def upload_callback(monitor):
nonlocal end_of_upload_time
if monitor.bytes_read == monitor.len:
end_of_upload_time = time.time()
m = MultipartEncoderMonitor.from_fields(
fields=files,
callback=upload_callback
)
try:
response = requests.post(f'{host}/api/ctel/images/process_sync/', headers={
'Authorization': token,
'Content-Type': m.content_type
}, data=m, timeout=300)
except requests.exceptions.Timeout as e:
print(e)
traceback.print_exc()
raise TimeoutError("Timeout when sending request to server.")
except Exception as e:
print(e)
traceback.print_exc()
raise e
upload_time = end_of_upload_time - start_time
process_time = time.time() - start_time - upload_time
2023-12-26 08:58:44 +00:00
parsed_data = None
try:
parsed_data = response.json()
except:
pass
if ensure_success:
assert upload_time > 0
assert process_time > 0
assert num_files > 0
assert response.status_code == 200
2023-12-26 07:09:31 +00:00
2023-12-26 08:58:44 +00:00
assert parsed_data is not None
assert "id" in parsed_data
assert "pages" in parsed_data
assert "created_at" in parsed_data
assert "data" in parsed_data
assert "files" in parsed_data
2023-12-26 07:09:31 +00:00
return {
"upload_time": upload_time,
"process_time": process_time,
2023-12-26 08:58:44 +00:00
"data": parsed_data if parsed_data is not None else response.content,
2023-12-26 07:09:31 +00:00
"num_files": num_files,
}
def get_field(document, label):
for field in document["content"]:
if field["label"] == label:
return field["value"]
return None