forked from zalando-stups/piu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_cli.py
89 lines (65 loc) · 4 KB
/
test_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
from click.testing import CliRunner
from mock import MagicMock
import yaml
import zign.api
from piu.cli import cli
def test_missing_reason():
runner = CliRunner()
with runner.isolated_filesystem():
result = runner.invoke(cli, ['[email protected]'], catch_exceptions=False)
assert 'Missing argument "reason"' in result.output
def test_success(monkeypatch):
response = MagicMock(status_code=200, text='**MAGIC-SUCCESS**')
monkeypatch.setattr('zign.api.get_named_token', MagicMock(return_value={'access_token': '123'}))
monkeypatch.setattr('requests.post', MagicMock(return_value=response))
monkeypatch.setattr('keyring.set_password', MagicMock())
runner = CliRunner()
with runner.isolated_filesystem():
result = runner.invoke(cli, ['[email protected]', '--lifetime=15', '--even-url=https://localhost/', '--odd-host=odd.example.org', '--password=foobar', 'my reason'], catch_exceptions=False)
assert response.text in result.output
def test_bad_request(monkeypatch):
response = MagicMock(status_code=400, text='**MAGIC-BAD-REQUEST**')
monkeypatch.setattr('zign.api.get_named_token', MagicMock(return_value={'access_token': '123'}))
monkeypatch.setattr('requests.post', MagicMock(return_value=response))
monkeypatch.setattr('keyring.set_password', MagicMock())
runner = CliRunner()
with runner.isolated_filesystem():
result = runner.invoke(cli, ['req', '--lifetime=15', '--even-url=https://localhost/', '--password=foobar', 'myuser@odd-host', 'my reason'], catch_exceptions=False)
assert response.text in result.output
assert 'Server returned status 400:' in result.output
def test_auth_failure(monkeypatch):
response = MagicMock(status_code=403, text='**MAGIC-AUTH-FAILED**')
monkeypatch.setattr('zign.api.get_named_token', MagicMock(return_value={'access_token': '123'}))
monkeypatch.setattr('requests.post', MagicMock(return_value=response))
monkeypatch.setattr('keyring.set_password', MagicMock())
runner = CliRunner()
with runner.isolated_filesystem():
result = runner.invoke(cli, ['r', '--even-url=https://localhost/', '--password=invalid', 'myuser@odd-host', 'my reason'], catch_exceptions=False)
assert response.text in result.output
assert 'Server returned status 403:' in result.output
def test_dialog(monkeypatch):
response = MagicMock(status_code=200, text='**MAGIC-SUCCESS**')
monkeypatch.setattr('zign.api.get_named_token', MagicMock(return_value={'access_token': '123'}))
monkeypatch.setattr('requests.post', MagicMock(return_value=response))
monkeypatch.setattr('requests.get', MagicMock(return_value=response))
monkeypatch.setattr('socket.getaddrinfo', MagicMock())
monkeypatch.setattr('keyring.set_password', MagicMock())
monkeypatch.setattr('keyring.get_password', MagicMock(return_value=None))
runner = CliRunner()
with runner.isolated_filesystem():
result = runner.invoke(cli, ['--config-file=config.yaml', 'req', '[email protected]', 'my reason'], catch_exceptions=False, input='even\nodd\npassword\n\n')
assert result.exit_code == 0
assert response.text in result.output
def test_oauth_failure(monkeypatch):
response = MagicMock(status_code=200, text='**MAGIC-SUCCESS**')
monkeypatch.setattr('zign.api.get_named_token', MagicMock(side_effect=zign.api.ServerError('**MAGIC-FAIL**')))
monkeypatch.setattr('requests.post', MagicMock(return_value=response))
monkeypatch.setattr('requests.get', MagicMock(return_value=response))
monkeypatch.setattr('socket.getaddrinfo', MagicMock())
monkeypatch.setattr('keyring.set_password', MagicMock())
monkeypatch.setattr('keyring.get_password', MagicMock(return_value=None))
runner = CliRunner()
with runner.isolated_filesystem():
result = runner.invoke(cli, ['--config-file=config.yaml', 'req', '[email protected]', 'my reason'], catch_exceptions=False, input='even\nodd\npassword\n\n')
assert result.exit_code == 500
assert 'Server error: **MAGIC-FAIL**' in result.output