/
cli_test.py
85 lines (70 loc) · 2.8 KB
/
cli_test.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
import json
import pytest
from Crypto.PublicKey import RSA
from click.testing import CliRunner
from transcriptic.cli import cli
@pytest.fixture(scope="session")
def temp_tx_dotfile(tmpdir_factory):
path = tmpdir_factory.mktemp("config").join(".transcriptic")
config = {
"email": "somebody@transcriptic.com",
"token": "foobarinvalid",
"organization_id": "transcriptic",
"api_root": "http://foo:5555",
"analytics": True,
"user_id": "ufoo2",
"feature_groups": ["can_submit_autoprotocol", "can_upload_packages"],
}
with open(str(path), "w") as f:
json.dump(config, f)
return path
@pytest.fixture()
def temp_ssh_key(tmpdir_factory):
dir_path = tmpdir_factory.mktemp("ssh")
private_key_path = str(dir_path.join("private.pem"))
public_key_path = str(dir_path.join("public.pem"))
private_key = RSA.generate(2048)
with open(private_key_path, "wb") as private_key_file:
private_key_file.write(private_key.exportKey("PEM"))
pubkey = private_key.publickey()
with open(public_key_path, "wb") as public_key_file:
public_key_file.write(pubkey.exportKey("OpenSSH"))
yield private_key_path, public_key_path
@pytest.fixture
def cli_test_runner(temp_tx_dotfile):
runner = CliRunner(env={"TRANSCRIPTIC_CONFIG": str(temp_tx_dotfile)})
yield runner
def test_kebab_case(cli_test_runner):
result = cli_test_runner.invoke(cli, ["--help"])
command_str = result.output.split("Commands:\n")[1]
commands = [x.strip() for x in command_str.split("\n") if x != ""]
is_kebab = lambda x: x.islower() and ("_" not in x)
assert all([is_kebab(cmd) for cmd in commands])
def test_login_nonexistent_key_path(cli_test_runner):
result = cli_test_runner.invoke(
cli,
["login", "--rsa-key", "/temp/path/invalid_key.pem"],
input="\n".join(["email@foo.com", "barpw"]),
)
assert "Invalid value for '--rsa-key'" in result.output
def test_login_random_file_for_key(cli_test_runner, tmpdir_factory):
path = tmpdir_factory.mktemp("foo").join("random-file")
with open(str(path), "w") as fh:
fh.write("this is not an rsa key")
result = cli_test_runner.invoke(
cli,
["login", "--rsa-key", str(path)],
input="\n".join(["email@foo.com", "barpw"]),
)
assert (
"Error loading RSA key: Could not parse the specified RSA Key, "
"ensure it is a PRIVATE key in PEM format" in result.output
)
def test_login_public_key(cli_test_runner, temp_ssh_key):
(private_key_path, public_key_path) = temp_ssh_key
result = cli_test_runner.invoke(
cli,
["login", "--rsa-key", public_key_path],
input="\n".join(["email@foo.com", "barpw"]),
)
assert "Error connecting to host: This is not a private key" in result.output