summaryrefslogtreecommitdiffstats
path: root/test/modules/md/test_001_store.py
blob: 995d40dc7be941cac6e5401d314f2ed18b240f1e (plain)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# test mod_md acme terms-of-service handling

import re

import pytest

from .md_env import MDTestEnv


def md_name(md):
    return md['name']


@pytest.mark.skipif(condition=not MDTestEnv.has_a2md(), reason="no a2md available")
class TestStore:

    @pytest.fixture(autouse=True, scope='function')
    def _method_scope(self, env):
        env.purge_store()
 
    # verify expected binary version
    def test_md_001_001(self, env: MDTestEnv):
        r = env.run([env.a2md_bin, "-V"])
        m = re.match(r'version: (\d+\.\d+\.\d+)(-git)?$', r.stdout)
        assert m, f"expected version info in '{r.stdout}'"

    # verify that store is clean
    def test_md_001_002(self, env: MDTestEnv):
        r = env.run(["find", env.store_dir])
        assert re.match(env.store_dir, r.stdout)

    # test case: add a single dns managed domain
    def test_md_001_100(self, env: MDTestEnv):
        dns = "greenbytes.de"
        env.check_json_contains(
            env.a2md(["store", "add", dns]).json['output'][0],
            {
                "name": dns,
                "domains": [dns],
                "contacts": [],
                "ca": {
                    "urls": [env.acme_url],
                    "proto": "ACME"
                },
                "state": 0
            })

    # test case: add > 1 dns managed domain
    def test_md_001_101(self, env: MDTestEnv):
        dns = ["greenbytes2.de", "www.greenbytes2.de", "mail.greenbytes2.de"]
        env.check_json_contains(
            env.a2md(["store", "add"] + dns).json['output'][0],
            {
                "name": dns[0],
                "domains": dns,
                "contacts": [],
                "ca": {
                    "urls": [env.acme_url],
                    "proto": "ACME"
                },
                "state": 0
            })

    # test case: add second managed domain
    def test_md_001_102(self, env: MDTestEnv):
        dns1 = ["test000-102.com", "test000-102a.com", "test000-102b.com"]
        assert env.a2md(["store", "add"] + dns1).exit_code == 0
        #
        # add second managed domain
        dns2 = ["greenbytes2.de", "www.greenbytes2.de", "mail.greenbytes2.de"]
        jout = env.a2md(["store", "add"] + dns2).json
        # assert: output covers only changed md
        assert len(jout['output']) == 1
        env.check_json_contains(jout['output'][0], {
            "name": dns2[0],
            "domains": dns2,
            "contacts": [],
            "ca": {
                "urls": [env.acme_url],
                "proto": "ACME"
            },
            "state": 0
        })

    # test case: add existing domain 
    def test_md_001_103(self, env: MDTestEnv):
        dns = "greenbytes.de"
        assert env.a2md(["store", "add", dns]).exit_code == 0
        # add same domain again
        assert env.a2md(["store", "add", dns]).exit_code == 1

    # test case: add without CA URL
    def test_md_001_104(self, env: MDTestEnv):
        dns = "greenbytes.de"
        args = [env.a2md_bin, "-d", env.store_dir, "-j", "store", "add", dns]
        jout = env.run(args).json
        assert len(jout['output']) == 1
        env.check_json_contains(jout['output'][0], {
            "name": dns,
            "domains": [dns],
            "contacts": [],
            "ca": {
                "proto": "ACME"
            },
            "state": 0
        })

    # test case: list empty store
    def test_md_001_200(self, env: MDTestEnv):
        assert env.a2md(["store", "list"]).json == env.EMPTY_JOUT

    # test case: list two managed domains
    def test_md_001_201(self, env: MDTestEnv):
        domains = [ 
            ["test000-201.com", "test000-201a.com", "test000-201b.com"],
            ["greenbytes2.de", "www.greenbytes2.de", "mail.greenbytes2.de"]
        ]
        for dns in domains:
            assert env.a2md(["store", "add"] + dns).exit_code == 0
        #
        # list all store content
        jout = env.a2md(["store", "list"]).json
        assert len(jout['output']) == len(domains)
        domains.reverse()
        jout['output'] = sorted(jout['output'], key=md_name)
        for i in range(0, len(jout['output'])):
            env.check_json_contains(jout['output'][i], {
                "name": domains[i][0],
                "domains": domains[i],
                "contacts": [],
                "ca": {
                    "urls": [env.acme_url],
                    "proto": "ACME"
                },
                "state": 0
            })

    # test case: remove managed domain
    def test_md_001_300(self, env: MDTestEnv):
        dns = "test000-300.com"
        assert env.a2md(["store", "add", dns]).exit_code == 0
        assert env.a2md(["store", "remove", dns]).json == env.EMPTY_JOUT
        assert env.a2md(["store", "list"]).json == env.EMPTY_JOUT

    # test case: remove from list of managed domains 
    def test_md_001_301(self, env: MDTestEnv):
        dns1 = ["test000-301.com", "test000-301a.com", "test000-301b.com"]
        assert env.a2md(["store", "add"] + dns1).exit_code == 0
        #
        dns2 = ["greenbytes2.de", "www.greenbytes2.de", "mail.greenbytes2.de"]
        jout1 = env.a2md(["store", "add"] + dns2).json
        # remove managed domain
        assert env.a2md(["store", "remove", "test000-301.com"]).json == env.EMPTY_JOUT
        # list store content
        assert env.a2md(["store", "list"]).json == jout1

    # test case: remove nonexisting managed domain
    def test_md_001_302(self, env: MDTestEnv):
        dns1 = "test000-302.com"
        r = env.a2md(["store", "remove", dns1])
        assert r.exit_code == 1
        assert r.json == {
            'status': 2, 'description': 'No such file or directory', 'output': []
        }

    # test case: force remove nonexisting managed domain
    def test_md_001_303(self, env: MDTestEnv):
        dns1 = "test000-303.com"
        assert env.a2md(["store", "remove", "-f", dns1]).json == env.EMPTY_JOUT

    # test case: null change
    def test_md_001_400(self, env: MDTestEnv):
        dns = "test000-400.com"
        r1 = env.a2md(["store", "add", dns])
        assert env.a2md(["store", "update", dns]).json == r1.json

    # test case: add dns to managed domain
    def test_md_001_401(self, env: MDTestEnv):
        dns1 = "test000-401.com"
        env.a2md(["store", "add", dns1])
        dns2 = "test-101.com"
        args = ["store", "update", dns1, "domains", dns1, dns2]
        assert env.a2md(args).json['output'][0]['domains'] == [dns1, dns2]

    # test case: change CA URL
    def test_md_001_402(self, env: MDTestEnv):
        dns = "test000-402.com"
        args = ["store", "add", dns]
        assert env.a2md(args).json['output'][0]['ca']['urls'][0] == env.acme_url
        nurl = "https://foo.com/"
        args = [env.a2md_bin, "-a", nurl, "-d", env.store_dir, "-j", "store", "update", dns]
        assert env.run(args).json['output'][0]['ca']['urls'][0] == nurl

    # test case: update nonexisting managed domain
    def test_md_001_403(self, env: MDTestEnv):
        dns = "test000-403.com"
        assert env.a2md(["store", "update", dns]).exit_code == 1

    # test case: update domains, throw away md name
    def test_md_001_404(self, env: MDTestEnv):
        dns1 = "test000-404.com"
        dns2 = "greenbytes.com"
        args = ["store", "add", dns1]
        assert env.a2md(args).json['output'][0]['domains'] == [dns1]
        # override domains list
        args = ["store", "update", dns1, "domains", dns2]
        assert env.a2md(args).json['output'][0]['domains'] == [dns2]

    # test case: update domains with empty dns list
    def test_md_001_405(self, env: MDTestEnv):
        dns1 = "test000-405.com"
        assert env.a2md(["store", "add", dns1]).exit_code == 0
        assert env.a2md(["store", "update", dns1, "domains"]).exit_code == 1