File: test_client.py

package info (click to toggle)
lexicon 3.21.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 72,688 kB
  • sloc: python: 20,075; sh: 94; makefile: 7
file content (177 lines) | stat: -rw-r--r-- 6,735 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# pylint: disable=missing-docstring
import os

import pytest

import lexicon.client
from lexicon.config import ConfigResolver


def test_client_basic_init(mock_provider):
    options = {
        "provider_name": "fakeprovider",
        "action": "list",
        "domain": "example.com",
        "type": "TXT",
    }
    client = lexicon.client.Client(ConfigResolver().with_dict(options))

    assert client.provider_name == options["provider_name"]
    assert client.config.resolve("lexicon:action") == options["action"]
    assert client.config.resolve("lexicon:domain") == options["domain"]
    assert client.config.resolve("lexicon:type") == options["type"]


def test_client_legacy_init(monkeypatch, mock_provider):
    monkeypatch.setenv("LEXICON_FAKEPROVIDER_AUTH_KEY", "MY_KEY")
    options = {
        "provider_name": "fakeprovider",
        "action": "list",
        "domain": "example.com",
        "type": "TXT",
    }
    with pytest.deprecated_call():
        client = lexicon.client.Client(options)

    assert client.provider_name == options["provider_name"]
    assert client.config.resolve("lexicon:action") == options["action"]
    assert client.config.resolve("lexicon:domain") == options["domain"]
    assert client.config.resolve("lexicon:type") == options["type"]
    assert client.config.resolve("lexicon:fakeprovider:auth_key") == "MY_KEY"


def test_client_init_when_domain_includes_subdomain_should_strip(mock_provider):
    options = {
        "provider_name": "fakeprovider",
        "action": "list",
        "domain": "www.example.com",
        "type": "TXT",
    }
    client = lexicon.client.Client(ConfigResolver().with_dict(options))

    assert client.provider_name == options["provider_name"]
    assert client.config.resolve("lexicon:action") == options["action"]
    assert client.config.resolve("lexicon:domain") == "example.com"
    assert client.config.resolve("lexicon:type") == options["type"]


def test_client_init_with_delegated_domain_name(mock_provider):
    options = {
        "provider_name": "fakeprovider",
        "action": "list",
        "domain": "www.sub.example.com",
        "delegated": "sub",
        "type": "TXT",
    }
    client = lexicon.client.Client(ConfigResolver().with_dict(options))

    assert client.provider_name == options["provider_name"]
    assert client.config.resolve("lexicon:action") == options["action"]
    assert client.config.resolve("lexicon:domain") == "sub.example.com"
    assert client.config.resolve("lexicon:type") == options["type"]


def test_client_init_with_delegated_domain_fqdn(mock_provider):
    options = {
        "provider_name": "fakeprovider",
        "action": "list",
        "domain": "www.sub.example.com",
        "delegated": "sub.example.com",
        "type": "TXT",
    }
    client = lexicon.client.Client(ConfigResolver().with_dict(options))

    assert client.provider_name == options["provider_name"]
    assert client.config.resolve("lexicon:action") == options["action"]
    assert client.config.resolve("lexicon:domain") == "sub.example.com"
    assert client.config.resolve("lexicon:type") == options["type"]


def test_client_init_with_same_delegated_domain_fqdn(mock_provider):
    options = {
        "provider_name": "fakeprovider",
        "action": "list",
        "domain": "www.example.com",
        "delegated": "example.com",
        "type": "TXT",
    }
    client = lexicon.client.Client(ConfigResolver().with_dict(options))

    assert client.provider_name == options["provider_name"]
    assert client.config.resolve("lexicon:action") == options["action"]
    assert client.config.resolve("lexicon:domain") == "example.com"
    assert client.config.resolve("lexicon:type") == options["type"]


def test_client_init_when_missing_provider_should_fail():
    options = {"action": "list", "domain": "example.com", "type": "TXT"}
    with pytest.raises(AttributeError):
        lexicon.client.Client(ConfigResolver().with_dict(options))


def test_client_init_when_missing_action_should_fail():
    options = {"provider_name": "fakeprovider", "domain": "example.com", "type": "TXT"}
    with pytest.raises(AttributeError):
        lexicon.client.Client(ConfigResolver().with_dict(options)).execute()


def test_client_init_when_missing_domain_should_fail():
    options = {"provider_name": "fakeprovider", "action": "list", "type": "TXT"}
    with pytest.raises(AttributeError):
        lexicon.client.Client(ConfigResolver().with_dict(options))


def test_client_init_when_missing_type_should_fail(mock_provider):
    options = {
        "provider_name": "fakeprovider",
        "action": "list",
        "domain": "example.com",
    }
    with pytest.raises(AttributeError):
        lexicon.client.Client(ConfigResolver().with_dict(options)).execute()


def test_client_parse_env_with_no_keys_should_do_nothing(monkeypatch, mock_provider):
    if os.environ.get("LEXICON_FAKEPROVIDER_TOKEN"):
        monkeypatch.delenv("LEXICON_FAKEPROVIDER_TOKEN")
    if os.environ.get("LEXICON_FAKEPROVIDER_USERNAME"):
        monkeypatch.delenv("LEXICON_FAKEPROVIDER_USERNAME")
    options = {
        "provider_name": "fakeprovider",
        "action": "list",
        "domain": "www.example.com",
        "type": "TXT",
    }
    client = lexicon.client.Client(ConfigResolver().with_dict(options).with_env())

    assert client.provider_name == options["provider_name"]
    assert client.config.resolve("lexicon:action") == options["action"]
    assert client.config.resolve("lexicon:domain") == "example.com"
    assert client.config.resolve("lexicon:type") == options["type"]
    assert client.config.resolve("lexicon:fakeprovider:auth_token") is None
    assert client.config.resolve("lexicon:fakeprovider:auth_username") is None


def test_client_parse_env_with_auth_keys(monkeypatch, mock_provider):
    monkeypatch.setenv("LEXICON_FAKEPROVIDER_TOKEN", "test-token")
    monkeypatch.setenv("LEXICON_FAKEPROVIDER_USERNAME", "test-username@example.com")
    options = {
        "provider_name": "fakeprovider",
        "action": "list",
        "domain": "www.example.com",
        "type": "TXT",
    }
    client = lexicon.client.Client(ConfigResolver().with_dict(options).with_env())

    assert client.provider_name == options["provider_name"]
    assert client.config.resolve("lexicon:action") == options["action"]
    assert client.config.resolve("lexicon:domain") == "example.com"
    assert client.config.resolve("lexicon:type") == options["type"]
    assert client.config.resolve("lexicon:fakeprovider:auth_token") == "test-token"
    assert (
        client.config.resolve("lexicon:fakeprovider:auth_username")
        == "test-username@example.com"
    )


# TODO: add tests for Provider loading?