mirror of
https://git.selfprivacy.org/SelfPrivacy/selfprivacy-rest-api.git
synced 2025-02-18 07:14:40 +00:00
test(tokens-repo): use mock time for recovery tokens expiration test
This commit is contained in:
parent
e0bd6efcb2
commit
3aa3d197e2
|
@ -32,9 +32,16 @@ DATE_FORMATS = [
|
||||||
]
|
]
|
||||||
|
|
||||||
# for expiration tests. If headache, consider freezegun
|
# for expiration tests. If headache, consider freezegun
|
||||||
|
RECOVERY_KEY_VALIDATION_DATETIME = "selfprivacy_api.models.tokens.recovery_key.datetime"
|
||||||
DEVICE_KEY_VALIDATION_DATETIME = "selfprivacy_api.models.tokens.new_device_key.datetime"
|
DEVICE_KEY_VALIDATION_DATETIME = "selfprivacy_api.models.tokens.new_device_key.datetime"
|
||||||
|
|
||||||
|
|
||||||
|
class NearFuture(datetime.datetime):
|
||||||
|
@classmethod
|
||||||
|
def now(cls):
|
||||||
|
return datetime.datetime.now() + datetime.timedelta(minutes=13)
|
||||||
|
|
||||||
|
|
||||||
def assert_original(filename):
|
def assert_original(filename):
|
||||||
assert read_json(filename) == TOKENS_FILE_CONTENTS
|
assert read_json(filename) == TOKENS_FILE_CONTENTS
|
||||||
|
|
||||||
|
@ -184,14 +191,7 @@ def test_get_and_authorize_token_after_12_minutes(
|
||||||
token = rest_get_new_device_token(authorized_client)
|
token = rest_get_new_device_token(authorized_client)
|
||||||
|
|
||||||
# TARDIS sounds
|
# TARDIS sounds
|
||||||
new_time = datetime.datetime.now() + datetime.timedelta(minutes=13)
|
mock = mocker.patch(DEVICE_KEY_VALIDATION_DATETIME, NearFuture)
|
||||||
|
|
||||||
class warped_spacetime(datetime.datetime):
|
|
||||||
@classmethod
|
|
||||||
def now(cls):
|
|
||||||
return new_time
|
|
||||||
|
|
||||||
mock = mocker.patch(DEVICE_KEY_VALIDATION_DATETIME, warped_spacetime)
|
|
||||||
|
|
||||||
response = client.post(
|
response = client.post(
|
||||||
"/auth/new_device/authorize",
|
"/auth/new_device/authorize",
|
||||||
|
@ -320,7 +320,7 @@ def test_generate_recovery_token(authorized_client, client, tokens_file):
|
||||||
|
|
||||||
@pytest.mark.parametrize("timeformat", DATE_FORMATS)
|
@pytest.mark.parametrize("timeformat", DATE_FORMATS)
|
||||||
def test_generate_recovery_token_with_expiration_date(
|
def test_generate_recovery_token_with_expiration_date(
|
||||||
authorized_client, client, tokens_file, timeformat
|
authorized_client, client, tokens_file, timeformat, mocker
|
||||||
):
|
):
|
||||||
# Generate token with expiration date
|
# Generate token with expiration date
|
||||||
# Generate expiration date in the future
|
# Generate expiration date in the future
|
||||||
|
@ -345,29 +345,17 @@ def test_generate_recovery_token_with_expiration_date(
|
||||||
rest_recover_with_mnemonic(client, mnemonic_token, "recover_device2")
|
rest_recover_with_mnemonic(client, mnemonic_token, "recover_device2")
|
||||||
|
|
||||||
# Try to use token after expiration date
|
# Try to use token after expiration date
|
||||||
new_data = read_json(tokens_file)
|
mock = mocker.patch(RECOVERY_KEY_VALIDATION_DATETIME, NearFuture)
|
||||||
new_data["recovery_token"]["expiration"] = datetime.datetime.now().strftime(
|
device_name = "recovery_device3"
|
||||||
"%Y-%m-%dT%H:%M:%S.%f"
|
|
||||||
)
|
|
||||||
write_json(tokens_file, new_data)
|
|
||||||
recovery_response = client.post(
|
recovery_response = client.post(
|
||||||
"/auth/recovery_token/use",
|
"/auth/recovery_token/use",
|
||||||
json={"token": mnemonic_token, "device": "recovery_device3"},
|
json={"token": mnemonic_token, "device": device_name},
|
||||||
)
|
)
|
||||||
assert recovery_response.status_code == 404
|
assert recovery_response.status_code == 404
|
||||||
# Assert that the token was not created in JSON
|
# Assert that the token was not created
|
||||||
assert read_json(tokens_file)["tokens"] == new_data["tokens"]
|
assert device_name not in [
|
||||||
|
token["name"] for token in rest_get_tokens_info(authorized_client)
|
||||||
# Get the status of the token
|
]
|
||||||
response = authorized_client.get("/auth/recovery_token")
|
|
||||||
assert response.status_code == 200
|
|
||||||
assert response.json() == {
|
|
||||||
"exists": True,
|
|
||||||
"valid": False,
|
|
||||||
"date": time_generated,
|
|
||||||
"expiration": new_data["recovery_token"]["expiration"],
|
|
||||||
"uses_left": None,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("timeformat", DATE_FORMATS)
|
@pytest.mark.parametrize("timeformat", DATE_FORMATS)
|
||||||
|
|
Loading…
Reference in a new issue