This commit is contained in:
Inex Code 2022-07-05 15:54:21 +03:00
parent 5711cf66b0
commit e5405dfc6b
3 changed files with 41 additions and 25 deletions

View file

@ -1,3 +1,6 @@
"""Tests configuration."""
# pylint: disable=redefined-outer-name
# pylint: disable=unused-argument
import pytest
from flask import testing
from selfprivacy_api.app import create_app
@ -5,6 +8,7 @@ from selfprivacy_api.app import create_app
@pytest.fixture
def tokens_file(mocker, shared_datadir):
"""Mock tokens file."""
mock = mocker.patch(
"selfprivacy_api.utils.TOKENS_FILE", shared_datadir / "tokens.json"
)
@ -13,6 +17,7 @@ def tokens_file(mocker, shared_datadir):
@pytest.fixture
def app():
"""Flask application."""
app = create_app(
{
"ENABLE_SWAGGER": "1",
@ -24,10 +29,12 @@ def app():
@pytest.fixture
def client(app, tokens_file):
"""Flask unauthorized test client."""
return app.test_client()
class AuthorizedClient(testing.FlaskClient):
"""Flask authorized test client."""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.token = "TEST_TOKEN"
@ -40,6 +47,7 @@ class AuthorizedClient(testing.FlaskClient):
class WrongAuthClient(testing.FlaskClient):
"""Flask client with wrong token"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.token = "WRONG_TOKEN"
@ -53,16 +61,19 @@ class WrongAuthClient(testing.FlaskClient):
@pytest.fixture
def authorized_client(app, tokens_file):
"""Authorized test client fixture."""
app.test_client_class = AuthorizedClient
return app.test_client()
@pytest.fixture
def wrong_auth_client(app, tokens_file):
"""Wrong token test client fixture."""
app.test_client_class = WrongAuthClient
return app.test_client()
@pytest.fixture
def runner(app, tokens_file):
"""Flask test runner."""
return app.test_cli_runner()

View file

@ -2,8 +2,6 @@
# pylint: disable=unused-argument
# pylint: disable=missing-function-docstring
import datetime
import json
import pytest
from mnemonic import Mnemonic
from tests.common import generate_api_query, read_json, write_json

View file

@ -127,7 +127,7 @@ def test_graphql_wrong_auth(wrong_auth_client):
assert response.json.get("data") is None
API_GET_DOMAIN_INFO = """
domainInfo {
domainInfo() {
domain
hostname
provider
@ -155,6 +155,12 @@ def dns_record(type="A", name="test.tld", content=None, ttl=3600, priority=None)
"priority": priority,
}
def is_dns_record_in_array(records, dns_record) -> bool:
for record in records:
if record["type"] == dns_record["type"] and record["name"] == dns_record["name"] and record["content"] == dns_record["content"] and record["ttl"] == dns_record["ttl"] and record["priority"] == dns_record["priority"]:
return True
return False
def test_graphql_get_domain(authorized_client, domain_file, mock_get_ip4, mock_get_ip6, turned_on):
"""Test get domain"""
response = authorized_client.get(
@ -168,28 +174,29 @@ def test_graphql_get_domain(authorized_client, domain_file, mock_get_ip4, mock_g
assert response.json["data"]["system"]["domainInfo"]["domain"] == "test.tld"
assert response.json["data"]["system"]["domainInfo"]["hostname"] == "test-instance"
assert response.json["data"]["system"]["domainInfo"]["provider"] == "HETZNER"
assert response.json["data"]["system"]["domainInfo"]["requiredDnsRecords"] == [
dns_record(),
dns_record(type="AAAA"),
dns_record(name="api.test.tld"),
dns_record(name="api.test.tld", type="AAAA"),
dns_record(name="cloud.test.tld"),
dns_record(name="cloud.test.tld", type="AAAA"),
dns_record(name="git.test.tld"),
dns_record(name="git.test.tld", type="AAAA"),
dns_record(name="meet.test.tld"),
dns_record(name="meet.test.tld", type="AAAA"),
dns_record(name="password.test.tld"),
dns_record(name="password.test.tld", type="AAAA"),
dns_record(name="social.test.tld"),
dns_record(name="social.test.tld", type="AAAA"),
dns_record(name="vpn.test.tld"),
dns_record(name="vpn.test.tld", type="AAAA"),
dns_record(name="test.tld", type="MX", content="test.tld", priority=10),
dns_record(name="_dmarc.test.tld", type="TXT", content="v=DMARC1; p=none", ttl=18000),
dns_record(name="test.tld", type="TXT", content="v=spf1 a mx ip4:157.90.247.192 -all", ttl=18000),
dns_record(name="selector._domainkey.test.tld", type="TXT", content="I am a DKIM key", ttl=18000),
]
dns_records = response.json["data"]["system"]["domainInfo"]["requiredDnsRecords"]
assert is_dns_record_in_array(dns_records, dns_record())
assert is_dns_record_in_array(dns_records, dns_record(type="AAAA"))
assert is_dns_record_in_array(dns_records, dns_record(name="api.test.tld"))
assert is_dns_record_in_array(dns_records, dns_record(name="api.test.tld", type="AAAA"))
assert is_dns_record_in_array(dns_records, dns_record(name="cloud.test.tld"))
assert is_dns_record_in_array(dns_records, dns_record(name="cloud.test.tld", type="AAAA"))
assert is_dns_record_in_array(dns_records, dns_record(name="git.test.tld"))
assert is_dns_record_in_array(dns_records, dns_record(name="git.test.tld", type="AAAA"))
assert is_dns_record_in_array(dns_records, dns_record(name="meet.test.tld"))
assert is_dns_record_in_array(dns_records, dns_record(name="meet.test.tld", type="AAAA"))
assert is_dns_record_in_array(dns_records, dns_record(name="password.test.tld"))
assert is_dns_record_in_array(dns_records, dns_record(name="password.test.tld", type="AAAA"))
assert is_dns_record_in_array(dns_records, dns_record(name="social.test.tld"))
assert is_dns_record_in_array(dns_records, dns_record(name="social.test.tld", type="AAAA"))
assert is_dns_record_in_array(dns_records, dns_record(name="vpn.test.tld"))
assert is_dns_record_in_array(dns_records, dns_record(name="vpn.test.tld", type="AAAA"))
assert is_dns_record_in_array(dns_records, dns_record(name="test.tld", type="MX", content="test.tld", priority=10))
assert is_dns_record_in_array(dns_records, dns_record(name="_dmarc.test.tld", type="TXT", content="v=DMARC1; p=none", ttl=18000))
assert is_dns_record_in_array(dns_records, dns_record(name="test.tld", type="TXT", content="v=spf1 a mx ip4:157.90.247.192 -all", ttl=18000))
assert is_dns_record_in_array(dns_records, dns_record(name="selector._domainkey.test.tld", type="TXT", content="I am a DKIM key", ttl=18000))
def test_graphql
API_GET_TIMEZONE = """
settings {