|
2 | 2 |
|
3 | 3 | from unittest.mock import Mock, ANY, patch |
4 | 4 | from serial import SerialException |
5 | | -from nrfcredstore.cli import main |
| 5 | +from nrfcredstore.cli import main, parse_args |
6 | 6 |
|
7 | 7 | from nrfcredstore.credstore import CredType |
8 | 8 | from nrfcredstore.exceptions import NoATClientException, ATCommandError |
|
11 | 11 | class TestCli(): |
12 | 12 |
|
13 | 13 | @pytest.fixture |
14 | | - def at_client(self): |
15 | | - at_client = Mock() |
16 | | - return at_client |
| 14 | + def command_interface(self): |
| 15 | + command_interface = Mock() |
| 16 | + return command_interface |
17 | 17 |
|
18 | 18 | @pytest.fixture |
19 | | - def credstore(self, at_client): |
| 19 | + def credstore(self, command_interface): |
20 | 20 | credstore = Mock() |
21 | | - credstore.at_client = at_client |
| 21 | + credstore.command_interface = command_interface |
22 | 22 | return credstore |
23 | 23 |
|
24 | | - @pytest.fixture |
25 | | - def offline(self, credstore): |
26 | | - credstore.funk_mode.return_value = True |
27 | | - |
28 | 24 | @pytest.fixture |
29 | 25 | def empty_cred_list(self, credstore): |
30 | 26 | credstore.list.return_value = [] |
31 | 27 |
|
32 | | - def test_device_passed_to_connect(self, credstore, at_client, offline, empty_cred_list): |
33 | | - main(['/dev/tty.usb', 'list'], credstore) |
34 | | - at_client.connect.assert_called_with('/dev/tty.usb', ANY, ANY) |
35 | | - |
36 | | - def test_baudrate_passed_to_connect(self, credstore, at_client, offline, empty_cred_list): |
37 | | - main(['fakedev', '--baudrate', '9600', 'list'], credstore) |
38 | | - at_client.connect.assert_called_with(ANY, 9600, ANY) |
39 | | - |
40 | | - def test_timeout_passed_to_connect(self, credstore, at_client, offline, empty_cred_list): |
41 | | - main(['fakedev', '--timeout', '3', 'list'], credstore) |
42 | | - at_client.connect.assert_called_with(ANY, ANY, 3) |
43 | | - |
44 | | - def test_at_client_verify(self, credstore, at_client, offline, empty_cred_list): |
45 | | - main(['fakedev', 'list'], credstore) |
46 | | - at_client.verify.assert_called() |
47 | | - |
48 | | - @patch('builtins.print') |
49 | | - def test_at_client_verify_fail(self, mock_print, credstore, at_client): |
50 | | - at_client.verify.side_effect = NoATClientException() |
| 28 | + # non-responsive device |
| 29 | + def test_non_responsive_device(self, credstore, command_interface): |
| 30 | + command_interface.detect_shell_mode.side_effect = TimeoutError() |
51 | 31 | with pytest.raises(SystemExit) as e: |
52 | | - main(['fakedev', 'list'], credstore) |
53 | | - assert 'does not respond to AT commands' in mock_print.call_args[0][0] |
| 32 | + main(parse_args(['fakedev', 'list']), credstore) |
| 33 | + assert e.type == SystemExit |
| 34 | + assert e.value.code == 12 |
54 | 35 |
|
55 | | - def test_at_client_enable_error_codes(self, credstore, at_client, offline, empty_cred_list): |
56 | | - main(['fakedev', 'list'], credstore) |
57 | | - at_client.enable_error_codes.assert_called() |
58 | 36 |
|
59 | 37 | def test_list_default(self, credstore, offline, empty_cred_list): |
60 | | - main(['fakedev', 'list'], credstore) |
| 38 | + main(parse_args(['fakedev', 'list']), credstore) |
61 | 39 | credstore.list.assert_called_with(None, CredType.ANY) |
62 | 40 |
|
63 | 41 | def test_list_with_tag(self, credstore, offline, empty_cred_list): |
64 | | - main(['fakedev', 'list', '--tag', '123'], credstore) |
| 42 | + main(parse_args(['fakedev', 'list', '--tag', '123']), credstore) |
65 | 43 | credstore.list.assert_called_with(123, ANY) |
66 | 44 |
|
67 | 45 | def test_list_with_type(self, credstore, offline, empty_cred_list): |
68 | | - main(['fakedev', 'list', '--tag', '123', '--type', 'CLIENT_KEY'], credstore) |
| 46 | + main(parse_args(['fakedev', 'list', '--tag', '123', '--type', 'CLIENT_KEY']), credstore) |
69 | 47 | credstore.list.assert_called_with(ANY, CredType.CLIENT_KEY) |
70 | 48 |
|
71 | 49 | def test_write_tag_and_type(self, credstore, offline): |
72 | 50 | credstore.write.return_value = True |
73 | | - main(['fakedev', 'write', '123', 'ROOT_CA_CERT', 'tests/fixtures/root-ca.pem'], credstore) |
| 51 | + main(parse_args(['fakedev', 'write', '123', 'ROOT_CA_CERT', 'tests/fixtures/root-ca.pem']), credstore) |
74 | 52 | credstore.write.assert_called_with(123, CredType.ROOT_CA_CERT, ANY) |
75 | 53 |
|
76 | 54 | @patch('builtins.open') |
77 | 55 | def test_write_file(self, mock_file, credstore, offline): |
78 | 56 | credstore.write.return_value = True |
79 | | - main(['fakedev', 'write', '123', 'ROOT_CA_CERT', 'foo.pem'], credstore) |
| 57 | + main(parse_args(['fakedev', 'write', '123', 'ROOT_CA_CERT', 'foo.pem']), credstore) |
80 | 58 | mock_file.assert_called_with('foo.pem', 'r', ANY, ANY, ANY) |
81 | 59 |
|
82 | 60 | @patch('builtins.open') |
83 | 61 | def test_write_psk_file(self, mock_file, credstore, offline): |
84 | 62 | credstore.write.return_value = True |
85 | | - main(['fakedev', 'write', '123', 'PSK', 'foo.psk'], credstore) |
| 63 | + main(parse_args(['fakedev', 'write', '123', 'PSK', 'foo.psk']), credstore) |
86 | 64 | mock_file.assert_called_with('foo.psk', 'r', ANY, ANY, ANY) |
87 | 65 |
|
88 | 66 | def test_delete(self, credstore, offline): |
89 | 67 | credstore.delete.return_value = True |
90 | | - main(['fakedev', 'delete', '123', 'CLIENT_KEY'], credstore) |
| 68 | + main(parse_args(['fakedev', 'delete', '123', 'CLIENT_KEY']), credstore) |
91 | 69 | credstore.delete.assert_called_with(123, CredType.CLIENT_KEY) |
92 | 70 |
|
93 | 71 | def test_delete_any_should_fail(self, credstore, offline): |
94 | 72 | with pytest.raises(SystemExit): |
95 | | - main(['fakedev', 'delete', '123', 'ANY'], credstore) |
| 73 | + main(parse_args(['fakedev', 'delete', '123', 'ANY']), credstore) |
96 | 74 |
|
97 | 75 | @patch('builtins.open') |
98 | 76 | def test_generate_tag(self, mock_file, credstore, offline): |
99 | 77 | credstore.keygen.return_value = True |
100 | | - main(['fakedev', 'generate', '123', 'foo.der'], credstore) |
| 78 | + main(parse_args(['fakedev', 'generate', '123', 'foo.der']), credstore) |
101 | 79 | credstore.keygen.assert_called_with(123, ANY, ANY) |
102 | 80 |
|
103 | 81 | @patch('builtins.open') |
104 | 82 | def test_generate_file(self, mock_file, credstore, offline): |
105 | 83 | credstore.keygen.return_value = True |
106 | | - main(['fakedev', 'generate', '123', 'foo.der'], credstore) |
| 84 | + main(parse_args(['fakedev', 'generate', '123', 'foo.der']), credstore) |
107 | 85 | mock_file.assert_called_with('foo.der', 'wb', ANY, ANY, ANY) |
108 | 86 |
|
109 | 87 | @patch('builtins.open') |
110 | 88 | def test_generate_with_attributes(self, credstore, offline): |
111 | 89 | credstore.keygen.return_value = True |
112 | | - main(['fakedev', 'generate', '123', 'foo.der', '--attributes', 'CN=foo'], credstore) |
| 90 | + main(parse_args(['fakedev', 'generate', '123', 'foo.der', '--attributes', 'CN=foo']), credstore) |
113 | 91 | credstore.keygen.assert_called_with(123, ANY, 'CN=foo') |
114 | 92 |
|
115 | 93 | def test_no_at_client_exit_code(self, credstore, at_client): |
116 | 94 | at_client.verify.side_effect = NoATClientException() |
117 | 95 | with pytest.raises(SystemExit) as e: |
118 | | - main(['fakedev', 'list'], credstore) |
| 96 | + main(parse_args(['fakedev', 'list']), credstore) |
119 | 97 | assert e.type == SystemExit |
120 | 98 | assert e.value.code == 10 |
121 | 99 |
|
122 | 100 | def test_at_command_error_exit_code(self, credstore, at_client): |
123 | 101 | at_client.verify.side_effect = ATCommandError() |
124 | 102 | with pytest.raises(SystemExit) as e: |
125 | | - main(['fakedev', 'list'], credstore) |
| 103 | + main(parse_args(['fakedev', 'list']), credstore) |
126 | 104 | assert e.type == SystemExit |
127 | 105 | assert e.value.code == 11 |
128 | 106 |
|
129 | 107 | def test_timeout_error_exit_code(self, credstore, at_client): |
130 | 108 | at_client.verify.side_effect = TimeoutError() |
131 | 109 | with pytest.raises(SystemExit) as e: |
132 | | - main(['fakedev', 'list'], credstore) |
| 110 | + main(parse_args(['fakedev', 'list']), credstore) |
133 | 111 | assert e.type == SystemExit |
134 | 112 | assert e.value.code == 12 |
135 | 113 |
|
136 | 114 | def test_serial_exception_exit_code(self, credstore, at_client): |
137 | 115 | at_client.connect.side_effect = SerialException() |
138 | 116 | with pytest.raises(SystemExit) as e: |
139 | | - main(['fakedev', 'list'], credstore) |
| 117 | + main(parse_args(['fakedev', 'list']), credstore) |
140 | 118 | assert e.type == SystemExit |
141 | 119 | assert e.value.code == 13 |
142 | 120 |
|
143 | 121 | def test_unhandled_exception_exit_code(self, credstore, at_client): |
144 | 122 | at_client.verify.side_effect = Exception() |
145 | 123 | with pytest.raises(SystemExit) as e: |
146 | | - main(['fakedev', 'list'], credstore) |
| 124 | + main(parse_args(['fakedev', 'list']), credstore) |
147 | 125 | assert e.type == SystemExit |
148 | 126 | assert e.value.code == 1 |
0 commit comments