Debian Codegraph @

ansible_collections/community/hrobot/tests/unit/plugins/modules/test_ssh_key_info.py

raw
1 # Copyright (c) 2021 Felix Fontein <[email protected]>
2 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
3 # SPDX-License-Identifier: GPL-3.0-or-later
4
5 from __future__ import (absolute_import, division, print_function)
6 __metaclass__ = type
7
8
9 from ansible_collections.community.internal_test_tools.tests.unit.utils.fetch_url_module_framework import (
10 FetchUrlCall,
11 BaseTestModule,
12 )
13
14 from ansible_collections.community.hrobot.plugins.module_utils.robot import BASE_URL
15 from ansible_collections.community.hrobot.plugins.modules import ssh_key_info
16
17
18 class TestHetznerSSHKeyInfo(BaseTestModule):
19 MOCK_ANSIBLE_MODULEUTILS_BASIC_ANSIBLEMODULE = 'ansible_collections.community.hrobot.plugins.modules.ssh_key_info.AnsibleModule'
20 MOCK_ANSIBLE_MODULEUTILS_URLS_FETCH_URL = 'ansible_collections.community.hrobot.plugins.module_utils.robot.fetch_url'
21
22 def test_no_keys(self, mocker):
23 result = self.run_module_success(mocker, ssh_key_info, {
24 'hetzner_user': 'test',
25 'hetzner_password': 'hunter2',
26 }, [
27 FetchUrlCall('GET', 200)
28 .result_json([])
29 .expect_basic_auth('test', 'hunter2')
30 .expect_force_basic_auth(True)
31 .expect_url('{0}/key'.format(BASE_URL)),
32 ])
33 assert result['changed'] is False
34 assert result['ssh_keys'] == []
35
36 def test_no_keys_404(self, mocker):
37 result = self.run_module_success(mocker, ssh_key_info, {
38 'hetzner_user': '',
39 'hetzner_password': '',
40 }, [
41 FetchUrlCall('GET', 404)
42 .result_json({
43 'error': {
44 'status': 404,
45 'code': 'NOT_FOUND',
46 'message': 'No keys found',
47 },
48 })
49 .expect_url('{0}/key'.format(BASE_URL)),
50 ])
51 assert result['changed'] is False
52 assert result['ssh_keys'] == []
53
54 def test_single_key(self, mocker):
55 result = self.run_module_success(mocker, ssh_key_info, {
56 'hetzner_user': '',
57 'hetzner_password': '',
58 }, [
59 FetchUrlCall('GET', 200)
60 .result_json([
61 {
62 'key': {
63 'name': 'key1',
64 'fingerprint': '56:29:99:a4:5d:ed:ac:95:c1:f5:88:82:90:5d:dd:10',
65 'type': 'ECDSA',
66 'size': 521,
67 'data': 'ecdsa-sha2-nistp521 AAAAE2VjZHNh ...'
68 },
69 },
70 ])
71 .expect_url('{0}/key'.format(BASE_URL)),
72 ])
73 assert result['changed'] is False
74 assert result['ssh_keys'] == [{
75 'name': 'key1',
76 'fingerprint': '56:29:99:a4:5d:ed:ac:95:c1:f5:88:82:90:5d:dd:10',
77 'type': 'ECDSA',
78 'size': 521,
79 'data': 'ecdsa-sha2-nistp521 AAAAE2VjZHNh ...'
80 }]