From d930426f251c4a014217305095fc06b138955420 Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Wed, 28 Dec 2022 11:00:55 +0000 Subject: [PATCH 01/27] test(tokens-repo): make empty_keys fixture (and derived) shareable --- .../empty_keys.json | 9 -------- .../test_repository/test_tokens_repository.py | 23 +++++++++++++++---- .../test_tokens_repository/empty_keys.json | 9 -------- 3 files changed, 19 insertions(+), 22 deletions(-) delete mode 100644 tests/test_graphql/test_repository/test_json_tokens_repository/empty_keys.json delete mode 100644 tests/test_graphql/test_repository/test_tokens_repository/empty_keys.json diff --git a/tests/test_graphql/test_repository/test_json_tokens_repository/empty_keys.json b/tests/test_graphql/test_repository/test_json_tokens_repository/empty_keys.json deleted file mode 100644 index 2131ddf..0000000 --- a/tests/test_graphql/test_repository/test_json_tokens_repository/empty_keys.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "tokens": [ - { - "token": "KG9ni-B-CMPk327Zv1qC7YBQaUGaBUcgdkvMvQ2atFI", - "name": "primary_token", - "date": "2022-07-15 17:41:31.675698" - } - ] -} diff --git a/tests/test_graphql/test_repository/test_tokens_repository.py b/tests/test_graphql/test_repository/test_tokens_repository.py index 020a868..8b8b089 100644 --- a/tests/test_graphql/test_repository/test_tokens_repository.py +++ b/tests/test_graphql/test_repository/test_tokens_repository.py @@ -32,22 +32,37 @@ ORIGINAL_DEVICE_NAMES = [ "forth_token", ] +EMPTY_KEYS_JSON = """ +{ + "tokens": [ + { + "token": "KG9ni-B-CMPk327Zv1qC7YBQaUGaBUcgdkvMvQ2atFI", + "name": "primary_token", + "date": "2022-07-15 17:41:31.675698" + } + ] +} +""" + def mnemonic_from_hex(hexkey): return Mnemonic(language="english").to_mnemonic(bytes.fromhex(hexkey)) @pytest.fixture -def empty_keys(mocker, datadir): - mocker.patch("selfprivacy_api.utils.TOKENS_FILE", new=datadir / "empty_keys.json") - assert read_json(datadir / "empty_keys.json")["tokens"] == [ +def empty_keys(mocker, tmpdir): + tokens_file = tmpdir / "empty_keys.json" + with open(tokens_file, "w") as file: + file.write(EMPTY_KEYS_JSON) + mocker.patch("selfprivacy_api.utils.TOKENS_FILE", new=tokens_file) + assert read_json(tokens_file)["tokens"] == [ { "token": "KG9ni-B-CMPk327Zv1qC7YBQaUGaBUcgdkvMvQ2atFI", "name": "primary_token", "date": "2022-07-15 17:41:31.675698", } ] - return datadir + return tmpdir @pytest.fixture diff --git a/tests/test_graphql/test_repository/test_tokens_repository/empty_keys.json b/tests/test_graphql/test_repository/test_tokens_repository/empty_keys.json deleted file mode 100644 index 2131ddf..0000000 --- a/tests/test_graphql/test_repository/test_tokens_repository/empty_keys.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "tokens": [ - { - "token": "KG9ni-B-CMPk327Zv1qC7YBQaUGaBUcgdkvMvQ2atFI", - "name": "primary_token", - "date": "2022-07-15 17:41:31.675698" - } - ] -} From 1768fe278a69b2b5caa5516194d98fbb50918a1c Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Wed, 28 Dec 2022 11:09:24 +0000 Subject: [PATCH 02/27] test(tokens-repo): make empty_tokens fixture, even more minimal --- .../test_repository/test_tokens_repository.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/tests/test_graphql/test_repository/test_tokens_repository.py b/tests/test_graphql/test_repository/test_tokens_repository.py index 8b8b089..ee1b9e0 100644 --- a/tests/test_graphql/test_repository/test_tokens_repository.py +++ b/tests/test_graphql/test_repository/test_tokens_repository.py @@ -44,6 +44,8 @@ EMPTY_KEYS_JSON = """ } """ +EMPTY_TOKENS_JSON = ' {"tokens": []}' + def mnemonic_from_hex(hexkey): return Mnemonic(language="english").to_mnemonic(bytes.fromhex(hexkey)) @@ -65,6 +67,16 @@ def empty_keys(mocker, tmpdir): return tmpdir +@pytest.fixture +def empty_tokens(mocker, tmpdir): + tokens_file = tmpdir / "empty_tokens.json" + with open(tokens_file, "w") as file: + file.write(EMPTY_TOKENS_JSON) + mocker.patch("selfprivacy_api.utils.TOKENS_FILE", new=tokens_file) + assert read_json(tokens_file)["tokens"] == [] + return tmpdir + + @pytest.fixture def mock_new_device_key_generate(mocker): mock = mocker.patch( @@ -153,7 +165,7 @@ def mock_recovery_key_generate(mocker): @pytest.fixture -def empty_json_repo(empty_keys): +def empty_json_repo(empty_tokens): repo = JsonTokensRepository() for token in repo.get_tokens(): repo.delete_token(token) From 40d331d01f6985e7a571d022a816611623856e85 Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Wed, 28 Dec 2022 11:14:36 +0000 Subject: [PATCH 03/27] test(tokens-repo): offload empty_keys fixture to json tests --- .../test_json_tokens_repository.py | 29 ++++++++++++++++++- .../test_repository/test_tokens_repository.py | 27 ----------------- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/tests/test_graphql/test_repository/test_json_tokens_repository.py b/tests/test_graphql/test_repository/test_json_tokens_repository.py index af8c844..23df9df 100644 --- a/tests/test_graphql/test_repository/test_json_tokens_repository.py +++ b/tests/test_graphql/test_repository/test_json_tokens_repository.py @@ -25,7 +25,6 @@ from test_tokens_repository import ( mock_recovery_key_generate, mock_generate_token, mock_new_device_key_generate, - empty_keys, ) ORIGINAL_TOKEN_CONTENT = [ @@ -51,6 +50,18 @@ ORIGINAL_TOKEN_CONTENT = [ }, ] +EMPTY_KEYS_JSON = """ +{ + "tokens": [ + { + "token": "KG9ni-B-CMPk327Zv1qC7YBQaUGaBUcgdkvMvQ2atFI", + "name": "primary_token", + "date": "2022-07-15 17:41:31.675698" + } + ] +} +""" + @pytest.fixture def tokens(mocker, datadir): @@ -59,6 +70,22 @@ def tokens(mocker, datadir): return datadir +@pytest.fixture +def empty_keys(mocker, tmpdir): + tokens_file = tmpdir / "empty_keys.json" + with open(tokens_file, "w") as file: + file.write(EMPTY_KEYS_JSON) + mocker.patch("selfprivacy_api.utils.TOKENS_FILE", new=tokens_file) + assert read_json(tokens_file)["tokens"] == [ + { + "token": "KG9ni-B-CMPk327Zv1qC7YBQaUGaBUcgdkvMvQ2atFI", + "name": "primary_token", + "date": "2022-07-15 17:41:31.675698", + } + ] + return tmpdir + + @pytest.fixture def null_keys(mocker, datadir): mocker.patch("selfprivacy_api.utils.TOKENS_FILE", new=datadir / "null_keys.json") diff --git a/tests/test_graphql/test_repository/test_tokens_repository.py b/tests/test_graphql/test_repository/test_tokens_repository.py index ee1b9e0..b172f13 100644 --- a/tests/test_graphql/test_repository/test_tokens_repository.py +++ b/tests/test_graphql/test_repository/test_tokens_repository.py @@ -32,17 +32,6 @@ ORIGINAL_DEVICE_NAMES = [ "forth_token", ] -EMPTY_KEYS_JSON = """ -{ - "tokens": [ - { - "token": "KG9ni-B-CMPk327Zv1qC7YBQaUGaBUcgdkvMvQ2atFI", - "name": "primary_token", - "date": "2022-07-15 17:41:31.675698" - } - ] -} -""" EMPTY_TOKENS_JSON = ' {"tokens": []}' @@ -51,22 +40,6 @@ def mnemonic_from_hex(hexkey): return Mnemonic(language="english").to_mnemonic(bytes.fromhex(hexkey)) -@pytest.fixture -def empty_keys(mocker, tmpdir): - tokens_file = tmpdir / "empty_keys.json" - with open(tokens_file, "w") as file: - file.write(EMPTY_KEYS_JSON) - mocker.patch("selfprivacy_api.utils.TOKENS_FILE", new=tokens_file) - assert read_json(tokens_file)["tokens"] == [ - { - "token": "KG9ni-B-CMPk327Zv1qC7YBQaUGaBUcgdkvMvQ2atFI", - "name": "primary_token", - "date": "2022-07-15 17:41:31.675698", - } - ] - return tmpdir - - @pytest.fixture def empty_tokens(mocker, tmpdir): tokens_file = tmpdir / "empty_tokens.json" From cee0419a5227f7e0ddc8f89bb0799d522f4b5214 Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Wed, 28 Dec 2022 12:44:51 +0000 Subject: [PATCH 04/27] test(tokens-repo): remove test tokens.json files except for one which will temporarily remain gitkeeps are to prevent shared_datadir from erroring out in a freshly cloned repo. for now huey database and jobs fixtures use shared_datadir --- tests/conftest.py | 15 +++++++++++---- tests/test_graphql/data/gitkeep | 0 tests/test_graphql/data/tokens.json | 14 -------------- tests/test_rest_endpoints/data/tokens.json | 14 -------------- .../test_rest_endpoints/services/data/tokens.json | 9 --------- tests/test_rest_endpoints/services/gitkeep | 0 6 files changed, 11 insertions(+), 41 deletions(-) create mode 100644 tests/test_graphql/data/gitkeep delete mode 100644 tests/test_graphql/data/tokens.json delete mode 100644 tests/test_rest_endpoints/data/tokens.json delete mode 100644 tests/test_rest_endpoints/services/data/tokens.json create mode 100644 tests/test_rest_endpoints/services/gitkeep diff --git a/tests/conftest.py b/tests/conftest.py index ea7a66a..4b65d20 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -4,18 +4,25 @@ import os import pytest from fastapi.testclient import TestClient +from shutil import copy +import os.path as path def pytest_generate_tests(metafunc): os.environ["TEST_MODE"] = "true" +def global_data_dir(): + return path.join(path.dirname(__file__), "data") + + @pytest.fixture -def tokens_file(mocker, shared_datadir): +def tokens_file(mocker, tmpdir): """Mock tokens file.""" - mock = mocker.patch( - "selfprivacy_api.utils.TOKENS_FILE", shared_datadir / "tokens.json" - ) + tmp_file = tmpdir / "tokens.json" + source_file = path.join(global_data_dir(), "tokens.json") + copy(source_file, tmp_file) + mock = mocker.patch("selfprivacy_api.utils.TOKENS_FILE", tmp_file) return mock diff --git a/tests/test_graphql/data/gitkeep b/tests/test_graphql/data/gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_graphql/data/tokens.json b/tests/test_graphql/data/tokens.json deleted file mode 100644 index 9be9d02..0000000 --- a/tests/test_graphql/data/tokens.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "tokens": [ - { - "token": "TEST_TOKEN", - "name": "test_token", - "date": "2022-01-14 08:31:10.789314" - }, - { - "token": "TEST_TOKEN2", - "name": "test_token2", - "date": "2022-01-14 08:31:10.789314" - } - ] -} \ No newline at end of file diff --git a/tests/test_rest_endpoints/data/tokens.json b/tests/test_rest_endpoints/data/tokens.json deleted file mode 100644 index 9be9d02..0000000 --- a/tests/test_rest_endpoints/data/tokens.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "tokens": [ - { - "token": "TEST_TOKEN", - "name": "test_token", - "date": "2022-01-14 08:31:10.789314" - }, - { - "token": "TEST_TOKEN2", - "name": "test_token2", - "date": "2022-01-14 08:31:10.789314" - } - ] -} \ No newline at end of file diff --git a/tests/test_rest_endpoints/services/data/tokens.json b/tests/test_rest_endpoints/services/data/tokens.json deleted file mode 100644 index 9d35420..0000000 --- a/tests/test_rest_endpoints/services/data/tokens.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "tokens": [ - { - "token": "TEST_TOKEN", - "name": "Test Token", - "date": "2022-01-14 08:31:10.789314" - } - ] -} \ No newline at end of file diff --git a/tests/test_rest_endpoints/services/gitkeep b/tests/test_rest_endpoints/services/gitkeep new file mode 100644 index 0000000..e69de29 From c5dc09d5dd9c134a7c7366ae5d69ee19c0ed33b8 Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Wed, 28 Dec 2022 14:51:25 +0000 Subject: [PATCH 05/27] test(tokens-repo): break out assert_original() in rest --- tests/test_rest_endpoints/test_auth.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/tests/test_rest_endpoints/test_auth.py b/tests/test_rest_endpoints/test_auth.py index 12de0cf..bb322e9 100644 --- a/tests/test_rest_endpoints/test_auth.py +++ b/tests/test_rest_endpoints/test_auth.py @@ -37,6 +37,10 @@ DATE_FORMATS = [ ] +def assert_original(filename): + assert read_json(filename) == TOKENS_FILE_CONTETS + + def test_get_tokens_info(authorized_client, tokens_file): response = authorized_client.get("/auth/tokens") assert response.status_code == 200 @@ -58,7 +62,7 @@ def test_get_tokens_unauthorized(client, tokens_file): def test_delete_token_unauthorized(client, tokens_file): response = client.delete("/auth/tokens") assert response.status_code == 401 - assert read_json(tokens_file) == TOKENS_FILE_CONTETS + assert_original(tokens_file) def test_delete_token(authorized_client, tokens_file): @@ -82,7 +86,7 @@ def test_delete_self_token(authorized_client, tokens_file): "/auth/tokens", json={"token_name": "test_token"} ) assert response.status_code == 400 - assert read_json(tokens_file) == TOKENS_FILE_CONTETS + assert_original(tokens_file) def test_delete_nonexistent_token(authorized_client, tokens_file): @@ -90,13 +94,13 @@ def test_delete_nonexistent_token(authorized_client, tokens_file): "/auth/tokens", json={"token_name": "test_token3"} ) assert response.status_code == 404 - assert read_json(tokens_file) == TOKENS_FILE_CONTETS + assert_original(tokens_file) def test_refresh_token_unauthorized(client, tokens_file): response = client.post("/auth/tokens") assert response.status_code == 401 - assert read_json(tokens_file) == TOKENS_FILE_CONTETS + assert_original(tokens_file) def test_refresh_token(authorized_client, tokens_file): @@ -112,7 +116,7 @@ def test_refresh_token(authorized_client, tokens_file): def test_get_new_device_auth_token_unauthorized(client, tokens_file): response = client.post("/auth/new_device") assert response.status_code == 401 - assert read_json(tokens_file) == TOKENS_FILE_CONTETS + assert_original(tokens_file) def test_get_new_device_auth_token(authorized_client, tokens_file): @@ -133,13 +137,13 @@ def test_get_and_delete_new_device_token(authorized_client, tokens_file): "/auth/new_device", json={"token": response.json()["token"]} ) assert response.status_code == 200 - assert read_json(tokens_file) == TOKENS_FILE_CONTETS + assert_original(tokens_file) def test_delete_token_unauthenticated(client, tokens_file): response = client.delete("/auth/new_device") assert response.status_code == 401 - assert read_json(tokens_file) == TOKENS_FILE_CONTETS + assert_original(tokens_file) def test_get_and_authorize_new_device(client, authorized_client, tokens_file): @@ -163,7 +167,7 @@ def test_authorize_new_device_with_invalid_token(client, tokens_file): json={"token": "invalid_token", "device": "new_device"}, ) assert response.status_code == 404 - assert read_json(tokens_file) == TOKENS_FILE_CONTETS + assert_original(tokens_file) def test_get_and_authorize_used_token(client, authorized_client, tokens_file): @@ -214,7 +218,7 @@ def test_authorize_without_token(client, tokens_file): json={"device": "new_device"}, ) assert response.status_code == 422 - assert read_json(tokens_file) == TOKENS_FILE_CONTETS + assert_original(tokens_file) # Recovery tokens @@ -243,7 +247,7 @@ def test_authorize_without_token(client, tokens_file): def test_get_recovery_token_status_unauthorized(client, tokens_file): response = client.get("/auth/recovery_token") assert response.status_code == 401 - assert read_json(tokens_file) == TOKENS_FILE_CONTETS + assert_original(tokens_file) def test_get_recovery_token_when_none_exists(authorized_client, tokens_file): @@ -256,7 +260,7 @@ def test_get_recovery_token_when_none_exists(authorized_client, tokens_file): "expiration": None, "uses_left": None, } - assert read_json(tokens_file) == TOKENS_FILE_CONTETS + assert_original(tokens_file) def test_generate_recovery_token(authorized_client, client, tokens_file): From 5812b57ced5bdfda14454938b98d6022c9fa8be0 Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Wed, 28 Dec 2022 15:16:58 +0000 Subject: [PATCH 06/27] test(tokens-repo): break out rest_get_token_info() --- tests/test_rest_endpoints/test_auth.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tests/test_rest_endpoints/test_auth.py b/tests/test_rest_endpoints/test_auth.py index bb322e9..bd4efae 100644 --- a/tests/test_rest_endpoints/test_auth.py +++ b/tests/test_rest_endpoints/test_auth.py @@ -41,10 +41,14 @@ def assert_original(filename): assert read_json(filename) == TOKENS_FILE_CONTETS -def test_get_tokens_info(authorized_client, tokens_file): - response = authorized_client.get("/auth/tokens") +def rest_get_tokens_info(client): + response = client.get("/auth/tokens") assert response.status_code == 200 - assert response.json() == [ + return response.json() + + +def test_get_tokens_info(authorized_client, tokens_file): + assert rest_get_tokens_info(authorized_client) == [ {"name": "test_token", "date": "2022-01-14T08:31:10.789314", "is_caller": True}, { "name": "test_token2", From 7b5fb4b2df8aa9b31bcacff63d33cf91c80551ad Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Wed, 28 Dec 2022 15:26:42 +0000 Subject: [PATCH 07/27] test(tokens-repo): use rest token info in token deletion test --- tests/test_rest_endpoints/test_auth.py | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/tests/test_rest_endpoints/test_auth.py b/tests/test_rest_endpoints/test_auth.py index bd4efae..6199265 100644 --- a/tests/test_rest_endpoints/test_auth.py +++ b/tests/test_rest_endpoints/test_auth.py @@ -74,15 +74,9 @@ def test_delete_token(authorized_client, tokens_file): "/auth/tokens", json={"token_name": "test_token2"} ) assert response.status_code == 200 - assert read_json(tokens_file) == { - "tokens": [ - { - "token": "TEST_TOKEN", - "name": "test_token", - "date": "2022-01-14 08:31:10.789314", - } - ] - } + assert rest_get_tokens_info(authorized_client) == [ + {"name": "test_token", "date": "2022-01-14T08:31:10.789314", "is_caller": True} + ] def test_delete_self_token(authorized_client, tokens_file): From cb46c5e43be7def018bb201c255e0c76428cc612 Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Wed, 28 Dec 2022 15:43:43 +0000 Subject: [PATCH 08/27] test(tokens-repo): check refreshed token validity by trying to auth --- tests/test_rest_endpoints/test_auth.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/tests/test_rest_endpoints/test_auth.py b/tests/test_rest_endpoints/test_auth.py index 6199265..44b543d 100644 --- a/tests/test_rest_endpoints/test_auth.py +++ b/tests/test_rest_endpoints/test_auth.py @@ -5,11 +5,6 @@ import datetime import pytest from mnemonic import Mnemonic -from selfprivacy_api.repositories.tokens.json_tokens_repository import ( - JsonTokensRepository, -) - -TOKEN_REPO = JsonTokensRepository() from tests.common import read_json, write_json @@ -105,7 +100,8 @@ def test_refresh_token(authorized_client, tokens_file): response = authorized_client.post("/auth/tokens") assert response.status_code == 200 new_token = response.json()["token"] - assert TOKEN_REPO.get_token_by_token_string(new_token) is not None + authorized_client.headers.update({"Authorization": "Bearer " + new_token}) + assert rest_get_tokens_info(authorized_client) is not None # new device From 243f9be225d7b6d7fc4101647536b9ba0d2370ed Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Wed, 28 Dec 2022 15:48:40 +0000 Subject: [PATCH 09/27] test(tokens-repo): delete standalone get new device test At rest api level, we can only check the existence of new device token by using it, and this test already exists. --- tests/test_rest_endpoints/test_auth.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/tests/test_rest_endpoints/test_auth.py b/tests/test_rest_endpoints/test_auth.py index 44b543d..3d6b256 100644 --- a/tests/test_rest_endpoints/test_auth.py +++ b/tests/test_rest_endpoints/test_auth.py @@ -113,14 +113,6 @@ def test_get_new_device_auth_token_unauthorized(client, tokens_file): assert_original(tokens_file) -def test_get_new_device_auth_token(authorized_client, tokens_file): - response = authorized_client.post("/auth/new_device") - assert response.status_code == 200 - assert "token" in response.json() - token = Mnemonic(language="english").to_entropy(response.json()["token"]).hex() - assert read_json(tokens_file)["new_device"]["token"] == token - - def test_get_and_delete_new_device_token(authorized_client, tokens_file): response = authorized_client.post("/auth/new_device") assert response.status_code == 200 From 51cc35708ed9f6d0fabc6c910b97710f8f278d4e Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Wed, 28 Dec 2022 16:17:06 +0000 Subject: [PATCH 10/27] test(tokens-repo): break out getting new device token --- tests/test_rest_endpoints/test_auth.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/tests/test_rest_endpoints/test_auth.py b/tests/test_rest_endpoints/test_auth.py index 3d6b256..b8e1292 100644 --- a/tests/test_rest_endpoints/test_auth.py +++ b/tests/test_rest_endpoints/test_auth.py @@ -132,15 +132,20 @@ def test_delete_token_unauthenticated(client, tokens_file): assert_original(tokens_file) -def test_get_and_authorize_new_device(client, authorized_client, tokens_file): - response = authorized_client.post("/auth/new_device") +def rest_get_new_device_token(client): + response = client.post("/auth/new_device") assert response.status_code == 200 assert "token" in response.json() - token = Mnemonic(language="english").to_entropy(response.json()["token"]).hex() - assert read_json(tokens_file)["new_device"]["token"] == token + return response.json()["token"] + + +def test_get_and_authorize_new_device(client, authorized_client, tokens_file): response = client.post( "/auth/new_device/authorize", - json={"token": response.json()["token"], "device": "new_device"}, + json={ + "token": rest_get_new_device_token(authorized_client), + "device": "new_device", + }, ) assert response.status_code == 200 assert read_json(tokens_file)["tokens"][2]["token"] == response.json()["token"] @@ -157,21 +162,17 @@ def test_authorize_new_device_with_invalid_token(client, tokens_file): def test_get_and_authorize_used_token(client, authorized_client, tokens_file): - response = authorized_client.post("/auth/new_device") - assert response.status_code == 200 - assert "token" in response.json() - token = Mnemonic(language="english").to_entropy(response.json()["token"]).hex() - assert read_json(tokens_file)["new_device"]["token"] == token + token_to_be_used_2_times = rest_get_new_device_token(authorized_client) response = client.post( "/auth/new_device/authorize", - json={"token": response.json()["token"], "device": "new_device"}, + json={"token": token_to_be_used_2_times, "device": "new_device"}, ) assert response.status_code == 200 assert read_json(tokens_file)["tokens"][2]["token"] == response.json()["token"] assert read_json(tokens_file)["tokens"][2]["name"] == "new_device" response = client.post( "/auth/new_device/authorize", - json={"token": response.json()["token"], "device": "new_device"}, + json={"token": token_to_be_used_2_times, "device": "new_device"}, ) assert response.status_code == 404 From 8b19c9c01339dddee568dc05f5459b938129bd10 Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Wed, 28 Dec 2022 16:27:08 +0000 Subject: [PATCH 11/27] test(tokens-repo): break out checking token validity --- tests/test_rest_endpoints/test_auth.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/tests/test_rest_endpoints/test_auth.py b/tests/test_rest_endpoints/test_auth.py index b8e1292..9467f49 100644 --- a/tests/test_rest_endpoints/test_auth.py +++ b/tests/test_rest_endpoints/test_auth.py @@ -36,6 +36,11 @@ def assert_original(filename): assert read_json(filename) == TOKENS_FILE_CONTETS +def assert_token_valid(client, token): + client.headers.update({"Authorization": "Bearer " + token}) + assert rest_get_tokens_info(client) is not None + + def rest_get_tokens_info(client): response = client.get("/auth/tokens") assert response.status_code == 200 @@ -100,8 +105,7 @@ def test_refresh_token(authorized_client, tokens_file): response = authorized_client.post("/auth/tokens") assert response.status_code == 200 new_token = response.json()["token"] - authorized_client.headers.update({"Authorization": "Bearer " + new_token}) - assert rest_get_tokens_info(authorized_client) is not None + assert_token_valid(authorized_client, new_token) # new device @@ -148,8 +152,7 @@ def test_get_and_authorize_new_device(client, authorized_client, tokens_file): }, ) assert response.status_code == 200 - assert read_json(tokens_file)["tokens"][2]["token"] == response.json()["token"] - assert read_json(tokens_file)["tokens"][2]["name"] == "new_device" + assert_token_valid(authorized_client, response.json()["token"]) def test_authorize_new_device_with_invalid_token(client, tokens_file): @@ -168,8 +171,7 @@ def test_get_and_authorize_used_token(client, authorized_client, tokens_file): json={"token": token_to_be_used_2_times, "device": "new_device"}, ) assert response.status_code == 200 - assert read_json(tokens_file)["tokens"][2]["token"] == response.json()["token"] - assert read_json(tokens_file)["tokens"][2]["name"] == "new_device" + assert_token_valid(authorized_client, response.json()["token"]) response = client.post( "/auth/new_device/authorize", json={"token": token_to_be_used_2_times, "device": "new_device"}, From 6dd724b682dcad82f03131d2007e33af39ae5019 Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Wed, 28 Dec 2022 16:37:34 +0000 Subject: [PATCH 12/27] test(tokens-repo): make new device tests a bit more readable --- tests/test_rest_endpoints/test_auth.py | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/tests/test_rest_endpoints/test_auth.py b/tests/test_rest_endpoints/test_auth.py index 9467f49..93be5ee 100644 --- a/tests/test_rest_endpoints/test_auth.py +++ b/tests/test_rest_endpoints/test_auth.py @@ -118,14 +118,8 @@ def test_get_new_device_auth_token_unauthorized(client, tokens_file): def test_get_and_delete_new_device_token(authorized_client, tokens_file): - response = authorized_client.post("/auth/new_device") - assert response.status_code == 200 - assert "token" in response.json() - token = Mnemonic(language="english").to_entropy(response.json()["token"]).hex() - assert read_json(tokens_file)["new_device"]["token"] == token - response = authorized_client.delete( - "/auth/new_device", json={"token": response.json()["token"]} - ) + token = rest_get_new_device_token(authorized_client) + response = authorized_client.delete("/auth/new_device", json={"token": token}) assert response.status_code == 200 assert_original(tokens_file) @@ -144,10 +138,11 @@ def rest_get_new_device_token(client): def test_get_and_authorize_new_device(client, authorized_client, tokens_file): + token = rest_get_new_device_token(authorized_client) response = client.post( "/auth/new_device/authorize", json={ - "token": rest_get_new_device_token(authorized_client), + "token": token, "device": "new_device", }, ) @@ -172,6 +167,7 @@ def test_get_and_authorize_used_token(client, authorized_client, tokens_file): ) assert response.status_code == 200 assert_token_valid(authorized_client, response.json()["token"]) + response = client.post( "/auth/new_device/authorize", json={"token": token_to_be_used_2_times, "device": "new_device"}, @@ -182,11 +178,7 @@ def test_get_and_authorize_used_token(client, authorized_client, tokens_file): def test_get_and_authorize_token_after_12_minutes( client, authorized_client, tokens_file ): - response = authorized_client.post("/auth/new_device") - assert response.status_code == 200 - assert "token" in response.json() - token = Mnemonic(language="english").to_entropy(response.json()["token"]).hex() - assert read_json(tokens_file)["new_device"]["token"] == token + token = rest_get_new_device_token(authorized_client) file_data = read_json(tokens_file) file_data["new_device"]["expiration"] = str( @@ -196,7 +188,7 @@ def test_get_and_authorize_token_after_12_minutes( response = client.post( "/auth/new_device/authorize", - json={"token": response.json()["token"], "device": "new_device"}, + json={"token": token, "device": "new_device"}, ) assert response.status_code == 404 From 5bf7e81351d2acef43c66bd7444e1f41dab05d57 Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Wed, 28 Dec 2022 17:09:19 +0000 Subject: [PATCH 13/27] test(tokens-repo): travel in time to check expiration --- tests/test_rest_endpoints/test_auth.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/tests/test_rest_endpoints/test_auth.py b/tests/test_rest_endpoints/test_auth.py index 93be5ee..f428904 100644 --- a/tests/test_rest_endpoints/test_auth.py +++ b/tests/test_rest_endpoints/test_auth.py @@ -31,6 +31,9 @@ DATE_FORMATS = [ "%Y-%m-%d %H:%M:%S.%f", ] +# for expiration tests. If headache, consider freezegun +DEVICE_KEY_VALIDATION_DATETIME = "selfprivacy_api.models.tokens.new_device_key.datetime" + def assert_original(filename): assert read_json(filename) == TOKENS_FILE_CONTETS @@ -176,15 +179,19 @@ def test_get_and_authorize_used_token(client, authorized_client, tokens_file): def test_get_and_authorize_token_after_12_minutes( - client, authorized_client, tokens_file + client, authorized_client, tokens_file, mocker ): token = rest_get_new_device_token(authorized_client) - file_data = read_json(tokens_file) - file_data["new_device"]["expiration"] = str( - datetime.datetime.now() - datetime.timedelta(minutes=13) - ) - write_json(tokens_file, file_data) + # TARDIS sounds + new_time = datetime.datetime.now() + datetime.timedelta(minutes=13) + + class warped_spacetime(datetime.datetime): + @classmethod + def now(cls): + return new_time + + mock = mocker.patch(DEVICE_KEY_VALIDATION_DATETIME, warped_spacetime) response = client.post( "/auth/new_device/authorize", From d55232735d1e11c76383c73e57bb6d0ab66a83e7 Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Fri, 30 Dec 2022 10:13:31 +0000 Subject: [PATCH 14/27] test(tokens-repo): fix typo in contets --- tests/test_rest_endpoints/test_auth.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_rest_endpoints/test_auth.py b/tests/test_rest_endpoints/test_auth.py index f428904..80cc2eb 100644 --- a/tests/test_rest_endpoints/test_auth.py +++ b/tests/test_rest_endpoints/test_auth.py @@ -9,7 +9,7 @@ from mnemonic import Mnemonic from tests.common import read_json, write_json -TOKENS_FILE_CONTETS = { +TOKENS_FILE_CONTENTS = { "tokens": [ { "token": "TEST_TOKEN", @@ -36,7 +36,7 @@ DEVICE_KEY_VALIDATION_DATETIME = "selfprivacy_api.models.tokens.new_device_key.d def assert_original(filename): - assert read_json(filename) == TOKENS_FILE_CONTETS + assert read_json(filename) == TOKENS_FILE_CONTENTS def assert_token_valid(client, token): From b526e0b6ad03cd8a4457af346aa5425a0d5e7980 Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Fri, 30 Dec 2022 10:27:51 +0000 Subject: [PATCH 15/27] test(tokens-repo): do not read json in generate recovery test --- tests/test_rest_endpoints/test_auth.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/tests/test_rest_endpoints/test_auth.py b/tests/test_rest_endpoints/test_auth.py index 80cc2eb..7d1c88f 100644 --- a/tests/test_rest_endpoints/test_auth.py +++ b/tests/test_rest_endpoints/test_auth.py @@ -257,21 +257,19 @@ def test_generate_recovery_token(authorized_client, client, tokens_file): assert response.status_code == 200 assert "token" in response.json() mnemonic_token = response.json()["token"] - token = Mnemonic(language="english").to_entropy(mnemonic_token).hex() - assert read_json(tokens_file)["recovery_token"]["token"] == token - time_generated = read_json(tokens_file)["recovery_token"]["date"] - assert time_generated is not None + # Try to get token status + response = authorized_client.get("/auth/recovery_token") + assert response.status_code == 200 + assert "date" in response.json() + time_generated = response.json()["date"] + # Assert that the token was generated near the current time assert ( datetime.datetime.strptime(time_generated, "%Y-%m-%dT%H:%M:%S.%f") - datetime.timedelta(seconds=5) < datetime.datetime.now() ) - - # Try to get token status - response = authorized_client.get("/auth/recovery_token") - assert response.status_code == 200 assert response.json() == { "exists": True, "valid": True, @@ -287,8 +285,7 @@ def test_generate_recovery_token(authorized_client, client, tokens_file): ) assert recovery_response.status_code == 200 new_token = recovery_response.json()["token"] - assert read_json(tokens_file)["tokens"][2]["token"] == new_token - assert read_json(tokens_file)["tokens"][2]["name"] == "recovery_device" + assert_token_valid(authorized_client, new_token) # Try to use token again recovery_response = client.post( @@ -297,8 +294,7 @@ def test_generate_recovery_token(authorized_client, client, tokens_file): ) assert recovery_response.status_code == 200 new_token = recovery_response.json()["token"] - assert read_json(tokens_file)["tokens"][3]["token"] == new_token - assert read_json(tokens_file)["tokens"][3]["name"] == "recovery_device2" + assert_token_valid(authorized_client, new_token) @pytest.mark.parametrize("timeformat", DATE_FORMATS) From 2a577aedb7b267de3f1e9de7eb1bcc3d33a6dd0d Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Fri, 30 Dec 2022 10:52:00 +0000 Subject: [PATCH 16/27] test(tokens-repo): break out obtaining recovery tokens --- tests/test_rest_endpoints/test_auth.py | 36 ++++++++++++++------------ 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/tests/test_rest_endpoints/test_auth.py b/tests/test_rest_endpoints/test_auth.py index 7d1c88f..49a1f3b 100644 --- a/tests/test_rest_endpoints/test_auth.py +++ b/tests/test_rest_endpoints/test_auth.py @@ -251,12 +251,25 @@ def test_get_recovery_token_when_none_exists(authorized_client, tokens_file): assert_original(tokens_file) -def test_generate_recovery_token(authorized_client, client, tokens_file): - # Generate token without expiration and uses_left - response = authorized_client.post("/auth/recovery_token") +def rest_make_recovery_token(client, expires_at=None, timeformat=None): + if expires_at is None: + response = client.post("/auth/recovery_token") + else: + assert timeformat is not None + expires_at_str = expires_at.strftime(timeformat) + response = client.post( + "/auth/recovery_token", + json={"expiration": expires_at_str}, + ) + assert response.status_code == 200 assert "token" in response.json() - mnemonic_token = response.json()["token"] + return response.json()["token"] + + +def test_generate_recovery_token(authorized_client, client, tokens_file): + # Generate token without expiration and uses_left + mnemonic_token = rest_make_recovery_token(authorized_client) # Try to get token status response = authorized_client.get("/auth/recovery_token") @@ -304,20 +317,9 @@ def test_generate_recovery_token_with_expiration_date( # Generate token with expiration date # Generate expiration date in the future expiration_date = datetime.datetime.now() + datetime.timedelta(minutes=5) - expiration_date_str = expiration_date.strftime(timeformat) - response = authorized_client.post( - "/auth/recovery_token", - json={"expiration": expiration_date_str}, + mnemonic_token = rest_make_recovery_token( + authorized_client, expires_at=expiration_date, timeformat=timeformat ) - assert response.status_code == 200 - assert "token" in response.json() - mnemonic_token = response.json()["token"] - token = Mnemonic(language="english").to_entropy(mnemonic_token).hex() - assert read_json(tokens_file)["recovery_token"]["token"] == token - assert datetime.datetime.strptime( - read_json(tokens_file)["recovery_token"]["expiration"], "%Y-%m-%dT%H:%M:%S.%f" - ) == datetime.datetime.strptime(expiration_date_str, timeformat) - time_generated = read_json(tokens_file)["recovery_token"]["date"] assert time_generated is not None # Assert that the token was generated near the current time From 38f9eb825a1e03ac0a370ad8fa4cb7cbac26a790 Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Fri, 30 Dec 2022 11:14:34 +0000 Subject: [PATCH 17/27] test(tokens-repo): break out recovery time operations --- tests/test_rest_endpoints/test_auth.py | 47 ++++++++++++++------------ 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/tests/test_rest_endpoints/test_auth.py b/tests/test_rest_endpoints/test_auth.py index 49a1f3b..1a3093c 100644 --- a/tests/test_rest_endpoints/test_auth.py +++ b/tests/test_rest_endpoints/test_auth.py @@ -267,23 +267,34 @@ def rest_make_recovery_token(client, expires_at=None, timeformat=None): return response.json()["token"] -def test_generate_recovery_token(authorized_client, client, tokens_file): - # Generate token without expiration and uses_left - mnemonic_token = rest_make_recovery_token(authorized_client) - - # Try to get token status - response = authorized_client.get("/auth/recovery_token") +def rest_get_recovery_status(client): + response = client.get("/auth/recovery_token") assert response.status_code == 200 - assert "date" in response.json() - time_generated = response.json()["date"] + return response.json() - # Assert that the token was generated near the current time + +def rest_get_recovery_date(client): + status = rest_get_recovery_status(client) + assert "date" in status + return status["date"] + + +def assert_recovery_recent(time_generated): assert ( datetime.datetime.strptime(time_generated, "%Y-%m-%dT%H:%M:%S.%f") - datetime.timedelta(seconds=5) < datetime.datetime.now() ) - assert response.json() == { + + +def test_generate_recovery_token(authorized_client, client, tokens_file): + # Generate token without expiration and uses_left + mnemonic_token = rest_make_recovery_token(authorized_client) + + time_generated = rest_get_recovery_date(authorized_client) + assert_recovery_recent(time_generated) + + assert rest_get_recovery_status(authorized_client) == { "exists": True, "valid": True, "date": time_generated, @@ -320,19 +331,11 @@ def test_generate_recovery_token_with_expiration_date( mnemonic_token = rest_make_recovery_token( authorized_client, expires_at=expiration_date, timeformat=timeformat ) - time_generated = read_json(tokens_file)["recovery_token"]["date"] - assert time_generated is not None - # Assert that the token was generated near the current time - assert ( - datetime.datetime.strptime(time_generated, "%Y-%m-%dT%H:%M:%S.%f") - - datetime.timedelta(seconds=5) - < datetime.datetime.now() - ) - # Try to get token status - response = authorized_client.get("/auth/recovery_token") - assert response.status_code == 200 - assert response.json() == { + time_generated = rest_get_recovery_date(authorized_client) + assert_recovery_recent(time_generated) + + assert rest_get_recovery_status(authorized_client) == { "exists": True, "valid": True, "date": time_generated, From b254e40961d5351b7f02ce59f9388c539f444a95 Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Fri, 30 Dec 2022 11:51:52 +0000 Subject: [PATCH 18/27] test(tokens-repo): break out recovery token use --- tests/test_rest_endpoints/test_auth.py | 31 ++++++++++++-------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/tests/test_rest_endpoints/test_auth.py b/tests/test_rest_endpoints/test_auth.py index 1a3093c..c426d54 100644 --- a/tests/test_rest_endpoints/test_auth.py +++ b/tests/test_rest_endpoints/test_auth.py @@ -287,6 +287,17 @@ def assert_recovery_recent(time_generated): ) +def rest_recover_with_mnemonic(client, mnemonic_token, device_name): + recovery_response = client.post( + "/auth/recovery_token/use", + json={"token": mnemonic_token, "device": device_name}, + ) + assert recovery_response.status_code == 200 + new_token = recovery_response.json()["token"] + assert_token_valid(client, new_token) + return new_token + + def test_generate_recovery_token(authorized_client, client, tokens_file): # Generate token without expiration and uses_left mnemonic_token = rest_make_recovery_token(authorized_client) @@ -302,23 +313,9 @@ def test_generate_recovery_token(authorized_client, client, tokens_file): "uses_left": None, } - # Try to use the token - recovery_response = client.post( - "/auth/recovery_token/use", - json={"token": mnemonic_token, "device": "recovery_device"}, - ) - assert recovery_response.status_code == 200 - new_token = recovery_response.json()["token"] - assert_token_valid(authorized_client, new_token) - - # Try to use token again - recovery_response = client.post( - "/auth/recovery_token/use", - json={"token": mnemonic_token, "device": "recovery_device2"}, - ) - assert recovery_response.status_code == 200 - new_token = recovery_response.json()["token"] - assert_token_valid(authorized_client, new_token) + rest_recover_with_mnemonic(client, mnemonic_token, "recover_device") + # And again + rest_recover_with_mnemonic(client, mnemonic_token, "recover_device2") @pytest.mark.parametrize("timeformat", DATE_FORMATS) From fc24290f1c8040a70b9624c9be8535850047e2d4 Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Fri, 30 Dec 2022 12:01:04 +0000 Subject: [PATCH 19/27] test(tokens-repo): use new recovery functions --- tests/test_rest_endpoints/test_auth.py | 42 +++----------------------- 1 file changed, 5 insertions(+), 37 deletions(-) diff --git a/tests/test_rest_endpoints/test_auth.py b/tests/test_rest_endpoints/test_auth.py index c426d54..bdfb579 100644 --- a/tests/test_rest_endpoints/test_auth.py +++ b/tests/test_rest_endpoints/test_auth.py @@ -340,25 +340,9 @@ def test_generate_recovery_token_with_expiration_date( "uses_left": None, } - # Try to use the token - recovery_response = client.post( - "/auth/recovery_token/use", - json={"token": mnemonic_token, "device": "recovery_device"}, - ) - assert recovery_response.status_code == 200 - new_token = recovery_response.json()["token"] - assert read_json(tokens_file)["tokens"][2]["token"] == new_token - assert read_json(tokens_file)["tokens"][2]["name"] == "recovery_device" - - # Try to use token again - recovery_response = client.post( - "/auth/recovery_token/use", - json={"token": mnemonic_token, "device": "recovery_device2"}, - ) - assert recovery_response.status_code == 200 - new_token = recovery_response.json()["token"] - assert read_json(tokens_file)["tokens"][3]["token"] == new_token - assert read_json(tokens_file)["tokens"][3]["name"] == "recovery_device2" + rest_recover_with_mnemonic(client, mnemonic_token, "recover_device") + # And again + rest_recover_with_mnemonic(client, mnemonic_token, "recover_device2") # Try to use token after expiration date new_data = read_json(tokens_file) @@ -450,16 +434,7 @@ def test_generate_recovery_token_with_limited_uses( } # Try to use the token - recovery_response = client.post( - "/auth/recovery_token/use", - json={"token": mnemonic_token, "device": "recovery_device"}, - ) - assert recovery_response.status_code == 200 - new_token = recovery_response.json()["token"] - assert read_json(tokens_file)["tokens"][2]["token"] == new_token - assert read_json(tokens_file)["tokens"][2]["name"] == "recovery_device" - - assert read_json(tokens_file)["recovery_token"]["uses_left"] == 1 + rest_recover_with_mnemonic(client, mnemonic_token, "recover_device") # Get the status of the token response = authorized_client.get("/auth/recovery_token") @@ -473,14 +448,7 @@ def test_generate_recovery_token_with_limited_uses( } # Try to use token again - recovery_response = client.post( - "/auth/recovery_token/use", - json={"token": mnemonic_token, "device": "recovery_device2"}, - ) - assert recovery_response.status_code == 200 - new_token = recovery_response.json()["token"] - assert read_json(tokens_file)["tokens"][3]["token"] == new_token - assert read_json(tokens_file)["tokens"][3]["name"] == "recovery_device2" + rest_recover_with_mnemonic(client, mnemonic_token, "recover_device2") # Get the status of the token response = authorized_client.get("/auth/recovery_token") From 406f5ee921b75d0e33f4c48c0a67a810fcc7db3d Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Fri, 30 Dec 2022 12:33:21 +0000 Subject: [PATCH 20/27] test(tokens-repo): use mock time for recovery tokens expiration test --- tests/test_rest_endpoints/test_auth.py | 44 ++++++++++---------------- 1 file changed, 16 insertions(+), 28 deletions(-) diff --git a/tests/test_rest_endpoints/test_auth.py b/tests/test_rest_endpoints/test_auth.py index bdfb579..309cc6c 100644 --- a/tests/test_rest_endpoints/test_auth.py +++ b/tests/test_rest_endpoints/test_auth.py @@ -32,9 +32,16 @@ DATE_FORMATS = [ ] # 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" +class NearFuture(datetime.datetime): + @classmethod + def now(cls): + return datetime.datetime.now() + datetime.timedelta(minutes=13) + + def assert_original(filename): 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) # TARDIS sounds - new_time = datetime.datetime.now() + datetime.timedelta(minutes=13) - - class warped_spacetime(datetime.datetime): - @classmethod - def now(cls): - return new_time - - mock = mocker.patch(DEVICE_KEY_VALIDATION_DATETIME, warped_spacetime) + mock = mocker.patch(DEVICE_KEY_VALIDATION_DATETIME, NearFuture) response = client.post( "/auth/new_device/authorize", @@ -320,7 +320,7 @@ def test_generate_recovery_token(authorized_client, client, tokens_file): @pytest.mark.parametrize("timeformat", DATE_FORMATS) 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 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") # Try to use token after expiration date - new_data = read_json(tokens_file) - new_data["recovery_token"]["expiration"] = datetime.datetime.now().strftime( - "%Y-%m-%dT%H:%M:%S.%f" - ) - write_json(tokens_file, new_data) + mock = mocker.patch(RECOVERY_KEY_VALIDATION_DATETIME, NearFuture) + device_name = "recovery_device3" recovery_response = client.post( "/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 that the token was not created in JSON - assert read_json(tokens_file)["tokens"] == new_data["tokens"] - - # 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, - } + # Assert that the token was not created + assert device_name not in [ + token["name"] for token in rest_get_tokens_info(authorized_client) + ] @pytest.mark.parametrize("timeformat", DATE_FORMATS) From a252c11e7d092da1ceba3e1dd0c78772b1b946fc Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Fri, 30 Dec 2022 12:52:12 +0000 Subject: [PATCH 21/27] test(tokens-repo): allow ading uses in a helper recovery function --- tests/test_rest_endpoints/test_auth.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/tests/test_rest_endpoints/test_auth.py b/tests/test_rest_endpoints/test_auth.py index 309cc6c..33eb76a 100644 --- a/tests/test_rest_endpoints/test_auth.py +++ b/tests/test_rest_endpoints/test_auth.py @@ -251,15 +251,23 @@ def test_get_recovery_token_when_none_exists(authorized_client, tokens_file): assert_original(tokens_file) -def rest_make_recovery_token(client, expires_at=None, timeformat=None): - if expires_at is None: - response = client.post("/auth/recovery_token") - else: +def rest_make_recovery_token(client, expires_at=None, timeformat=None, uses=None): + json = {} + + if expires_at is not None: assert timeformat is not None expires_at_str = expires_at.strftime(timeformat) + json["expiration"] = expires_at_str + + if uses is not None: + json["uses"] = uses + + if json == {}: + response = client.post("/auth/recovery_token") + else: response = client.post( "/auth/recovery_token", - json={"expiration": expires_at_str}, + json=json, ) assert response.status_code == 200 From cd1afb8464ceedf9ba9409850e94f817cc90da1c Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Fri, 30 Dec 2022 13:04:57 +0000 Subject: [PATCH 22/27] test(tokens-repo): refactor the rest of auth tests --- tests/test_rest_endpoints/test_auth.py | 46 ++++++-------------------- 1 file changed, 10 insertions(+), 36 deletions(-) diff --git a/tests/test_rest_endpoints/test_auth.py b/tests/test_rest_endpoints/test_auth.py index 33eb76a..7e55900 100644 --- a/tests/test_rest_endpoints/test_auth.py +++ b/tests/test_rest_endpoints/test_auth.py @@ -378,7 +378,7 @@ def test_generate_recovery_token_with_expiration_in_the_past( json={"expiration": expiration_date_str}, ) assert response.status_code == 400 - assert "recovery_token" not in read_json(tokens_file) + assert not rest_get_recovery_status(authorized_client)["exists"] def test_generate_recovery_token_with_invalid_time_format( @@ -391,37 +391,19 @@ def test_generate_recovery_token_with_invalid_time_format( json={"expiration": expiration_date}, ) assert response.status_code == 422 - assert "recovery_token" not in read_json(tokens_file) + assert not rest_get_recovery_status(authorized_client)["exists"] def test_generate_recovery_token_with_limited_uses( authorized_client, client, tokens_file ): # Generate token with limited uses - response = authorized_client.post( - "/auth/recovery_token", - json={"uses": 2}, - ) - assert response.status_code == 200 - assert "token" in response.json() - mnemonic_token = response.json()["token"] - token = Mnemonic(language="english").to_entropy(mnemonic_token).hex() - assert read_json(tokens_file)["recovery_token"]["token"] == token - assert read_json(tokens_file)["recovery_token"]["uses_left"] == 2 + mnemonic_token = rest_make_recovery_token(authorized_client, uses=2) - # Get the date of the token - time_generated = read_json(tokens_file)["recovery_token"]["date"] - assert time_generated is not None - assert ( - datetime.datetime.strptime(time_generated, "%Y-%m-%dT%H:%M:%S.%f") - - datetime.timedelta(seconds=5) - < datetime.datetime.now() - ) + time_generated = rest_get_recovery_date(authorized_client) + assert_recovery_recent(time_generated) - # Try to get token status - response = authorized_client.get("/auth/recovery_token") - assert response.status_code == 200 - assert response.json() == { + assert rest_get_recovery_status(authorized_client) == { "exists": True, "valid": True, "date": time_generated, @@ -432,10 +414,7 @@ def test_generate_recovery_token_with_limited_uses( # Try to use the token rest_recover_with_mnemonic(client, mnemonic_token, "recover_device") - # Get the status of the token - response = authorized_client.get("/auth/recovery_token") - assert response.status_code == 200 - assert response.json() == { + assert rest_get_recovery_status(authorized_client) == { "exists": True, "valid": True, "date": time_generated, @@ -446,10 +425,7 @@ def test_generate_recovery_token_with_limited_uses( # Try to use token again rest_recover_with_mnemonic(client, mnemonic_token, "recover_device2") - # Get the status of the token - response = authorized_client.get("/auth/recovery_token") - assert response.status_code == 200 - assert response.json() == { + assert rest_get_recovery_status(authorized_client) == { "exists": True, "valid": False, "date": time_generated, @@ -464,8 +440,6 @@ def test_generate_recovery_token_with_limited_uses( ) assert recovery_response.status_code == 404 - assert read_json(tokens_file)["recovery_token"]["uses_left"] == 0 - def test_generate_recovery_token_with_negative_uses( authorized_client, client, tokens_file @@ -476,7 +450,7 @@ def test_generate_recovery_token_with_negative_uses( json={"uses": -2}, ) assert response.status_code == 400 - assert "recovery_token" not in read_json(tokens_file) + assert not rest_get_recovery_status(authorized_client)["exists"] def test_generate_recovery_token_with_zero_uses(authorized_client, client, tokens_file): @@ -486,4 +460,4 @@ def test_generate_recovery_token_with_zero_uses(authorized_client, client, token json={"uses": 0}, ) assert response.status_code == 400 - assert "recovery_token" not in read_json(tokens_file) + assert not rest_get_recovery_status(authorized_client)["exists"] From 2f0a7c35f3da6d600497f0c9988ba6f0a8d74541 Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Fri, 30 Dec 2022 13:11:10 +0000 Subject: [PATCH 23/27] test(tokens-repo): beautify test_auth.py --- tests/test_rest_endpoints/test_auth.py | 115 +++++++++++++------------ 1 file changed, 59 insertions(+), 56 deletions(-) diff --git a/tests/test_rest_endpoints/test_auth.py b/tests/test_rest_endpoints/test_auth.py index 7e55900..17585fb 100644 --- a/tests/test_rest_endpoints/test_auth.py +++ b/tests/test_rest_endpoints/test_auth.py @@ -57,6 +57,64 @@ def rest_get_tokens_info(client): return response.json() +def rest_make_recovery_token(client, expires_at=None, timeformat=None, uses=None): + json = {} + + if expires_at is not None: + assert timeformat is not None + expires_at_str = expires_at.strftime(timeformat) + json["expiration"] = expires_at_str + + if uses is not None: + json["uses"] = uses + + if json == {}: + response = client.post("/auth/recovery_token") + else: + response = client.post( + "/auth/recovery_token", + json=json, + ) + + assert response.status_code == 200 + assert "token" in response.json() + return response.json()["token"] + + +def rest_get_recovery_status(client): + response = client.get("/auth/recovery_token") + assert response.status_code == 200 + return response.json() + + +def rest_get_recovery_date(client): + status = rest_get_recovery_status(client) + assert "date" in status + return status["date"] + + +def assert_recovery_recent(time_generated): + assert ( + datetime.datetime.strptime(time_generated, "%Y-%m-%dT%H:%M:%S.%f") + - datetime.timedelta(seconds=5) + < datetime.datetime.now() + ) + + +def rest_recover_with_mnemonic(client, mnemonic_token, device_name): + recovery_response = client.post( + "/auth/recovery_token/use", + json={"token": mnemonic_token, "device": device_name}, + ) + assert recovery_response.status_code == 200 + new_token = recovery_response.json()["token"] + assert_token_valid(client, new_token) + return new_token + + +# Tokens + + def test_get_tokens_info(authorized_client, tokens_file): assert rest_get_tokens_info(authorized_client) == [ {"name": "test_token", "date": "2022-01-14T08:31:10.789314", "is_caller": True}, @@ -118,7 +176,7 @@ def test_refresh_token(authorized_client, tokens_file): assert_token_valid(authorized_client, new_token) -# new device +# New device def test_get_new_device_auth_token_unauthorized(client, tokens_file): @@ -251,61 +309,6 @@ def test_get_recovery_token_when_none_exists(authorized_client, tokens_file): assert_original(tokens_file) -def rest_make_recovery_token(client, expires_at=None, timeformat=None, uses=None): - json = {} - - if expires_at is not None: - assert timeformat is not None - expires_at_str = expires_at.strftime(timeformat) - json["expiration"] = expires_at_str - - if uses is not None: - json["uses"] = uses - - if json == {}: - response = client.post("/auth/recovery_token") - else: - response = client.post( - "/auth/recovery_token", - json=json, - ) - - assert response.status_code == 200 - assert "token" in response.json() - return response.json()["token"] - - -def rest_get_recovery_status(client): - response = client.get("/auth/recovery_token") - assert response.status_code == 200 - return response.json() - - -def rest_get_recovery_date(client): - status = rest_get_recovery_status(client) - assert "date" in status - return status["date"] - - -def assert_recovery_recent(time_generated): - assert ( - datetime.datetime.strptime(time_generated, "%Y-%m-%dT%H:%M:%S.%f") - - datetime.timedelta(seconds=5) - < datetime.datetime.now() - ) - - -def rest_recover_with_mnemonic(client, mnemonic_token, device_name): - recovery_response = client.post( - "/auth/recovery_token/use", - json={"token": mnemonic_token, "device": device_name}, - ) - assert recovery_response.status_code == 200 - new_token = recovery_response.json()["token"] - assert_token_valid(client, new_token) - return new_token - - def test_generate_recovery_token(authorized_client, client, tokens_file): # Generate token without expiration and uses_left mnemonic_token = rest_make_recovery_token(authorized_client) From d3c337853e029147f4aac3ae5291b8c319f128a8 Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Mon, 2 Jan 2023 14:33:48 +0000 Subject: [PATCH 24/27] fix(tokens-repo): readd gitkeep to services data folder after rebase --- tests/test_rest_endpoints/services/{ => data}/gitkeep | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/test_rest_endpoints/services/{ => data}/gitkeep (100%) diff --git a/tests/test_rest_endpoints/services/gitkeep b/tests/test_rest_endpoints/services/data/gitkeep similarity index 100% rename from tests/test_rest_endpoints/services/gitkeep rename to tests/test_rest_endpoints/services/data/gitkeep From 7a27af917372a51fda6f5fa6a56c0576c19b9074 Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Mon, 2 Jan 2023 15:49:55 +0000 Subject: [PATCH 25/27] test(tokens-repo): new assert_original(), no more json --- tests/test_rest_endpoints/test_auth.py | 118 ++++++++++++++----------- 1 file changed, 65 insertions(+), 53 deletions(-) diff --git a/tests/test_rest_endpoints/test_auth.py b/tests/test_rest_endpoints/test_auth.py index 17585fb..40960f0 100644 --- a/tests/test_rest_endpoints/test_auth.py +++ b/tests/test_rest_endpoints/test_auth.py @@ -3,10 +3,6 @@ # pylint: disable=missing-function-docstring import datetime import pytest -from mnemonic import Mnemonic - - -from tests.common import read_json, write_json TOKENS_FILE_CONTENTS = { @@ -14,12 +10,12 @@ TOKENS_FILE_CONTENTS = { { "token": "TEST_TOKEN", "name": "test_token", - "date": "2022-01-14 08:31:10.789314", + "date": datetime.datetime(2022, 1, 14, 8, 31, 10, 789314), }, { "token": "TEST_TOKEN2", "name": "test_token2", - "date": "2022-01-14 08:31:10.789314", + "date": datetime.datetime(2022, 1, 14, 8, 31, 10, 789314), }, ] } @@ -42,8 +38,17 @@ class NearFuture(datetime.datetime): return datetime.datetime.now() + datetime.timedelta(minutes=13) -def assert_original(filename): - assert read_json(filename) == TOKENS_FILE_CONTENTS +def assert_original(client): + new_tokens = rest_get_tokens_info(client) + + for token in TOKENS_FILE_CONTENTS["tokens"]: + assert_token_valid(client, token["token"]) + for new_token in new_tokens: + if new_token["name"] == token["name"]: + assert ( + datetime.datetime.fromisoformat(new_token["date"]) == token["date"] + ) + assert_no_recovery(client) def assert_token_valid(client, token): @@ -57,6 +62,17 @@ def rest_get_tokens_info(client): return response.json() +def rest_try_authorize_new_device(client, token, device_name): + response = client.post( + "/auth/new_device/authorize", + json={ + "token": token, + "device": device_name, + }, + ) + return response + + def rest_make_recovery_token(client, expires_at=None, timeformat=None, uses=None): json = {} @@ -101,6 +117,10 @@ def assert_recovery_recent(time_generated): ) +def assert_no_recovery(client): + assert not rest_get_recovery_status(client)["exists"] + + def rest_recover_with_mnemonic(client, mnemonic_token, device_name): recovery_response = client.post( "/auth/recovery_token/use", @@ -131,10 +151,10 @@ def test_get_tokens_unauthorized(client, tokens_file): assert response.status_code == 401 -def test_delete_token_unauthorized(client, tokens_file): +def test_delete_token_unauthorized(client, authorized_client, tokens_file): response = client.delete("/auth/tokens") assert response.status_code == 401 - assert_original(tokens_file) + assert_original(authorized_client) def test_delete_token(authorized_client, tokens_file): @@ -152,7 +172,7 @@ def test_delete_self_token(authorized_client, tokens_file): "/auth/tokens", json={"token_name": "test_token"} ) assert response.status_code == 400 - assert_original(tokens_file) + assert_original(authorized_client) def test_delete_nonexistent_token(authorized_client, tokens_file): @@ -160,13 +180,13 @@ def test_delete_nonexistent_token(authorized_client, tokens_file): "/auth/tokens", json={"token_name": "test_token3"} ) assert response.status_code == 404 - assert_original(tokens_file) + assert_original(authorized_client) -def test_refresh_token_unauthorized(client, tokens_file): +def test_refresh_token_unauthorized(client, authorized_client, tokens_file): response = client.post("/auth/tokens") assert response.status_code == 401 - assert_original(tokens_file) + assert_original(authorized_client) def test_refresh_token(authorized_client, tokens_file): @@ -179,23 +199,26 @@ def test_refresh_token(authorized_client, tokens_file): # New device -def test_get_new_device_auth_token_unauthorized(client, tokens_file): +def test_get_new_device_auth_token_unauthorized(client, authorized_client, tokens_file): response = client.post("/auth/new_device") assert response.status_code == 401 - assert_original(tokens_file) + assert "token" not in response.json() + assert "detail" in response.json() + # We only can check existence of a token we know. -def test_get_and_delete_new_device_token(authorized_client, tokens_file): +def test_get_and_delete_new_device_token(client, authorized_client, tokens_file): token = rest_get_new_device_token(authorized_client) response = authorized_client.delete("/auth/new_device", json={"token": token}) assert response.status_code == 200 - assert_original(tokens_file) + assert rest_try_authorize_new_device(client, token, "new_device").status_code == 404 -def test_delete_token_unauthenticated(client, tokens_file): - response = client.delete("/auth/new_device") +def test_delete_token_unauthenticated(client, authorized_client, tokens_file): + token = rest_get_new_device_token(authorized_client) + response = client.delete("/auth/new_device", json={"token": token}) assert response.status_code == 401 - assert_original(tokens_file) + assert rest_try_authorize_new_device(client, token, "new_device").status_code == 200 def rest_get_new_device_token(client): @@ -207,38 +230,29 @@ def rest_get_new_device_token(client): def test_get_and_authorize_new_device(client, authorized_client, tokens_file): token = rest_get_new_device_token(authorized_client) - response = client.post( - "/auth/new_device/authorize", - json={ - "token": token, - "device": "new_device", - }, - ) + response = rest_try_authorize_new_device(client, token, "new_device") assert response.status_code == 200 assert_token_valid(authorized_client, response.json()["token"]) -def test_authorize_new_device_with_invalid_token(client, tokens_file): - response = client.post( - "/auth/new_device/authorize", - json={"token": "invalid_token", "device": "new_device"}, - ) +def test_authorize_new_device_with_invalid_token( + client, authorized_client, tokens_file +): + response = rest_try_authorize_new_device(client, "invalid_token", "new_device") assert response.status_code == 404 - assert_original(tokens_file) + assert_original(authorized_client) def test_get_and_authorize_used_token(client, authorized_client, tokens_file): token_to_be_used_2_times = rest_get_new_device_token(authorized_client) - response = client.post( - "/auth/new_device/authorize", - json={"token": token_to_be_used_2_times, "device": "new_device"}, + response = rest_try_authorize_new_device( + client, token_to_be_used_2_times, "new_device" ) assert response.status_code == 200 assert_token_valid(authorized_client, response.json()["token"]) - response = client.post( - "/auth/new_device/authorize", - json={"token": token_to_be_used_2_times, "device": "new_device"}, + response = rest_try_authorize_new_device( + client, token_to_be_used_2_times, "new_device" ) assert response.status_code == 404 @@ -251,20 +265,18 @@ def test_get_and_authorize_token_after_12_minutes( # TARDIS sounds mock = mocker.patch(DEVICE_KEY_VALIDATION_DATETIME, NearFuture) - response = client.post( - "/auth/new_device/authorize", - json={"token": token, "device": "new_device"}, - ) + response = rest_try_authorize_new_device(client, token, "new_device") assert response.status_code == 404 + assert_original(authorized_client) -def test_authorize_without_token(client, tokens_file): +def test_authorize_without_token(client, authorized_client, tokens_file): response = client.post( "/auth/new_device/authorize", json={"device": "new_device"}, ) assert response.status_code == 422 - assert_original(tokens_file) + assert_original(authorized_client) # Recovery tokens @@ -290,10 +302,10 @@ def test_authorize_without_token(client, tokens_file): # - if request is invalid, returns 400 -def test_get_recovery_token_status_unauthorized(client, tokens_file): +def test_get_recovery_token_status_unauthorized(client, authorized_client, tokens_file): response = client.get("/auth/recovery_token") assert response.status_code == 401 - assert_original(tokens_file) + assert_original(authorized_client) def test_get_recovery_token_when_none_exists(authorized_client, tokens_file): @@ -306,7 +318,7 @@ def test_get_recovery_token_when_none_exists(authorized_client, tokens_file): "expiration": None, "uses_left": None, } - assert_original(tokens_file) + assert_original(authorized_client) def test_generate_recovery_token(authorized_client, client, tokens_file): @@ -381,7 +393,7 @@ def test_generate_recovery_token_with_expiration_in_the_past( json={"expiration": expiration_date_str}, ) assert response.status_code == 400 - assert not rest_get_recovery_status(authorized_client)["exists"] + assert_no_recovery(authorized_client) def test_generate_recovery_token_with_invalid_time_format( @@ -394,7 +406,7 @@ def test_generate_recovery_token_with_invalid_time_format( json={"expiration": expiration_date}, ) assert response.status_code == 422 - assert not rest_get_recovery_status(authorized_client)["exists"] + assert_no_recovery(authorized_client) def test_generate_recovery_token_with_limited_uses( @@ -453,7 +465,7 @@ def test_generate_recovery_token_with_negative_uses( json={"uses": -2}, ) assert response.status_code == 400 - assert not rest_get_recovery_status(authorized_client)["exists"] + assert_no_recovery(authorized_client) def test_generate_recovery_token_with_zero_uses(authorized_client, client, tokens_file): @@ -463,4 +475,4 @@ def test_generate_recovery_token_with_zero_uses(authorized_client, client, token json={"uses": 0}, ) assert response.status_code == 400 - assert not rest_get_recovery_status(authorized_client)["exists"] + assert_no_recovery(authorized_client) From 91bbc3520b091c967f3fe78077cb1273222f4ee0 Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Mon, 2 Jan 2023 17:22:18 +0000 Subject: [PATCH 26/27] test(tokens-repo): make shared test token state use token repo api for loading --- tests/conftest.py | 69 ++++++++++++++++--- tests/test_graphql/test_api_devices.py | 6 +- .../test_repository/test_tokens_repository.py | 26 ------- 3 files changed, 64 insertions(+), 37 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 4b65d20..bba3915 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -4,8 +4,34 @@ import os import pytest from fastapi.testclient import TestClient -from shutil import copy import os.path as path +import datetime + +# from selfprivacy_api.actions.api_tokens import TOKEN_REPO +from selfprivacy_api.models.tokens.token import Token +from selfprivacy_api.repositories.tokens.json_tokens_repository import ( + JsonTokensRepository, +) + +from tests.common import read_json + +EMPTY_TOKENS_JSON = ' {"tokens": []}' + + +TOKENS_FILE_CONTENTS = { + "tokens": [ + { + "token": "TEST_TOKEN", + "name": "test_token", + "date": datetime.datetime(2022, 1, 14, 8, 31, 10, 789314), + }, + { + "token": "TEST_TOKEN2", + "name": "test_token2", + "date": datetime.datetime(2022, 1, 14, 8, 31, 10, 789314), + }, + ] +} def pytest_generate_tests(metafunc): @@ -17,13 +43,40 @@ def global_data_dir(): @pytest.fixture -def tokens_file(mocker, tmpdir): - """Mock tokens file.""" - tmp_file = tmpdir / "tokens.json" - source_file = path.join(global_data_dir(), "tokens.json") - copy(source_file, tmp_file) - mock = mocker.patch("selfprivacy_api.utils.TOKENS_FILE", tmp_file) - return mock +def empty_tokens(mocker, tmpdir): + tokenfile = tmpdir / "empty_tokens.json" + with open(tokenfile, "w") as file: + file.write(EMPTY_TOKENS_JSON) + mocker.patch("selfprivacy_api.utils.TOKENS_FILE", new=tokenfile) + assert read_json(tokenfile)["tokens"] == [] + return tmpdir + + +@pytest.fixture +def empty_json_repo(empty_tokens): + repo = JsonTokensRepository() + for token in repo.get_tokens(): + repo.delete_token(token) + assert repo.get_tokens() == [] + return repo + + +@pytest.fixture +def tokens_file(empty_json_repo, tmpdir): + """A state with tokens""" + for token in TOKENS_FILE_CONTENTS["tokens"]: + empty_json_repo._store_token( + Token( + token=token["token"], + device_name=token["name"], + created_at=token["date"], + ) + ) + # temporary return for compatibility with older tests + + tokenfile = tmpdir / "empty_tokens.json" + assert path.exists(tokenfile) + return tokenfile @pytest.fixture diff --git a/tests/test_graphql/test_api_devices.py b/tests/test_graphql/test_api_devices.py index 07cf42a..c546238 100644 --- a/tests/test_graphql/test_api_devices.py +++ b/tests/test_graphql/test_api_devices.py @@ -17,12 +17,12 @@ TOKENS_FILE_CONTETS = { { "token": "TEST_TOKEN", "name": "test_token", - "date": "2022-01-14 08:31:10.789314", + "date": "2022-01-14T08:31:10.789314", }, { "token": "TEST_TOKEN2", "name": "test_token2", - "date": "2022-01-14 08:31:10.789314", + "date": "2022-01-14T08:31:10.789314", }, ] } @@ -118,7 +118,7 @@ def test_graphql_delete_token(authorized_client, tokens_file): { "token": "TEST_TOKEN", "name": "test_token", - "date": "2022-01-14 08:31:10.789314", + "date": "2022-01-14T08:31:10.789314", } ] } diff --git a/tests/test_graphql/test_repository/test_tokens_repository.py b/tests/test_graphql/test_repository/test_tokens_repository.py index b172f13..a2dbb7a 100644 --- a/tests/test_graphql/test_repository/test_tokens_repository.py +++ b/tests/test_graphql/test_repository/test_tokens_repository.py @@ -16,13 +16,9 @@ from selfprivacy_api.repositories.tokens.exceptions import ( TokenNotFound, NewDeviceKeyNotFound, ) -from selfprivacy_api.repositories.tokens.json_tokens_repository import ( - JsonTokensRepository, -) from selfprivacy_api.repositories.tokens.redis_tokens_repository import ( RedisTokensRepository, ) -from tests.common import read_json ORIGINAL_DEVICE_NAMES = [ @@ -33,23 +29,10 @@ ORIGINAL_DEVICE_NAMES = [ ] -EMPTY_TOKENS_JSON = ' {"tokens": []}' - - def mnemonic_from_hex(hexkey): return Mnemonic(language="english").to_mnemonic(bytes.fromhex(hexkey)) -@pytest.fixture -def empty_tokens(mocker, tmpdir): - tokens_file = tmpdir / "empty_tokens.json" - with open(tokens_file, "w") as file: - file.write(EMPTY_TOKENS_JSON) - mocker.patch("selfprivacy_api.utils.TOKENS_FILE", new=tokens_file) - assert read_json(tokens_file)["tokens"] == [] - return tmpdir - - @pytest.fixture def mock_new_device_key_generate(mocker): mock = mocker.patch( @@ -137,15 +120,6 @@ def mock_recovery_key_generate(mocker): return mock -@pytest.fixture -def empty_json_repo(empty_tokens): - repo = JsonTokensRepository() - for token in repo.get_tokens(): - repo.delete_token(token) - assert repo.get_tokens() == [] - return repo - - @pytest.fixture def empty_redis_repo(): repo = RedisTokensRepository() From 710ad7221b5464b2f8d337903c64707389abc637 Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Mon, 2 Jan 2023 18:22:04 +0000 Subject: [PATCH 27/27] refactor(tokens-repo): delete a stray comment --- tests/conftest.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/conftest.py b/tests/conftest.py index bba3915..891e4e9 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -7,7 +7,6 @@ from fastapi.testclient import TestClient import os.path as path import datetime -# from selfprivacy_api.actions.api_tokens import TOKEN_REPO from selfprivacy_api.models.tokens.token import Token from selfprivacy_api.repositories.tokens.json_tokens_repository import ( JsonTokensRepository,