From 68d0ee8c5d9e3ece448224b1b865abde72f9bdf4 Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Wed, 28 Aug 2024 16:29:26 +0000 Subject: [PATCH] test(system): dns migration --- .../graphql/mutations/system_mutations.py | 4 +- tests/test_graphql/test_system.py | 70 ++++++++++++++++++- tests/test_system.py | 12 ++-- 3 files changed, 79 insertions(+), 7 deletions(-) diff --git a/selfprivacy_api/graphql/mutations/system_mutations.py b/selfprivacy_api/graphql/mutations/system_mutations.py index b305437..c304cc7 100644 --- a/selfprivacy_api/graphql/mutations/system_mutations.py +++ b/selfprivacy_api/graphql/mutations/system_mutations.py @@ -55,7 +55,7 @@ class SSHSettingsInput: @strawberry.input -class SetDNSProviderInput: +class SetDnsProviderInput: """Input type to set the provider""" provider: DnsProvider @@ -225,7 +225,7 @@ class SystemMutations: ) @strawberry.mutation(permission_classes=[IsAuthenticated]) - def set_dns_provider(self, input: SetDNSProviderInput) -> GenericMutationReturn: + def set_dns_provider(self, input: SetDnsProviderInput) -> GenericMutationReturn: try: set_dns_provider(input.provider, input.api_token) diff --git a/tests/test_graphql/test_system.py b/tests/test_graphql/test_system.py index 36a1cc1..dc83182 100644 --- a/tests/test_graphql/test_system.py +++ b/tests/test_graphql/test_system.py @@ -4,9 +4,17 @@ import os import pytest +from selfprivacy_api.graphql.queries.providers import DnsProvider + from tests.common import generate_system_query, read_json -from tests.test_graphql.common import assert_empty +from tests.test_graphql.common import ( + assert_empty, + assert_ok, + assert_errorcode, + get_data, +) from tests.test_dkim import no_dkim_file, dkim_file +from tests.test_system import assert_provider @pytest.fixture @@ -123,6 +131,23 @@ def mock_dkim_key(mocker): return mock +def api_set_dns_provider_raw(authorized_client, provider: str, token: str): + response = authorized_client.post( + "/graphql", + json={ + "query": API_SET_DNS_PROVIDER_MUTATION, + "variables": { + "input": {"provider": provider, "apiToken": token}, + }, + }, + ) + return response + + +def api_set_dns_provider(authorized_client, provider: DnsProvider, token: str): + return api_set_dns_provider_raw(authorized_client, provider.value, token) + + API_PYTHON_VERSION_INFO = """ info { pythonVersion @@ -1002,3 +1027,46 @@ def test_graphql_pull_system_broken_repo( assert mock_os_chdir.call_count == 2 assert mock_os_chdir.call_args_list[0][0][0] == "/etc/nixos" assert mock_os_chdir.call_args_list[1][0][0] == current_dir + + +API_SET_DNS_PROVIDER_MUTATION = """ +mutation TestSetDnsProvider($input: SetDnsProviderInput!) { + system { + setDnsProvider(input: $input) { + success + message + code + } + } +} +""" + + +def test_set_dns_provider(authorized_client, generic_userdata): + provider = DnsProvider.DIGITALOCEAN + token = "someRandomToken" + + response = api_set_dns_provider(authorized_client, provider, token) + data = get_data(response)["system"]["setDnsProvider"] + assert_ok(data) + assert_provider(provider.value, token) + + +def test_set_dns_provider_nonexistent(authorized_client, generic_userdata): + provider = "BOGUSINC" + token = "someRandomToken" + + response = api_set_dns_provider_raw(authorized_client, provider, token) + assert_empty(response) + + # Test that nothing has indeed changed + with pytest.raises(AssertionError): + assert_provider(provider, token) + + +def test_set_dns_provider_unauthorized(client, generic_userdata): + provider = DnsProvider.DIGITALOCEAN + token = "someRandomToken" + + response = api_set_dns_provider(client, provider, token) + assert_empty(response) diff --git a/tests/test_system.py b/tests/test_system.py index e469ea2..8bd667e 100644 --- a/tests/test_system.py +++ b/tests/test_system.py @@ -6,16 +6,20 @@ from selfprivacy_api.actions.system import set_dns_provider from selfprivacy_api.graphql.queries.providers import DnsProvider +def assert_provider(provider_str: str, key: str): + with ReadUserData() as user_data: + assert user_data["dns"]["provider"] == provider_str + with ReadUserData(file_type=UserDataFiles.SECRETS) as secrets: + assert secrets["dns"]["apiKey"] == key + + def test_set_dns(generic_userdata): token = "testytesty" provider = DnsProvider.DESEC set_dns_provider(provider, token) - with ReadUserData() as user_data: - assert user_data["dns"]["provider"] == "DESEC" - with ReadUserData(file_type=UserDataFiles.SECRETS) as secrets: - assert secrets["dns"]["apiKey"] == token + assert_provider(provider.value, token) # uname is just an arbitrary command expected to be everywhere we care