forked from canonical/snapcraft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_commands_register_key.py
327 lines (283 loc) · 13.3 KB
/
test_commands_register_key.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-
#
# Copyright (C) 2016 Canonical Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import json
import logging
from textwrap import dedent
from unittest import mock
import fixtures
from simplejson.scanner import JSONDecodeError
from snapcraft.main import main
from snapcraft import (
storeapi,
tests,
)
_sample_keys = [
{
'name': 'default',
'sha3-384': (
'vdEeQvRxmZ26npJCFaGnl-VfGz0lU2jZ'
'ZkWp_s7E-RxVCNtH2_mtjcxq2NkDKkIp'),
},
{
'name': 'another',
'sha3-384': (
'JsfToV5hO2eN9l89pYYCKXUioTERrZII'
'HUgQQd47jW8YNNBskupiIjWYd3KXLY_D'),
},
]
def get_sample_key(name):
for key in _sample_keys:
if key['name'] == name:
return key
raise KeyError(name)
def mock_snap_output(command, *args, **kwargs):
if command == ['snap', 'keys', '--json']:
return json.dumps(_sample_keys)
elif command[:2] == ['snap', 'export-key']:
if not command[2].startswith('--account='):
raise AssertionError('Unhandled command: {}'.format(command))
account_id = command[2][len('--account='):]
name = command[3]
# This isn't a full account-key-request assertion, but it's enough
# for testing.
return dedent('''\
type: account-key-request
account-id: {account_id}
name: {name}
public-key-sha3-384: {sha3_384}
''').format(
account_id=account_id, name=name,
sha3_384=get_sample_key(name)['sha3-384'])
else:
raise AssertionError('Unhandled command: {}'.format(command))
class RegisterKeyTestCase(tests.TestCase):
def setUp(self):
super().setUp()
self.fake_logger = fixtures.FakeLogger(level=logging.INFO)
self.useFixture(self.fake_logger)
self.fake_terminal = tests.fixture_setup.FakeTerminal()
self.useFixture(self.fake_terminal)
@mock.patch('subprocess.check_output')
@mock.patch('snapcraft.internal.repo.is_package_installed')
def test_register_key_snapd_not_installed(self, mock_installed,
mock_check_output):
mock_installed.return_value = False
with self.assertRaises(SystemExit) as raised:
main(['register-key'])
mock_installed.assert_called_with('snapd')
self.assertEqual(0, mock_check_output.call_count)
self.assertEqual(1, raised.exception.code)
self.assertIn(
'The snapd package is not installed.', self.fake_logger.output)
@mock.patch.object(storeapi.SCAClient, 'register_key')
@mock.patch.object(storeapi.SCAClient, 'get_account_information')
@mock.patch.object(storeapi.StoreClient, 'login')
@mock.patch('subprocess.check_output')
@mock.patch('getpass.getpass')
@mock.patch('builtins.input')
@mock.patch('snapcraft.internal.repo.is_package_installed')
def test_register_key_successfully(self, mock_installed, mock_input,
mock_getpass, mock_check_output,
mock_login,
mock_get_account_information,
mock_register_key):
mock_installed.return_value = True
mock_input.side_effect = ['sample.person@canonical.com', '123456']
mock_getpass.return_value = 'secret'
mock_check_output.side_effect = mock_snap_output
mock_login.side_effect = [
storeapi.errors.StoreTwoFactorAuthenticationRequired(), None]
mock_get_account_information.return_value = {'account_id': 'abcd'}
main(['register-key', 'default'])
self.assertEqual(
'Login successful.\n'
'Registering key ...\n'
'Done. The key "default" ({}) may be used to sign your '
'assertions.\n'.format(get_sample_key('default')['sha3-384']),
self.fake_logger.output)
mock_login.assert_called_with(
'sample.person@canonical.com', 'secret',
one_time_password='123456', acls=['modify_account_key'],
save=False)
self.assertEqual(1, mock_register_key.call_count)
expected_assertion = dedent('''\
type: account-key-request
account-id: abcd
name: default
public-key-sha3-384: {}
''').format(get_sample_key('default')['sha3-384'])
mock_register_key.assert_called_once_with(expected_assertion)
@mock.patch('subprocess.check_output')
@mock.patch('builtins.input')
@mock.patch('snapcraft.internal.repo.is_package_installed')
def test_register_key_no_keys(self, mock_installed, mock_input,
mock_check_output):
mock_installed.return_value = True
mock_check_output.return_value = json.dumps([])
with self.assertRaises(SystemExit) as raised:
main(['register-key'])
self.assertEqual(0, mock_input.call_count)
self.assertEqual(1, raised.exception.code)
self.assertIn('You have no usable keys.\n', self.fake_logger.output)
@mock.patch('subprocess.check_output')
@mock.patch('builtins.input')
@mock.patch('snapcraft.internal.repo.is_package_installed')
def test_register_key_no_keys_null(self, mock_installed, mock_input,
mock_check_output):
# Some versions of snapd serialise an empty list as "null" rather
# than "[]".
mock_installed.return_value = True
mock_check_output.return_value = json.dumps(None)
with self.assertRaises(SystemExit) as raised:
main(['register-key'])
self.assertEqual(0, mock_input.call_count)
self.assertEqual(1, raised.exception.code)
self.assertIn('You have no usable keys.\n', self.fake_logger.output)
@mock.patch('subprocess.check_output')
@mock.patch('builtins.input')
@mock.patch('snapcraft.internal.repo.is_package_installed')
def test_register_key_no_keys_with_name(self, mock_installed, mock_input,
mock_check_output):
mock_installed.return_value = True
mock_check_output.side_effect = mock_snap_output
with self.assertRaises(SystemExit) as raised:
main(['register-key', 'nonexistent'])
self.assertEqual(0, mock_input.call_count)
self.assertEqual(1, raised.exception.code)
self.assertIn(
'You have no usable key named "nonexistent".\n',
self.fake_logger.output)
@mock.patch.object(storeapi.SCAClient, 'register_key')
@mock.patch.object(storeapi.SCAClient, 'get_account_information')
@mock.patch.object(storeapi.StoreClient, 'login')
@mock.patch('subprocess.check_output')
@mock.patch('getpass.getpass')
@mock.patch('builtins.input')
@mock.patch('snapcraft.internal.repo.is_package_installed')
def test_register_key_login_failed(self, mock_installed, mock_input,
mock_getpass, mock_check_output,
mock_login,
mock_get_account_information,
mock_register_key):
mock_installed.return_value = True
mock_check_output.side_effect = mock_snap_output
mock_login.side_effect = storeapi.errors.StoreAuthenticationError(
'test')
with self.assertRaises(SystemExit) as raised:
main(['register-key', 'default'])
self.assertEqual(0, mock_get_account_information.call_count)
self.assertEqual(0, mock_register_key.call_count)
self.assertEqual(1, raised.exception.code)
self.assertIn(
'Cannot continue without logging in successfully.\n',
self.fake_logger.output)
@mock.patch.object(storeapi.SCAClient, 'register_key')
@mock.patch.object(storeapi.SCAClient, 'get_account_information')
@mock.patch.object(storeapi.StoreClient, 'login')
@mock.patch('subprocess.check_output')
@mock.patch('getpass.getpass')
@mock.patch('builtins.input')
@mock.patch('snapcraft.internal.repo.is_package_installed')
def test_register_key_account_info_failed(self, mock_installed, mock_input,
mock_getpass, mock_check_output,
mock_login,
mock_get_account_information,
mock_register_key):
mock_installed.return_value = True
mock_check_output.side_effect = mock_snap_output
response = mock.Mock()
response.json.side_effect = JSONDecodeError('mock-fail', 'doc', 1)
response.status_code = 500
response.reason = 'Internal Server Error'
mock_get_account_information.side_effect = (
storeapi.errors.StoreAccountInformationError(response))
with self.assertRaises(SystemExit) as raised:
main(['register-key', 'default'])
self.assertEqual(1, raised.exception.code)
self.assertIn(
'Error fetching account information from store: '
'500 Internal Server Error\n',
self.fake_logger.output)
@mock.patch.object(storeapi.SCAClient, 'register_key')
@mock.patch.object(storeapi.SCAClient, 'get_account_information')
@mock.patch.object(storeapi.StoreClient, 'login')
@mock.patch('subprocess.check_output')
@mock.patch('getpass.getpass')
@mock.patch('builtins.input')
@mock.patch('snapcraft.internal.repo.is_package_installed')
def test_register_key_failed(self, mock_installed, mock_input,
mock_getpass, mock_check_output, mock_login,
mock_get_account_information,
mock_register_key):
mock_installed.return_value = True
mock_check_output.side_effect = mock_snap_output
mock_get_account_information.return_value = {'account_id': 'abcd'}
response = mock.Mock()
response.json.side_effect = JSONDecodeError('mock-fail', 'doc', 1)
response.status_code = 500
response.reason = 'Internal Server Error'
mock_register_key.side_effect = (
storeapi.errors.StoreKeyRegistrationError(response))
with self.assertRaises(SystemExit) as raised:
main(['register-key', 'default'])
self.assertEqual(1, raised.exception.code)
self.assertIn(
'Key registration failed: 500 Internal Server Error\n',
self.fake_logger.output)
@mock.patch.object(storeapi.SCAClient, 'register_key')
@mock.patch.object(storeapi.SCAClient, 'get_account_information')
@mock.patch.object(storeapi.StoreClient, 'login')
@mock.patch('subprocess.check_output')
@mock.patch('getpass.getpass')
@mock.patch('builtins.input')
@mock.patch('snapcraft.internal.repo.is_package_installed')
def test_register_key_select_key(self, mock_installed, mock_input,
mock_getpass, mock_check_output,
mock_login, mock_get_account_information,
mock_register_key):
mock_installed.return_value = True
mock_input.side_effect = ['x', '2', 'sample.person@canonical.com']
mock_getpass.return_value = 'secret'
mock_check_output.side_effect = mock_snap_output
mock_get_account_information.return_value = {'account_id': 'abcd'}
main(['register-key'])
expected_output = dedent('''\
Select a key:
Number Name SHA3-384 fingerprint
1 default {default_sha3_384}
2 another {another_sha3_384}
''').format(
default_sha3_384=get_sample_key('default')['sha3-384'],
another_sha3_384=get_sample_key('another')['sha3-384'])
self.assertIn(expected_output, self.fake_terminal.getvalue())
mock_input.assert_has_calls(
[mock.call('Key number: '), mock.call('Key number: ')])
self.assertEqual(
'We strongly recommend enabling multi-factor authentication: '
'https://help.ubuntu.com/community/SSO/FAQs/2FA\n'
'Login successful.\n'
'Registering key ...\n'
'Done. The key "another" ({}) may be used to sign your '
'assertions.\n'.format(get_sample_key('another')['sha3-384']),
self.fake_logger.output)
self.assertEqual(1, mock_register_key.call_count)
expected_assertion = dedent('''\
type: account-key-request
account-id: abcd
name: another
public-key-sha3-384: {}
''').format(get_sample_key('another')['sha3-384'])
mock_register_key.assert_called_once_with(expected_assertion)