mirror of
https://git.selfprivacy.org/SelfPrivacy/selfprivacy-rest-api.git
synced 2025-03-18 20:39:46 +00:00
test(tokens-repo): break out assert_original() in graphql device tests
This commit is contained in:
parent
2f707cc0cc
commit
d26d115172
2 changed files with 32 additions and 37 deletions
|
@ -32,6 +32,8 @@ TOKENS_FILE_CONTENTS = {
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DEVICE_WE_AUTH_TESTS_WITH = TOKENS_FILE_CONTENTS["tokens"][0]
|
||||||
|
|
||||||
|
|
||||||
def pytest_generate_tests(metafunc):
|
def pytest_generate_tests(metafunc):
|
||||||
os.environ["TEST_MODE"] = "true"
|
os.environ["TEST_MODE"] = "true"
|
||||||
|
@ -107,7 +109,9 @@ def authorized_client(tokens_file, huey_database, jobs_file):
|
||||||
from selfprivacy_api.app import app
|
from selfprivacy_api.app import app
|
||||||
|
|
||||||
client = TestClient(app)
|
client = TestClient(app)
|
||||||
client.headers.update({"Authorization": "Bearer TEST_TOKEN"})
|
client.headers.update(
|
||||||
|
{"Authorization": "Bearer " + DEVICE_WE_AUTH_TESTS_WITH["token"]}
|
||||||
|
)
|
||||||
return client
|
return client
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -11,21 +11,7 @@ from selfprivacy_api.repositories.tokens.json_tokens_repository import (
|
||||||
from selfprivacy_api.models.tokens.token import Token
|
from selfprivacy_api.models.tokens.token import Token
|
||||||
|
|
||||||
from tests.common import generate_api_query, read_json, write_json
|
from tests.common import generate_api_query, read_json, write_json
|
||||||
|
from tests.conftest import DEVICE_WE_AUTH_TESTS_WITH, TOKENS_FILE_CONTENTS
|
||||||
TOKENS_FILE_CONTETS = {
|
|
||||||
"tokens": [
|
|
||||||
{
|
|
||||||
"token": "TEST_TOKEN",
|
|
||||||
"name": "test_token",
|
|
||||||
"date": "2022-01-14T08:31:10.789314",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"token": "TEST_TOKEN2",
|
|
||||||
"name": "test_token2",
|
|
||||||
"date": "2022-01-14T08:31:10.789314",
|
|
||||||
},
|
|
||||||
]
|
|
||||||
}
|
|
||||||
|
|
||||||
API_DEVICES_QUERY = """
|
API_DEVICES_QUERY = """
|
||||||
devices {
|
devices {
|
||||||
|
@ -41,27 +27,30 @@ def token_repo():
|
||||||
return JsonTokensRepository()
|
return JsonTokensRepository()
|
||||||
|
|
||||||
|
|
||||||
def test_graphql_tokens_info(authorized_client, tokens_file):
|
def assert_original(client):
|
||||||
response = authorized_client.post(
|
response = client.post(
|
||||||
"/graphql",
|
"/graphql",
|
||||||
json={"query": generate_api_query([API_DEVICES_QUERY])},
|
json={"query": generate_api_query([API_DEVICES_QUERY])},
|
||||||
)
|
)
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
assert response.json().get("data") is not None
|
assert response.json().get("data") is not None
|
||||||
assert response.json()["data"]["api"]["devices"] is not None
|
devices = response.json()["data"]["api"]["devices"]
|
||||||
assert len(response.json()["data"]["api"]["devices"]) == 2
|
assert devices is not None
|
||||||
assert (
|
original_devices = TOKENS_FILE_CONTENTS["tokens"]
|
||||||
response.json()["data"]["api"]["devices"][0]["creationDate"]
|
assert len(devices) == len(original_devices)
|
||||||
== "2022-01-14T08:31:10.789314"
|
for original_device in original_devices:
|
||||||
)
|
assert original_device["name"] in [device["name"] for device in devices]
|
||||||
assert response.json()["data"]["api"]["devices"][0]["isCaller"] is True
|
for device in devices:
|
||||||
assert response.json()["data"]["api"]["devices"][0]["name"] == "test_token"
|
if device["name"] == DEVICE_WE_AUTH_TESTS_WITH["name"]:
|
||||||
assert (
|
assert device["isCaller"] is True
|
||||||
response.json()["data"]["api"]["devices"][1]["creationDate"]
|
else:
|
||||||
== "2022-01-14T08:31:10.789314"
|
assert device["isCaller"] is False
|
||||||
)
|
if device["name"] == original_device["name"]:
|
||||||
assert response.json()["data"]["api"]["devices"][1]["isCaller"] is False
|
assert device["creationDate"] == original_device["date"].isoformat()
|
||||||
assert response.json()["data"]["api"]["devices"][1]["name"] == "test_token2"
|
|
||||||
|
|
||||||
|
def test_graphql_tokens_info(authorized_client, tokens_file):
|
||||||
|
assert_original(authorized_client)
|
||||||
|
|
||||||
|
|
||||||
def test_graphql_tokens_info_unauthorized(client, tokens_file):
|
def test_graphql_tokens_info_unauthorized(client, tokens_file):
|
||||||
|
@ -139,7 +128,7 @@ def test_graphql_delete_self_token(authorized_client, tokens_file):
|
||||||
assert response.json()["data"]["deleteDeviceApiToken"]["success"] is False
|
assert response.json()["data"]["deleteDeviceApiToken"]["success"] is False
|
||||||
assert response.json()["data"]["deleteDeviceApiToken"]["message"] is not None
|
assert response.json()["data"]["deleteDeviceApiToken"]["message"] is not None
|
||||||
assert response.json()["data"]["deleteDeviceApiToken"]["code"] == 400
|
assert response.json()["data"]["deleteDeviceApiToken"]["code"] == 400
|
||||||
assert read_json(tokens_file) == TOKENS_FILE_CONTETS
|
assert_original(authorized_client)
|
||||||
|
|
||||||
|
|
||||||
def test_graphql_delete_nonexistent_token(authorized_client, tokens_file):
|
def test_graphql_delete_nonexistent_token(authorized_client, tokens_file):
|
||||||
|
@ -157,7 +146,7 @@ def test_graphql_delete_nonexistent_token(authorized_client, tokens_file):
|
||||||
assert response.json()["data"]["deleteDeviceApiToken"]["success"] is False
|
assert response.json()["data"]["deleteDeviceApiToken"]["success"] is False
|
||||||
assert response.json()["data"]["deleteDeviceApiToken"]["message"] is not None
|
assert response.json()["data"]["deleteDeviceApiToken"]["message"] is not None
|
||||||
assert response.json()["data"]["deleteDeviceApiToken"]["code"] == 404
|
assert response.json()["data"]["deleteDeviceApiToken"]["code"] == 404
|
||||||
assert read_json(tokens_file) == TOKENS_FILE_CONTETS
|
assert_original(authorized_client)
|
||||||
|
|
||||||
|
|
||||||
REFRESH_TOKEN_MUTATION = """
|
REFRESH_TOKEN_MUTATION = """
|
||||||
|
@ -294,7 +283,7 @@ def test_graphql_get_and_delete_new_device_key(authorized_client, tokens_file):
|
||||||
assert response.json()["data"]["invalidateNewDeviceApiKey"]["success"] is True
|
assert response.json()["data"]["invalidateNewDeviceApiKey"]["success"] is True
|
||||||
assert response.json()["data"]["invalidateNewDeviceApiKey"]["message"] is not None
|
assert response.json()["data"]["invalidateNewDeviceApiKey"]["message"] is not None
|
||||||
assert response.json()["data"]["invalidateNewDeviceApiKey"]["code"] == 200
|
assert response.json()["data"]["invalidateNewDeviceApiKey"]["code"] == 200
|
||||||
assert read_json(tokens_file) == TOKENS_FILE_CONTETS
|
assert_original(authorized_client)
|
||||||
|
|
||||||
|
|
||||||
AUTHORIZE_WITH_NEW_DEVICE_KEY_MUTATION = """
|
AUTHORIZE_WITH_NEW_DEVICE_KEY_MUTATION = """
|
||||||
|
@ -347,7 +336,9 @@ def test_graphql_get_and_authorize_new_device(client, authorized_client, tokens_
|
||||||
assert read_json(tokens_file)["tokens"][2]["name"] == "new_device"
|
assert read_json(tokens_file)["tokens"][2]["name"] == "new_device"
|
||||||
|
|
||||||
|
|
||||||
def test_graphql_authorize_new_device_with_invalid_key(client, tokens_file):
|
def test_graphql_authorize_new_device_with_invalid_key(
|
||||||
|
client, authorized_client, tokens_file
|
||||||
|
):
|
||||||
response = client.post(
|
response = client.post(
|
||||||
"/graphql",
|
"/graphql",
|
||||||
json={
|
json={
|
||||||
|
@ -367,7 +358,7 @@ def test_graphql_authorize_new_device_with_invalid_key(client, tokens_file):
|
||||||
response.json()["data"]["authorizeWithNewDeviceApiKey"]["message"] is not None
|
response.json()["data"]["authorizeWithNewDeviceApiKey"]["message"] is not None
|
||||||
)
|
)
|
||||||
assert response.json()["data"]["authorizeWithNewDeviceApiKey"]["code"] == 404
|
assert response.json()["data"]["authorizeWithNewDeviceApiKey"]["code"] == 404
|
||||||
assert read_json(tokens_file) == TOKENS_FILE_CONTETS
|
assert_original(authorized_client)
|
||||||
|
|
||||||
|
|
||||||
def test_graphql_get_and_authorize_used_key(client, authorized_client, tokens_file):
|
def test_graphql_get_and_authorize_used_key(client, authorized_client, tokens_file):
|
||||||
|
|
Loading…
Add table
Reference in a new issue