2022-06-24 18:14:20 +00:00
|
|
|
import json
|
2023-01-11 17:02:01 +00:00
|
|
|
from datetime import datetime, timezone, timedelta
|
2022-06-29 17:39:46 +00:00
|
|
|
from mnemonic import Mnemonic
|
2022-06-24 18:14:20 +00:00
|
|
|
|
2024-03-18 17:40:48 +00:00
|
|
|
from selfprivacy_api.jobs import Job, JobStatus
|
|
|
|
|
2023-01-04 16:49:40 +00:00
|
|
|
# for expiration tests. If headache, consider freezegun
|
2023-01-11 17:02:01 +00:00
|
|
|
RECOVERY_KEY_VALIDATION_DATETIME = "selfprivacy_api.models.tokens.time.datetime"
|
|
|
|
DEVICE_KEY_VALIDATION_DATETIME = RECOVERY_KEY_VALIDATION_DATETIME
|
2023-01-04 16:49:40 +00:00
|
|
|
|
2023-11-01 16:46:36 +00:00
|
|
|
|
2024-03-08 12:40:02 +00:00
|
|
|
def ten_hours_into_future_naive():
|
|
|
|
return datetime.now() + timedelta(hours=10)
|
2023-11-01 16:46:36 +00:00
|
|
|
|
|
|
|
|
2024-03-08 12:40:02 +00:00
|
|
|
def ten_hours_into_future_naive_utc():
|
|
|
|
return datetime.utcnow() + timedelta(hours=10)
|
2023-11-10 12:29:25 +00:00
|
|
|
|
|
|
|
|
2024-03-08 12:40:02 +00:00
|
|
|
def ten_hours_into_future():
|
|
|
|
return datetime.now(timezone.utc) + timedelta(hours=10)
|
2023-11-01 16:46:36 +00:00
|
|
|
|
|
|
|
|
2024-01-09 18:58:09 +00:00
|
|
|
def ten_minutes_into_past_naive():
|
|
|
|
return datetime.now() - timedelta(minutes=10)
|
2023-11-01 16:46:36 +00:00
|
|
|
|
|
|
|
|
2024-01-09 18:58:09 +00:00
|
|
|
def ten_minutes_into_past_naive_utc():
|
|
|
|
return datetime.utcnow() - timedelta(minutes=10)
|
2023-11-10 12:29:25 +00:00
|
|
|
|
|
|
|
|
2024-01-09 18:58:09 +00:00
|
|
|
def ten_minutes_into_past():
|
|
|
|
return datetime.now(timezone.utc) - timedelta(minutes=10)
|
2023-01-04 16:49:40 +00:00
|
|
|
|
2023-01-11 17:02:01 +00:00
|
|
|
|
|
|
|
class NearFuture(datetime):
|
2023-01-04 16:49:40 +00:00
|
|
|
@classmethod
|
2023-01-11 17:02:01 +00:00
|
|
|
def now(cls, tz=None):
|
2024-03-08 12:40:02 +00:00
|
|
|
return datetime.now(tz) + timedelta(hours=13)
|
2023-01-04 16:49:40 +00:00
|
|
|
|
2023-11-13 16:15:12 +00:00
|
|
|
@classmethod
|
|
|
|
def utcnow(cls):
|
2024-03-08 12:40:02 +00:00
|
|
|
return datetime.utcnow() + timedelta(hours=13)
|
2023-11-13 16:15:12 +00:00
|
|
|
|
2022-07-07 13:53:19 +00:00
|
|
|
|
2022-06-24 18:14:20 +00:00
|
|
|
def read_json(file_path):
|
|
|
|
with open(file_path, "r", encoding="utf-8") as file:
|
|
|
|
return json.load(file)
|
|
|
|
|
|
|
|
|
|
|
|
def write_json(file_path, data):
|
|
|
|
with open(file_path, "w", encoding="utf-8") as file:
|
|
|
|
json.dump(data, file, indent=4)
|
2022-06-29 17:39:46 +00:00
|
|
|
|
2022-07-07 13:53:19 +00:00
|
|
|
|
2022-06-29 17:39:46 +00:00
|
|
|
def generate_api_query(query_array):
|
|
|
|
return "query TestApi {\n api {" + "\n".join(query_array) + "}\n}"
|
|
|
|
|
2022-07-07 13:53:19 +00:00
|
|
|
|
2022-07-05 05:14:37 +00:00
|
|
|
def generate_system_query(query_array):
|
|
|
|
return "query TestSystem {\n system {" + "\n".join(query_array) + "}\n}"
|
|
|
|
|
2022-07-07 13:53:19 +00:00
|
|
|
|
2022-08-01 10:40:40 +00:00
|
|
|
def generate_users_query(query_array):
|
|
|
|
return "query TestUsers {\n users {" + "\n".join(query_array) + "}\n}"
|
|
|
|
|
|
|
|
|
2023-06-14 10:06:01 +00:00
|
|
|
def generate_backup_query(query_array):
|
|
|
|
return "query TestBackup {\n backup {" + "\n".join(query_array) + "}\n}"
|
|
|
|
|
|
|
|
|
2024-05-15 13:01:07 +00:00
|
|
|
def generate_jobs_query(query_array):
|
|
|
|
return "query TestJobs {\n jobs {" + "\n".join(query_array) + "}\n}"
|
|
|
|
|
|
|
|
|
2023-09-20 14:29:06 +00:00
|
|
|
def generate_service_query(query_array):
|
|
|
|
return "query TestService {\n services {" + "\n".join(query_array) + "}\n}"
|
|
|
|
|
|
|
|
|
2022-06-29 17:39:46 +00:00
|
|
|
def mnemonic_to_hex(mnemonic):
|
|
|
|
return Mnemonic(language="english").to_entropy(mnemonic).hex()
|
2023-01-06 10:59:59 +00:00
|
|
|
|
|
|
|
|
2023-11-10 17:10:01 +00:00
|
|
|
def assert_recovery_recent(time_generated: str):
|
|
|
|
assert datetime.fromisoformat(time_generated) - timedelta(seconds=5) < datetime.now(
|
|
|
|
timezone.utc
|
2023-01-06 10:59:59 +00:00
|
|
|
)
|
2024-03-18 17:40:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
def assert_job_errored(job: Job):
|
|
|
|
assert job is not None
|
|
|
|
assert job.status == JobStatus.ERROR
|
|
|
|
|
|
|
|
# consider adding a useful error message to an errored-out job
|
|
|
|
assert job.error is not None
|
|
|
|
assert job.error != ""
|