-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathtest_testing_cli.py
257 lines (213 loc) · 7.54 KB
/
test_testing_cli.py
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# Copyright (c) The Diem Core Contributors
# SPDX-License-Identifier: Apache-2.0
from click.testing import CliRunner, Result
from diem.testing import cli
from diem.testing.suites import envs
from diem.testing.miniwallet import ServerConfig, RestClient
from diem.testing import LocalAccount
from diem import identifier, testnet, utils
from typing import List
import json, threading, pytest, pkg_resources, time, asyncio
@pytest.fixture(autouse=True)
def disable_self_check(monkeypatch) -> None:
monkeypatch.delenv(envs.SELF_CHECK)
@pytest.fixture()
def runner() -> CliRunner:
return CliRunner()
def test_version(runner: CliRunner) -> None:
version = pkg_resources.require("diem")[0].version
for version_opt in ["-v", "--version"]:
result = runner.invoke(cli.main, [version_opt])
assert result.exit_code == 0
assert ("version %s" % version) in result.output
def test_help_shortcut(runner: CliRunner) -> None:
for fn in [cli.main, cli.start_server, cli.test, cli.gen_diem_account_config]:
result = runner.invoke(fn, ["-h"])
assert result.exit_code == 0
assert ("-h, --help") in result.output
def test_gen_diem_account_config(runner: CliRunner) -> None:
result = runner.invoke(cli.gen_diem_account_config, [])
assert result.exit_code == 0
account = LocalAccount.from_dict(json.loads(result.output))
assert account.private_key
assert account.compliance_key
default = LocalAccount()
default.private_key = account.private_key
default.compliance_key = account.compliance_key
assert default == account
def test_start_server_and_run_test_with_matching_keywords(runner: CliRunner) -> None:
conf = start_target_server(runner)
result = start_test(
runner,
conf,
[
"-k",
"test_invalid_jws_message_signature or test_receive_payment_with_general_metadata_and_valid_from_and_to_subaddresses",
],
)
assert result.exit_code == 0, result.output
def test_run_test_with_pytest_args(runner: CliRunner) -> None:
conf = start_target_server(runner)
result = start_test(
runner,
conf,
[
"--pytest-args",
"-k 'test_invalid_jws_message_signature or test_receive_payment_with_general_metadata_and_valid_from_and_to_subaddresses'",
],
)
assert result.exit_code == 0, result.output
def test_run_disable_offchainv2_tests_by_default(runner: CliRunner) -> None:
conf = start_target_server(runner)
result = start_test(
runner,
conf,
[
"--pytest-args",
"-k 'test_invalid_x_request_id'",
],
)
assert result.exit_code == pytest.ExitCode.NO_TESTS_COLLECTED, result.output
def test_include_offchainv2_tests(runner: CliRunner) -> None:
conf = start_target_server(runner)
result = start_test(
runner,
conf,
[
"--include-offchain-v2",
"--pytest-args",
"-k 'test_invalid_x_request_id'",
],
)
assert result.exit_code == 0, result.output
def test_load_diem_account_config_file(runner: CliRunner) -> None:
app_config_file = "app.json"
stub_config_file = "stub.json"
with runner.isolated_filesystem():
LocalAccount(hrp=identifier.DM).write_to_file(app_config_file)
LocalAccount(hrp=identifier.DM).write_to_file(stub_config_file)
conf = start_target_server(runner, ["-i", app_config_file])
result = start_test(
runner,
conf,
[
"-i",
stub_config_file,
"-k",
"test_receive_payment_with_general_metadata_and_valid_from_and_to_subaddresses",
],
)
assert result.exit_code == 0, result.output
# use the same configuration file for multiple times testing
result = start_test(
runner,
conf,
[
"-i",
stub_config_file,
"-k",
"test_send_payment_with_valid_inputs_under_the_travel_rule_threshold",
],
)
assert result.exit_code == 0, result.output
def test_hrp_option(runner: CliRunner, event_loop: asyncio.events.AbstractEventLoop) -> None:
conf = start_target_server(runner, ["--hrp", "xdm"])
c = RestClient(server_url=conf.base_url, name="test-hrp-option")
account = event_loop.run_until_complete(c.create_account())
account_identifier = event_loop.run_until_complete(account.generate_account_identifier())
assert account_identifier[:3] == "xdm"
result = start_test(
runner,
conf,
[
"--stub-hrp",
"xdm",
"-k",
"test_generate_account_identifier",
],
)
assert result.exit_code == 0, result.output
def test_hrp_option_overwrites_hrp_from_diem_account_config_file(
runner: CliRunner, event_loop: asyncio.events.AbstractEventLoop
) -> None:
app_config_file = "app.json"
stub_config_file = "stub.json"
with runner.isolated_filesystem():
LocalAccount(hrp=identifier.DM).write_to_file(app_config_file)
LocalAccount(hrp=identifier.DM).write_to_file(stub_config_file)
conf = start_target_server(runner, ["-i", app_config_file, "--hrp", "xdm"])
c = RestClient(server_url=conf.base_url, name="x")
account = event_loop.run_until_complete(c.create_account())
account_identifier = event_loop.run_until_complete(account.generate_account_identifier())
assert account_identifier[:3] == "xdm"
result = start_test(
runner,
conf,
[
"-i",
stub_config_file,
"--stub-hrp",
"xdm",
"-k",
"test_generate_account_identifier",
],
)
assert result.exit_code == 0, result.output
def test_wait_timeout_for_target_ready(runner: CliRunner) -> None:
start = time.time()
result = start_test(
runner,
ServerConfig(),
[
"--wait-for-target-timeout",
1,
"-k",
"test_create_a_blank_account",
],
)
assert result.exit_code == 1, result.output
duration = time.time() - start
assert duration < 2
def start_test(runner: CliRunner, conf: ServerConfig, options: List[str] = []) -> Result:
stub_conf = ServerConfig()
return runner.invoke(
cli.test,
[
"--target",
conf.base_url,
"--jsonrpc",
testnet.JSON_RPC_URL,
"--faucet",
testnet.FAUCET_URL,
"--stub-bind-host",
"0.0.0.0",
"--stub-bind-port",
stub_conf.port,
"--stub-diem-account-base-url",
stub_conf.base_url,
]
+ options,
)
def start_target_server(runner: CliRunner, options: List[str] = []) -> ServerConfig:
conf = ServerConfig(host="0.0.0.0")
conf.base_url = "http://127.0.0.1:%s" % conf.port
def start_server():
runner.invoke(
cli.start_server,
[
"--jsonrpc",
testnet.JSON_RPC_URL,
"--faucet",
testnet.FAUCET_URL,
"--host",
conf.host,
"--port",
conf.port,
"--diem-account-base-url",
conf.base_url,
]
+ options,
)
threading.Thread(target=start_server, daemon=True).start()
utils.wait_for_port(conf.port)
return conf